adding basic motor functionality

master
Drew Bednar 3 years ago
parent d78807e948
commit 794869cfeb

@ -0,0 +1,12 @@
PIN_CONFIG = {
"left": {
"encoder": {"cpr": 1320, "pin_a": 0, "pin_b": 1},
"motor": {"pwm": 23, "in1": 22, "in2": 21},
},
"right": {
"encoder": {"cpr": 1320, "pin_a": 2, "pin_b": 3},
"motor": {"pwm": 19, "in1": 18, "in2": 17},
},
}
DUTY_MAX = 65535

@ -1,4 +1,5 @@
__upy_clank_version__ = '0.1.0'
__upy_clank_version__ = "0.1.0"
def main():
pass

@ -0,0 +1,59 @@
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),
),
)
Loading…
Cancel
Save