Create example scripts for the EDUCATOR robot#13
Create example scripts for the EDUCATOR robot#13dwalton76 merged 1 commit intoev3dev:masterfrom gregcowell:educator
Conversation
| #!/usr/bin/env python3 | ||
| """Make robot say whatever color it observes with the color sensor.""" | ||
|
|
||
| from ev3dev.sensor.lego import ColorSensor |
There was a problem hiding this comment.
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.
| def __init__(self): | ||
| """Create Gyro object if connected.""" | ||
| super().__init__() | ||
| assert self.connected, 'Connect a Gyro to a sensor port' |
There was a problem hiding this comment.
| 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: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
I'd be game for putting this to the GyroSensor class instead...other gyro programs may find this handy
There was a problem hiding this comment.
Thinking outloud...would be cool if long term we can make all of the sensor files work with poll()
There was a problem hiding this comment.
Okay if I raise a pull request to add wait_until_changed_by() to the GyroSensor class ? I’ll include a short sleep.
| while abs(start_angle - self.value()) < delta: | ||
| pass | ||
|
|
||
| motor_pair = MoveSteering('outB', 'outC') |
There was a problem hiding this comment.
Use OUTPUT_B and OUTPUT_C constants so this works on all ev3dev platforms
| super().__init__() | ||
| assert self.connected, 'Connect a Gyro to a sensor port' | ||
|
|
||
| def wait_until_changed_by(self, delta): |
There was a problem hiding this comment.
@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.
|
I've made the required changes except for the import. When I change: to: the import fails. 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 |
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 |
| # 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) |
There was a problem hiding this comment.
This is cool :) How accurate of a square does it make?
There was a problem hiding this comment.
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.
| @@ -0,0 +1,34 @@ | |||
| #!/usr/bin/env python3 | |||
There was a problem hiding this comment.
Maybe call this square-gyro.py since the non-gyro equivalent is square.py?
|
|
||
| # Start robot moving forward | ||
| motor_pair.on(steering=0, speed_pct=10) | ||
| # Wait until robot touches wall |
There was a problem hiding this comment.
This is very minor but adding a blank line before the start of a new comment makes things easier to read.
| 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 |
There was a problem hiding this comment.
Would put a small sleep here else it will hammer the CPU
There was a problem hiding this comment.
I’ll add a 10ms sleep to the loop.
|
I've made all the required changes to the scripts. |
WasabiFan
left a comment
There was a problem hiding this comment.
These are looking quite clean with the new APIs!
|
Greg any interest in updating the demo for BALANCER? |
Yep, I'm happy to do that. |
|
Awesome thank you :) |
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).