mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Add bee-bot / turtle robot example, close #443
This commit is contained in:
34
docs/examples/robot_buttons_2.py
Normal file
34
docs/examples/robot_buttons_2.py
Normal 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()
|
||||
@@ -331,8 +331,12 @@ Button controlled 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
|
||||
=========================
|
||||
|
||||
Reference in New Issue
Block a user