From 794869cfeb5d2ad42116433ec13bd232effb48c3 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sun, 10 Jul 2022 14:18:04 -0400 Subject: [PATCH] adding basic motor functionality --- firmware/upy_clank/config.py | 12 ++++++++ firmware/upy_clank/main.py | 5 +-- firmware/upy_clank/motor.py | 59 ++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 firmware/upy_clank/config.py create mode 100644 firmware/upy_clank/motor.py diff --git a/firmware/upy_clank/config.py b/firmware/upy_clank/config.py new file mode 100644 index 0000000..63e5004 --- /dev/null +++ b/firmware/upy_clank/config.py @@ -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 diff --git a/firmware/upy_clank/main.py b/firmware/upy_clank/main.py index cdab06d..3ae0c80 100644 --- a/firmware/upy_clank/main.py +++ b/firmware/upy_clank/main.py @@ -1,4 +1,5 @@ -__upy_clank_version__ = '0.1.0' +__upy_clank_version__ = "0.1.0" + def main(): - pass \ No newline at end of file + pass diff --git a/firmware/upy_clank/motor.py b/firmware/upy_clank/motor.py new file mode 100644 index 0000000..5534ae1 --- /dev/null +++ b/firmware/upy_clank/motor.py @@ -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), + ), + )