Skip to content

Commit 88e79b9

Browse files
committed
pbio/drivebase: Separate use_gyro setter.
This allows it to be toggled during runtime.
1 parent 210e2b5 commit 88e79b9

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

lib/pbio/include/pbio/drivebase.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef struct _pbio_drivebase_t {
3333
pbio_control_t control_distance;
3434
} pbio_drivebase_t;
3535

36-
pbio_error_t pbio_drivebase_get_drivebase(pbio_drivebase_t **db_address, pbio_servo_t *left, pbio_servo_t *right, int32_t wheel_diameter, int32_t axle_track, bool use_gyro);
36+
pbio_error_t pbio_drivebase_get_drivebase(pbio_drivebase_t **db_address, pbio_servo_t *left, pbio_servo_t *right, int32_t wheel_diameter, int32_t axle_track);
3737

3838
// Drive base status:
3939

@@ -58,6 +58,7 @@ pbio_error_t pbio_drivebase_stop(pbio_drivebase_t *db, pbio_control_on_completio
5858
pbio_error_t pbio_drivebase_get_state_user(pbio_drivebase_t *db, int32_t *distance, int32_t *drive_speed, int32_t *angle, int32_t *turn_rate);
5959
pbio_error_t pbio_drivebase_get_drive_settings(const pbio_drivebase_t *db, int32_t *drive_speed, int32_t *drive_acceleration, int32_t *drive_deceleration, int32_t *turn_rate, int32_t *turn_acceleration, int32_t *turn_deceleration);
6060
pbio_error_t pbio_drivebase_set_drive_settings(pbio_drivebase_t *db, int32_t drive_speed, int32_t drive_acceleration, int32_t drive_deceleration, int32_t turn_rate, int32_t turn_acceleration, int32_t turn_deceleration);
61+
pbio_error_t pbio_drivebase_set_use_gyro(pbio_drivebase_t *db, bool use_gyro);
6162

6263
#if PBIO_CONFIG_DRIVEBASE_SPIKE
6364

lib/pbio/src/drivebase.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,9 @@ static pbio_error_t pbio_drivebase_stop_from_servo(void *drivebase, bool clear_p
242242
* @param [in] right Right servo instance.
243243
* @param [in] wheel_diameter Wheel diameter in um.
244244
* @param [in] axle_track Distance between wheel-ground contact points in um.
245-
* @param [in] use_gyro Whether to use a gyro for heading (true) or the builtin rotation sensors (false).
246245
* @return Error code.
247246
*/
248-
pbio_error_t pbio_drivebase_get_drivebase(pbio_drivebase_t **db_address, pbio_servo_t *left, pbio_servo_t *right, int32_t wheel_diameter, int32_t axle_track, bool use_gyro) {
247+
pbio_error_t pbio_drivebase_get_drivebase(pbio_drivebase_t **db_address, pbio_servo_t *left, pbio_servo_t *right, int32_t wheel_diameter, int32_t axle_track) {
249248

250249
// Can't build a drive base with just one motor.
251250
if (left == right) {
@@ -287,8 +286,8 @@ pbio_error_t pbio_drivebase_get_drivebase(pbio_drivebase_t **db_address, pbio_se
287286
db->left = left;
288287
db->right = right;
289288

290-
// Whether to use the gyro for steering and driving straight.
291-
db->use_gyro = use_gyro;
289+
// By default, don't use gyro.
290+
db->use_gyro = false;
292291

293292
// Set parents of both servos, so they can stop this drivebase.
294293
pbio_parent_set(&left->parent, db, pbio_drivebase_stop_from_servo);
@@ -336,6 +335,28 @@ pbio_error_t pbio_drivebase_get_drivebase(pbio_drivebase_t **db_address, pbio_se
336335
return PBIO_SUCCESS;
337336
}
338337

338+
/**
339+
* Makes the drivebase use gyro or motor rotation sensors for heading control.
340+
*
341+
* This function will stop the drivebase if it is running.
342+
*
343+
* @param [in] db Drivebase instance.
344+
* @param [in] use_gyro Whether to use the gyro for heading control.
345+
* @return Error code.
346+
*/
347+
pbio_error_t pbio_drivebase_set_use_gyro(pbio_drivebase_t *db, bool use_gyro) {
348+
349+
// We stop so that new commands will reinitialize the state using the
350+
// newly selected input for heading control.
351+
pbio_error_t err = pbio_drivebase_stop(db, PBIO_CONTROL_ON_COMPLETION_COAST);
352+
if (err != PBIO_SUCCESS) {
353+
return err;
354+
}
355+
356+
db->use_gyro = use_gyro;
357+
return PBIO_SUCCESS;
358+
}
359+
339360
/**
340361
* Stops a drivebase.
341362
*
@@ -821,7 +842,7 @@ pbio_error_t pbio_drivebase_is_stalled(pbio_drivebase_t *db, bool *stalled, uint
821842
* @return Error code.
822843
*/
823844
pbio_error_t pbio_drivebase_get_drivebase_spike(pbio_drivebase_t **db_address, pbio_servo_t *left, pbio_servo_t *right) {
824-
pbio_error_t err = pbio_drivebase_get_drivebase(db_address, left, right, 1000, 1000, false);
845+
pbio_error_t err = pbio_drivebase_get_drivebase(db_address, left, right, 1000, 1000);
825846

826847
// The application input for spike bases is degrees per second average
827848
// between both wheels, so in millidegrees this is x1000.

lib/pbio/test/src/test_drivebase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static PT_THREAD(test_drivebase_basics(struct pt *pt)) {
8282
tt_uint_op(pbio_servo_setup(srv_right, id, PBIO_DIRECTION_CLOCKWISE, 1000, true, 0), ==, PBIO_SUCCESS);
8383

8484
// Set up the drivebase.
85-
tt_uint_op(pbio_drivebase_get_drivebase(&db, srv_left, srv_right, 56000, 112000, false), ==, PBIO_SUCCESS);
85+
tt_uint_op(pbio_drivebase_get_drivebase(&db, srv_left, srv_right, 56000, 112000), ==, PBIO_SUCCESS);
8686
tt_uint_op(pbio_drivebase_get_state_user(db, &drive_distance, &drive_speed, &turn_angle_start, &turn_rate), ==, PBIO_SUCCESS);
8787
tt_uint_op(pbio_drivebase_is_stalled(db, &stalled, &stall_duration), ==, PBIO_SUCCESS);
8888
tt_want(!stalled);

pybricks/robotics/pb_type_drivebase.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ STATIC mp_obj_t pb_type_DriveBase_make_new(const mp_obj_type_t *type, size_t n_a
7272
srv_left,
7373
srv_right,
7474
pb_obj_get_scaled_int(wheel_diameter_in, 1000),
75-
pb_obj_get_scaled_int(axle_track_in, 1000),
76-
// Use gyro if creating instance of GyroDriveBase.
77-
type != &pb_type_drivebase));
75+
pb_obj_get_scaled_int(axle_track_in, 1000)));
76+
77+
// Use gyro if creating instance of GyroDriveBase.
78+
pb_assert(pbio_drivebase_set_use_gyro(self->db, type != &pb_type_drivebase));
7879

7980
#if PYBRICKS_PY_COMMON_CONTROL
8081
// Create instances of the Control class

0 commit comments

Comments
 (0)