0

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?

1 Answer 1

2

Some things to note.

  • motor2_reverse() and motor2_stop() were changing motor1
  • servo pulses continue at the set pulse width until you either stop the pulses (pulse width 0) or change the pulse width.
  • continuous rotation servos normally stop when you set a pulse width of 1500 µs. Some have a recessed tuning screw to fine tune the stop pulse width.

I have edited the code as follows.

#!/usr/bin/env python

import sys, tty, termios, time, pigpio

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)
    print("motor1=1600")

def motor1_reverse():
    dit.set_servo_pulsewidth(servos[0], 1400)
    print("motor1=1400")

def motor1_stop():
    dit.set_servo_pulsewidth(servos[0], 1500)
    print("motor1=1500")

def motor2_forward():
    dit.set_servo_pulsewidth(servos[1], 1600)
    print("motor2=1600")

def motor2_reverse():
    dit.set_servo_pulsewidth(servos[1], 1400)
    print("motor2=1400")

def motor2_stop():
    dit.set_servo_pulsewidth(servos[1], 1500)
    print("motor2=1500")

while True:
    char = getch()

    print("          " + char)

    if char == "w":
        motor1_forward()
        motor2_forward()

    elif char == "s":
        motor1_reverse()
        motor2_reverse()

    elif char == "a":
        motor1_stop()
        motor2_forward()

    elif char == "d":
        motor1_forward()
        motor2_stop()

    elif char == "x":
        print("STOPPED")
        motor1_stop()
        motor2_stop()
        time.sleep(1)
        for s in servos: # stop servo pulses
            dit.set_servo_pulsewidth(s, 0)
        dit.stop()
        break

dit.stop()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.