Motorsteuerung
Speichere diesen Code auf dem Pico unter dem Namen motor.py.
from machine import Pin, PWM
import utime
class Motor:
def __init__(self, pinNo):
self.MAX_SPEED = 65535 # The absolute maximum is: 65535.
self.gpio = pinNo
self._on = False
self.speed=0
self.pwm1=PWM(Pin(pinNo))
self.pwm1.freq(2000)
self.pwm1.duty_u16(0)
def setSpeed(self,s):
self._on=True
self.speed=s
self.pwm1.duty_u16(int(self.MAX_SPEED*s/100))
def off(self):
self._on=False
self.pwm1.duty_u16(0)
def on(self):
self._on=True
self.pwm1.duty_u16(int(self.MAX_SPEED*self.speed/100))
class BiMotor(Motor):
def __init__(self, pinNo):
super().__init__(pinNo)
self.forward=True
self.pwm2=PWM(Pin(pinNo+1))
self.pwm2.duty_u16(0)
def setForward(self,forward):
if self.forward==forward:
return
self.pwm1.duty_u16(0)
self.pwm1,self.pwm2=self.pwm2,self.pwm1
self.forward=forward
self.pwm1.duty_u16(int(self.MAX_SPEED*self.speed/100))
Beispiel für die Anwendung dieser Bibliothek
# import motor
from motor import Motor, BiMotor
# Initialize motors
motor_left= BiMotor(10) # Initializing pins 11 and 12 for left motor.
motor_right= BiMotor(14) # Initializing pins 14 and 15 for right motor.
motor_left.setForward(True)
motor_right.setForward(True)
motor_left.setSpeed(50)
motor_right.setSpeed(50)