12345678910111213141516171819202122232425262728293031323334353637 |
- import wiringpi
- class Wheel:
- __pin1=0
- __pin2=0
- # 初始化轮
- def __init__(self,pin1,pin2,pwm):
- self.__pin1=pin1
- self.__pin2=pin2
- wiringpi.pinMode(self.__pin1,1)
- wiringpi.pinMode(self.__pin2,1)
- wiringpi.digitalWrite(self.__pin1,0)
- wiringpi.digitalWrite(self.__pin2,0)
- # 设置pwm使能
- self.__pwm=pwm
- wiringpi.pinMode(self.__pwm,1)
- # 设置周期10ms的pwm控制,0.1*100=10,并初始化占空比为0%,无速度
- # wiringpi.pwmSetClock(2)
- wiringpi.softPwmCreate(self.__pwm,0,100)
- wiringpi.softPwmWrite(self.__pwm,0)
- # 前转
- def forward(self):
- wiringpi.digitalWrite(self.__pin1,1)
- wiringpi.digitalWrite(self.__pin2,0)
- # 后转
- def backward(self):
- wiringpi.digitalWrite(self.__pin1,0)
- wiringpi.digitalWrite(self.__pin2,1)
- # 停止
- def stop(self):
- wiringpi.digitalWrite(self.__pin1,0)
- wiringpi.digitalWrite(self.__pin2,0)
- # 设置速度
- def setSpeed(self,speed):
- if speed<0 or speed>100:
- return
- wiringpi.softPwmWrite(self.__pwm,speed)
|