Skip to content

python3-ev3dev v2 angles of zero work badly with on_for_*() #507

Closed

Description

@ndward

ev3dev version: 4.14.61-ev3dev-2.2.2-ev3
ev3dev-lang-python version:
ii python3-ev3dev 1.2.0 all Python language bindings for ev3dev
ii python3-ev3dev2 2.0.0~beta1 all Python language bindings for ev3dev

This is a separate issue to the problem of negative angles not being handled correctly by the new python3-ev3dev v2 motor functions that I reported in issue #504.

It seems to me that if angles of zero are used with the new v2 commands on_for_degrees() and on_for_rotations() for motor pairs then the program will freeze. This is surely a bug.

I recently tried to run this script which simply sets the speed of the robot to equal the ambient light intensity:

 #!/usr/bin/env python3
from ev3dev2.motor import MoveSteering, OUTPUT_B, OUTPUT_C
from ev3dev2.sensor.lego import ColorSensor
from ev3dev2.button import Button
from time import sleep

cl = ColorSensor()
btn = Button()
steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)

while not btn.any():    # Stop program by pressing any button
    steer_pair.on(steering=0,speed=cl.ambient_light_intensity)
    sleep(0.2)

The script should work but quickly fails because as soon as the light intensity falls to zero there is an error:
Traceback (most recent call last):
File "/home/robot/MINE/example_6.py", line 12, in
steer_pair.on(steering=0,speed=cl.ambient_light_intensity)
File "/usr/lib/python3/dist-packages/ev3dev2/motor.py", line 1913, in on
MoveTank.on(self, SpeedNativeUnits(left_speed), SpeedNativeUnits(right_speed))
File "/usr/lib/python3/dist-packages/ev3dev2/motor.py", line 1844, in on
(left_speed_native_units, right_speed_native_units) = self._unpack_speeds_to_native_units(left_speed, right_speed)
File "/usr/lib/python3/dist-packages/ev3dev2/motor.py", line 1739, in _unpack_speeds_to_native_units
"Either left_speed or right_speed must be non-zero"
AssertionError: Either left_speed or right_speed must be non-zero

There also seem to be a problem with single motor commands that use angles, though in this case a warning is triggered rather than an error. Here is a script to demonstrate how an angle of zero makes single motor commands give a warning (as opposed to an error) while an angle of zero for a motor pair makes the program freeze:

#!/usr/bin/env python3
from ev3dev2.motor import MoveSteering, LargeMotor, OUTPUT_B, OUTPUT_C
from time import sleep
from ev3dev2.sound import Sound

sound = Sound()
lm = LargeMotor(OUTPUT_B)
steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)

sound.beep()
'''
For single motors, 
degrees=0 triggers a WARNING that zero is not valid but it is a warning
not an error, and the program continues correctly
'''
lm.on_for_degrees(speed_pct=30, degrees = 0)

sleep(5)
sound.beep()
'''
For motor pairs, 
degrees=0 causes the script to FREEZE, so it never completes
'''
steer_pair.on_for_degrees(steering=0, speed=30, degrees = 0)

As shown in the first script, it is perfectly normal that a motor or motor pair may sometimes receive the order to rotate through zero degrees, so the motor or motor pair must be able to handle this without giving a warning or terminating the program. I imagine EV3-G is perfectly happy with power values of zero and that they do cause the program to terminate.

Activity

  1. self-assigned this
    on Sep 8, 2018
  2. matskew commented on Sep 8, 2018

    @matskew

    motor.py's wait(self, cond, timeout=None) method polls the motor waiting for a change. It assumes that when it is called the motor is running, and it is waiting for a change to either 'stalled' or 'not running'. Unfortunately if you ask the motor to move zero, then it never was running in the first place. Thus the polling will block forever.

    You can narrow the race condition by moving the poll to the end of wait()'s while loop, and therefore checking the condition before blocking.

  3. WasabiFan commented on Sep 24, 2018

    @WasabiFan
    Member

    Fix released in 2.0.0~beta2. To upgrade:

    sudo apt-get update
    sudo apt-get install --only-upgrade python3-ev3dev2
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions