Last active
December 9, 2023 10:37
-
-
Save cedricbonhomme/0032f5418e6efbf63dda325586665983 to your computer and use it in GitHub Desktop.
ev3dev with Python and the Mindstorm robot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
from ev3dev2.led import Leds | |
from ev3dev2.sound import Sound | |
from ev3dev2.motor import OUTPUT_B, OUTPUT_C, SpeedPercent, MoveTank | |
from ev3dev2.sensor.lego import InfraredSensor, TouchSensor | |
sound = Sound() | |
leds = Leds() | |
ts = TouchSensor() | |
ir = InfraredSensor() | |
tank_drive = MoveTank(OUTPUT_B, OUTPUT_C) | |
leds.set_color("LEFT", "GREEN") | |
leds.set_color("RIGHT", "GREEN") | |
sound.speak("Hello Elliot ! Attrape moi !") | |
sound.speak("Trois, deux, un. Go !") | |
while not ts.wait_for_pressed(1): | |
if ir.proximity < 55: | |
# 22% should be approximately 16cm: obstacle detected ! | |
# change color of the leds then turn... | |
leds.set_color("LEFT", "RED") | |
leds.set_color("RIGHT", "RED") | |
tank_drive.on_for_seconds(SpeedPercent(60), SpeedPercent(30), 4) | |
else: | |
leds.set_color("LEFT", "GREEN") | |
leds.set_color("RIGHT", "GREEN") | |
# drive in a turn for 2 rotations of the outer motor | |
# the first two parameters can be unit classes or percentages. | |
tank_drive.on_for_rotations(SpeedPercent(90), SpeedPercent(90), 3) | |
sound.speak("Bye-bye !") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment