Skip to content

Commit 4f1041a

Browse files
committed
pybricks.common.IMU.reset_heading: Raise on drive base busy.
This partially reverts commit fee5925, then implements the alternate behavior. See pybricks/support#1818
1 parent fee5925 commit 4f1041a

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
- Reduced default angular velocity stationary threshold from an undocumented
4343
5 deg/s to 3 deg/s to reduce unwanted calibration while moving ([support#1105]).
4444
- If `imu.reset_heading()` is called while a drive base is actively using the
45-
gyro, the drive base will stop to avoid confusion ([support#1818]).
45+
gyro, an exception will be raised ([support#1818]).
4646

4747
### Fixed
4848
- Fixed not able to connect to new Technic Move hub with `LWP3Device()`.

lib/pbio/include/pbio/drivebase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pbio_error_t pbio_drivebase_get_drivebase(pbio_drivebase_t **db_address, pbio_se
5555

5656
void pbio_drivebase_update_all(void);
5757
bool pbio_drivebase_update_loop_is_running(pbio_drivebase_t *db);
58-
void pbio_drivebase_stop_all_when_gyro_used(void);
58+
bool pbio_drivebase_any_uses_gyro(void);
5959
bool pbio_drivebase_is_done(const pbio_drivebase_t *db);
6060
pbio_error_t pbio_drivebase_is_stalled(pbio_drivebase_t *db, bool *stalled, uint32_t *stall_duration);
6161

lib/pbio/src/drivebase.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -765,18 +765,25 @@ pbio_error_t pbio_drivebase_reset(pbio_drivebase_t *db, int32_t distance, int32_
765765
}
766766

767767
/**
768-
* Stops all drivebases that use the gyro. Called by the imu module when the
769-
* imu heading is reset. Resetting it would throw off ongoing drivebase
770-
* controls, so we should stop them.
768+
* Tests if any drive base is currently actively using the gyro.
769+
*
770+
* @return @c true if the gyro is being used, else @c false
771771
*/
772-
void pbio_drivebase_stop_all_when_gyro_used(void) {
772+
bool pbio_drivebase_any_uses_gyro(void) {
773773
for (uint8_t i = 0; i < PBIO_CONFIG_NUM_DRIVEBASES; i++) {
774774
pbio_drivebase_t *db = &drivebases[i];
775-
if (pbio_drivebase_update_loop_is_running(db) && db->use_gyro) {
776-
// Let errors pass.
777-
pbio_drivebase_stop(db, PBIO_CONTROL_ON_COMPLETION_COAST);
775+
776+
// Only consider activated drive bases that use the gyro.
777+
if (!pbio_drivebase_update_loop_is_running(db) || !db->use_gyro) {
778+
continue;
779+
}
780+
781+
// Active controller means driving or holding, so gyro is in use.
782+
if (pbio_control_is_active(&db->control_distance) || pbio_control_is_active(&db->control_heading)) {
783+
return true;
778784
}
779785
}
786+
return false;
780787
}
781788

782789
/**

lib/pbio/src/imu.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <pbio/angle.h>
1212
#include <pbio/config.h>
1313
#include <pbio/dcmotor.h>
14-
#include <pbio/drivebase.h>
1514
#include <pbio/error.h>
1615
#include <pbio/geometry.h>
1716
#include <pbio/imu.h>
@@ -268,13 +267,6 @@ float pbio_imu_get_heading(void) {
268267
*/
269268
void pbio_imu_set_heading(float desired_heading) {
270269
heading_offset = pbio_imu_get_heading() + heading_offset - desired_heading;
271-
272-
// Callbacks to other resources to inform that the gyro has been (re)set.
273-
274-
// REVISIT: At the moment, only drivebases use the gyro. If more resources
275-
// need it, we can enable subscribing to the imu and have a callback
276-
// called here on resets.
277-
pbio_drivebase_stop_all_when_gyro_used();
278270
}
279271

280272
/**

pybricks/common/pb_type_imu.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <stdbool.h>
1010
#include <string.h>
1111

12+
#include <pbio/drivebase.h>
1213
#include <pbio/error.h>
1314
#include <pbio/geometry.h>
1415
#include <pbio/imu.h>
@@ -213,6 +214,10 @@ static mp_obj_t pb_type_imu_reset_heading(size_t n_args, const mp_obj_t *pos_arg
213214
pb_type_imu_obj_t, self,
214215
PB_ARG_REQUIRED(angle));
215216

217+
if (pbio_drivebase_any_uses_gyro()) {
218+
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Can't reset heading while gyro in use. Stop driving first."));
219+
}
220+
216221
// Set the new angle
217222
(void)self;
218223
pbio_imu_set_heading(mp_obj_get_float(angle_in));

0 commit comments

Comments
 (0)