Refactor python module structure
This commit is contained in:
43
control/gpiozero/motor_session.py
Normal file
43
control/gpiozero/motor_session.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from gpiozero import Servo, Device
|
||||
from gpiozero.pins.pigpio import PiGPIOFactory
|
||||
import subprocess
|
||||
|
||||
class Motor:
|
||||
def __init__(self, motor_pin=19):
|
||||
subprocess.call(['sudo', 'pigpiod'])
|
||||
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)
|
||||
Reference in New Issue
Block a user