Add servo inheritance hierarchy to motor control
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
import time
|
import time
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
from RPi import GPIO
|
from RPi import GPIO
|
||||||
|
|
||||||
|
import MotorControl.servo as servo
|
||||||
|
|
||||||
"""This module controls the Motor and Servo control.
|
"""This module controls the Motor and Servo control.
|
||||||
|
|
||||||
This module provides an interface for interacting with
|
This module provides an interface for interacting with
|
||||||
@@ -9,16 +12,23 @@ the motor on the Traxxas Slash using a Raspberry Pi
|
|||||||
3B+.
|
3B+.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class MotorController:
|
class Motor(servo.Servo, ABC):
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def arm(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MotorController(Motor):
|
||||||
def __init__(self, pin_number, frequency, duty_cycle=0):
|
def __init__(self, pin_number, frequency, duty_cycle=0):
|
||||||
self._duty_cycle = duty_cycle
|
self._duty_cycle = duty_cycle
|
||||||
self._operating_frequency = frequency
|
self._operating_frequency = frequency
|
||||||
self._running = False
|
self._running = False
|
||||||
self.pin_number = pin_number
|
self.pin_number = pin_number
|
||||||
|
|
||||||
# Pulse width was mentioned to be 6-7.2% by uni group.
|
# Operating Frequency should be 50Hz
|
||||||
|
|
||||||
GPIO.setmode(GPIO.OUT)
|
GPIO.setmode(GPIO.BCM)
|
||||||
GPIO.setup(self.pin_number, self._operating_frequency)
|
GPIO.setup(self.pin_number, self._operating_frequency)
|
||||||
self._control_pin = GPIO.PWM(self.pin_number, self._duty_cycle)
|
self._control_pin = GPIO.PWM(self.pin_number, self._duty_cycle)
|
||||||
|
|
||||||
@@ -32,36 +42,43 @@ class MotorController:
|
|||||||
if value > 0 and value < 100:
|
if value > 0 and value < 100:
|
||||||
self._duty_cycle = value
|
self._duty_cycle = value
|
||||||
# Change the current duty cycle of the GPIO.
|
# Change the current duty cycle of the GPIO.
|
||||||
self._control_pin.changeDutyCycle
|
self._control_pin.changeDutyCycle(value)
|
||||||
|
|
||||||
def start(self):
|
def _start(self):
|
||||||
if self._running:
|
if self._running:
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
self._control_pin.start(self._duty_cycle)
|
self._control_pin.start(self._duty_cycle)
|
||||||
|
|
||||||
def stop(self):
|
def _stop(self):
|
||||||
self._control_pin.stop()
|
self._control_pin.stop()
|
||||||
|
|
||||||
def arm(self):
|
def arm(self):
|
||||||
# Set pulse width 0
|
# Set pulse width max
|
||||||
|
self._control_pin.start(7.2)
|
||||||
|
|
||||||
|
# Now wait until ESC is turned on.
|
||||||
# Wait 1s
|
input("Please turn on the ESC, then press enter")
|
||||||
|
|
||||||
# Set pulse width to maximum
|
|
||||||
|
|
||||||
# Wait 1s
|
|
||||||
|
|
||||||
# set pulse width minimum
|
# set pulse width minimum
|
||||||
|
self._control_pin.ChangeDutyCycle(6)
|
||||||
# Wait 1s
|
# Wait 1s
|
||||||
|
|
||||||
# Set pulse width to idle
|
# Set middle.
|
||||||
|
self._control_pin.ChangeDutyCycle(6.6)
|
||||||
|
# Also try setting to max again if necessary, once it's tested again.
|
||||||
|
|
||||||
|
# Stop the control pin (set it to idle).
|
||||||
|
self._control_pin.stop()
|
||||||
|
|
||||||
|
# Now just start the control pin to use the slash, by setting the throttle.
|
||||||
|
|
||||||
|
@property
|
||||||
|
def percent(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def setThrottle(self, throttle: float):
|
@percent.setter
|
||||||
|
def percent(self, value):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class MotorDetails:
|
class MotorDetails:
|
||||||
|
|||||||
19
MotorControl/servo.py
Normal file
19
MotorControl/servo.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
"""
|
||||||
|
This module provides a base class for interacting with servo-like code.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class Servo(ABC):
|
||||||
|
|
||||||
|
@property
|
||||||
|
@abstractmethod
|
||||||
|
def percent(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@percent.setter
|
||||||
|
@abstractmethod
|
||||||
|
def percent(self, forward):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
13
MotorControl/steering.py
Normal file
13
MotorControl/steering.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import MotorControl.servo as servo
|
||||||
|
|
||||||
|
class SteeringServo(servo.Servo):
|
||||||
|
|
||||||
|
@property
|
||||||
|
def percent(self):
|
||||||
|
# Set the
|
||||||
|
pass
|
||||||
|
|
||||||
|
@percent.setter
|
||||||
|
def percent(self, forward):
|
||||||
|
#
|
||||||
|
pass
|
||||||
Reference in New Issue
Block a user