You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
class PWMMotor:
|
|
|
|
def __init__(self, dir_pin1, dir_pin2, enable_pin, flip_dir=False):
|
|
self._dir_pin1 = dir_pin1
|
|
self._dir_pin2 = dir_pin2
|
|
self._enable = enable_pin
|
|
self._flip_dir = flip_dir
|
|
self._direction = True
|
|
self._speed = 0.0
|
|
|
|
@property
|
|
def forward(self):
|
|
return self._direction
|
|
|
|
@forward.setter
|
|
def forward(self, direction):
|
|
check = not direction if self._flip_dir else direction
|
|
if check:
|
|
self._dir_pin1.on()
|
|
self._dir_pin2.off()
|
|
self._direction = True
|
|
else:
|
|
self._dir_pin1.off()
|
|
self._dir_pin2.on()
|
|
self._direction = False
|
|
|
|
@property
|
|
def speed(self):
|
|
return self._speed
|
|
|
|
@speed.setter
|
|
def speed(self, percentage):
|
|
f_percentage = float(percentage)
|
|
if f_percentage < 0 or f_percentage > 100:
|
|
raise ValueError('Speed must be a float or int from 0 to 100')
|
|
self._enable.duty(int(1023 / 100 * f_percentage))
|
|
self._speed = f_percentage
|
|
|
|
|
|
class ServoMotor(PWMMotor):
|
|
|
|
def __init__(self, dir_pin1, dir_pin2, enable_pin, flip_dir=False, sensor_pin_a=None, sensor_pin_b=None):
|
|
super().__init__(dir_pin1, dir_pin2, enable_pin, flip_dir=False)
|
|
sensor_pin_a = sensor_pin_a
|
|
sensor_pin_b = sensor_pin_b
|
|
|
|
|
|
class MotorController:
|
|
|
|
def __init__(self, dir_pin_a1=None, dir_pin_a2=None, enable_pin_a=None, flip_dir_a=False, dir_pin_b1=None,
|
|
dir_pin_b2=None,
|
|
enable_pin_b=None, flip_dir_b=False):
|
|
self.motor_a = PWMMotor(dir_pin_a1, dir_pin_a2, enable_pin_a, flip_dir_a)
|
|
self.motor_b = PWMMotor(dir_pin_b1, dir_pin_b2, enable_pin_b, flip_dir_b)
|