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.
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
3 years ago
|
from machine import Pin, PWM
|
||
|
|
||
|
|
||
|
class Motor:
|
||
|
def __init__(self, pwm_pin, in1_pin, in2_pin) -> None:
|
||
|
self.pwm = pwm_pin
|
||
|
self.in1 = in1_pin
|
||
|
self.in2 = in2_pin
|
||
|
|
||
|
|
||
|
class MotorController:
|
||
|
def __init__(self, left_motor, right_motor) -> None:
|
||
|
self.left = left_motor
|
||
|
self.right = right_motor
|
||
|
|
||
|
def forward(self):
|
||
|
self.left.in1.value(1)
|
||
|
self.left.in2.value(0)
|
||
|
self.right.in2.value(1)
|
||
|
self.right.in1.value(0)
|
||
|
self.left.pwm.duty_u16(65535 // 2)
|
||
|
self.right.pwm.duty_u16(65535 // 2)
|
||
|
|
||
|
def reverse(self):
|
||
|
self.left.in1.value(0)
|
||
|
self.left.in2.value(1)
|
||
|
self.right.in2.value(0)
|
||
|
self.right.in1.value(1)
|
||
|
self.left.pwm.duty_u16(65535 // 2)
|
||
|
self.right.pwm.duty_u16(65535 // 2)
|
||
|
|
||
|
def stop(self):
|
||
|
self.left.pwm.duty_u16(0)
|
||
|
self.right.pwm.duty_u16(0)
|
||
|
|
||
|
@classmethod
|
||
|
def from_config(cls, pin_config):
|
||
|
return cls(
|
||
|
Motor(
|
||
|
# 100Hz duty cycle, range 0-65535
|
||
|
pwm_pin=PWM(
|
||
|
Pin(pin_config["left"]["motor"]["pwm"], Pin.OUT),
|
||
|
freq=100,
|
||
|
duty_u16=0,
|
||
|
),
|
||
|
in1_pin=Pin(pin_config["left"]["motor"]["in1"], Pin.OUT),
|
||
|
in2_pin=Pin(pin_config["left"]["motor"]["in2"], Pin.OUT),
|
||
|
),
|
||
|
Motor(
|
||
|
# 100Hz duty cycle, range 0-65535
|
||
|
pwm_pin=PWM(
|
||
|
Pin(pin_config["right"]["motor"]["pwm"], Pin.OUT),
|
||
|
freq=100,
|
||
|
duty_u16=0,
|
||
|
),
|
||
|
in1_pin=Pin(pin_config["right"]["motor"]["in1"], Pin.OUT),
|
||
|
in2_pin=Pin(pin_config["right"]["motor"]["in2"], Pin.OUT),
|
||
|
),
|
||
|
)
|