mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Implement Motor, Robot and RyanteckRobot
This commit is contained in:
@@ -19,7 +19,6 @@ from .output_devices import (
|
|||||||
LED,
|
LED,
|
||||||
Buzzer,
|
Buzzer,
|
||||||
Motor,
|
Motor,
|
||||||
Robot,
|
|
||||||
RGBLED,
|
RGBLED,
|
||||||
)
|
)
|
||||||
from .boards import (
|
from .boards import (
|
||||||
@@ -29,4 +28,6 @@ from .boards import (
|
|||||||
PiTraffic,
|
PiTraffic,
|
||||||
FishDish,
|
FishDish,
|
||||||
TrafficHat,
|
TrafficHat,
|
||||||
|
Robot,
|
||||||
|
RyanteckRobot,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from .input_devices import Button
|
from .input_devices import Button
|
||||||
from .output_devices import LED, Buzzer
|
from .output_devices import LED, Buzzer, Motor
|
||||||
from .devices import GPIODeviceError
|
from .devices import GPIODeviceError
|
||||||
|
|
||||||
from time import sleep
|
from time import sleep
|
||||||
@@ -213,3 +213,88 @@ class TrafficHat(FishDish):
|
|||||||
self.buzzer = Buzzer(5)
|
self.buzzer = Buzzer(5)
|
||||||
self.button = Button(25)
|
self.button = Button(25)
|
||||||
self._all = self._leds + (self.buzzer,)
|
self._all = self._leds + (self.buzzer,)
|
||||||
|
|
||||||
|
|
||||||
|
class Robot(object):
|
||||||
|
"""
|
||||||
|
Generic dual-motor Robot.
|
||||||
|
"""
|
||||||
|
def __init__(self, left=None, right=None):
|
||||||
|
if not all([left, right]):
|
||||||
|
raise GPIODeviceError('left and right motor pins must be provided')
|
||||||
|
|
||||||
|
left_forward, left_back = left
|
||||||
|
right_forward, right_back = right
|
||||||
|
|
||||||
|
self._left = Motor(forward=left_forward, back=left_back)
|
||||||
|
self._right = Motor(forward=right_forward, back=right_back)
|
||||||
|
|
||||||
|
def left(self, seconds=None):
|
||||||
|
"""
|
||||||
|
Turn left. If seconds given, stop after given number of seconds.
|
||||||
|
|
||||||
|
seconds: None
|
||||||
|
Number of seconds to turn left for
|
||||||
|
"""
|
||||||
|
self._left.forward()
|
||||||
|
self._right.backward()
|
||||||
|
if seconds is not None:
|
||||||
|
sleep(seconds)
|
||||||
|
self._left.stop()
|
||||||
|
self._right.stop()
|
||||||
|
|
||||||
|
def right(self, seconds=None):
|
||||||
|
"""
|
||||||
|
Turn right. If seconds given, stop after given number of seconds.
|
||||||
|
|
||||||
|
seconds: None
|
||||||
|
Number of seconds to turn right for
|
||||||
|
"""
|
||||||
|
self._right.forward()
|
||||||
|
self._left.backward()
|
||||||
|
if seconds is not None:
|
||||||
|
sleep(seconds)
|
||||||
|
self._left.stop()
|
||||||
|
self._right.stop()
|
||||||
|
|
||||||
|
def forward(self, seconds=None):
|
||||||
|
"""
|
||||||
|
Drive forward. If seconds given, stop after given number of seconds.
|
||||||
|
|
||||||
|
seconds: None
|
||||||
|
Number of seconds to drive forward for
|
||||||
|
"""
|
||||||
|
self._left.forward()
|
||||||
|
self._right.forward()
|
||||||
|
if seconds is not None:
|
||||||
|
sleep(seconds)
|
||||||
|
self._left.stop()
|
||||||
|
self._right.stop()
|
||||||
|
|
||||||
|
def backward(self, seconds=None):
|
||||||
|
"""
|
||||||
|
Drive backward. If seconds given, stop after given number of seconds.
|
||||||
|
|
||||||
|
seconds: None
|
||||||
|
Number of seconds to drive backward for
|
||||||
|
"""
|
||||||
|
self._left.backward()
|
||||||
|
self._right.backward()
|
||||||
|
if seconds is not None:
|
||||||
|
sleep(seconds)
|
||||||
|
self._left.stop()
|
||||||
|
self._right.stop()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
"""
|
||||||
|
Stop both motors.
|
||||||
|
"""
|
||||||
|
self._left.stop()
|
||||||
|
self._right.stop()
|
||||||
|
|
||||||
|
|
||||||
|
class RyanteckRobot(Robot):
|
||||||
|
def __init__(self):
|
||||||
|
left = (17, 18)
|
||||||
|
right = (22, 23)
|
||||||
|
super(RyanteckRobot, self).__init__(left=left, right=right)
|
||||||
|
|||||||
@@ -243,64 +243,34 @@ class RGBLED(object):
|
|||||||
self._blue.value = b
|
self._blue.value = b
|
||||||
|
|
||||||
|
|
||||||
class Motor(OutputDevice):
|
class Motor(object):
|
||||||
"""
|
"""
|
||||||
Generic single-direction motor.
|
Generic bi-directional motor.
|
||||||
"""
|
"""
|
||||||
pass
|
def __init__(self, forward=None, back=None):
|
||||||
|
if not all([forward, back]):
|
||||||
|
raise GPIODeviceError('forward and back pins must be provided')
|
||||||
|
|
||||||
|
self._forward = OutputDevice(forward)
|
||||||
|
self._backward = OutputDevice(back)
|
||||||
|
|
||||||
class Robot(object):
|
def forward(self):
|
||||||
"""
|
|
||||||
Generic single-direction dual-motor Robot.
|
|
||||||
"""
|
|
||||||
def __init__(self, left=None, right=None):
|
|
||||||
if not all([left, right]):
|
|
||||||
raise GPIODeviceError('left and right pins must be provided')
|
|
||||||
|
|
||||||
self._left = Motor(left)
|
|
||||||
self._right = Motor(right)
|
|
||||||
|
|
||||||
def left(self, seconds=None):
|
|
||||||
"""
|
"""
|
||||||
Turns left for a given number of seconds.
|
Turn the motor on, forwards
|
||||||
|
|
||||||
seconds: None
|
|
||||||
Number of seconds to turn left for
|
|
||||||
"""
|
"""
|
||||||
self._left.on()
|
self._forward.on()
|
||||||
if seconds is not None:
|
self._backward.off()
|
||||||
sleep(seconds)
|
|
||||||
self._left.off()
|
|
||||||
|
|
||||||
def right(self, seconds=None):
|
def backward(self):
|
||||||
"""
|
"""
|
||||||
Turns right for a given number of seconds.
|
Turn the motor on, backwards
|
||||||
|
|
||||||
seconds: None
|
|
||||||
Number of seconds to turn right for
|
|
||||||
"""
|
"""
|
||||||
self._right.on()
|
self._backward.on()
|
||||||
if seconds is not None:
|
self._forward.off()
|
||||||
sleep(seconds)
|
|
||||||
self._right.off()
|
|
||||||
|
|
||||||
def forwards(self, seconds=None):
|
|
||||||
"""
|
|
||||||
Drives forward for a given number of seconds.
|
|
||||||
|
|
||||||
seconds: None
|
|
||||||
Number of seconds to drive forward for
|
|
||||||
"""
|
|
||||||
self.left()
|
|
||||||
self.right()
|
|
||||||
if seconds is not None:
|
|
||||||
sleep(seconds)
|
|
||||||
self.stop()
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""
|
"""
|
||||||
Stops both motors.
|
Stop the motor
|
||||||
"""
|
"""
|
||||||
self._left.off()
|
self._forward.off()
|
||||||
self._right.off()
|
self._backward.off()
|
||||||
|
|||||||
Reference in New Issue
Block a user