Files
picar/MotorControl/gpiozero/motor_session.py

46 lines
1.3 KiB
Python

import os
from gpiozero import Servo, Device
from gpiozero.pins.pigpio import PiGPIOFactory
GPIOZERO_PIN_FACTORY = "piggpio"
class Motor:
def __init__(self, motor_pin=19):
# This may not work properly, should print.
Device.pin_factory = PiGPIOFactory()
print('Using pin factory:')
print(Device.pin_factory)
self.set_motor_pin(motor_pin)
self.initialise_motor()
def initialise_motor(self):
if self._motor_pin is None:
print("Motor pin number is not set.")
return
self._servo = Servo(self._motor_pin, pin_factory=Device.pin_factory)
def set_throttle(self, throttle):
try:
if throttle < -1 or throttle > 1:
print("Not setting throttle, invalid value set.")
return False
self._servo.value = throttle
except TypeError:
print("throttle should be a number, preferably a float.")
return False
return True
def set_motor_pin(self, value):
if isinstance(value, int):
if value < 2 or value > 21:
print("Invalid GPIO pin")
return False
self._motor_pin = value
return True
else:
print("Value must be an int.")
return False
def stop(self):
self.set_throttle(0)