Skip to content

Commit

Permalink
fix: resolve motor re-plug bug
Browse files Browse the repository at this point in the history
  • Loading branch information
afarago committed Oct 9, 2024
1 parent cb4e33b commit 4b63eec
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions hub-scripts/_builtin_port_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,33 +151,40 @@ def update_force_sensor(port, port_index, type_id):
# Any motor with rotation sensors.
def update_motor(port, port_index, type_id):
motor = Motor(port)
while True:
angle = motor.angle()
angle_mod = motor.angle() % 360
if angle_mod > 180:
angle_mod -= 360
rotations = round((angle - angle_mod) / 360)
data = f"a={motor.angle()}°"
if angle != angle_mod:
data += f"\tr={rotations}R\tra={angle_mod}°"
msg = f"{port}\t{type_id}\t{data}"

# check commands
if len(port_commands[port_index]):
command = port_commands[port_index].pop(0)
if command[0] == ord("r"):
direction = command[1]
yield motor.run_time(100 * direction, 300, Stop.COAST, wait=False)

yield msg
try:
while True:
angle = motor.angle()
angle_mod = motor.angle() % 360
if angle_mod > 180:
angle_mod -= 360
rotations = round((angle - angle_mod) / 360)
data = f"a={motor.angle()}°"
if angle != angle_mod:
data += f"\tr={rotations}R\tra={angle_mod}°"
msg = f"{port}\t{type_id}\t{data}"

# check commands
if len(port_commands[port_index]):
command = port_commands[port_index].pop(0)
if command[0] == ord("r"):
direction = command[1]
yield motor.run_time(100 * direction, 300, Stop.COAST, wait=False)

yield msg
except:
if motor: motor.close()
raise


# Any motor without rotation sensors.
def update_dc_motor(port, port_index, type_id):
motor = DCMotor(port)
while True:
yield f"{port}\t{type_id}"

try:
while True:
yield f"{port}\t{type_id}"
except:
if motor: motor.close()
raise

# Any unknown Powered Up device.
def unknown_pup_device(port, port_index, type_id):
Expand Down

0 comments on commit 4b63eec

Please sign in to comment.