# Software PWM Servo.py
import RPi.GPIO as GPIO
import time
P_SERVO = [11,12]
fPWM = 50
a = 10
b = 2
def setup():
global pwm
global pwm1
GPIO.setmode(GPIO.BOARD)
GPIO.setup(P_SERVO, GPIO.OUT)
pwm = GPIO.PWM(P_SERVO[0], fPWM)
pwm1 = GPIO.PWM(P_SERVO[1], fPWM)
pwm.start(0)
pwm1.start(0)
def setDirection(direction):
duty = a / 180 * direction + b
pwm.ChangeDutyCycle(2)
pwm1.ChangeDutyCycle(2)
time.sleep(1)
pwm.ChangeDutyCycle(12)
pwm1.ChangeDutyCycle(12)
time.sleep(1)
print "direction =", direction, "-> duty =", duty
time.sleep(1)
print "starting"
setup()
for direction in range(0, 10, 10):
setDirection(direction)
direction = 0
setDirection(0)
GPIO.cleanup()
print "done"
评论
发表评论