mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
Implemented "run away" script
This commit is contained in:
parent
1171ee2668
commit
8f65d1553a
@ -1,18 +1,22 @@
|
|||||||
from vernie import *
|
from vernie import *
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
|
||||||
|
|
||||||
robot = Vernie()
|
robot = Vernie()
|
||||||
running = True
|
running = True
|
||||||
|
|
||||||
|
robot.say("Place your hand in front of sensor")
|
||||||
|
|
||||||
|
|
||||||
def callback(color, distance):
|
def callback(color, distance):
|
||||||
del color # drop unused
|
if color == COLOR_WHITE:
|
||||||
speed = (10 - distance) / 10
|
robot.motor_AB.timed(0.1, 0.2, async=True)
|
||||||
secs = (10 - distance) / 10
|
elif color != COLOR_NONE:
|
||||||
logging.info("%s => %s %s", distance, speed, secs)
|
print(color)
|
||||||
if speed <= 1:
|
else:
|
||||||
robot.motor_AB.timed(secs, -speed)
|
speed = (10 - distance + 1) / 10.0
|
||||||
|
secs = (10 - distance + 1) / 10.0
|
||||||
|
print("Distance is %.1f inches, I'm running back with %s%% speed!" % (distance, int(speed * 100)))
|
||||||
|
if speed <= 1:
|
||||||
|
robot.motor_AB.timed(secs / 1, -speed, async=True)
|
||||||
|
|
||||||
|
|
||||||
def on_btn(pressed):
|
def on_btn(pressed):
|
||||||
@ -23,9 +27,12 @@ def on_btn(pressed):
|
|||||||
|
|
||||||
robot.button.subscribe(on_btn)
|
robot.button.subscribe(on_btn)
|
||||||
robot.color_distance_sensor.subscribe(callback)
|
robot.color_distance_sensor.subscribe(callback)
|
||||||
|
robot.led.set_color(COLOR_GREEN)
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
robot.led.set_color(COLOR_BLACK)
|
||||||
robot.color_distance_sensor.unsubscribe(callback)
|
robot.color_distance_sensor.unsubscribe(callback)
|
||||||
robot.button.unsubscribe(on_btn)
|
robot.button.unsubscribe(on_btn)
|
||||||
|
time.sleep(5) # let color change
|
||||||
|
Loading…
x
Reference in New Issue
Block a user