42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
import os
|
|
from gpiozero import Servo
|
|
|
|
GPIOZERO_PIN_FACTORY = "piggpio"
|
|
|
|
class Motor:
|
|
def __init__(self, motor_pin=19):
|
|
os.environ["GPIOZERO_PIN_FACTORY"] = GPIOZERO_PIN_FACTORY
|
|
os.system("sudo pigpiod")
|
|
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)
|
|
|
|
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) |