The servo can be turned clockwise, counter clockwise, and to the 90 degree position, but it lacks a way to query the current rotation. The rotation needs to be calculated manually and this script is a first attempt.
#!/usr/bin/env python import RPi.GPIO as GPIO import datetime import time servo_pin = 22 servo_pin2 = 18 # 60 degrees / 0.1seconds servo_speed = 0.1 GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(servo_pin, GPIO.OUT) GPIO.setup(servo_pin2, GPIO.OUT) last_time = datetime.datetime.now() current_time = datetime.datetime.now() sumTime = datetime.timedelta(0, 0) accuracy = 0.01 targetRotation = 0 currentRotation = 90 pulse1 = GPIO.PWM(servo_pin, 50) pulse2 = GPIO.PWM(servo_pin2, 50) logTime = datetime.datetime.now() def log(msg): global deltaTime global logTime if (logTime < datetime.datetime.now()): logTime = datetime.datetime.now() + datetime.timedelta(0, 0.5) print msg return def reset(pulse): pulse.start(7.5); pulse.ChangeDutyCycle(7.5) return def update(pulse, targetRotation): global deltaTime global sumTime global servo_speed global accuracy global currentRotation log ("TargetRotation: " + str(targetRotation) + " CurrentRotation: "+str(currentRotation)) if (targetRotation == 90): pulse.ChangeDutyCycle(7.5) if ((currentRotation - targetRotation) < -accuracy): currentRotation += servo_speed elif ((currentRotation - targetRotation) > accuracy): currentRotation -= servo_speed else: pulse.ChangeDutyCycle(0) elif ((currentRotation - targetRotation) < -accuracy): pulse.ChangeDutyCycle(12.5) currentRotation += servo_speed elif ((currentRotation - targetRotation) > accuracy): pulse.ChangeDutyCycle(2.5) currentRotation -= servo_speed else: pulse.ChangeDutyCycle(0) return try: reset(pulse1) reset(pulse2) time.sleep(1) print "setup complete" while True: last_time = current_time current_time = datetime.datetime.now() deltaTime = current_time - last_time; sumTime += deltaTime; if (sumTime.total_seconds() > 3.0): #print (sumTime) sumTime -= datetime.timedelta(0, 3) targetRotation = (targetRotation + 45) % 180 update(pulse1, targetRotation); update(pulse2, targetRotation); time.sleep(0); except KeyboardInterrupt: print '\r\nProgam complete.' GPIO.cleanup();