Skip to content

Create example scripts for the EDUCATOR robot#13

Merged
dwalton76 merged 1 commit intoev3dev:masterfrom
gregcowell:educator
Oct 12, 2017
Merged

Create example scripts for the EDUCATOR robot#13
dwalton76 merged 1 commit intoev3dev:masterfrom
gregcowell:educator

Conversation

@gregcowell
Copy link
Copy Markdown
Member

This addresses issue #2. These are just simple demonstration scripts that use the new high level API. I'd like to create a more challenging script later (maze solver).

Comment thread robots/EDUCATOR/color.py
#!/usr/bin/env python3
"""Make robot say whatever color it observes with the color sensor."""

from ev3dev.sensor.lego import ColorSensor
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.

Can you import from ev3dev.sensor here instead? Frankly, I think we should make the lego module "python private" and only advertise the ev3dev.sensor module. I don't see a reason that a user would want more detail.

Comment thread robots/EDUCATOR/gyro.py Outdated
def __init__(self):
"""Create Gyro object if connected."""
super().__init__()
assert self.connected, 'Connect a Gyro to a sensor port'
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.

Comment thread robots/EDUCATOR/gyro.py Outdated
def wait_until_changed_by(self, delta):
"""Wait until angle has changed by specified amount."""
start_angle = self.value()
while abs(start_angle - self.value()) < delta:
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.

Hmmmm... busy-waiting is a bit scary as the EV3 has so little processing power this could entirely lock up the brick. Can we add a short sleep?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd be game for putting this to the GyroSensor class instead...other gyro programs may find this handy

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thinking outloud...would be cool if long term we can make all of the sensor files work with poll()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay if I raise a pull request to add wait_until_changed_by() to the GyroSensor class ? I’ll include a short sleep.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

sounds good

Comment thread robots/EDUCATOR/gyro.py Outdated
while abs(start_angle - self.value()) < delta:
pass

motor_pair = MoveSteering('outB', 'outC')
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.

Use OUTPUT_B and OUTPUT_C constants so this works on all ev3dev platforms

Comment thread robots/EDUCATOR/gyro.py Outdated
super().__init__()
assert self.connected, 'Connect a Gyro to a sensor port'

def wait_until_changed_by(self, delta):
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.

@dwalton76 What are your thoughts on making this core API? We could do similar things for the ultrasonic sensor and others. Probably not an "initial release" task though.

@gregcowell
Copy link
Copy Markdown
Member Author

I've made the required changes except for the import. When I change:

from ev3dev.sensor.lego import ColorSensor

to:

from ev3dev.sensor import ColorSensor

the import fails.

Is a change required to sensor/__init__.py to make this work as intended ?

@WasabiFan
Copy link
Copy Markdown
Member

Is a change required to sensor/init.py to make this work as intended ?

@dwalton can we rearrange things? I don't think that having a lego module is useful; it would make more sense to me to either put them all into one file or put each into its own and re-export them in the init file.

@dlech
Copy link
Copy Markdown
Member

dlech commented Oct 10, 2017

can we rearrange things? I don't think that having a lego module is useful;

It may not make sense now, but if we make specialized classes for the dozens of 3rd party sensors, the it will be nice to have a bit more organization.

For example, in this case, we have the ColorSensor class. But how do you know if this is the LEGO EV3 color sensor, the LEGO NXT color sensor or the HiTechnic v1 color sensor or the HiTechnic v2 color sensor?

Comment thread robots/EDUCATOR/gyro.py Outdated
# Spin robot to the left
motor_pair.on(steering=-100, speed_pct=5)
# Wait until angle changed by 90 degrees
gyro.wait_until_changed_by(90)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is cool :) How accurate of a square does it make?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It’s reasonably accurate so long as the robot is not spinning too fast. It seems to work as well as the EV3-G equivalent. I’ve found the gyro sensor tends to lose accuracy as the batteries lose charge. Also, very occasionally the gyro sensor readings will continuously increment without any angular movement. This is nothing to do with ev3dev though. The same behaviour occurs with EV3-G.

Comment thread robots/EDUCATOR/gyro.py
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe call this square-gyro.py since the non-gyro equivalent is square.py?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea.

Comment thread robots/EDUCATOR/touch.py

# Start robot moving forward
motor_pair.on(steering=0, speed_pct=10)
# Wait until robot touches wall
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is very minor but adding a blank line before the start of a new comment makes things easier to read.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yep, I’ll fix that.

Comment thread robots/EDUCATOR/ultrasonic.py Outdated
motor_pair.on(steering=0, speed_pct=10)
# Wait until robot less than 3.5cm from cuboid
while ultrasonic_sensor.distance_centimeters > 3.5:
pass
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would put a small sleep here else it will hammer the CPU

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I’ll add a 10ms sleep to the loop.

@gregcowell
Copy link
Copy Markdown
Member Author

I've made all the required changes to the scripts.

Copy link
Copy Markdown
Member

@WasabiFan WasabiFan left a comment

Choose a reason for hiding this comment

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

These are looking quite clean with the new APIs!

Copy link
Copy Markdown
Collaborator

@dwalton76 dwalton76 left a comment

Choose a reason for hiding this comment

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

Very nice

@dwalton76 dwalton76 merged commit 70988d7 into ev3dev:master Oct 12, 2017
@dwalton76
Copy link
Copy Markdown
Collaborator

Greg any interest in updating the demo for BALANCER?

@gregcowell
Copy link
Copy Markdown
Member Author

Greg any interest in updating the demo for BALANCER?

Yep, I'm happy to do that.

@gregcowell gregcowell deleted the educator branch October 12, 2017 00:33
@dwalton76
Copy link
Copy Markdown
Collaborator

Awesome thank you :)

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.

4 participants