Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
python-ev3dev2 (2.0.0~beta3) stable; urgency=medium

* GyroSensor wait_until_angle_changed_by added direction_sensitive option
* Added MoveDifferential
* Sound: removed play() in favor of play_file()
* suppress unittest ResourceWarning messages
Expand Down
22 changes: 19 additions & 3 deletions ev3dev2/sensor/lego.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,16 +626,32 @@ def reset(self):
self._ensure_mode(self.MODE_GYRO_ANG)
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, direction_sensitive=False):
"""
Wait until angle has changed by specified amount.
If ``direction_sensitive`` is True we will wait until angle has changed
by ``delta`` and with the correct sign.
If ``direction_sensitive`` is False (default) we will wait until angle has changed
by ``delta`` in either direction.
"""
assert self.mode in (self.MODE_GYRO_G_A, self.MODE_GYRO_ANG,
self.MODE_TILT_ANG),\
'Gyro mode should be MODE_GYRO_ANG, MODE_GYRO_G_A or MODE_TILT_ANG'
start_angle = self.value(0)
while abs(start_angle - self.value(0)) < delta:
time.sleep(0.01)

if direction_sensitive:
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.

while (self.value(0) - start_angle) < delta:
time.sleep(0.01)
else:
delta *= -1
while (start_angle - self.value(0)) < delta:
time.sleep(0.01)
else:
while abs(start_angle - self.value(0)) < delta:
time.sleep(0.01)


class InfraredSensor(Sensor, ButtonBase):
Expand Down