Skip to content

Instantly share code, notes, and snippets.

@designdevy
Created September 15, 2021 06:34
Show Gist options
  • Save designdevy/63f0074e96fb03ef9c0e3f2631fc5c96 to your computer and use it in GitHub Desktop.
Save designdevy/63f0074e96fb03ef9c0e3f2631fc5c96 to your computer and use it in GitHub Desktop.
from motor_driver import * # import motor_driver library
# Drive Motor
motor = motor_driver(16,17) # M2A = 16, M2B = 17
motor.controlSpeed(50) # move forward at speed 50
utime.sleep(5) # sleep 5 second
motor.brake() # brake the motor
import machine
import utime
class motor_driver():
def __init__(self,MA,MB):
self.MA = machine.PWM(machine.Pin(MA))
self.MB = machine.PWM(machine.Pin(MB))
# Will return a integer
def _convert(self, x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
def controlSpeed(self, speed):
speed = self._convert(speed,0,100,0,65534)
if speed > 0:
self.MA.duty_u16(speed)
self.MB.duty_u16(0)
else:
self.MA.duty_u16(0)
self.MB.duty_u16(abs(speed))
def brake(self):
self.controlSpeed(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment