Skip to content

Commit 368da6b

Browse files
committed
pbio/imu: Prevent numeric divergence.
If the user sets only one value, the other value was first obtained with a getter and then set again. Since both operations could introduce rounding errors, this may not be a stable operation over time. Instead, only set a new value if it is given.
1 parent b577af5 commit 368da6b

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

lib/pbio/src/imu.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,19 @@ bool pbio_imu_is_stationary(void) {
137137
* Sets the thresholds that define when the hub is stationary. When the
138138
* measurements are steadily below these levels, the orientation module
139139
* automatically recalibrates.
140+
*
141+
* If a value is negative, it is ignored.
140142
*
141143
* @param [in] angular_velocity Angular velocity threshold in deg/s.
142144
* @param [in] acceleration Acceleration threshold in mm/s^2
143145
*/
144146
void pbio_imu_set_stationary_thresholds(float angular_velocity, float acceleration) {
145-
imu_config->gyro_stationary_threshold = pbio_int_math_bind(angular_velocity / imu_config->gyro_scale, 1, INT16_MAX);
146-
imu_config->accel_stationary_threshold = pbio_int_math_bind(acceleration / imu_config->accel_scale, 1, INT16_MAX);
147+
if (angular_velocity >= 0) {
148+
imu_config->gyro_stationary_threshold = pbio_int_math_bind(angular_velocity / imu_config->gyro_scale, 1, INT16_MAX);
149+
}
150+
if (acceleration >= 0) {
151+
imu_config->accel_stationary_threshold = pbio_int_math_bind(acceleration / imu_config->accel_scale, 1, INT16_MAX);
152+
}
147153
}
148154

149155
/**

pybricks/common/pb_type_imu.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,27 +168,25 @@ STATIC mp_obj_t pb_type_imu_settings(size_t n_args, const mp_obj_t *pos_args, mp
168168

169169
(void)self;
170170

171-
float angular_velocity;
172-
float acceleration;
173-
pbio_imu_get_stationary_thresholds(&angular_velocity, &acceleration);
174-
175171
// Return current values if no arguments are given.
176172
if (angular_velocity_threshold_in == mp_const_none && acceleration_threshold_in == mp_const_none) {
173+
float angular_velocity;
174+
float acceleration;
175+
pbio_imu_get_stationary_thresholds(&angular_velocity, &acceleration);
177176
mp_obj_t ret[] = {
178177
mp_obj_new_float_from_f(angular_velocity),
179178
mp_obj_new_float_from_f(acceleration),
180179
};
181180
return mp_obj_new_tuple(MP_ARRAY_SIZE(ret), ret);
182181
}
183182

184-
// Otherwise set new values.
185-
if (angular_velocity_threshold_in != mp_const_none) {
186-
angular_velocity = mp_obj_get_float(angular_velocity_threshold_in);
187-
}
188-
if (acceleration_threshold_in != mp_const_none) {
189-
acceleration = mp_obj_get_float(acceleration_threshold_in);
190-
}
191-
pbio_imu_set_stationary_thresholds(angular_velocity, acceleration);
183+
// Otherwise set new values, only if given.
184+
pbio_imu_set_stationary_thresholds(
185+
angular_velocity_threshold_in == mp_const_none ? -1.0f : mp_obj_get_float(angular_velocity_threshold_in),
186+
acceleration_threshold_in == mp_const_none ? -1.0f : mp_obj_get_float(acceleration_threshold_in)
187+
);
188+
189+
// Request that changed settings are saved on shutdown.
192190
pbsys_storage_settings_save_imu_settings();
193191
return mp_const_none;
194192
}

0 commit comments

Comments
 (0)