Add bee-bot / turtle robot example, close #443

This commit is contained in:
Ben Nuttall
2017-01-08 15:58:50 +00:00
parent c586fe07f5
commit ed12ac1994
3 changed files with 39 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
from gpiozero import Button, Robot
from time import sleep
from signal import pause
robot = Robot((17, 18), (22, 23))
left = Button(2)
right = Button(3)
forward = Button(4)
backward = Button(5)
go = Button(6)
instructions = []
def add_instruction(btn):
instructions.append({
left: (-1, 1),
right: (1, -1),
forward: (1, 1),
backward: (-1, -1),
}[btn])
def do_instructions():
instructions.append((0, 0))
robot.source_delay = 0.5
robot.source = instructions
sleep(robot.source_delay * len(instructions))
del instructions[:]
go.when_pressed = do_instructions
for button in (left, right, forward, backward):
button.when_pressed = add_instruction
pause()

View File

@@ -331,8 +331,12 @@ Button controlled robot
Use four GPIO buttons as forward/back/left/right controls for a robot: Use four GPIO buttons as forward/back/left/right controls for a robot:
.. literalinclude:: examples/robot_buttons.py .. literalinclude:: examples/robot_buttons_1.py
Alternatively, use four buttons to program the directions and add a fifth
button to process them in turn, like a Bee-Bot or Turtle robot.
.. literalinclude:: examples/robot_buttons_2.py
Keyboard controlled robot Keyboard controlled robot
========================= =========================