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] Running to an absolute angle immediately after boot fails sometimes #361

Closed
laurensvalk opened this issue Jun 2, 2021 · 8 comments
Labels
bug Something isn't working software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: motors Issues involving motors

Comments

@laurensvalk
Copy link
Member

laurensvalk commented Jun 2, 2021

Describe the bug
Sometimes, going to an absolute position does not work. Mostly immediately after boot, so when running a program from flash.

To reproduce

I haven't identified a consistent reproducable case yet, but it has happened several times now. I'm also not entirely sure it is only after boot, but it does seem that way. I've only tried on Primehub so far.

Perhaps the motor is synched up, but did not receive its first data value yet.

For example, this script should put two motors 180 degrees apart, but sometimes it puts them in parallel.

from pybricks.pupdevices import Motor
from pybricks.parameters import Port, Direction

SPEED = 150

right_leg = Motor(Port.A, Direction.COUNTERCLOCKWISE)
left_leg = Motor(Port.B)

left_leg.run_target(SPEED, 0)
right_leg.run_target(SPEED, 180)
@laurensvalk laurensvalk added bug Something isn't working topic: motors Issues involving motors software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) labels Jun 2, 2021
@laurensvalk
Copy link
Member Author

Perhaps the motor is synched up, but did not receive its first data value yet.

This appears to confirm it. Flash this along with the firmware so you can run it after boot.

The first time you run this it shows 0 for all positions, while it should show the absolute angle instead.

from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port
from pybricks.tools import wait

hub = PrimeHub()

angles = {}

for motor_port in (Port.A, Port.C, Port.D, Port.E):
    motor = Motor(motor_port)
    char = repr(motor_port)[5]
    angles[char] = motor.angle()
    
while True:
    for char, angle in angles.items():
        hub.display.char(char)
        wait(500)
        hub.display.number(angle)
        wait(1000)
        hub.display.off()
        wait(500)

@laurensvalk
Copy link
Member Author

In the setup during pbio_uartdev_update, we wait for (and parse) one full message. But we send the magic command for multi-mode feedback motors only after that, so we don't wait for one full message of that.

Therefore, motor data is not defined even though the motor is synced up and so we read zero.

laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Jun 4, 2021
The original comment suggested that this is used to let external devices know that a device is ready, but this isn't strictly true. It may still be reporting garbage data at this point. So, we move it to the point where it has received one good data message.

It would be better to have a proper function to check this, but this can be done as part of the bigger lpf2 driver overhaul in a future release.

pybricks/support#361
laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Jun 4, 2021
This getter was masking several different possible errors into just one generic error. We also use it in just one place, so it makes sense unpack it and save a bit of space.

pybricks/support#361
laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Jun 4, 2021
This allows higher level code to call this again as neeeded.

pybricks/support#361
laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Jun 4, 2021
At the drv level, the dc motor getter is performing several checks whether the motor is ready, so do those first.

pybricks/support#361
laurensvalk added a commit to pybricks/pybricks-micropython that referenced this issue Jun 4, 2021
When starting scripts immediately after boot, they often failed because the sensors had not yet finished syncing up. This avoids a crash by waiting for the sensor to sync up if a UART device is detected.

Related to pybricks/support#361
@laurensvalk
Copy link
Member Author

In short, this series of commits:

  • Fixes the original issue of the absolute position being read as 0 soon after boot/plug in.
  • Avoids motor ENODEV if a script is run too soon after plugging motor in or booting up, waits instead.
  • Avoids sensor ENODEV if a script is run too soon after plugging sensor in or booting up, waits instead.

You'll still get ENODEV if you're so fast that it's still running the device connection manager, but this is much less likely and therefore much less annoying.

Since it fixes a bug and improves the user experience, I think it's worth including this in v3.0. Do you want to review this @dlech before merging?

@dlech
Copy link
Member

dlech commented Jun 4, 2021

It all looks reasonable to me. We should probably wait and call what we have now 3.0 though since this isn't a critical bug.

@laurensvalk
Copy link
Member Author

I’d say the rotation sensor giving garbage values is probably a critical bug though. For example, a humanoid robot will fall flat on its face.

@dlech
Copy link
Member

dlech commented Jun 4, 2021

FWIW, when I say "critical bug", I mean it frequently affects a large number of users and there is no workaround.

@laurensvalk
Copy link
Member Author

Right, but in robotics I’d include mechanical failures even if they don’t occur very often - safety first. There’s no such thing as a soft reboot if it just jumped off your desk.

For example, depending on the user script, this will try to flip the motor by up to 180 degrees at maximum power, which is probably critical.

And since we have the option to save programs permanently on the hub, this is part of the release features.

@laurensvalk
Copy link
Member Author

Merged, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working software: pybricks-micropython Issues with Pybricks MicroPython firmware (or EV3 runtime) topic: motors Issues involving motors
Projects
None yet
Development

No branches or pull requests

2 participants