I am making a project where I want to use a PC/Xbox controller to control my servos with analog sticks. I've been following this tutorial and I've used the code down below to control my servo motor with an analog stick, but encountered a problem of servo jitter, while using RPi.GPIO. I wrote a simple program, which controls the servo with the use of PIGPIO library and it works perfectly. Can anybody help me to transform the code below to use a PIGPIO library instead?
Thank you.
Code I'm using with RPi.GPIO:
import RPi.GPIO as GPIO
import math
import xbox
GPIO_SERVO_PIN = 20
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(GPIO_SERVO_PIN, GPIO.OUT)
def updateServo(pwm, angle):
duty = float(angle) / 10.0 + 2.5
pwm.ChangeDutyCycle(duty)
def angleFromCoords(x,y):
angle = 0.0
if x==0.0 and y==0.0:
angle = 90.0
elif x>=0.0 and y>=0.0:
# first quadrant
angle = math.degrees(math.atan(y/x)) if x!=0.0 else 90.0
elif x<0.0 and y>=0.0:
# second quadrant
angle = math.degrees(math.atan(y/x))
angle += 180.0
elif x<0.0 and y<0.0:
# third quadrant
angle = math.degrees(math.atan(y/x))
angle += 180.0
elif x>=0.0 and y<0.0:
# third quadrant
angle = math.degrees(math.atan(y/x)) if x!=0.0 else -90.0
angle += 360.0
return angle
if __name__ == '__main__':
joy = xbox.Joystick()
pwm = GPIO.PWM(GPIO_SERVO_PIN, 100)
pwm.start(5)
while not joy.Back():
# Servo
x, y = joy.leftStick()
angle = angleFromCoords(x,y)
if angle > 180 and angle < 270:
angle = 180
elif angle >= 270:
angle = 0
updateServo(pwm, angle)
joy.close()
pwm.stop()`