Page 9 of 13
Page 153
class uniMotor(PWMOutputDevice):
def __init__(self, pin=None, active_high=True, initial_value=0, pin_factory=None):
super(uniMotor, self).__init__(pin, active_high, initial_value, pin_factory=pin_factory)
def speed(self, value):
self._write(value)
motor = uniMotor(4)
motor.speed(0.5)
sleep(10)
Page 161
from gpiozero import Servo
from time import sleep
servo = Servo(4)
while True:
servo.min()
sleep(0.5)
servo.mid()
sleep(0.5)
servo.max()
sleep(0.5)
Page 162
from gpiozero import Servo
class ServoInvert(Servo):
@Servo.value.setter
def value(self, value):
if value is None:
self.pwm_device.pin.frequency = None
elif -1 <= value <= 1:
self.pwm_device.pin.frequency = int(1 / self.frame_width)
self.pwm_device.value = (self._min_dc + self._dc_range *((value - self._min_value) / self._value_range))
else:
raise OutputDeviceBadValue(
"Servo value must be between -1 and 1, or None")
Page 171
from gpiozero import DigitalOutputDevice
from time import sleep
class StepperBi4():
def __init__(self, A=None, Am=None, B=None, Bm=None):
self.phase = 0
self.gpios = tuple(DigitalOutputDevice(pin) for pin in (A, B, Am, Bm))
self.halfstepSeq = [
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[1, 0, 0, 1]
]
def setPhase(self, phase):
self.phase = phase
for gpio, state in zip(self.gpios, self.halfstepSeq[phase]):
gpio.value = state
def stepForward(self):
self.phase = (self.phase+1) % 8
self.setPhase(self.phase)
def stepReverse(self):
self.phase = (self.phase-1) % 8
self.setPhase(self.phase)
step = StepperBi4(4, 17, 27, 22)
step.setPhase(0)
while True:
sleep(0.001)
step.stepForward()