Skip to content

Commit f298a37

Browse files
committed
pbio/imu: Prevent calibration while motors in use.
1 parent ed10485 commit f298a37

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
- On SPIKE Prime Hub and Robot Inventor Hub, moved Bluetooth indications to
3636
the Bluetooth light. Only warning lights will be shown on the main button
3737
light. See ([support#1716]) and ([pybricks-micropython#261]).
38+
- Allow gyro calibration only while all motors are coasting ([support#1840]) to
39+
prevent recalibration during very steady moves.
3840

3941
### Fixed
4042
- Fixed not able to connect to new Technic Move hub with `LWP3Device()`.
@@ -56,6 +58,7 @@
5658
[support#1622]: https://github.com/pybricks/support/issues/1622
5759
[support#1678]: https://github.com/pybricks/support/issues/1678
5860
[support#1716]: https://github.com/pybricks/support/issues/1716
61+
[support#1840]: https://github.com/pybricks/support/issues/1840
5962

6063
## [3.5.0] - 2024-04-11
6164

lib/pbio/include/pbio/dcmotor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ typedef struct _pbio_dcmotor_t {
7171

7272
/** @cond INTERNAL */
7373
void pbio_dcmotor_stop_all(bool clear_parents);
74+
bool pbio_dcmotor_all_coasting(void);
7475
pbio_error_t pbio_dcmotor_coast(pbio_dcmotor_t *dcmotor);
7576
pbio_error_t pbio_dcmotor_set_voltage(pbio_dcmotor_t *dcmotor, int32_t voltage);
7677
int32_t pbio_dcmotor_get_max_voltage(pbdrv_legodev_type_id_t id);
@@ -100,6 +101,10 @@ pbio_error_t pbio_dcmotor_user_command(pbio_dcmotor_t *dcmotor, bool coast, int3
100101
static inline void pbio_dcmotor_stop_all(bool clear_parents) {
101102
}
102103

104+
static inline bool pbio_dcmotor_any_active(void) {
105+
return false;
106+
}
107+
103108
static inline pbio_error_t pbio_dcmotor_get_dcmotor(pbdrv_legodev_dev_t *legodev, pbio_dcmotor_t **dcmotor) {
104109
*dcmotor = NULL;
105110
return PBIO_ERROR_NOT_SUPPORTED;

lib/pbio/src/dcmotor.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ void pbio_dcmotor_stop_all(bool clear_parents) {
5959
}
6060
}
6161

62+
/**
63+
* Tests if all dc motors are coasting.
64+
*
65+
* @return @c true if all motors are coasting, @c false otherwise.
66+
*/
67+
bool pbio_dcmotor_all_coasting(void) {
68+
for (uint8_t i = 0; i < PBIO_CONFIG_DCMOTOR_NUM_DEV; i++) {
69+
if (dcmotors[i].actuation_now != PBIO_DCMOTOR_ACTUATION_COAST) {
70+
return false;
71+
}
72+
}
73+
return true;
74+
}
75+
6276
/**
6377
* Stops and closes DC motor instance so it can be used in another application.
6478
*

lib/pbio/src/imu.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <pbio/angle.h>
1212
#include <pbio/config.h>
13+
#include <pbio/dcmotor.h>
1314
#include <pbio/error.h>
1415
#include <pbio/geometry.h>
1516
#include <pbio/imu.h>
@@ -60,6 +61,11 @@ bool pbio_imu_is_ready(void) {
6061
// Called by driver to process unfiltered gyro and accelerometer data recorded while stationary.
6162
static void pbio_imu_handle_stationary_data_func(const int32_t *gyro_data_sum, const int32_t *accel_data_sum, uint32_t num_samples) {
6263

64+
// Don't update if not stationary
65+
if (!pbio_imu_is_stationary()) {
66+
return;
67+
}
68+
6369
// If the IMU calibration hasn't been updated in a long time, reset the
6470
// stationary counter so that the calibration values get a large weight.
6571
if (!pbio_imu_is_ready()) {
@@ -126,12 +132,12 @@ pbio_error_t pbio_imu_set_base_orientation(pbio_geometry_xyz_t *front_side_axis,
126132
}
127133

128134
/**
129-
* Checks if the IMU is currently stationary.
135+
* Checks if the IMU is currently stationary and motors are not moving.
130136
*
131137
* @return True if it has been stationary for about a second, false if moving.
132138
*/
133139
bool pbio_imu_is_stationary(void) {
134-
return pbdrv_imu_is_stationary(imu_dev);
140+
return pbdrv_imu_is_stationary(imu_dev) && pbio_dcmotor_all_coasting();
135141
}
136142

137143
// Measured rotation of the Z axis in the user frame for exactly one rotation

0 commit comments

Comments
 (0)