Skip to content

Commit

Permalink
pbio/sys/main: turn off all motor outputs on shutdown
Browse files Browse the repository at this point in the history
In most cases, pbio_stop_all() was already stopping motors, but it
doesn't turn off motor outputs for non-motor devices that need extra
power. This was causing the SPIKE Light Matrix to turn on all green LEDs
when any hub was turned off.

This fix is a bit of a hack since it is going behind the back of the
higher-level I/O port stuff, but it works for now.
  • Loading branch information
dlech committed Sep 7, 2024
1 parent b85a8d4 commit 9727891
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- Fixed not able to connect to new Technic Move hub with `LWP3Device()`.
- Removed `gc_collect()` from `tools.run_task()` loop to fix unwanted delays.
- Fixed `await wait(0)` never yielding, so parallel tasks could lock up ([support#1429]).
- Fixed some sensors not powering off when the hub is turned off.

### Removed
- Removed `loop_time` argument to `pybricks.tools.run_task` as this wasn't
Expand Down
15 changes: 15 additions & 0 deletions lib/pbio/include/pbdrv/motor_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ pbio_error_t pbdrv_motor_driver_set_duty_cycle(pbdrv_motor_driver_dev_t *driver,
*/
pbio_error_t pbdrv_motor_driver_get_device_type_id(pbio_port_id_t port, pbdrv_legodev_type_id_t *type_id);

/**
* Powers off all motor outputs.
*/
static inline void pbdrv_motor_driver_power_off(void) {
for (uint8_t i = 0; i < PBDRV_CONFIG_MOTOR_DRIVER_NUM_DEV; i++) {
pbdrv_motor_driver_dev_t *driver;
if (pbdrv_motor_driver_get_dev(i, &driver) == PBIO_SUCCESS) {
pbdrv_motor_driver_coast(driver);
}
}
}

#else

static inline pbio_error_t pbdrv_motor_driver_get_dev(uint8_t id, pbdrv_motor_driver_dev_t **driver) {
Expand All @@ -94,6 +106,9 @@ static inline pbio_error_t pbdrv_motor_driver_get_device_type_id(pbio_port_id_t
return PBIO_ERROR_NOT_SUPPORTED;
}

static inline void pbdrv_motor_driver_power_off(void) {
}

#endif

#endif // _PBDRV_MOTOR_DRIVER_H_
Expand Down
13 changes: 13 additions & 0 deletions lib/pbio/sys/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdint.h>

#include <pbdrv/ioport.h>
#include <pbdrv/motor_driver.h>
#include <pbdrv/reset.h>
#include <pbdrv/usb.h>
#include <pbio/main.h>
Expand Down Expand Up @@ -113,10 +114,22 @@ int main(int argc, char **argv) {
while (pbio_do_one_event()) {
}

<<<<<<< HEAD
// Some hubs will turn themselves back on if I/O port VCC is off when
// we call pbdrv_reset_power_off(). So don't turn off power until the
// return value of pbdrv_ioport_power_off() tells us it is safe to do
// so.
||||||| parent of b693ffcf3 (pbio/sys/main: turn off all motor outputs on shutdown)
// Some hubs will turn themselves back on if VCC is off when we call
// pbdrv_reset_power_off(). So don't turn off power until the return
// value of pbdrv_ioport_power_off() tells us it is safe to do so.
=======
pbdrv_motor_driver_power_off();

// Some hubs will turn themselves back on if VCC is off when we call
// pbdrv_reset_power_off(). So don't turn off power until the return
// value of pbdrv_ioport_power_off() tells us it is safe to do so.
>>>>>>> b693ffcf3 (pbio/sys/main: turn off all motor outputs on shutdown)
if (pbdrv_ioport_power_off()) {
continue;
}
Expand Down

0 comments on commit 9727891

Please sign in to comment.