Created
September 15, 2021 06:34
-
-
Save designdevy/63f0074e96fb03ef9c0e3f2631fc5c96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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