You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] InventorHub hangs when running simple program with UltrasonicSensor #190
I created the below simple program with the Tricky bot turning on and off the lights of its Ultrasonic Sensor.
I ran the program with pybricksdev through BLE, and every time the program would hang and fail to terminate. The only way to terminate is to take out the battery from the InventorHub.
from pybricks.hubs import InventorHub
from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor
from pybricks.parameters import Direction, Port
from pybricks.robotics import DriveBase
class Tricky:
WHEEL_DIAMETER = 44
AXLE_TRACK = 88
def __init__(
self,
left_wheel_motor_port: Port = Port.A,
right_wheel_motor_port: Port = Port.B,
sport_motor_port: Port = Port.C,
distance_sensor_port: Port = Port.D,
color_sensor_port: Port = Port.E):
self.hub = InventorHub()
self.driving_base = \
DriveBase(
left_motor=Motor(port=left_wheel_motor_port,
positive_direction=Direction.CLOCKWISE),
right_motor=Motor(port=right_wheel_motor_port,
positive_direction=Direction.CLOCKWISE),
wheel_diameter=self.WHEEL_DIAMETER,
axle_track=self.AXLE_TRACK)
self.sport_motor = Motor(port=sport_motor_port,
positive_direction=Direction.CLOCKWISE)
self.color_sensor = ColorSensor(port=color_sensor_port)
self.distance_sensor = UltrasonicSensor(port=distance_sensor_port)
if __name__ == '__main__':
TRICKY = Tricky()
TRICKY.distance_sensor.lights.on(100)
TRICKY.distance_sensor.lights.off()
@laurensvalk I can confirm that the program works with any wait time of at least 1 milisec (i.e. wait(1) works), while any wait time less than 1 milisec (e.g. wait(0.999999)) would cause the Inventor Hub to hang.
(Actually, wait(0.999999999) works sporadically -- but still causes hanging from time to time).
So the magic threshold seems to be very, very precisely 1 milisec!
Question is: why? :) Is there a magic number behind the implementation that causes this behavior?
This one is interesting because the mode switch already triggers a 50ms delay, and setting it to 60ms does not resolve it. So it's strange that 1ms in the user program would make the difference.
This one is interesting because the mode switch already triggers a 50ms delay, and setting it to 60ms does not resolve it. So it's strange that 1ms in the user program would make the difference.
It's unrelated to mode changing. Rather, it's related to setting values on the sensor. I've added a delay as a workaround. We may or may not be able to fix this at the driver level when we revisit the drivers for the LUMP devices.
@laurensvalk@dlech I've installed the latest firmware and can confirm that the problem has gone away in our Tricky programs.
Thank you for the quick fix.
Vinh
P.S.: Great to learn of the Hub's proven durability. Can't afford to fund any replacements -- Santa in December seemed to have a good budget but Daddy in January is fundamentally broke. 😄
Activity
laurensvalk commentedon Jan 4, 2021
The following program works for me:
Could you test your example in Pybricks Code? And potentially make it smaller to identify what causes it to hang?
TheVinhLuong102 commentedon Jan 4, 2021
@laurensvalk I can confirm that the program works with any wait time of at least 1 milisec (i.e. wait(1) works), while any wait time less than 1 milisec (e.g. wait(0.999999)) would cause the Inventor Hub to hang.
(Actually, wait(0.999999999) works sporadically -- but still causes hanging from time to time).
So the magic threshold seems to be very, very precisely 1 milisec!
Question is: why? :) Is there a magic number behind the implementation that causes this behavior?
Vinh
dlech commentedon Jan 4, 2021
Waiting has a 1 millisecond resolution so everything after the decimal is ignored.
wait(0.999)is the same aswait(0).TheVinhLuong102 commentedon Jan 4, 2021
@dlech, I see. So this issue reduces to the
ultrasonic_sensor.lights.on()function needing await()then. :)laurensvalk commentedon Jan 5, 2021
Thanks @TheVinhLuong102 for investigating further; that's very helpful.
Bug can be reproduced with
and also with
This one is interesting because the mode switch already triggers a 50ms delay, and setting it to 60ms does not resolve it. So it's strange that 1ms in the user program would make the difference.
pybricks/util_pb/pb_device_stm32: delay set values
laurensvalk commentedon Jan 5, 2021
It's unrelated to mode changing. Rather, it's related to setting values on the sensor. I've added a delay as a workaround. We may or may not be able to fix this at the driver level when we revisit the drivers for the LUMP devices.
laurensvalk commentedon Jan 5, 2021
Fixed via pybricks/pybricks-micropython@f94ab4e
Thanks for reporting!
TheVinhLuong102 commentedon Jan 5, 2021
Fantastic @laurensvalk, thank you!
So when should we redownload and reinstall the firmware for this change to take effect? Are your firmware builds created nightly?
P.S.: Now I can tell Antoni this is not Daddy's fault. He was like "Daddy you are playing so roughly with my Hub -- you are breaking it!!" :))
laurensvalk commentedon Jan 5, 2021
Any time you like. The download link and getting started steps in #167 still apply; just grab the latest build via that link.
We never managed to truly break a hub so far, and believe me we've tried 😄
TheVinhLuong102 commentedon Jan 5, 2021
@laurensvalk @dlech I've installed the latest firmware and can confirm that the problem has gone away in our Tricky programs.
Thank you for the quick fix.
Vinh
P.S.: Great to learn of the Hub's proven durability. Can't afford to fund any replacements -- Santa in December seemed to have a good budget but Daddy in January is fundamentally broke. 😄