Skip to content

New gyro angle and rate properties not working? #517

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

Having run some scripts that use the gyro sensor my impression is that the new properties angle and rate do not work (or else I am not using them correctly). Here is a script I used to test the new gyro properties. Note that there is no code to set the mode of the gyro sensor – I am assuming that is not necessary.

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

steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
gyro = GyroSensor()

option = 1
if option == 1:
    print('Angle: ' + str(gyro.angle) + ' Rate: ' + str(gyro.rate), file=sys.stderr)
    sleep(2)
    print('Angle: ' + str(gyro.angle) + ' Rate: ' + str(gyro.rate), file=sys.stderr)
else:
    print('Rate and Angle: ' + str(gyro.rate_and_angle), file=sys.stderr)
    sleep(2)
    print('Rate and Angle: ' + str(gyro.rate_and_angle), file=sys.stderr)

steer_pair.on_for_seconds(steering=100, speed=20, seconds=6, block=False)

for i in range(15):
    if option == 1:
        print('Angle: ' + str(gyro.angle) + ' Rate: ' + str(gyro.rate), file=sys.stderr)
    else:
        print('Rate and Angle: ' + str(gyro.rate_and_angle), file=sys.stderr)
    sleep(0.5)

With option = 1 the scripts obtains separate angle and rate readings and with option= 2 the script obtains combined rate_and_angle readings. Two initial readings are made before the robot moves, then the robot (standard Educator Vehicle) is made to turn on the spot for six seconds (it turns at about 90 degrees per second, so about 540 degrees in 6s) and then readings are taken while the robot turns. Readings are printed to the output panel of VS Code.

With option = 1 I was expecting to see the angle increase steadily and the rate to remain steady at around 90 (degrees per second). Here are my results:
Angle: 0 Rate: 1
Angle: 0 Rate: 1
Angle: 0 Rate: 0
Angle: 0 Rate: 0
Angle: 0 Rate: -1
Angle: 0 Rate: -1
Angle: 0 Rate: 1
Angle: 0 Rate: 0
Angle: 0 Rate: 0
Angle: 0 Rate: 0
Angle: 0 Rate: 1
Angle: 0 Rate: -1
Angle: 0 Rate: -1
Angle: 0 Rate: 0
Angle: 0 Rate: 0
Angle: 0 Rate: 0
Angle: 0 Rate: 0

These results seem totally wrong.

With option 2 (combined rate_and_angle readings) I got the following
Rate and Angle: (0, 1)
Rate and Angle: (0, 1)
Rate and Angle: (0, 0)
Rate and Angle: (33, 88)
Rate and Angle: (82, 86)
Rate and Angle: (126, 92)
Rate and Angle: (173, 89)
Rate and Angle: (218, 86)
Rate and Angle: (265, 96)
Rate and Angle: (313, 93)
Rate and Angle: (362, 93)
Rate and Angle: (408, 98)
Rate and Angle: (456, 89)
Rate and Angle: (505, 89)
Rate and Angle: (550, 88)
Rate and Angle: (582, 0)
Rate and Angle: (582, 0)

This seems entirely plausible (note how the last two readings were taken after the robot stopped turning) but the readings are clearly angle and rate, in that order, and not rate and angle as indicated in the name of the property.

When I ran option 1 again I got this message
Traceback (most recent call last):
File "/home/robot/MINE/gyro_test4.py", line 13, in
print('Angle: ' + str(gyro.angle) + ' Rate: ' + str(gyro.rate), file=sys.stderr)
File "/usr/lib/python3/dist-packages/ev3dev2/sensor/lego.py", line 536, in angle
self.mode = self.MODE_GYRO_ANG
File "/usr/lib/python3/dist-packages/ev3dev2/sensor/init.py", line 191, in mode
self._mode = self.set_attr_string(self._mode, 'mode', value)
File "/usr/lib/python3/dist-packages/ev3dev2/init.py", line 275, in set_attr_string
return self._set_attribute(attribute, name, value)
File "/usr/lib/python3/dist-packages/ev3dev2/init.py", line 238, in _set_attribute
self._raise_friendly_access_error(ex, name)
File "/usr/lib/python3/dist-packages/ev3dev2/init.py", line 259, in _raise_friendly_access_error
raise driver_error
File "/usr/lib/python3/dist-packages/ev3dev2/init.py", line 235, in _set_attribute
attribute.write(value)
TimeoutError: [Errno 110] Connection timed out

Further testing confirmed that every time I switched from reading combined rate_and_angle to separate readings of angle and rate I got that error, requiring me to unplug and reconnect the gyro sensor.

I also modified the script to try the old gyro value() function i.e. I first ran
gyro.mode='GYRO-ANG'
and then repeatedly ran
print('Value: ' + str(gyro.value()), file=sys.stderr)
and got the following values for angle only
Value: 0
Angle: 0
Value: 0
Value: 33
Value: 75
Value: 116
Value: 151
Value: 193
Value: 234
Value: 281
Value: 328
Value: 375
Value: 423
Value: 467
Value: 508
Value: 541
Value: 541

These values seem entirely plausible and in line with the angles returned by the rate_and_angle property.

Conclusions, assuming my script is valid:

  • The new property rate_and_angle works fine except that the returned values are in the wrong order relative to the name of the property.
  • The new property angle does not work.
  • The new property rate does not work.
  • Reading angle or rate (I tried both separately) after reading rate_and_angle causes an error.

Can someone please tell me what's wrong with my script or confirm/refute my observations?

Activity

  1. dlech commented on Sep 12, 2018

    @dlech
    Member

    To be sure that this is not a gyro drift problem, make sure the robot is completely at rest when starting the program. The slightest movement can cause the sensor reading to act as you have described. Normally, switching modes is enough to "reset" the sensor, but sometimes, the sensor can just get in a bad state and you have to unplug it and plug it back in to get it to behave normally again.

  2. ndward commented on Sep 12, 2018

    @ndward
    Author

    The gyro was definitely at rest each time the program was started, and I don't think drift could explain the huge difference between the combined and separate readings noted above.

    Talking about starting the program reminds me of another question: angle is relative to what? The documentation says angle: The number of degrees that the sensor has been rotated since it was put into this mode.

    But isn't it really the angle since the program was launched? Is it the same thing?

  3. dlech commented on Sep 12, 2018

    @dlech
    Member

    Ah, I see what is going on now. When you read each value individually, the sensor switches modes, which resets the sensor each time you read the property. If you want to read both values without the angle resetting to 0, the only way to do it to use the rate_and_angle property.

    The angle is relative to the position of the robot when you turned it on. Unless you switch modes on the sensor (which happens when you read a property), then the angle is reset to 0.

  4. ndward commented on Sep 12, 2018

    @ndward
    Author

    I modified the script like this so that with option = 1 the only mode that is used is angle

    #!/usr/bin/env python3
    from ev3dev2.motor import MoveSteering, OUTPUT_B, OUTPUT_C
    from ev3dev2.sensor.lego import GyroSensor
    import sys
    from time import sleep
    
    steer_pair = MoveSteering(OUTPUT_B, OUTPUT_C)
    gyro = GyroSensor()
    
    option = 1
    if option == 1:
        print('Angle: ' + str(gyro.angle), file=sys.stderr)
        sleep(2)
        print('Angle: ' + str(gyro.angle), file=sys.stderr)
    else:
        print('Rate and Angle: ' + str(gyro.rate_and_angle), file=sys.stderr)
        sleep(2)
        print('Rate and Angle: ' + str(gyro.rate_and_angle), file=sys.stderr)
    
    steer_pair.on_for_seconds(steering=100, speed=20, seconds=6, block=False)
    
    for i in range(15):
        if option == 1:
            print('Angle: ' + str(gyro.angle), file=sys.stderr)
        else:
            print('Rate and Angle: ' + str(gyro.rate_and_angle), file=sys.stderr)
        sleep(0.5)
    

    That gave these results:
    Angle: 0
    Angle: 0
    Angle: 0
    Angle: 19
    Angle: 3
    Angle: 1
    Angle: 1
    Angle: 0
    Angle: -2
    Angle: 0
    Angle: 2
    Angle: -3
    Angle: 5
    Angle: -1
    Angle: 3
    Angle: -9
    Angle: 0

    While these readings were being taken the robot turned about 540° but the readings don't show this at all (unlike when I read rate_and_angle). I don't see that the angle values have any worth.

  5. WasabiFan commented on Sep 12, 2018

    @WasabiFan
    Member

    Was the sensor mounted in the correct direction, so that the rotating arrows align with the way the robot is spinning?

  6. dlech commented on Sep 12, 2018

    @dlech
    Member

    I wonder if #502 will fix the problem. I think that since each call to the angle property currently sets the mode, that could be resetting the angle reading back to 0.

  7. WasabiFan commented on Sep 12, 2018

    @WasabiFan
    Member

    @dlech I'm thinking the same thing. I plan to track down a gyro sensor do some testing on that later today if someone doesn't get to it first.

  8. ndward commented on Sep 12, 2018

    @ndward
    Author

    Yes, the sensor was correctly mounted.

  9. WasabiFan commented on Sep 13, 2018

    @WasabiFan
    Member

    I couldn't find a gyro sensor so I won't be able to test this right now. I am guessing that #502 will fix the issue here, so unless someone else tests it first I'll leave this until we release those changes so that @ndward can try it out.

  10. 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
    

    @ndward Can you confirm that this is resolved?

  11. dwalton76 commented on Jun 8, 2019

    @dwalton76
    Collaborator

    @ndward please re-open if this is still an issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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