Skip to content

Commit b73e164

Browse files
committed
pbio/drivebase: Don't stop on calling use_gyro with same argument.
If this is called again while already set, this is now a no-op, instead of stopping the controls as it needs to do when toggling. Fixes pybricks/support#1858 Also add note about gyro speed control to reflect 26b8e62
1 parent 759ffa2 commit b73e164

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
### Fixed
88
- Fixed persistent data not being deleted when swapping
99
from `3.6.0b1` to `3.5.0` and back to `3.6.0b1` ([support#1846]).
10+
- Fixed controls stopping if `use_gyro` is called again with the same
11+
argument as already active ([support#1858]).
1012

1113
[support#1846]: https://github.com/pybricks/support/issues/1846
14+
[support#1858]: https://github.com/pybricks/support/issues/1858
1215

1316
## [3.6.0b1] - 2024-09-24
1417

lib/pbio/src/drivebase.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ static pbio_error_t pbio_drivebase_get_state_control(pbio_drivebase_t *db, pbio_
179179

180180
// Optionally use gyro to override the heading source for more accuracy.
181181
// The gyro manages its own offset, so we don't need to subtract it here.
182+
// Note that the heading speed estimate (used for derivative control still
183+
// uses the motor estimate rather than the gyro speed, to guarantee the
184+
// same stability properties to stabilize the motors.)
182185
if (db->use_gyro) {
183186
pbio_imu_get_heading_scaled(&state_heading->position, &state_heading->speed, db->control_heading.settings.ctl_steps_per_app_step);
184187
}
@@ -361,8 +364,10 @@ pbio_error_t pbio_drivebase_get_drivebase(pbio_drivebase_t **db_address, pbio_se
361364
// geometry, so it is done after the scaling is set.
362365
pbio_drivebase_reset(db, 0, 0);
363366

364-
// Finish setup. By default, don't use gyro.
365-
return pbio_drivebase_set_use_gyro(db, false);
367+
// By default, don't use gyro for steering control.
368+
db->use_gyro = false;
369+
370+
return PBIO_SUCCESS;
366371
}
367372

368373
/**
@@ -376,6 +381,11 @@ pbio_error_t pbio_drivebase_get_drivebase(pbio_drivebase_t **db_address, pbio_se
376381
*/
377382
pbio_error_t pbio_drivebase_set_use_gyro(pbio_drivebase_t *db, bool use_gyro) {
378383

384+
// No need to reset controls if already in correct mode.
385+
if (db->use_gyro == use_gyro) {
386+
return PBIO_SUCCESS;
387+
}
388+
379389
// We stop so that new commands will reinitialize the state using the
380390
// newly selected input for heading control.
381391
pbio_error_t err = pbio_drivebase_stop(db, PBIO_CONTROL_ON_COMPLETION_COAST);

0 commit comments

Comments
 (0)