I am brand new to using the GPIO pins on the Raspberry PI and I'm having a lot of trouble trying to control two continuous rotation servos using pigpio with python and a keyboard.
Servos here: https://www.amazon.com/Parallax-Inc-Continuous-Rotation-Servo/dp/B00B886KSE
The problem is that the servos only seem to be responding half of the time.
This is the only code that I'm running.
#!/usr/bin/env python
import sys, tty, termios, time, pigpio, RPi.GPIO
#motor 1 = GPIO 4 (pin 7) = left motor
#motor 2 = GPIO 26 (pin 37) = right motor
servos = [4,26]
dit = pigpio.pi()
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def motor1_forward():
dit.set_servo_pulsewidth(servos[0], 1600)
def motor1_reverse():
dit.set_servo_pulsewidth(servos[0], 1400)
def motor1_stop():
dit.set_servo_pulsewidth(servos[0], 1500)
def motor2_forward():
dit.set_servo_pulsewidth(servos[1], 1600)
def motor2_reverse():
dit.set_servo_pulsewidth(servos[0], 1400)
def motor2_stop():
dit.set_servo_pulsewidth(servos[0], 1500)
while True:
char = getch()
print " " + char
if(char == "w"):
motor1_forward()
motor2_forward()
if(char == "s"):
motor1_reverse()
motor2_reverse()
if(char == "a"):
motor1_stop()
motor2_forward()
if(char == "d"):
motor2_stop()
motor1_forward()
if(char == "x"):
print("STOPPED")
motor1_stop()
motor2_stop()
dit.stop()
break
char = ""
dit.stop()
My questions are:
- Why are the servos only starting and stopping when they want to?
- Why do the motors keep spinning even if no key is being pressed?