I am working on a self driving rc car project and I need a servo to steer my car. As I was getting a bit jitter with RPi.GPIO I tried pigpio library. I am using a Raspbery Pi Model B. But it won't move my servo. I am using pin 10 which is gpio 16. My this code with rpi.GPIO is working -
from time import sleep
import RPi.GPIO as gpio
gpio.setmode(gpio.BOARD)
gpio.setup(10,gpio.OUT)
pwm = gpio.PWM(10,80)
pwm.start(0)
def SetAngle(angle):
duty = angle / 18 + 2
gpio.output(10, True)
pwm.ChangeDutyCycle(duty)
sleep(1)
gpio.output(10, False)
# pwm.ChangeDutyCycle(0)
try:
while True:
angle = float(input('ENTER ANGLE PLEASE: '))
SetAngle(angle)
except:
SetAngle(0)
pwm.stop()
gpio.cleanup()
But this code using pigpio is not -
import pigpio
import math
GPIO_SERVO_PIN = 10
MIN_ANG=-180.0 #degrees
MAX_ANG=180.0 #degrees
MIN_PW=1000 # microseconds
MAX_PW=2000 # microseconds
ANG_RANGE=MAX_ANG-MIN_ANG
PW_RANGE=MAX_PW-MIN_PW
PWAR=float(PW_RANGE)/ANG_RANGE
RAD2DEG=180.0/math.pi
def angleToPulseWidth(angle):
"""
angle is mapped to valid pulse widths for servo
which are determined by experiment.
"""
assert MIN_ANG <= angle <= MAX_ANG
return MIN_PW + ((angle - MIN_ANG) * PWAR)
pi = pigpio.pi()
if not pi.connected:
exit()
while True:
angle = float(input('Please enter a angle: '))
pw = angleToPulseWidth(angle)
pi.set_servo_pulsewidth(GPIO_SERVO_PIN, pw)
I have run the following command also which let's us use the pigpio library -
sudo pigpiod