Skip to content

Commit 5ed7298

Browse files
laurensvalkdlech
authored andcommitted
pbio/dcmotor: Implement close() to re-use device.
Fixes: pybricks/support#904
1 parent be3beb4 commit 5ed7298

File tree

7 files changed

+48
-0
lines changed

7 files changed

+48
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
## [Unreleased]
66

7+
### Added
8+
- Added `close()` method to `DCMotor` and `Motor` so they can be closed and
9+
re-initialized later.
10+
11+
[support#904]: https://github.com/pybricks/support/issues/904
12+
713
## [3.2.2] - 2023-01-06
814

915
### Fixed

lib/pbio/include/pbio/dcmotor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ void pbio_dcmotor_stop_all(bool clear_parents);
6565

6666
// Setup and status:
6767

68+
pbio_error_t pbio_dcmotor_close(pbio_dcmotor_t *dcmotor);
6869
pbio_error_t pbio_dcmotor_get_dcmotor(pbio_port_id_t port, pbio_dcmotor_t **dcmotor);
6970
pbio_error_t pbio_dcmotor_setup(pbio_dcmotor_t *dcmotor, pbio_direction_t direction);
7071
void pbio_dcmotor_get_state(pbio_dcmotor_t *dcmotor, pbio_dcmotor_actuation_t *actuation, int32_t *voltage_now);

lib/pbio/src/dcmotor.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ void pbio_dcmotor_stop_all(bool clear_parents) {
6060
}
6161
}
6262

63+
/**
64+
* Stops and closes DC motor instance so it can be used in another application.
65+
*
66+
* @param [in] dcmotor The DC motor instance.
67+
* @return Error code.
68+
*/
69+
pbio_error_t pbio_dcmotor_close(pbio_dcmotor_t *dcmotor) {
70+
71+
// Coast the motor and remember error.
72+
pbio_error_t stop_err = pbio_dcmotor_coast(dcmotor);
73+
74+
// Stop its parents and reset parent objects to free up this motor
75+
// for use in new objects, even if stopping has failed.
76+
pbio_error_t parent_err = pbio_parent_stop(&dcmotor->parent, true);
77+
78+
// Return original error corresponding to stopping the motor.
79+
if (stop_err != PBIO_SUCCESS) {
80+
return stop_err;
81+
}
82+
83+
// Return error of trying to stop parent structure.
84+
return parent_err;
85+
}
86+
6387
/**
6488
* Sets up the DC motor instance to be used in an application.
6589
*

lib/pbio/src/servo.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ static void pbio_servo_update_loop_set_state(pbio_servo_t *srv, bool update) {
5959
* @return True if up and running, false if not.
6060
*/
6161
bool pbio_servo_update_loop_is_running(pbio_servo_t *srv) {
62+
63+
// Servo must be the parent of its dc motor.
64+
if (!pbio_parent_equals(&srv->dcmotor->parent, srv)) {
65+
pbio_servo_update_loop_set_state(srv, false);
66+
}
67+
6268
return srv->run_update_loop;
6369
}
6470

pybricks/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ extern const mp_obj_type_t pb_type_DCMotor;
107107

108108
// Nonstatic objects shared between Motor and DCMotor
109109
void common_DCMotor_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
110+
MP_DECLARE_CONST_FUN_OBJ_1(common_DCMotor_close_obj);
110111
MP_DECLARE_CONST_FUN_OBJ_KW(common_DCMotor_duty_obj);
111112
MP_DECLARE_CONST_FUN_OBJ_1(common_DCMotor_stop_obj);
112113
MP_DECLARE_CONST_FUN_OBJ_1(common_DCMotor_brake_obj);

pybricks/common/pb_type_dcmotor.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,17 @@ STATIC mp_obj_t common_DCMotor_dc_settings(size_t n_args, const mp_obj_t *pos_ar
125125
}
126126
MP_DEFINE_CONST_FUN_OBJ_KW(common_DCMotor_dc_settings_obj, 1, common_DCMotor_dc_settings);
127127

128+
// pybricks._common.DCMotor.close
129+
// pybricks._common.Motor.close
130+
STATIC mp_obj_t common_DCMotor_close(mp_obj_t self_in) {
131+
pb_assert(pbio_dcmotor_close(get_dcmotor_from_object(self_in)));
132+
return mp_const_none;
133+
}
134+
MP_DEFINE_CONST_FUN_OBJ_1(common_DCMotor_close_obj, common_DCMotor_close);
135+
128136
// dir(pybricks.builtins.DCMotor)
129137
STATIC const mp_rom_map_elem_t common_DCMotor_locals_dict_table[] = {
138+
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&common_DCMotor_close_obj) },
130139
{ MP_ROM_QSTR(MP_QSTR_dc), MP_ROM_PTR(&common_DCMotor_duty_obj) },
131140
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&common_DCMotor_stop_obj) },
132141
{ MP_ROM_QSTR(MP_QSTR_brake), MP_ROM_PTR(&common_DCMotor_brake_obj) },

pybricks/common/pb_type_motor.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ STATIC const mp_rom_map_elem_t common_Motor_locals_dict_table[] = {
376376
//
377377
// Methods common to DC motors and encoded motors
378378
//
379+
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&common_DCMotor_close_obj) },
379380
{ MP_ROM_QSTR(MP_QSTR_dc), MP_ROM_PTR(&common_DCMotor_duty_obj) },
380381
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&common_DCMotor_stop_obj) },
381382
{ MP_ROM_QSTR(MP_QSTR_brake), MP_ROM_PTR(&common_DCMotor_brake_obj) },

0 commit comments

Comments
 (0)