Skip to content

GyroSensor wait_until_angle_changed_by added direction_sensitive option - #551

Merged
dwalton76 merged 5 commits into
ev3dev:ev3dev-stretchfrom
dwalton76:wait_until_angle_changed_by
Nov 8, 2018
Merged

dwalton76 merged 5 commits into
ev3dev:ev3dev-stretchfrom
dwalton76:wait_until_angle_changed_by

Conversation

@dwalton76

Copy link
Copy Markdown
Collaborator

For issue #534

@dwalton76

dwalton76 commented Oct 31, 2018 •

Copy link
Copy Markdown
Collaborator Author

Tested via

#!/usr/bin/env python3

from ev3dev2.sensor.lego import GyroSensor
from ev3dev2.auto import INPUT_1, INPUT_2

gyro = GyroSensor(INPUT_2)
print("{} angle {}".format(gyro, gyro.angle))
gyro.wait_until_angle_changed_by(-90, True)
print("{} angle {}".format(gyro, gyro.angle))

Comment thread ev3dev2/sensor/lego.py Outdated
self._direct = self.set_attr_raw(self._direct, 'direct', 17)

def wait_until_angle_changed_by(self, delta):
def wait_until_angle_changed_by(self, delta, honor_direction=False):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "honor" might be confusing to non-native English speakers; could we call it something else? Maybe direction_sensitive? specific_direction?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

direction_sensitive sounds good

Comment thread ev3dev2/sensor/lego.py
time.sleep(0.01)

if honor_direction:
if delta > 0:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a lot of duplicated code. Can we combine this to be one loop? I think this condition might work:

while (
        (abs(self.value(0) - start_angle) < delta)  # the sensor still hasn't moved far enough... 
        or (honor_direction and copysign(delta, 1) == copysign(self.value(0) - start_angle, 1))) # … or, it has moved in the wrong direction

I'm not entirely sure my line break structure will fly with the Python interpreter, but you get the gist.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I find the expanded/verbose model a little easier to follow :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to remove the duplicated loop code... whether that's with a more clever loop condition or something else doesn't matter to me.

Comment thread debian/changelog Resolved
@WasabiFan WasabiFan changed the title GyroSensor wait_until_angle_changed_by added honor_direction option GyroSensor wait_until_angle_changed_by added direction_sensitive option Nov 7, 2018
@dwalton76
dwalton76 merged commit 21205ea into ev3dev:ev3dev-stretch Nov 8, 2018
@dwalton76
dwalton76 deleted the wait_until_angle_changed_by branch November 8, 2018 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants