From a3bbc90200245ec9ceb272a4981b75cfa000930f Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Thu, 26 Oct 2023 14:15:50 +0200 Subject: [PATCH] pybricks.robotics.DriveBase: Add brake(). Fixes https://github.com/pybricks/support/issues/881 --- CHANGELOG.md | 2 ++ pybricks/robotics/pb_type_drivebase.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5df23f57e..bef2ade5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Added - Added `hub.buttons` as an alias for `hub.button` on buttons with one hub ([support#1254]). +- Implemented `brake` for `DriveBase` ([support#881]). ### Changed - The `use_gyro` method is added to the normal `DriveBase` class instead of @@ -27,6 +28,7 @@ tight loop ([support#1151]). [pybricks-micropython#104]: https://github.com/pybricks/pybricks-micropython/pull/104 +[support#881]: https://github.com/pybricks/support/issues/881 [support#1054]: https://github.com/pybricks/support/issues/1054 [support#1140]: https://github.com/pybricks/support/issues/1140 [support#1151]: https://github.com/pybricks/support/issues/1151 diff --git a/pybricks/robotics/pb_type_drivebase.c b/pybricks/robotics/pb_type_drivebase.c index 0f154e559..f9c08dabf 100644 --- a/pybricks/robotics/pb_type_drivebase.c +++ b/pybricks/robotics/pb_type_drivebase.c @@ -222,6 +222,20 @@ STATIC mp_obj_t pb_type_DriveBase_stop(mp_obj_t self_in) { } MP_DEFINE_CONST_FUN_OBJ_1(pb_type_DriveBase_stop_obj, pb_type_DriveBase_stop); +// pybricks.robotics.DriveBase.brake +STATIC mp_obj_t pb_type_DriveBase_brake(mp_obj_t self_in) { + + // Cancel awaitables. + pb_type_DriveBase_obj_t *self = MP_OBJ_TO_PTR(self_in); + pb_type_awaitable_update_all(self->awaitables, PB_TYPE_AWAITABLE_OPT_CANCEL_ALL); + + // Stop hardware. + pb_assert(pbio_drivebase_stop(self->db, PBIO_CONTROL_ON_COMPLETION_BRAKE)); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(pb_type_DriveBase_brake_obj, pb_type_DriveBase_brake); + // pybricks.robotics.DriveBase.distance STATIC mp_obj_t pb_type_DriveBase_distance(mp_obj_t self_in) { 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[] = { { MP_ROM_QSTR(MP_QSTR_turn), MP_ROM_PTR(&pb_type_DriveBase_turn_obj) }, { MP_ROM_QSTR(MP_QSTR_drive), MP_ROM_PTR(&pb_type_DriveBase_drive_obj) }, { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&pb_type_DriveBase_stop_obj) }, + { MP_ROM_QSTR(MP_QSTR_brake), MP_ROM_PTR(&pb_type_DriveBase_brake_obj) }, { MP_ROM_QSTR(MP_QSTR_distance), MP_ROM_PTR(&pb_type_DriveBase_distance_obj) }, { MP_ROM_QSTR(MP_QSTR_angle), MP_ROM_PTR(&pb_type_DriveBase_angle_obj) }, { MP_ROM_QSTR(MP_QSTR_done), MP_ROM_PTR(&pb_type_DriveBase_done_obj) },