freenove_car/Code/Server/servo.py
a838557765 d8141ca9d0 Update
Fixed the problem of rpi_ws281x library and starting the server without
interface
2021-01-04 15:56:32 +08:00

51 lines
1.6 KiB
Python

import time
from PCA9685 import PCA9685
class Servo:
def __init__(self):
self.PwmServo = PCA9685(0x40, debug=True)
self.PwmServo.setPWMFreq(50)
self.PwmServo.setServoPulse(8,1500)
self.PwmServo.setServoPulse(9,1500)
def setServoPwm(self,channel,angle,error=10):
angle=int(angle)
if channel=='0':
self.PwmServo.setServoPulse(8,2500-int((angle+error)/0.09))
elif channel=='1':
self.PwmServo.setServoPulse(9,500+int((angle+error)/0.09))
elif channel=='2':
self.PwmServo.setServoPulse(10,500+int((angle+error)/0.09))
elif channel=='3':
self.PwmServo.setServoPulse(11,500+int((angle+error)/0.09))
elif channel=='4':
self.PwmServo.setServoPulse(12,500+int((angle+error)/0.09))
elif channel=='5':
self.PwmServo.setServoPulse(13,500+int((angle+error)/0.09))
elif channel=='6':
self.PwmServo.setServoPulse(14,500+int((angle+error)/0.09))
elif channel=='7':
self.PwmServo.setServoPulse(15,500+int((angle+error)/0.09))
# Main program logic follows:
if __name__ == '__main__':
print("Now servos will rotate to 90°.")
print("If they have already been at 90°, nothing will be observed.")
print("Please keep the program running when installing the servos.")
print("After that, you can press ctrl-C to end the program.")
pwm=Servo()
while True:
try :
pwm.setServoPwm('0',90)
pwm.setServoPwm('1',90)
except KeyboardInterrupt:
print ("\nEnd of program")
break