Math
Counter
¶
A simple counter class that increments a counter each time the increment_counter argument is set true. To reset the counter, call update with increment_counter set to false.
Author: Kevin Best, 9/25/2024 https://github.com/tkevinbest
Source code in opensourceleg/math/math.py
current_count
property
¶
Returns the current count
EdgeDetector
¶
Used to calculate rising and falling edges of a digital signal in real time. Call edgeDetector.update(digitalSignal) to update the detector. Then read edgeDetector.rising_edge or falling edge to know if the event occurred.
Author: Kevin Best https://github.com/tkevinbest
Source code in opensourceleg/math/math.py
SaturatingRamp
¶
Creates a signal that ramps between 0 and 1 at the specified rate. Looks like a trapezoid in the time domain Used to slowly enable joint torque for smooth switching at startup. Call saturatingRamp.update() to update the value of the ramp and return the value. Can also access saturatingRamp.value without updating.
Author: Kevin Best https://github.com/tkevinbest
Source code in opensourceleg/math/math.py
__init__(loop_frequency=100, ramp_time=1.0)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loop_frequency
|
int
|
Rate in Hz (default 100 Hz). Defaults to 100. |
100
|
ramp_time
|
float
|
Time to complete the ramp. Defaults to 1.0. |
1.0
|
Source code in opensourceleg/math/math.py
update(enable_ramp=False)
¶
Updates the ramp value and returns it as a float. If enable_ramp is true, ramp value increases Otherwise decreases.
Example usage
torque = torque * ramp.update(enable_ramp)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enable_ramp
|
bool
|
If enable_ramp is true, ramp value increases. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
value |
float
|
Scalar between 0 and 1. |
Source code in opensourceleg/math/math.py
ThermalModel
¶
Enhanced thermal model with outlier filtering and soft-limiting safety controller.
Based on A Modular Framework for Task-Agnostic, Energy Shaping Control of Lower Limb Exoskeletons
by Jianping Lin, Gray C. Thomas, Nikhil V. Divekar, Vamsi Peddinti, & Robert D. Gregg
Thermal Circuit Model
Two-node lumped system with winding and case temperatures.
Equations: 1: Cw * dTw/dt = I²R(T) + (Th-Tw)/Rwh 2: Ch * dTh/dt = (Tw-Th)/Rwh + (Ta-Th)/Rha
Features
- Integrated outlier detection for current/temperature sensors
- Soft-limiting thermal safety controller with formal guarantees
- Physically sensible value validation with slope-based projection
- Backward compatible API with enhanced methods
- Motor-specific parameter configuration via MOTOR_CONSTANTS
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
motor_constants
|
MOTOR_CONSTANTS
|
MOTOR_CONSTANTS instance with thermal parameters |
required |
actuator_tag
|
str
|
Actuator identifier for error messages. Defaults to "actuator" |
'actuator'
|
ambient_temperature
|
float
|
Ambient temperature in °C. Defaults to 21.0 |
21.0
|
Source code in opensourceleg/math/math.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
check_thermal_limits_and_raise()
¶
Check thermal limits and raise exceptions if exceeded. This replaces the thermal limit checking logic from actuator update method.
Raises:
| Type | Description |
|---|---|
ThermalLimitException
|
If hard thermal limits are exceeded |
Source code in opensourceleg/math/math.py
get_thermal_scale_factor()
¶
Calculate thermal scaling factor using Jianping's soft-limiting approach.
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Scale factor [0,1] for torque/current scaling |
Source code in opensourceleg/math/math.py
update(dt, motor_current, case_temperature=None, factor_of_safety=1.0)
¶
Updates winding and case temperatures with sensor diagnosis and returns scale factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
float
|
Time step in seconds. Defaults to 1/200 (200Hz) |
required |
motor_current
|
float
|
Motor current in mA. Defaults to 0 |
required |
case_temperature
|
Optional[float]
|
External case temperature in °C. If None, uses thermal model. |
None
|
factor_of_safety
|
float
|
Factor of safety for thermal predictions. Defaults to 1.0 |
1.0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Thermal scale factor [0,1] for torque/current scaling |
Thermal Dynamics
Cw * dTw/dt = I²R(T) + (Tc-Tw)/Rwc Cc * dTc/dt = (Tw-Tc)/Rwc + (Ta-Tc)/Rca
Source code in opensourceleg/math/math.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
clamp_within_vector_range(input_value, input_vector)
¶
This function ensures that input_value remains within the range spanned by the input_vector. If the input_value falls outside the vector's bounds, it'll return the appropriate max or min value from the vector.
Example
clamp_within_vector_range(10, [0,1,2,3]) = 3 clamp_within_vector_range(-10, [0,1,2,3]) = 0
Source code in opensourceleg/math/math.py
from_twos_complement(value, bit_length)
¶
Converts a 2's complement integer to a signed integer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
2's complement integer |
required |
bit_length
|
int
|
Number of bits of 2's complement representation |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Signed integer |
Author: Axel Sjögren Holtz (axel.sjogren.holtz@vgregion.se)
Raises:
| Type | Description |
|---|---|
TypeError
|
If value or bit_length is not an integer |
ValueError
|
If value is negative, bit_length is negative, or value exceeds bit_length |
Source code in opensourceleg/math/math.py
to_twos_complement(value, bit_length)
¶
Converts a signed integer to 2's complement for a defined number of bits as an unsigned integer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
Signed integer to convert |
required |
bit_length
|
int
|
Number of bits of 2's complement representation |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Unsigned integer 2's complement |
Author: Axel Sjögren Holtz (axel.sjogren.holtz@vgregion.se)
Raises:
| Type | Description |
|---|---|
ValueError
|
If value is too small or too large for the given bit length |