30 lines
784 B
Python
30 lines
784 B
Python
# motors.py
|
|
from machine import Pin, PWM
|
|
|
|
class Motor:
|
|
def __init__(self, pin1=13, pin2=14, freq=20000):
|
|
# Two PWM objects, one per pin
|
|
self._pwm1 = PWM(Pin(pin1), freq=freq, duty=0)
|
|
self._pwm2 = PWM(Pin(pin2), freq=freq, duty=0)
|
|
|
|
def move(self, value):
|
|
# Clamp input
|
|
if value > 1023:
|
|
value = 1023
|
|
elif value < -1023:
|
|
value = -1023
|
|
|
|
if value == 0:
|
|
# Stop: both low
|
|
self._pwm1.duty(0)
|
|
self._pwm2.duty(0)
|
|
elif value > 0:
|
|
# Forward: pin1 PWM, pin2 low
|
|
self._pwm1.duty(value)
|
|
self._pwm2.duty(0)
|
|
else:
|
|
# Backward: pin2 PWM, pin1 low
|
|
self._pwm1.duty(0)
|
|
self._pwm2.duty(-value)
|
|
|