Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] print() while the hub is disconnected can briefly hang the hub. #738

Closed
laurensvalk opened this issue Oct 18, 2022 · 4 comments
Closed
Assignees
Labels
bug Something isn't working hub: primehub/inventorhub Issues related to the LEGO SPIKE Prime hub and LEGO MINDSTORMS Robot Invetor hub software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime)

Comments

@laurensvalk
Copy link
Member

Describe the bug
Doing a lot of print statements while the hub is disconnected can briefly hang the hub.

The data is supposed to be ignored when the buffer is full but apparently that is not quite happening as intended.

To reproduce, run this on a SPIKE "drivebase", or just attach two motors and let them spin freely. Run it without Pybricks Code connected. When it starts printing at the end, you can consistently see the 3x3 matrix animation get stuck briefly.

Occasionally (not often), it also pushes the motors out of sync (#679), which is how I first noticed this.

from pybricks.pupdevices import Motor
from pybricks.tools import wait
from pybricks.parameters import Port, Direction
from pybricks.robotics import DriveBase
from pybricks import version

print(version)

# Initialize default "Driving Base" with medium motors and wheels.
left_motor = Motor(Port.C, Direction.COUNTERCLOCKWISE)
right_motor = Motor(Port.D)
drive_base = DriveBase(left_motor, right_motor, wheel_diameter=56, axle_track=112)

# Allocate logs for motors and controller signals.
DURATION = 6000
left_motor.log.start(DURATION)
right_motor.log.start(DURATION)
drive_base.distance_control.log.start(DURATION)
drive_base.heading_control.log.start(DURATION)

# Drive straight forward and back again.
drive_base.straight(500)
drive_base.straight(-500)

# Wait so we can also log hold capability, then turn off the motor completely.
wait(100)
drive_base.stop()

# Transfer data logs.
print("Transferring data...")
left_motor.log.save("servo_left.txt")
right_motor.log.save("servo_right.txt")
drive_base.distance_control.log.save("control_distance.txt")
drive_base.heading_control.log.save("control_heading.txt")
print("Done")
@laurensvalk laurensvalk added triage Issues that have not been triaged yet bug Something isn't working hub: primehub/inventorhub Issues related to the LEGO SPIKE Prime hub and LEGO MINDSTORMS Robot Invetor hub software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) and removed triage Issues that have not been triaged yet labels Oct 18, 2022
@dlech
Copy link
Member

dlech commented Oct 18, 2022

Are we sure the problem is with printing and not log.save? When there is no connection, it will loop through all log lines without ever calling the contiki event loop (normally, printing will block when the buffer is full and call MICROPY_EVENT_POLL_HOOK).

We already have:

        // Writing data can take a while, so give MicroPython some time too
        mp_handle_pending(true);

in this loop. Maybe we need to change it to MICROPY_EVENT_POLL_HOOK?

Not calling the contiki events would also explain the motors going out of sync.

@laurensvalk
Copy link
Member Author

Good call, will try it out.

laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Oct 18, 2022
When the hub is not connected, it will still try to output
all the data, so we need to make sure that system resources
get some time while that happens.

Fixes: pybricks/support#738
@laurensvalk
Copy link
Member Author

That seems to work. The connected case does seem to run a lot slower that way. I'd have to dig into why that happens but I can live with that for now.

Patch incoming.

@dlech
Copy link
Member

dlech commented Oct 18, 2022

MICROPY_EVENT_POLL_HOOK waits for an interrupt before continuing. So we could consider calling:

MICROPY_VM_HOOK_LOOP
mp_handle_pending(true);

instead, which won't wait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hub: primehub/inventorhub Issues related to the LEGO SPIKE Prime hub and LEGO MINDSTORMS Robot Invetor hub software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime)
Projects
None yet
Development

No branches or pull requests

2 participants