0

I am currently creating a design project that involves controlling several servo motors for a raspberry pi 3. I want to use a keyboard to control the direction of the motors, and I am using curses to take in the keyboard input. My program runs, and the print function shows the correct key that was pressed, but the servos won't spin. I am using the pigpio library to drive the servos. The code I have is below. Any idea what i am doing wrong? Disclaimer: I am not a seasoned programmer, this is my first project with a raspberry pi and python.

import pigpio
import curses
pi=pigpio.pi()

screen = curses.initscr()
curses.noecho()
curses.cbreak()
screen.keypad(True)


if __name__ == '__main__':

while True:

    Char = screen.getch()


    if Char== ord('q'):
        pi.set_servo_pulsewidth(16, 1600)
        print ("rotate left")

    if Char==ord('e'):
        pi.set_servo_pulsewidth(16,1600)
        print (" rotate right")

    if Char==ord('a'):
        pi.set_servo_pulsewidth(12, 1600)
        pi.set_servo_pulsewidth(13, 0)
        print ("turn left")

    if Char==ord('d'):
        pi.set_servo_pulsewidth(12, 0)
        pi.set_servo_pulsewidth(13, 1400)
        print ("turn right")

    if Char==ord('s'):
        pi.set_servo_pulsewidth(12, 1400)
        pi.set_servo_pulsewidth(13, 1600)
        print ("backwards")

    if Char==ord('w'):
        pi.set_servo_pulsewidth(12, 1400)
        pi.set_servo_pulsewidth(13, 1400)
        print ("forwards")
    if Char == curses.KEY_UP:
        pi.set_servo_pulsewidth(18, 1600)
        print ("telescope up")

    if Char==curses.KEY_DOWN
        pi.set_servo_pulsewidth(18, 1400)
        print ("telescope Down")

    if Char==ord('x'):
        print("Program Ended")
        break

curses.nocbreak(); screen.keypad(0); curses.echo()
curses.endwin()enter code here

bumped to the homepage by Community 23 hours ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

  • How are the servos connected? A photo of the connections may be useful. Ordinary hobby servos don't spin - they go to the commanded angle and then stop. Which servo model do you have? – joan Feb 18 at 22:49
  • Ah sorry, they are continuous servo motors who's control wires are connected to GPIO 12, 13, 16, and 18 respectively. They are powered by a battery pack separate from the pi. – user10560187 Feb 18 at 23:09
  • Have you connected the -ve of the battery pack to a Pi ground pin? – joan Feb 19 at 8:14
  • I just connected the battery pack to ground pin, and I have a result. I can now use one motor. the one associated with the arrow keys GPIO(18). Whats weird is that its spinning the motor attached to GPIO (12), – user10560187 Feb 19 at 14:36
  • I found out my GPIO problem...I was looking at the pins upside down lol. But the applying -ve from the battery pack to a ground pin of the pi board worked. Everything is up and running, thank you very much for your help. – user10560187 Feb 19 at 15:45
0

If you're new to this, you might find it easier using GPIO Zero rather than pigpio, as it's a more friendly API, and provides a built-in Servo class. You can still use pigpio as your back-end (it's the best one for servos).

Example servo code:

from gpiozero import Servo
from time import sleep

servo = Servo(14)

servo.min()
sleep(1)
servo.mid()
sleep(1)
servo.max()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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