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

PWM for servos, motors, and LEDs plus.

Sun Jan 20, 2013 10:51 pm

***************************************************************************************
NEWER VERSION.

See http://www.raspberrypi.org/phpBB3/viewt ... 30#p347830
***************************************************************************************

I have just released pigpio version 2, a C library.

Latest version from http://abyz.co.uk/rpi/pigpio/

Video at https://www.youtube.com/watch?v=2vq0Q4dD6KE

N.B. The pigpio library uses the DMA (channel 14 by default) and the PWM peripherals.

It is likely to be incompatible with the accelerated X driver being tested as that may saturate the DMA bus and destroy its use for timing purposes.

Consider the pigpio library to be experimental and use at your own risk. That said I have not had a problem.

Its main features are:
  • provision of PWM on any number of gpios 0-31 simultaneously.
  • provision of servo pulses on any number of gpios 0-31 simultaneously.
  • callbacks when any of gpios 0-53 change state.
  • callbacks at timed intervals.
  • reading/writing all of the gpios in a bank (0-31, 32-53) as a single operation.
  • individually setting gpio modes, reading and writing.
NOTE: ALL gpios are identified by their Broadcom number.

There are two significant enhancements over version 1.
  • The PWM cycle is now 787Hz (previously 394Hz).
  • The levels of all (54) gpios are read and time stamped 200 thousand times per second (previously 1 thousand times per second).
Last edited by joan on Fri Dec 20, 2013 9:51 am, edited 4 times in total.

jjackowski
Posts: 44
Joined: Thu Jan 10, 2013 5:25 am

Re: PWM for servos, motors, and LEDs plus.

Fri Feb 01, 2013 3:57 am

This sounds very useful. I'm interested to see how you implemented it. I'm guessing that part is inside the pigpio.c.cpt file, but I don't know how to make sense of that file. What format is it? What program can work with it?

Thanks, and have fun!

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

Re: PWM for servos, motors, and LEDs plus.

Fri Feb 01, 2013 1:12 pm

The cpt is the encrypted C source.

I'm happy to release the source to anyone who wants it. My only condition is that you don't publish it.

woodengineer
Posts: 5
Joined: Sat Jun 09, 2012 12:32 pm

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 04, 2013 4:26 pm

Hello joan,

This library looks very cool. I would like to write a wrapper unit in pascal.
I have done this before with Gorden's WiringPi library.
In order to do this from pascal, I need an .o file of the library, which I can then link to from pascal code.
As far as I can tell, there is no way to compile the encrypted C file. Please provide the C source.
Once I have the .o file, I will be able to complete the wrapper unit.

Have a great day!

Thanks,
Alex

avaquero
Posts: 1
Joined: Mon Feb 11, 2013 5:26 am

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 11, 2013 5:38 am

joan, great library. Thanks!

woodengineer, you can get the .o file by:

Code: Select all

# ar -x libpigpio.a
I've done it to create a shared .so version of the library needed for my project which uses node.js and the FFI module.

skiddaw
Posts: 9
Joined: Sat Oct 29, 2011 6:19 pm

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 25, 2013 3:08 am

On my pi, in the pigpio folder I run tar -xf pigpio-V2.tar and get
tar: This does not look like a tar archive
Even tar -t ditto gets me nothing
any ideas?

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

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 25, 2013 9:32 am

skiddaw wrote:On my pi, in the pigpio folder I run tar -xf pigpio-V2.tar and get
tar: This does not look like a tar archive
Even tar -t ditto gets me nothing
any ideas?
Download the tar to a known location, then copy to your home directory. Install as follows.
cp Downloads/pigpio-V2.tar ~

cd ~

tar xvf pigpio-V2.tar

cd PIGPIO/

cp libpigpio.a-hard libpigpio.a

make install

make

sudo ./checklib

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 25, 2013 12:46 pm

@joan (or anyone else)

Is there a working way of using this from Python?

regards

Simon
PS I know all things are possible - I'm looking for "here is one I did earlier" :)
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

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

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 25, 2013 12:59 pm

simplesi wrote:@joan (or anyone else)

Is there a working way of using this from Python?

regards

Simon
PS I know all things are possible - I'm looking for "here is one I did earlier" :)
I don't know. Not ALL things are possible.

On balance I think it could be done. Someone with any experience of integrating Python and C could probably give a considered answer after half an hour or so.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 25, 2013 1:07 pm

I'll start the timer :)

I'm just wanting to outsource PWM to background processes as doing it "manually" e.g GPIO.11 on wait GPIO.11 off is not too bad but once you get into wanting 2 pins doing it at same time (plus a couple of servos!) then I soon start to have pulses arriving before they've been sent in the first place :)

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

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

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 25, 2013 1:19 pm

simplesi wrote:I'll start the timer :)
...
I'm afraid the timer will start if and when someone experienced in Python/C integration notices the thread and is interested enough to answer!

http://www.youtube.com/watch?v=aomqyL9VVl4 (persevere over the first half minute) shows an early C attempt at generating servo pulses. Glitches are fairly apparent as ill-mannered processes refuse to yield the processor.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4208
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 25, 2013 1:25 pm

I'm no expert, but I was reading the docs this morning :-)

There's no reason why most of the stuff couldn't be wrapped to work from Python, but callbacks may be a problem.

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

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 25, 2013 1:42 pm

rurwin wrote:I'm no expert, but I was reading the docs this morning :-)

There's no reason why most of the stuff couldn't be wrapped to work from Python, but callbacks may be a problem.
Richard (the servoblaster) and myself had a discussion re the relative merits of kernel versus userland daemon versus userland library implementation of gpio control.

I prefer a userland library as that most closely mirrors my usage.

However I think Richard's preference for a userland daemon for PWM and servo might better fit other's usage, I know Richard has a servo daemon, another chap has a PWM daemon. Perhaps they could be merged? That would at least be language neutral.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: PWM for servos, motors, and LEDs plus.

Mon Feb 25, 2013 5:06 pm

Love the semaphore servo's :)
Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

badook
Posts: 43
Joined: Sun Dec 16, 2012 7:44 pm

Re: PWM for servos, motors, and LEDs plus.

Tue Feb 26, 2013 2:07 pm

This is very useful indeed. I only wish it was fully open source, because it would be more in line with the linux and raspberry pi philosophy ;)

For those who prefer open source and/or are familiar with servoblaster, there is another project called pi-blaster specifically for dc motors which also uses DMA direct access to provide pwm.

radlen
Posts: 2
Joined: Thu Mar 21, 2013 7:02 pm

Re: PWM for servos, motors, and LEDs plus.

Thu Mar 21, 2013 7:19 pm

Hi,
I am using this great library with some quadrature encoders and the Raspi never misses any edges any more like it did when I started playing around with the GPIOs.
For my software it would be great to have an additional void* in the callback-function that one can provide when registring the callback.
The prototype would look like this:

Code: Select all

typedef void (*gpioAlertFuncEx_t)(int gpio, int level, uint32_t tick, void *userdata); 
and the function for registering the callback

Code: Select all

gpioSetAlertFuncEx(unsigned gpio, gpioAlertFunc_t f,  void *userdata); 
For educational purposes I am rewriting my application using C++ and what I have read so far is that this addition would also simplify interfacing with C++ classes.

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

Re: PWM for servos, motors, and LEDs plus.

Thu Mar 21, 2013 11:27 pm

radlen wrote:...
For my software it would be great to have an additional void* in the callback-function that one can provide when registring the callback.
The prototype would look like this:

Code: Select all

typedef void (*gpioAlertFuncEx_t)(int gpio, int level, uint32_t tick, void *userdata); 
and the function for registering the callback

Code: Select all

gpioSetAlertFuncEx(unsigned gpio, gpioAlertFunc_t f,  void *userdata); 
...
I've no objection in principle. I just couldn't think of any practical use at the time for a userdata parameter. Do you have a particular use in mind?

radlen
Posts: 2
Joined: Thu Mar 21, 2013 7:02 pm

Re: PWM for servos, motors, and LEDs plus.

Fri Mar 22, 2013 7:41 am

Thanks joan for your fast response.
I will try to explain what I try to achieve.
I am interfacing the Raspi with several quadrature encoders each using 2 GPIO pins. In my application the info for each encoder (which pins are used, the counter etc.) is stored in a structure.
I want to provide this pointer when registering the callback and I want to get back this pointer when the callback is actually called. Then I have the relation between the pins and the encoder info and I can proceed with the info. Otherwise I would have to store the info about the relation between the pins and the encoder on my own. I have already done this, but the way using the extra pointer is more elegant.
But why I asked for this addition is because I now want to interface with some C++ classes. I would register a pin for a callback and give the pointer to my encoder object. In the callback I can cast the given pointer to my class type and call the methods I need. As far as I have read this is the way how interfacing between a C library and C++ classes can be done.
I probably could do these additions myself but as far as I can see the sources for this great library are not released yet...

zog
Posts: 183
Joined: Sun Nov 20, 2011 5:43 pm
Contact: Website

Re: PWM for servos, motors, and LEDs plus.

Sat May 11, 2013 5:57 am

Hi Alex,
I was wondering how you had got on with wrapping joans library ? I have been playing about with Gordon's wiringpi library inside lazarus. Gordon's library only allows pulse with modulation on GPIO port 18, and he doesn't give full instructions on how to set it up. I am trying to make an IO package within lazarus to make it easier for other programmers to quickly implement lcd display drivers, motor controllers, etc within lazarus. I realise that you have been there and done some of these things before me and I don't want to duplicate your efforts unnecessasarily.

Jack

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

Re: PWM for servos, motors, and LEDs plus.

Sat May 11, 2013 7:26 am

zog wrote:Hi Alex,
I was wondering how you had got on with wrapping joans library ?
...
Jack
I am shortly to release an upgrade which will expose 95% of the functionality to other languages. It might be worth holding off for a day or so.

nrgful
Posts: 1
Joined: Fri Jun 07, 2013 8:59 am

Re: PWM for servos, motors, and LEDs plus.

Sun Jul 28, 2013 2:52 pm

Hello, I am working on a school project. I'm interesting in if I can use this library, however not every one pin is turned in to output (as I read in the documentation). For example, to make a quadrotor helicopter and using 4 pins of the R-Pi as PWM and other two pins as I2C input for reading values from IMU? Is it possible?
Thank you in advance

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

Re: PWM for servos, motors, and LEDs plus.

Sun Jul 28, 2013 7:20 pm

nrgful wrote:Hello, I am working on a school project. I'm interesting in if I can use this library, however not every one pin is turned in to output (as I read in the documentation). For example, to make a quadrotor helicopter and using 4 pins of the R-Pi as PWM and other two pins as I2C input for reading values from IMU? Is it possible?
Thank you in advance
Yes, you can leave the I2C gpios as I2C gpios and use other gpios for PWM.

In this video 11 gpios are used for PWM (servos and motors etc.) and the I2C gpios are used to access a GY-85 inertial Measurement Unit (IMU).

https://www.youtube.com/watch?v=2vq0Q4dD6KE
Last edited by joan on Fri Dec 20, 2013 9:26 am, edited 1 time in total.

User avatar
cyryllo
Posts: 5
Joined: Thu May 17, 2012 6:38 am
Location: Gdansk/Poland
Contact: Website

Re: PWM for servos, motors, and LEDs plus.

Thu Oct 10, 2013 8:14 am

Hello.
I have a question about the library pigpio. Function pigs wdog checks the state of the change pins. Where it saves these changes and how to read them?

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: No registered users and 15 guests