User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 586
Joined: Sat Nov 02, 2013 2:06 pm

pigpio - cleaning up GPIO pins

Sun Jul 12, 2015 7:45 pm

Hi;

I am playing around learning some Python and am trying to use pigopi. I can blink and LED and now I am trying to control some servos.

What pigpio command do I use to set the position of a servo, I tried using pi.set_PWM_dutycycle(<pin>, <vlaue> ) but think this is for controlling motor speed, I am using Micro 9g servos.

Thanks
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

User avatar
joan
Posts: 15781
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pigpio - cleaning up GPIO pins

Sun Jul 12, 2015 7:51 pm

You would normally use set_servo_pulsewidth.

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 586
Joined: Sat Nov 02, 2013 2:06 pm

Re: pigpio - cleaning up GPIO pins

Sun Jul 12, 2015 8:18 pm

Sorry - just worked it out by looking at the pigpio library again, its pi.set_servo_pulsewidth(<pin>, <time in ms>).

Here's my code for testing the servos on my Pan and Tilt camera mount, it simply runs in a loop with each servo going from stop / anticlockwise / centre / clockwise.

Code: Select all

import RPi.GPIO as GPIO
import time
import pigpio

pi = pigpio.pi()
#pi.set_mode(23, pigpio.OUTPUT) #set pin 23 as OUTPUT 
#pi.set_mode(24, pigpio.OUTPUT)
var=1
print "Start loop"

try:
        while var==1 :
                print "Set servo 1 & 2 off"
                pi.set_servo_pulsewidth(23, 0)             
                pi.set_servo_pulsewidth(24, 0)             
                time.sleep(1)
                print "Set sero 1 & 2 anticlockwise safe"
                pi.set_servo_pulsewidth(23, 1100)                
                pi.set_servo_pulsewidth(24, 1100)                
                time.sleep(1)
                print "Set servo 1 & 2 centre"
                pi.set_servo_pulsewidth(23, 1500)                
                pi.set_servo_pulsewidth(24, 1500)                
                time.sleep(1)
                print "Set servo 1 & 2 clockwise safe"
                pi.set_servo_pulsewidth(23, 1900)                
                pi.set_servo_pulsewidth(24, 1900)                
                time.sleep(1)

except KeyboardInterrupt:
        print "Cleaning up"
        pi.stop()
Question:
I have changed the timings slightly but at one setting, (clockwise I think) it still sound like the servo is draining power. Also when I interrupt the program the servo stays buzzing.
I have initiated a pi.stop() but is there something else I need to do to stop the servo draining power.
I have commented out the pi.setmode as I don't think its needed.

Any help would be appreciated.
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

User avatar
joan
Posts: 15781
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pigpio - cleaning up GPIO pins

Sun Jul 12, 2015 8:41 pm

You'll need to explicitly stop the servo pulses.

pigpio assumes that anything left running is meant to be left running, e.g. if you set a gpio high, it will be left high, if you started PWM or servo pulses on a gpio they will continue.

In your case you would need to change

Code: Select all

except KeyboardInterrupt:
        print "Cleaning up"
        pi.stop()
to

Code: Select all

except KeyboardInterrupt:
        print "Cleaning up"
        pi.set_servo_pulsewidth(23, 0)               
        pi.set_servo_pulsewidth(24, 0)               
        pi.stop()
If the servo is buzzing it may be acting against an end-stop.

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 586
Joined: Sat Nov 02, 2013 2:06 pm

Re: pigpio - cleaning up GPIO pins

Mon Jul 13, 2015 5:17 am

Thanks joan :-)
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

christoskar
Posts: 2
Joined: Fri Mar 30, 2018 6:57 pm

Re: pigpio - cleaning up GPIO pins

Fri Mar 30, 2018 7:02 pm

I am using a Raspberry Pi 2, Afro ESC’s, and t100 thrusters. I am a beginner. I am trying to connect the t100 thrusters to my raspberry pi 2. I had operated only one thruster with GPIO Pin 18 but I try to connect pins with different GPIO Pin but it isn’t work. I want connect four thruster. What’s happening?

Thanks
Christos

My program:

Code: Select all

from pygame import *
import RPi.GPIO as GPIO
import time
import pigpio

pi = pigpio.pi()
init()
screen = display.set_mode((640, 480))
display.set_caption('the program')

motor1=23
motor2=24
led1=22
led2=25

endProgram = False

while not endProgram:
    for e in event.get():
        if e.type ==KEYDOWN:
           if (e.key == K_a):
              print ("key a")
              pi.set_servo_pulsewidth(motor1, 1200)                
              pi.set_servo_pulsewidth(motor2, 1200)
              pi.set_servo_pulsewidth(led1, 1300)
              pi.set_servo_pulsewidth(led2, 1300)
              time.sleep(1)
           elif (e.key == K_c):
                print("key c")
                pi.set_servo_pulsewidth(motor1, 1800)                
                pi.set_servo_pulsewidth(motor2, 1850)
                pi.set_servo_pulsewidth(led1, 1700)
                pi.set_servo_pulsewidth(led2, 1700)
                time.sleep(1)
           elif (e.key == K_b):
                print("key b")
                pi.set_servo_pulsewidth(motor1, 1700)                
                pi.set_servo_pulsewidth(motor2, 1750)
                pi.set_servo_pulsewidth(led1, 1300)
                pi.set_servo_pulsewidth(led2, 1300)
                time.sleep(1)     
           elif (e.key == K_q):
                print("key b")
                pi.set_servo_pulsewidth(motor1, 0)                
                pi.set_servo_pulsewidth(motor2, 0)
                pi.set_servo_pulsewidth(led1, 0)
                pi.set_servo_pulsewidth(led2, 0)
                time.sleep(1)
           elif e.key == K_ESCAPE:
                endProgram = True
           else:
               print ("error")[code]
[/code]

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 586
Joined: Sat Nov 02, 2013 2:06 pm

Re: pigpio - cleaning up GPIO pins

Fri Mar 30, 2018 8:47 pm

This thread isn’t about 3 years old. A thruster is a brushless motor and an esc is used to drive it. Not sure why you are using set_servo?

Anyway I found this which should help you out:

http://www.instructables.com/id/Driving ... pberry-Pi/
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

Return to “Python”