Skip to content

Commit b577af5

Browse files
committed
pbio/sys/storage_settings: Make IMU settings persistent.
1 parent 50f6bbc commit b577af5

File tree

7 files changed

+70
-16
lines changed

7 files changed

+70
-16
lines changed

lib/pbio/drv/imu/imu_lsm6ds3tr_c_stm32.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,11 @@ static PT_THREAD(pbdrv_imu_lsm6ds3tr_c_stm32_init(struct pt *pt)) {
227227
PT_SPAWN(pt, &child, lsm6ds3tr_c_gy_full_scale_set(&child, ctx, LSM6DS3TR_C_2000dps));
228228
imu_dev->config.gyro_scale = lsm6ds3tr_c_from_fs2000dps_to_mdps(1) / 1000.0f;
229229

230-
// default noise thresholds, can be changed during runtime
231-
imu_dev->config.gyro_stationary_threshold = 71; // 5 deg/s
232-
imu_dev->config.accel_stationary_threshold = 1044; // 2500 mm/s^2, or approx 25% of gravity
230+
// Noise thresholds. Will be loaded from user preferences. Can be changed
231+
// during runtime. Zero for now, so measurements are never lower. So
232+
// calibration will not start until these values are loaded or set.
233+
imu_dev->config.gyro_stationary_threshold = 0;
234+
imu_dev->config.accel_stationary_threshold = 0;
233235

234236
// Configure INT1 to trigger when new gyro data is ready.
235237
PT_SPAWN(pt, &child, lsm6ds3tr_c_pin_int1_route_set(&child, ctx, (lsm6ds3tr_c_int1_route_t) {

lib/pbio/include/pbsys/storage_settings.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <pbio/error.h>
1919

20+
#include <pbio/config.h>
2021
#include <pbsys/config.h>
2122

2223
/**
@@ -33,27 +34,43 @@ typedef enum {
3334
* System settings. All data types are little-endian.
3435
*/
3536
typedef struct _pbsys_storage_settings_t {
37+
/** System setting flags. */
3638
uint32_t flags;
39+
#if PBIO_CONFIG_IMU
40+
/** Angular velocity threshold below which the IMU is considered stationary, in deg/s. */
41+
float gyro_stationary_threshold;
42+
/** Acceleration threshold below which the IMU is considered stationary, in mm/s^2. */
43+
float accel_stationary_threshold;
44+
#endif
3745
} pbsys_storage_settings_t;
3846

3947
#if PBSYS_CONFIG_STORAGE
4048

41-
void pbsys_storage_set_default_settings(pbsys_storage_settings_t *settings);
49+
void pbsys_storage_settings_set_defaults(pbsys_storage_settings_t *settings);
50+
51+
void pbsys_storage_settings_apply_loaded_settings(pbsys_storage_settings_t *settings);
4252

4353
bool pbsys_storage_settings_bluetooth_enabled(void);
4454

4555
void pbsys_storage_settings_bluetooth_enabled_request_toggle(void);
4656

57+
void pbsys_storage_settings_save_imu_settings(void);
58+
4759
#else
4860

49-
static inline void pbsys_storage_set_default_settings(pbsys_storage_settings_t *settings) {
61+
static inline void pbsys_storage_settings_set_defaults(pbsys_storage_settings_t *settings) {
62+
}
63+
static inline void pbsys_storage_settings_apply_loaded_settings(pbsys_storage_settings_t *settings) {
5064
}
5165
static inline bool pbsys_storage_settings_bluetooth_enabled(void) {
5266
return true;
5367
}
5468
static inline void pbsys_storage_settings_bluetooth_enabled_request_toggle(void) {
5569
}
5670

71+
static inline void pbsys_storage_settings_save_imu_settings(void) {
72+
}
73+
5774
#endif // PBSYS_CONFIG_STORAGE
5875

5976
#endif // _PBSYS_STORAGE_SETTINGS_H_

lib/pbio/sys/bluetooth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) {
293293

294294
// Ensures Bluetooth preferences are loaded before we read them.
295295
#if PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
296-
PROCESS_WAIT_EVENT_UNTIL(pbsys_storage_get_settings() != NULL);
296+
PROCESS_WAIT_EVENT_UNTIL(pbsys_storage_settings_get_settings() != NULL);
297297
#endif // PBSYS_CONFIG_STORAGE && PBSYS_CONFIG_BLUETOOTH_TOGGLE
298298

299299
pbdrv_bluetooth_set_on_event(pbsys_bluetooth_process_poll);

lib/pbio/sys/storage.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static pbsys_storage_data_map_t *map = &pbsys_user_ram_data_map.data_map;
3333

3434
static bool data_map_is_loaded = false;
3535

36-
pbsys_storage_settings_t *pbsys_storage_get_settings(void) {
36+
pbsys_storage_settings_t *pbsys_storage_settings_get_settings(void) {
3737
if (!data_map_is_loaded) {
3838
return NULL;
3939
}
@@ -246,7 +246,7 @@ PROCESS_THREAD(pbsys_storage_process, ev, data) {
246246
// Reset storage except for program data. It is sufficient to set its
247247
// size to 0, which is what happens here since it is in the map.
248248
memset(map, 0, sizeof(pbsys_storage_data_map_t));
249-
pbsys_storage_set_default_settings(&map->settings);
249+
pbsys_storage_settings_set_defaults(&map->settings);
250250

251251
// Set firmware version used to create current storage map.
252252
map->stored_firmware_version = PBIO_HEXVERSION;
@@ -255,6 +255,9 @@ PROCESS_THREAD(pbsys_storage_process, ev, data) {
255255
pbsys_storage_request_write();
256256
}
257257

258+
// Apply loaded settings as necesary.
259+
pbsys_storage_settings_apply_loaded_settings(&map->settings);
260+
258261
// Poke processes that await on system settings to become available.
259262
data_map_is_loaded = true;
260263
process_post(PROCESS_BROADCAST, PROCESS_EVENT_COM, NULL);

lib/pbio/sys/storage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ pbio_error_t pbsys_storage_set_program_size(uint32_t size);
1919
pbio_error_t pbsys_storage_set_program_data(uint32_t offset, const void *data, uint32_t size);
2020
pbio_error_t pbsys_storage_assert_program_valid(void);
2121
void pbsys_storage_get_program_data(pbsys_main_program_t *program);
22-
pbsys_storage_settings_t *pbsys_storage_get_settings(void);
22+
pbsys_storage_settings_t *pbsys_storage_settings_get_settings(void);
2323
void pbsys_storage_request_write(void);
2424

2525
#else
2626
static inline void pbsys_storage_init(void) {
2727
}
2828
static inline void pbsys_storage_deinit(void) {
2929
}
30-
static inline pbsys_storage_settings_t *pbsys_storage_get_settings(void) {
30+
static inline pbsys_storage_settings_t *pbsys_storage_settings_get_settings(void) {
3131
return NULL;
3232
}
3333
static inline pbio_error_t pbsys_storage_set_program_size(uint32_t size) {

lib/pbio/sys/storage_settings.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <pbdrv/bluetooth.h>
1313

1414
#include <pbio/error.h>
15+
#include <pbio/imu.h>
1516
#include <pbsys/status.h>
1617
#include <pbsys/storage_settings.h>
1718

@@ -23,15 +24,46 @@
2324
*
2425
* @param [in] settings Settings to populate.
2526
*/
26-
void pbsys_storage_set_default_settings(pbsys_storage_settings_t *settings) {
27+
void pbsys_storage_settings_set_defaults(pbsys_storage_settings_t *settings) {
2728
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
2829
settings->flags |= PBSYS_STORAGE_SETTINGS_FLAGS_BLUETOOTH_ENABLED;
2930
#endif
31+
#if PBIO_CONFIG_IMU
32+
settings->gyro_stationary_threshold = 5;
33+
settings->accel_stationary_threshold = 2500;
34+
#endif // PBIO_CONFIG_IMU
35+
}
36+
37+
/**
38+
* Applies the loaded settings and preferences after boot.
39+
*
40+
* @param [in] settings Settings to populate.
41+
*/
42+
void pbsys_storage_settings_apply_loaded_settings(pbsys_storage_settings_t *settings) {
43+
#if PBIO_CONFIG_IMU
44+
pbio_imu_set_stationary_thresholds(settings->gyro_stationary_threshold, settings->accel_stationary_threshold);
45+
#endif // PBIO_CONFIG_IMU
46+
}
47+
48+
/**
49+
* Copies the configured IMU settings to storage and requests them to be saved.
50+
*
51+
* @param [in] settings Settings to populate.
52+
*/
53+
void pbsys_storage_settings_save_imu_settings(void) {
54+
pbsys_storage_settings_t *settings = pbsys_storage_settings_get_settings();
55+
if (!settings) {
56+
return;
57+
}
58+
#if PBIO_CONFIG_IMU
59+
pbio_imu_get_stationary_thresholds(&settings->gyro_stationary_threshold, &settings->accel_stationary_threshold);
60+
pbsys_storage_request_write();
61+
#endif // PBIO_CONFIG_IMU
3062
}
3163

3264
bool pbsys_storage_settings_bluetooth_enabled(void) {
3365
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
34-
pbsys_storage_settings_t *settings = pbsys_storage_get_settings();
66+
pbsys_storage_settings_t *settings = pbsys_storage_settings_get_settings();
3567
if (!settings) {
3668
return true;
3769
}
@@ -43,7 +75,7 @@ bool pbsys_storage_settings_bluetooth_enabled(void) {
4375

4476
void pbsys_storage_settings_bluetooth_enabled_request_toggle(void) {
4577
#if PBSYS_CONFIG_BLUETOOTH_TOGGLE
46-
pbsys_storage_settings_t *settings = pbsys_storage_get_settings();
78+
pbsys_storage_settings_t *settings = pbsys_storage_settings_get_settings();
4779

4880
// Ignore toggle request in all but idle system status.
4981
if (!settings

pybricks/common/pb_type_imu.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <pbio/geometry.h>
1414
#include <pbio/imu.h>
1515

16+
#include <pbsys/storage_settings.h>
17+
1618
#include "py/obj.h"
1719

1820
#include <pybricks/common.h>
@@ -187,6 +189,7 @@ STATIC mp_obj_t pb_type_imu_settings(size_t n_args, const mp_obj_t *pos_args, mp
187189
acceleration = mp_obj_get_float(acceleration_threshold_in);
188190
}
189191
pbio_imu_set_stationary_thresholds(angular_velocity, acceleration);
192+
pbsys_storage_settings_save_imu_settings();
190193
return mp_const_none;
191194
}
192195
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pb_type_imu_settings_obj, 1, pb_type_imu_settings);
@@ -248,9 +251,6 @@ mp_obj_t pb_type_IMU_obj_new(mp_obj_t top_side_axis_in, mp_obj_t front_side_axis
248251

249252
pbio_imu_set_base_orientation(&front_side_axis, &top_side_axis);
250253

251-
// Default noise thresholds.
252-
pbio_imu_set_stationary_thresholds(5.0f, 2500.0f);
253-
254254
// Return singleton instance.
255255
return MP_OBJ_FROM_PTR(&singleton_imu_obj);
256256
}

0 commit comments

Comments
 (0)