Last active
August 29, 2015 14:11
-
-
Save cedricbonhomme/7af20d5d78f613f6d42f to your computer and use it in GitHub Desktop.
This file contains 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 python | |
# -*- coding: utf-8 -*- | |
# | |
# Avoids obstacles with the infrared sensor. | |
# | |
import time | |
from ev3.lego import LargeMotor | |
from ev3.lego import TouchSensor | |
from ev3.lego import InfraredSensor | |
LEFT = LargeMotor(port=LargeMotor.PORT.B) | |
RIGHT = LargeMotor(port=LargeMotor.PORT.C) | |
button = TouchSensor() | |
ir_sensor = InfraredSensor() | |
distance = 30 | |
def start_stop(): | |
while True: | |
time.sleep(1) | |
if button.is_pushed: | |
print "starting..." | |
walk() | |
def walk(): | |
while True: | |
time.sleep(1) | |
if button.is_pushed: | |
print "stopping..." | |
LEFT.stop() | |
RIGHT.stop() | |
break | |
if ir_sensor.prox <= distance: | |
print "obstacle detected" | |
LEFT.stop() | |
RIGHT.stop() | |
LEFT.run_forever(100, regulation_mode=False) | |
time.sleep(5) | |
LEFT.stop() | |
LEFT.run_forever(100, regulation_mode=False) | |
RIGHT.run_forever(100, regulation_mode=False) | |
if __name__ == "__main__": | |
# Point of entry in execution mode | |
start_stop() | |
LEFT.stop() | |
RIGHT.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment