From baffd0bbb178872082596454968ea8607c0a0c0d Mon Sep 17 00:00:00 2001 From: Piv <18462828+Piv200@users.noreply.github.com> Date: Wed, 17 Jul 2019 18:51:03 +0930 Subject: [PATCH] Add servo inheritance hierarchy to motor control --- MotorControl/motor.py | 49 +++++++++++++++++++++++++++------------- MotorControl/servo.py | 19 ++++++++++++++++ MotorControl/steering.py | 13 +++++++++++ 3 files changed, 65 insertions(+), 16 deletions(-) create mode 100644 MotorControl/servo.py create mode 100644 MotorControl/steering.py diff --git a/MotorControl/motor.py b/MotorControl/motor.py index 132757c..95a0352 100644 --- a/MotorControl/motor.py +++ b/MotorControl/motor.py @@ -1,7 +1,10 @@ import time +from abc import ABC, abstractmethod from RPi import GPIO +import MotorControl.servo as servo + """This module controls the Motor and Servo control. This module provides an interface for interacting with @@ -9,16 +12,23 @@ the motor on the Traxxas Slash using a Raspberry Pi 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): self._duty_cycle = duty_cycle self._operating_frequency = frequency self._running = False 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) self._control_pin = GPIO.PWM(self.pin_number, self._duty_cycle) @@ -32,36 +42,43 @@ class MotorController: if value > 0 and value < 100: self._duty_cycle = value # 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: return else: self._control_pin.start(self._duty_cycle) - def stop(self): + def _stop(self): self._control_pin.stop() def arm(self): - # Set pulse width 0 + # Set pulse width max + self._control_pin.start(7.2) - - # Wait 1s - - # Set pulse width to maximum - - # Wait 1s + # Now wait until ESC is turned on. + input("Please turn on the ESC, then press enter") # set pulse width minimum - + self._control_pin.ChangeDutyCycle(6) # 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 - def setThrottle(self, throttle: float): + @percent.setter + def percent(self, value): pass class MotorDetails: diff --git a/MotorControl/servo.py b/MotorControl/servo.py new file mode 100644 index 0000000..67d681b --- /dev/null +++ b/MotorControl/servo.py @@ -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 + + \ No newline at end of file diff --git a/MotorControl/steering.py b/MotorControl/steering.py new file mode 100644 index 0000000..51e72ca --- /dev/null +++ b/MotorControl/steering.py @@ -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 \ No newline at end of file