41 lines
1.0 KiB
Python
41 lines
1.0 KiB
Python
import time
|
|
|
|
from RPi import GPIO
|
|
|
|
"""This module controls the Motor and Servo module.
|
|
|
|
This module provides an interface for interacting with
|
|
the motor on the Traxxas Slash using a Raspberry Pi
|
|
3B+.
|
|
"""
|
|
|
|
class MotorController:
|
|
def __init__(self, control_pin, frequency, duty_cycle=0):
|
|
self._duty_cycle = duty_cycle
|
|
self._operating_frequency = frequency
|
|
self._running = False
|
|
|
|
GPIO.setmode(GPIO.OUT)
|
|
GPIO.setup(self._control_pin, self._operating_frequency)
|
|
self._control_pin = GPIO.PWM(control_pin, self._duty_cycle)
|
|
|
|
@property
|
|
def duty_cycle(self):
|
|
return self._duty_cycle
|
|
|
|
@duty_cycle.setter
|
|
def duty_cycle(self, value):
|
|
if value > 0 and value < 100:
|
|
self._duty_cycle = value
|
|
# Change the current duty cycle of the GPIO.
|
|
|
|
def start(self):
|
|
if self._running:
|
|
return
|
|
else:
|
|
self._control_pin.start(self._duty_cycle)
|
|
|
|
def stop(self):
|
|
self._control_pin.stop()
|
|
|
|
|