Skip to content

Commit fee5925

Browse files
committed
pbio/drivebase: Stop drivebase on gyro reset.
1 parent 0227860 commit fee5925

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
- Added one byte program identifier to the hub status report to the host.
2121
- Added interface and implementation for storing and selecting multiple code
2222
slots on the Prime Hub and Inventor Hub.
23+
- Added ability to set distance and angle in `DriveBase.reset()`. If the
24+
DriveBase is using the gyro, it will be set to the same angle. ([support#1617]).
2325

2426
### Changed
2527

@@ -36,9 +38,11 @@
3638
the Bluetooth light. Only warning lights will be shown on the main button
3739
light. See ([support#1716]) and ([pybricks-micropython#261]).
3840
- Allow gyro calibration only while all motors are coasting ([support#1840]) to
39-
prevent recalibration during very steady moves.
41+
prevent recalibration during very steady moves ([support#1687])
4042
- Reduced default angular velocity stationary threshold from an undocumented
41-
5 deg/s to 3 deg/s to reduce unwanted calibration while moving ([support#1105]).
43+
5 deg/s to 3 deg/s to reduce unwanted calibration while moving ([support#1105]).
44+
- 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]).
4246

4347
### Fixed
4448
- Fixed not able to connect to new Technic Move hub with `LWP3Device()`.
@@ -58,9 +62,12 @@
5862
[support#1429]: https://github.com/pybricks/support/issues/1429
5963
[support#1460]: https://github.com/pybricks/support/issues/1460
6064
[support#1615]: https://github.com/pybricks/support/issues/1615
65+
[support#1617]: https://github.com/pybricks/support/issues/1617
6166
[support#1622]: https://github.com/pybricks/support/issues/1622
6267
[support#1678]: https://github.com/pybricks/support/issues/1678
68+
[support#1687]: https://github.com/pybricks/support/issues/1687
6369
[support#1716]: https://github.com/pybricks/support/issues/1716
70+
[support#1818]: https://github.com/pybricks/support/issues/1818
6471
[support#1840]: https://github.com/pybricks/support/issues/1840
6572

6673
## [3.5.0] - 2024-04-11

lib/pbio/include/pbio/drivebase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +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);
5859
bool pbio_drivebase_is_done(const pbio_drivebase_t *db);
5960
pbio_error_t pbio_drivebase_is_stalled(pbio_drivebase_t *db, bool *stalled, uint32_t *stall_duration);
6061

lib/pbio/src/drivebase.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,8 @@ pbio_error_t pbio_drivebase_get_state_user(pbio_drivebase_t *db, int32_t *distan
725725
* If the gyro is being used for control, it will be reset to the same angle.
726726
*
727727
* @param [in] db The drivebase instance.
728-
* @param [out] distance Distance traveled in mm.
729-
* @param [out] angle Angle turned in degrees.
728+
* @param [in] distance Distance traveled in mm.
729+
* @param [in] angle Angle turned in degrees.
730730
* @return Error code.
731731
*/
732732
pbio_error_t pbio_drivebase_reset(pbio_drivebase_t *db, int32_t distance, int32_t angle) {
@@ -756,13 +756,28 @@ pbio_error_t pbio_drivebase_reset(pbio_drivebase_t *db, int32_t distance, int32_
756756
pbio_angle_from_low_res(&reported_new, angle, db->control_heading.settings.ctl_steps_per_app_step);
757757
pbio_angle_diff(&measured_heading.position, &reported_new, &db->heading_offset);
758758

759-
// Whether or not the gyro is being used, synchronize heading and drivebase
760-
// angle state.
761-
pbio_imu_set_heading(angle);
759+
// Synchronize heading and drivebase angle state if gyro in use.
760+
if (db->use_gyro) {
761+
pbio_imu_set_heading(angle);
762+
}
762763

763764
return PBIO_SUCCESS;
764765
}
765766

767+
/**
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.
771+
*/
772+
void pbio_drivebase_stop_all_when_gyro_used(void) {
773+
for (uint8_t i = 0; i < PBIO_CONFIG_NUM_DRIVEBASES; i++) {
774+
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);
778+
}
779+
}
780+
}
766781

767782
/**
768783
* Gets the drivebase settings in user units.

lib/pbio/src/imu.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <pbio/angle.h>
1212
#include <pbio/config.h>
1313
#include <pbio/dcmotor.h>
14+
#include <pbio/drivebase.h>
1415
#include <pbio/error.h>
1516
#include <pbio/geometry.h>
1617
#include <pbio/imu.h>
@@ -267,6 +268,13 @@ float pbio_imu_get_heading(void) {
267268
*/
268269
void pbio_imu_set_heading(float desired_heading) {
269270
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();
270278
}
271279

272280
/**

0 commit comments

Comments
 (0)