Skip to content

Instantly share code, notes, and snippets.

@cedricbonhomme
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cedricbonhomme/7af20d5d78f613f6d42f to your computer and use it in GitHub Desktop.
Save cedricbonhomme/7af20d5d78f613f6d42f to your computer and use it in GitHub Desktop.
#! /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