yvonnezoe
Posts: 127
Joined: Thu Feb 14, 2013 2:10 am
Contact: Website

Controlling servo from Raspberry Pi

Sun Feb 16, 2014 4:55 pm

Hey guys. I have been researching on the ways to control multiple servos with Raspberry Pi. My first impression was that I need a Gertboard or Arduino for that. I chose Gertboard because it already has ATmega inside. However, later i found out that I can control the servos with just a servo controller such as this one: http://www.pololu.com/product/1350/resources and this one http://www.adafruit.com/products/815#Technical_Details. Is it true?? Which one among those 2 is better?

Also, I need to connect some analog inputs (force sensors) to Raspberry Pi as well. I'm not sure about this part. Can I connect them directly to the Raspberry Pi GPIO?

In this case, what is Gertboard used for?? Is it still needed in my application?

How about the power supply issue if i connect a servo controller or sensors directly to Raspberry Pi?

My situation and problem are described here: http://raspberrypiwonderland.wordpress. ... /update-2/
Just started my Raspberry Pi journey >> http://yvonnezoe.wordpress.com

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

Re: Controlling servo from Raspberry Pi

Sun Feb 16, 2014 5:05 pm

Actually you don't need any additional hardware to control servos. Unlike DC motors and stepper motors you don't need a motor driver board. Servos have one built-in.

You can connect the servo control wire direct to a Pi gpio (one gpio per servo). Connect power to the servo from a battery or other power supply (the Pi will only be able to power tiny servos). Make sure you connect the servo power supply ground to a Pi ground.

https://www.youtube.com/watch?v=5luZQFxfLCo

pigpio will provide servo pulses on all the user gpios.

User avatar
mikronauts
Posts: 2716
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Controlling servo from Raspberry Pi

Sun Feb 16, 2014 8:56 pm

joan's pigpio works great. If you do not need a lot of resolution for the ADC, the PCF8591's are very easy to add., as are the MCP3008 and MCP3208 DIP ADC's.

The Adafroit i2c servo board is also a good deal.

I used pigpio for "SPIRE" (see Jan.2014 Servo Magazine, some info at http://www.mikronauts.com/robot-zoo/sprite/ )

I can control up to 24 servos with my RoboPi, which also has a 12 bit 8 channel MCP3208 ADC.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

yvonnezoe
Posts: 127
Joined: Thu Feb 14, 2013 2:10 am
Contact: Website

Re: Controlling servo from Raspberry Pi

Mon Feb 17, 2014 2:50 am

joan wrote:Actually you don't need any additional hardware to control servos. Unlike DC motors and stepper motors you don't need a motor driver board. Servos have one built-in.

You can connect the servo control wire direct to a Pi gpio (one gpio per servo). Connect power to the servo from a battery or other power supply (the Pi will only be able to power tiny servos). Make sure you connect the servo power supply ground to a Pi ground.

https://www.youtube.com/watch?v=5luZQFxfLCo

pigpio will provide servo pulses on all the user gpios.
Wow thanks for this great information! :D
Just to clarify a few things before I try this out...
I will be controlling about 5 micro servos (TowerPro Sg 9g) from a web app hosted on Raspberry Pi... from what i saw on PIGPIO site, it can be written in Python right? So in this case, my web app can do a "POST" using jQuery to the python file which will then control the servo movements?
But from what i know, Raspberry Pi only has 1 PWM output... how can I use it to control 5 servos? Do you have sample codes to show the controlling of servos with Python?
And to use PIGPIO, all i need is just to download it to my R-Pi?

Thank you so much!
Just started my Raspberry Pi journey >> http://yvonnezoe.wordpress.com

yvonnezoe
Posts: 127
Joined: Thu Feb 14, 2013 2:10 am
Contact: Website

Re: Controlling servo from Raspberry Pi

Mon Feb 17, 2014 2:53 am

mikronauts wrote:joan's pigpio works great. If you do not need a lot of resolution for the ADC, the PCF8591's are very easy to add., as are the MCP3008 and MCP3208 DIP ADC's.

The Adafroit i2c servo board is also a good deal.

I used pigpio for "SPIRE" (see Jan.2014 Servo Magazine, some info at http://www.mikronauts.com/robot-zoo/sprite/ )

I can control up to 24 servos with my RoboPi, which also has a 12 bit 8 channel MCP3208 ADC.
Thanks for the information :) I will take a detailed look at your project.
Just started my Raspberry Pi journey >> http://yvonnezoe.wordpress.com

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Controlling servo from Raspberry Pi

Mon Feb 17, 2014 3:18 am

yvonnezoe wrote: But from what i know, Raspberry Pi only has 1 PWM output... how can I use it to control 5 servos?
That is a bit of a red herring. Servo PWM is done in software by pigpio (or servoblaster if you prefer) on any, or many, GPIO.

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

Re: Controlling servo from Raspberry Pi

Mon Feb 17, 2014 8:50 am

yvonnezoe wrote: ...
Just to clarify a few things before I try this out...
I will be controlling about 5 micro servos (TowerPro Sg 9g) from a web app hosted on Raspberry Pi... from what i saw on PIGPIO site, it can be written in Python right? So in this case, my web app can do a "POST" using jQuery to the python file which will then control the servo movements?
But from what i know, Raspberry Pi only has 1 PWM output... how can I use it to control 5 servos? Do you have sample codes to show the controlling of servos with Python?
And to use PIGPIO, all i need is just to download it to my R-Pi?

Thank you so much!
Can't help with the web app side, my experience is too limited.

This code is an example of sending different pulses to each of 5 servos. You can use any of the user gpios (17 Rev.1 boards, 21 Rev.2).

servo.py
chmod +x servo.py

sudo pigpiod # if not already running

./servo.py

Code: Select all

#!/usr/bin/env python

import time

import pigpio

servos = [4,7,8,9,10]

moves = [[1500, 5],[1400,3],[1300,2],[1200,5],[1100,10]]
      
pigpio.start()
        
for m in moves:

   for s in servos:

      pw = m[0] + (s*50) # give each servo different pulsewidth for demo

      pigpio.set_servo_pulsewidth(s, pw)

      print("Servo {} {} micro pulses".format(s, pw))

   time.sleep(m[1])

   # switch all servos off

   for s in servos:

      pigpio.set_servo_pulsewidth(s, 0);

pigpio.stop()
http://abyz.co.uk/rpi/pigpio/download.html shows how to download and install. The installation includes the Python module, a command line utility pigs, and the pigpio daemon. If you have another (more powerful) Linux box I suggest you also grab piscope.

yvonnezoe
Posts: 127
Joined: Thu Feb 14, 2013 2:10 am
Contact: Website

Re: Controlling servo from Raspberry Pi

Tue Feb 18, 2014 4:43 pm

Thanks ame and joan for the reply. I have installed it successfully but when I run the library test

Code: Select all

sudo ./checklib
there are 2 things that failed.

Code: Select all

Library timer tests.
ABS time diff=8, REL time diff=66
ABS time diff=3, REL time diff=72
ABS time diff=3, REL time diff=71
ABS time diff=2, REL time diff=72
ABS time diff=2, REL time diff=72
ABS time diff=2, REL time diff=72
ABS time diff=3, REL time diff=72
ABS time diff=2, REL time diff=72
ABS time diff=53, REL time diff=19
ABS time diff=4, REL time diff=71
TEST 33: FAILED


Library gpioRead/Write_Bits_x_x_Set/Clear Tests
Expect 0 for pin 47 and >200000 for pin 48.
Expect the green LED to flash.

bank1=1000C04F, bank2=003F0000, 47=0 48=73536
bank1=1000C04F, bank2=003F0000, 47=0 48=73182
bank1=1000C04F, bank2=003F0000, 47=0 48=72452
bank1=0000C04F, bank2=003E0000, 47=0 48=72746
bank1=1000C04F, bank2=003F0000, 47=0 48=73701
bank1=1000C04F, bank2=003E0000, 47=0 48=73412
bank1=0000C04F, bank2=003E0000, 47=0 48=72998
bank1=0001C04F, bank2=003F0000, 47=0 48=72579
bank1=1000C04F, bank2=003F0000, 47=0 48=72490
bank1=0000C04F, bank2=003F0000, 47=0 48=73392
bank1=0000C04F, bank2=003E0000, 47=0 48=73921
bank1=0000C04F, bank2=003E0000, 47=0 48=73153
bank1=1001C04F, bank2=003E0000, 47=0 48=72664
bank1=1000C04F, bank2=003E0000, 47=0 48=73231
bank1=0000C04F, bank2=003F0000, 47=0 48=73808
bank1=0000C04F, bank2=003E0000, 47=0 48=73479
bank1=0000C04F, bank2=003F0000, 47=0 48=72988
bank1=1000C04F, bank2=003F0000, 47=0 48=72356
bank1=0000C04F, bank2=003E0000, 47=0 48=72765
bank1=1000C04F, bank2=003F0000, 47=0 48=73000
TEST 36: FAILED

Are those crucial?

Just to clarify, my next step would be connecting 1 servo (micro servo) signal line to one of the gpio, then power it from external battery (4xAA), and a common ground to pi's ground pin?
I'm quite worried because this is an important school project and I cannot afford to burn my board or anything.

thank you!
Just started my Raspberry Pi journey >> http://yvonnezoe.wordpress.com

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

Re: Controlling servo from Raspberry Pi

Tue Feb 18, 2014 5:28 pm

I'd ignore those fails. The timing one is me being too tight on the expected results. The gpio#48 count is something to do with the SD card clock. For reasons I don't understand it seems to slow down every now and then. I'm revamping the tests to reflect reality.
Just to clarify, my next step would be connecting 1 servo (micro servo) signal line to one of the gpio, then power it from external battery (4xAA), and a common ground to pi's ground pin?
That is correct.

yvonnezoe
Posts: 127
Joined: Thu Feb 14, 2013 2:10 am
Contact: Website

Re: Controlling servo from Raspberry Pi

Wed Feb 19, 2014 5:41 am

joan wrote: This code is an example of sending different pulses to each of 5 servos. You can use any of the user gpios (17 Rev.1 boards, 21 Rev.2).

servo.py
chmod +x servo.py

sudo pigpiod # if not already running

./servo.py

Code: Select all

#!/usr/bin/env python

import time

import pigpio

servos = [4,7,8,9,10]

moves = [[1500, 5],[1400,3],[1300,2],[1200,5],[1100,10]]
      
pigpio.start()
        
for m in moves:

   for s in servos:

      pw = m[0] + (s*50) # give each servo different pulsewidth for demo

      pigpio.set_servo_pulsewidth(s, pw)

      print("Servo {} {} micro pulses".format(s, pw))

   time.sleep(m[1])

   # switch all servos off

   for s in servos:

      pigpio.set_servo_pulsewidth(s, 0);

pigpio.stop()
http://abyz.co.uk/rpi/pigpio/download.html shows how to download and install. The installation includes the Python module, a command line utility pigs, and the pigpio daemon. If you have another (more powerful) Linux box I suggest you also grab piscope.
Thanks! May i ask, by

Code: Select all

servos = [4,7,8,9,10][
does it mean GPIO 4 GPIO 7 and so on? Or is it pin 4, 7,8....? And for

Code: Select all

moves = [[1500, 5],[1400,3],[1300,2],[1200,5],[1100,10]
, what do "1500" and "5" mean? is it "1500" for m[0] (for the pw) and "5" for m[1] (time to sleep)?

I have tested for one servo which i connected the signal to pin7, and power to 5V on R-Pi, ground to ground. I did not use ext power supply because 4xAA batteries gave me >6V, which exceeds the power needed for my micro servo (4.8-6V) so im not sure if that will be an issue. going to try multiple servos with R-Pi 5V now. :x
Just started my Raspberry Pi journey >> http://yvonnezoe.wordpress.com

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Controlling servo from Raspberry Pi

Wed Feb 19, 2014 6:23 am

Try 4x NiMH rechargeable batteries. They'll be a little lower in voltage than alkaline cells.

Or just do it anyway, you won't be exceeding the servo's specs by much, and eventually the cells will drop below 6V.

If you use the 5V supply from the Pi, bear in mind it is drawing current from the USB power supply feeding the Pi. If you draw too much current the voltage will droop and the Pi will reset. This is why a separate battery pack for the motors is better- if it goes flat the Pi will keep running.

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

Re: Controlling servo from Raspberry Pi

Wed Feb 19, 2014 8:31 am

yvonnezoe wrote: ...
Thanks! May i ask, by

Code: Select all

servos = [4,7,8,9,10][
does it mean GPIO 4 GPIO 7 and so on? Or is it pin 4, 7,8....? And for

Code: Select all

moves = [[1500, 5],[1400,3],[1300,2],[1200,5],[1100,10]
, what do "1500" and "5" mean? is it "1500" for m[0] (for the pw) and "5" for m[1] (time to sleep)?

I have tested for one servo which i connected the signal to pin7, and power to 5V on R-Pi, ground to ground. I did not use ext power supply because 4xAA batteries gave me >6V, which exceeds the power needed for my micro servo (4.8-6V) so im not sure if that will be an issue. going to try multiple servos with R-Pi 5V now. :x
pigpio only uses the Broadcom gpio numbering scheme, so 4 refers to Broadcom gpio 4 (pin P1-7 on current Pis).
Python lists are similar to 0 based arrays, so the entries in a list may be accessed by a 0 based subscript.

As you surmised the array is an array of positions and delays (for demo purposes).

1500 is the pulsewidth. Servos are positioned by sending a pulse (generally) between 1 and 2 milliseconds long. 1ms is max counterclockwise, 1.5ms is middle, 2ms is max clockwise.

pigpio uses microseconds so 1000 for max counterclockwise, 1500 for middle, and 2000 for max clockwise.

You can send pulsewidths between 500 and 2500. Only do so if your servo supports such extended settings. You can DAMAGE a servo by forcing it to move too far.

There are devices called UBECs which offer a cheap way to convert from higher voltages to say 5V or 6V. I use a UBEC to take power from a 12V supply to power my hobby servos. Google/eBay will show plenty of hits.

The worse that should happen if you draw too much from the Pi's 5V rail is the Pi abruptly switching off. It shouldn't do any harm unless the SD card is being written.

yvonnezoe
Posts: 127
Joined: Thu Feb 14, 2013 2:10 am
Contact: Website

Re: Controlling servo from Raspberry Pi

Wed Feb 19, 2014 1:23 pm

Noted with thanks! :)
I tried it out with 2 tower pro 90g micro servos and they seem to work well! see here http://www.youtube.com/watch?v=vzH2CAH4 ... e=youtu.be
When i set them to 1000 and 2000, the position did not reach 0 and 180 degree, so i tried 500 and 2500 (i saw these figures in the pigpio site). The positions are perfect! :)

Thank you once again for guiding me along!! :D
Just started my Raspberry Pi journey >> http://yvonnezoe.wordpress.com

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

Re: Controlling servo from Raspberry Pi

Wed Feb 19, 2014 1:55 pm

yvonnezoe wrote:Noted with thanks! :)
I tried it out with 2 tower pro 90g micro servos and they seem to work well! see here http://www.youtube.com/watch?v=vzH2CAH4 ... e=youtu.be
When i set them to 1000 and 2000, the position did not reach 0 and 180 degree, so i tried 500 and 2500 (i saw these figures in the pigpio site). The positions are perfect! :)

Thank you once again for guiding me along!! :D
If the servos ever start making a straining noise you'll have tried to rotate them too far.

Most servo models seem to rotate 90 degrees and expect 1 to 2 millisecond pulses. A few models rotate further (like yours).

For testing purposes you could just use pigs from the command line, e.g.

pigs s 4 1500 (or pigs servo 4 1500)

will send 1500 micro pulses to gpio 4.

yvonnezoe
Posts: 127
Joined: Thu Feb 14, 2013 2:10 am
Contact: Website

Re: Controlling servo from Raspberry Pi

Thu Feb 20, 2014 5:40 am

Oh, pigs does sound rather convenient, will try it out!! Thank you for the useful tip! :D
Just started my Raspberry Pi journey >> http://yvonnezoe.wordpress.com

froussel
Posts: 34
Joined: Thu Oct 04, 2012 3:53 pm

Re: Controlling servo from Raspberry Pi

Mon Jul 14, 2014 9:08 pm

Hi,

I try pigpio and :

Code: Select all

#!/usr/bin/env python

import time

import pigpio

servos = [4,7,8,9,10]

moves = [[1500, 5],[1400,3],[1300,2],[1200,5],[1100,10]]

pigpio.start()

for m in moves:

   for s in servos:

      pw = m[0] + (s*50) # give each servo different pulsewidth for demo

      pigpio.set_servo_pulsewidth(s, pw)

      print("Servo {} {} micro pulses".format(s, pw))

   time.sleep(m[1])

   # switch all servos off

   for s in servos:

      pigpio.set_servo_pulsewidth(s, 0);

pigpio.stop()



[email protected] ~ $ sudo pigpiod
[email protected] ~ $ ./servo.py
Traceback (most recent call last):
  File "./servo.py", line 11, in <module>
    pigpio.start()
AttributeError: 'module' object has no attribute 'start'
Help !

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

Re: Controlling servo from Raspberry Pi

Mon Jul 14, 2014 9:19 pm

I made a change to the Python module so that more than one Pi can be controlled at a time.

You now instantiate any Pi's you want to control. If that doesn't mean anything to you, don't worry about it.

For help within Python do help(pigpio) or pydoc pigpio from the command line.

Try

Code: Select all

#!/usr/bin/env python

import time

import pigpio

servos = [4,7,8,9,10]

moves = [[1500, 5],[1400,3],[1300,2],[1200,5],[1100,10]]

pi = pigpio.pi() # Connect to local Pi.

for m in moves:

   for s in servos:

      pw = m[0] + (s*50) # give each servo different pulsewidth for demo

      pi.set_servo_pulsewidth(s, pw)

      print("Servo {} {} micro pulses".format(s, pw))

   time.sleep(m[1])

   # switch all servos off

   for s in servos:

      pi.set_servo_pulsewidth(s, 0);

pi.stop()

froussel
Posts: 34
Joined: Thu Oct 04, 2012 3:53 pm

Re: Controlling servo from Raspberry Pi

Tue Jul 15, 2014 6:03 am

A big thank you for the quick response and quality.
For help within Python do help(pigpio)
???? but
pydoc pigpio : great !


I did not understand everything on the remote control .

For me, since my pc, I use Putty SSH, sharing windows folders and exchange files with WinSCP. Occasionally a little VNC.

We can do anything from a PC with pigpio ?

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

Re: Controlling servo from Raspberry Pi

Tue Jul 15, 2014 7:20 am

froussel wrote: ...
For me, since my pc, I use Putty SSH, sharing windows folders and exchange files with WinSCP. Occasionally a little VNC.

We can do anything from a PC with pigpio ?
The pigpio Python module should run unchanged on a Window's PC. You still need the pigpio daemon to be running on the Pi.

Instead of

pi = pigpio.pi() # Connect to local Pi.

you should use

pi = pigpio.pi("hard") # Connect to remote Pi by name.

or

pi = pigpio.pi("192.168.1.69") # Connect to remote Pi by IP address.

Where hard/192.168.1.69 are the name/IP address of a Pi running the pigpio daemon.

The example below is from a Linux laptop but Windows is just the same.

Code: Select all

mercury: $
PING hard.lan (192.168.1.69) 56(84) bytes of data.
64 bytes from hard.lan (192.168.1.69): icmp_seq=1 ttl=64 time=3.80 ms
64 bytes from hard.lan (192.168.1.69): icmp_seq=2 ttl=64 time=5.67 ms
^C
--- hard.lan ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 3.802/4.738/5.675/0.939 ms
mercury: $
Python 2.7.8 (default, Jul  4 2014, 13:08:34) 
[GCC 4.9.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pigpio
>>> pi = pigpio.pi("hard")
>>> pi2 = pigpio.pi("192.168.1.69")
>>> pi.get_current_tick()
1039908215
>>> pi2.get_current_tick()
1043345308
>>> pi.read(4)
0
>>> pi2.write(4,1)
0
>>> pi.read(4)
1
>>> pi2.write(4,0)
0
>>> pi.read(4)
0
>>> pi.stop()
>>> pi2.stop()
>>> 
mercury: $

froussel
Posts: 34
Joined: Thu Oct 04, 2012 3:53 pm

Re: Controlling servo from Raspberry Pi

Tue Jul 15, 2014 8:41 am

Thank you.
This opens up great prospects

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Controlling servo from Raspberry Pi

Tue Jul 15, 2014 12:12 pm

froussel wrote:Thank you.
This opens up great prospects
really!
always the best joan! i only miss a windows piscope :)

and since you have not to think i'm out of noobness, a stupid question: what's the best way to keep pigpio updated on my little raspy?

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

Re: Controlling servo from Raspberry Pi

Tue Jul 15, 2014 12:31 pm

pattagghiu wrote: ...
i only miss a windows piscope :)

and since you have not to think i'm out of noobness, a stupid question: what's the best way to keep pigpio updated on my little raspy?
I did try to compile piscope on Windows. I thought it would be reasonably straightforward. It didn't prove so for me and I gave up. There are Window's specific socket calls which is a minor irritation. GTK was the show stopper for me. I just couldn't find a way of building a working executable. I managed to create some pretty blank screens.

There is no automatic way of ensuring pigpio is up to date. I do bung it on github when I make a new release. I suppose that could be tracked.

froussel
Posts: 34
Joined: Thu Oct 04, 2012 3:53 pm

Re: Controlling servo from Raspberry Pi

Tue Jul 15, 2014 2:29 pm

I read a lot of posts of Joan : great

So I still have questions

1) if I've a Hall effect sensor connected to a GPIO, how to count the number of times it changes state?

2) frame PPM RC : (http://skymixer.net/electronics/84-rc-r ... ppm-signal)
a)a) can we do to feed a floor Tx like http://hobbyking.com/hobbyking/store/__ ... DSSS_.html
like http://p.loussouarn.free.fr/arduino/RcP ... Frame.html
b) with the signal for the servo: can plugging gpio decode (Raspi while doing something else and uses)
like http://p.loussouarn.free.fr/arduino/Sof ... lseIn.html

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

Re: Controlling servo from Raspberry Pi

Tue Jul 15, 2014 3:17 pm

froussel wrote:I read a lot of posts of Joan : great

So I still have questions

1) if I've a Hall effect sensor connected to a GPIO, how to count the number of times it changes state?

2) frame PPM RC : (http://skymixer.net/electronics/84-rc-r ... ppm-signal)
a)a) can we do to feed a floor Tx like http://hobbyking.com/hobbyking/store/__ ... DSSS_.html
like http://p.loussouarn.free.fr/arduino/RcP ... Frame.html
b) with the signal for the servo: can plugging gpio decode (Raspi while doing something else and uses)
like http://p.loussouarn.free.fr/arduino/Sof ... lseIn.html
You would normally use something called a callback. A callback is a piece of code you want to be executed when a certain event happens. For the gpios you are usually interested in level changes so all the Python modules provide callbacks on level change.

pigpio provides a callback called callback.

It takes 1, 2, or 3 parameters.

The simplest is callback(gpio). In pigpio this just keeps a count of rising edges on the gpio. See http://abyz.co.uk/rpi/pigpio/python.html#callback

I have added default (tally) callbacks to the servo code for gpios 4 and 7. The tally is printed once per loop.

Code: Select all

#!/usr/bin/env python

import time

import pigpio

servos = [4,7,8,9,10]

moves = [[1500, 5],[1400,3],[1300,2],[1200,5],[1100,10]]

pi = pigpio.pi() # Connect to local Pi.

cb4 = pi.callback(4)
cb7 = pi.callback(7)

for m in moves:

   for s in servos:

      pw = m[0] + (s*50) # give each servo different pulsewidth for demo

      pi.set_servo_pulsewidth(s, pw)

      print("Servo {} {} micro pulses".format(s, pw))

   time.sleep(m[1])

   print("tally 4 = {}, tally 7 = {}".format(cb4.tally(), cb7.tally()))

   # switch all servos off

   for s in servos:

      pi.set_servo_pulsewidth(s, 0);

pi.stop()
When I ran the code I got

Code: Select all

Servo 4 1700 micro pulses
Servo 7 1850 micro pulses
Servo 8 1900 micro pulses
Servo 9 1950 micro pulses
Servo 10 2000 micro pulses
tally 4 = 251, tally 7 = 251
Servo 4 1600 micro pulses
Servo 7 1750 micro pulses
Servo 8 1800 micro pulses
Servo 9 1850 micro pulses
Servo 10 1900 micro pulses
tally 4 = 402, tally 7 = 402
Servo 4 1500 micro pulses
Servo 7 1650 micro pulses
Servo 8 1700 micro pulses
Servo 9 1750 micro pulses
Servo 10 1800 micro pulses
tally 4 = 503, tally 7 = 502
Servo 4 1400 micro pulses
Servo 7 1550 micro pulses
Servo 8 1600 micro pulses
Servo 9 1650 micro pulses
Servo 10 1700 micro pulses
tally 4 = 754, tally 7 = 752
Servo 4 1300 micro pulses
Servo 7 1450 micro pulses
Servo 8 1500 micro pulses
Servo 9 1550 micro pulses
Servo 10 1600 micro pulses
tally 4 = 1254, tally 7 = 1252

Return to “Advanced users”