Skip to content

Commit a3bbc90

Browse files
committed
pybricks.robotics.DriveBase: Add brake().
Fixes pybricks/support#881
1 parent cf73380 commit a3bbc90

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Added
88
- Added `hub.buttons` as an alias for `hub.button` on buttons with one
99
hub ([support#1254]).
10+
- Implemented `brake` for `DriveBase` ([support#881]).
1011

1112
### Changed
1213
- The `use_gyro` method is added to the normal `DriveBase` class instead of
@@ -27,6 +28,7 @@
2728
tight loop ([support#1151]).
2829

2930
[pybricks-micropython#104]: https://github.com/pybricks/pybricks-micropython/pull/104
31+
[support#881]: https://github.com/pybricks/support/issues/881
3032
[support#1054]: https://github.com/pybricks/support/issues/1054
3133
[support#1140]: https://github.com/pybricks/support/issues/1140
3234
[support#1151]: https://github.com/pybricks/support/issues/1151

pybricks/robotics/pb_type_drivebase.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,20 @@ STATIC mp_obj_t pb_type_DriveBase_stop(mp_obj_t self_in) {
222222
}
223223
MP_DEFINE_CONST_FUN_OBJ_1(pb_type_DriveBase_stop_obj, pb_type_DriveBase_stop);
224224

225+
// pybricks.robotics.DriveBase.brake
226+
STATIC mp_obj_t pb_type_DriveBase_brake(mp_obj_t self_in) {
227+
228+
// Cancel awaitables.
229+
pb_type_DriveBase_obj_t *self = MP_OBJ_TO_PTR(self_in);
230+
pb_type_awaitable_update_all(self->awaitables, PB_TYPE_AWAITABLE_OPT_CANCEL_ALL);
231+
232+
// Stop hardware.
233+
pb_assert(pbio_drivebase_stop(self->db, PBIO_CONTROL_ON_COMPLETION_BRAKE));
234+
235+
return mp_const_none;
236+
}
237+
MP_DEFINE_CONST_FUN_OBJ_1(pb_type_DriveBase_brake_obj, pb_type_DriveBase_brake);
238+
225239
// pybricks.robotics.DriveBase.distance
226240
STATIC mp_obj_t pb_type_DriveBase_distance(mp_obj_t self_in) {
227241
pb_type_DriveBase_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -355,6 +369,7 @@ STATIC const mp_rom_map_elem_t pb_type_DriveBase_locals_dict_table[] = {
355369
{ MP_ROM_QSTR(MP_QSTR_turn), MP_ROM_PTR(&pb_type_DriveBase_turn_obj) },
356370
{ MP_ROM_QSTR(MP_QSTR_drive), MP_ROM_PTR(&pb_type_DriveBase_drive_obj) },
357371
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&pb_type_DriveBase_stop_obj) },
372+
{ MP_ROM_QSTR(MP_QSTR_brake), MP_ROM_PTR(&pb_type_DriveBase_brake_obj) },
358373
{ MP_ROM_QSTR(MP_QSTR_distance), MP_ROM_PTR(&pb_type_DriveBase_distance_obj) },
359374
{ MP_ROM_QSTR(MP_QSTR_angle), MP_ROM_PTR(&pb_type_DriveBase_angle_obj) },
360375
{ MP_ROM_QSTR(MP_QSTR_done), MP_ROM_PTR(&pb_type_DriveBase_done_obj) },

0 commit comments

Comments
 (0)