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

Re: New pigpio Python module

Fri Mar 06, 2015 10:15 am

Hello joan,
With pigpio and python code, since i've never used the trigger function, i was wondering if this

Code: Select all

pi.gpio_trigger(23, 10, 1)
is the correct way of doing this

Code: Select all

pi.write(23,1)
time.sleep(0.001)

I expect the first to be much more precise in timing than the second..
the gpio needs to be in output mode or it's managing that by itself?

As always thanks

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

Re: New pigpio Python module

Fri Mar 06, 2015 10:36 am

pattagghiu wrote:Hello joan,
With pigpio and python code, since i've never used the trigger function, i was wondering if this

Code: Select all

pi.gpio_trigger(23, 10, 1)
is the correct way of doing this

Code: Select all

pi.write(23,1)
time.sleep(0.001)

I expect the first to be much more precise in timing than the second..
the gpio needs to be in output mode or it's managing that by itself?
...
Yes, that's correct. Because the pigpio Python module is server/client based you need to use that call to get accurate short timings (otherwise the time would be swamped by the round trip time for the successive server/client socket messages).

You need to set the gpio as an output first.

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

Re: New pigpio Python module

Fri Mar 06, 2015 5:55 pm

joan wrote:Yes, that's correct. Because the pigpio Python module is server/client based you need to use that call to get accurate short timings (otherwise the time would be swamped by the round trip time for the successive server/client socket messages).
hello again,
is there a reason for limiting trigger length to 100us?

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

Re: New pigpio Python module

Fri Mar 06, 2015 6:17 pm

pattagghiu wrote:
joan wrote:Yes, that's correct. Because the pigpio Python module is server/client based you need to use that call to get accurate short timings (otherwise the time would be swamped by the round trip time for the successive server/client socket messages).
hello again,
is there a reason for limiting trigger length to 100us?
None at all, other than I wanted a limit. 100us happens to be the longest busy wait pigpio will do, any longer and system calls are used.

The only use I've ever identified is for the sonar ranger trigger. Unfortunately it turns out the sonar trigger length can be milliseconds (if not seconds) so the function probably isn't needed at all.

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

Re: New pigpio Python module

Sun Mar 08, 2015 10:13 am

Hello,
i'm so sorry, but i have another question :)

I havn't understood how this is intended to work
http://abyz.co.uk/rpi/pigpio/pig2vcd.html

I tried in this way but could not obtain anything..

Code: Select all

pi@raspone ~ $ pigs NO
3
pi@raspone ~ $ pigs NB 3 -1
pi@raspone ~ $ pig2vcd

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

Re: New pigpio Python module

Sun Mar 08, 2015 10:34 am

pattagghiu wrote:Hello,
i'm so sorry, but i have another question :)

I havn't understood how this is intended to work
http://abyz.co.uk/rpi/pigpio/pig2vcd.html

I tried in this way but could not obtain anything..

Code: Select all

pi@raspone ~ $ pigs NO
3
pi@raspone ~ $ pigs NB 3 -1
pi@raspone ~ $ pig2vcd
The NO command says you want to be notified about gpio level changes and returns a handle.

You use the handle to start/stop getting notifications.

A bit mask is used to indicate the gpios you are interested in, -1 says all.

The notifications are written to a pipe name /dev/pigpiox, where x is the handle number.

You can view the (binary) notification by typing
(cat /dev/pigpio3 | od -x) &
That'll launch a job.

Then generate changes with

pigs w 4 1 w 4 0 w 4 1 w 4 0

You'll see the notifications.

Kill that job.

Then

pig2vcd </dev/pigpio3 &

and repeat

pigs w 4 1 w 4 0 w 4 1 w 4 0

to see a VCD file.

Normally you'd use

pig2vcd </dev/pigpiox >filename

piscope uses notifications to get the gpio changes. It can also output VCD format files.

Now is as good a time as any to mention that piscope is now Public Domain at https://github.com/joan2937/piscope

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

Re: New pigpio Python module

Sun Mar 08, 2015 10:50 am

joan wrote:Normally you'd use
pig2vcd </dev/pigpiox >filename
lol, i would have never got this..
Thanks
Now is as good a time as any to mention that piscope is now Public Domain at https://github.com/joan2937/piscope
I tried to compile piscope on a ubuntu virtualbox and got bad things, so i passed to vcd viewers..

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

Re: New pigpio Python module

Sun Mar 08, 2015 11:02 am

piscope actually works pretty well on the Pi2 display. On earlier Pis it was jerky when used on the local display.

matter45
Posts: 10
Joined: Wed Mar 11, 2015 11:34 pm

Re: New pigpio Python module

Thu Mar 12, 2015 12:22 am

Hi Joan

Just wana say thanks for all the support. I dont know who did all the hard work to create pigpio but whoever did I wana say a huge thanks.

In saying that I have been at it trying to learn to use it for a couple of days now. I have hooked up an oscilliscope and did some test measurements and have noted the following:

set_PWM_frequency cannot specify an exact frequency above 1khz. the only frequency's i could achieve above 1khz where 2khz, 4khz and 8khz.

the smallest duty cycle pulse i could accomplish using set_PWM_dutycycle was 4.5us. Still really good. Ive managed to get 1.8us using trigger.

I am yet to play with hardware_PWM.

Before I spend excessive amount of time learning how to code in Python and use the pigpio, I want to know if it is possible to accomplish what i want to do.

I want to be able to send a pulse on 3 different GPIO's. But the timing on each pulse for each GPIO is critical. the frequencies will be the same, however each GPIO needs to send a pulse at a different time to each other. Looking over the pigpio Benchmark python script, it looks like it is possible by using callback. but then as i was reading on this thread you are restricted to 100us using the call back.

Joan talks about using "system calls are used" when under 100us is required. I am yet to research on this option.

Specifically, I would like all 3 pulses to be evenly pulsed at separate times on the same frequency. The idea that I have in my head is to measure when the first pulse occurs on the first GPIO then falls, then initiate a delay time, then pulse the second GPIO, measure when the second pulse falls, then pulse the third. then the process repeats.

Am I wasting my time thinking that pigpio can do this?

Cheers

Mathew

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

Re: New pigpio Python module

Thu Mar 12, 2015 8:17 am

http://abyz.co.uk/rpi/pigpio/cif.html#g ... Mfrequency documents the frequencies available when using the Python set_PWM_frequency function. Generally Python functions will be implemented by a C function with a recognisably similar name.

There are two hardware PWM channels available on the A+/B+/Pi2/Compute module. You should be able to get arbitrary frequencies with good accuracy. For the hardware PWM channels the dutycycle is set out of a million. So 10% is 100000, 75% is 750000 etc.

If you want to control the start time of each pulse in relation to the others you will have to use the waveform functions. The other functions do not allow any control of when the pulse starts.

Callbacks are not suitable for this type of control. The event causing the callback is time-stamped but no guarantee is made about how long it took between the event and the callback being called.

What frequency are your pulses? How many steps between off and fully on do you need? The answers will show if pigpio can do what you want or not.

matter45
Posts: 10
Joined: Wed Mar 11, 2015 11:34 pm

Re: New pigpio Python module

Thu Mar 12, 2015 10:09 am

Attached should clear up what I am trying to accomplish. Each line represents a GPIO and the frequency is at 5khz. The pulses in the scope pic are 10us wide.

As you can see the pulses between all 3 GPIO's are evened out between each other.

I also will need the ability to change the duty cycle from 10us. The range is from 1us to 50us.

Experimenting this afternoon on hardware_PWM and I am able to do this with two GPIO's by setting one frequency @ 5khz and the other @ 10khz, and the pulses that occur at the same time will cancel out the 10khz using an XOR gate transistor.

But if I can get all three pulses that would be awesome.

Can I say YOU ARE AWESOME Joan!
Attachments
3 seperate pulses.PNG
3 seperate pulses.PNG (9.39 KiB) Viewed 2973 times

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

Re: New pigpio Python module

Thu Mar 12, 2015 11:32 am

matter45 wrote:Attached should clear up what I am trying to accomplish. Each line represents a GPIO and the frequency is at 5khz. The pulses in the scope pic are 10us wide.

As you can see the pulses between all 3 GPIO's are evened out between each other.

I also will need the ability to change the duty cycle from 10us. The range is from 1us to 50us.
...
Okay, I think I understand. Waveforms are the way to go. Let me think about it for a while.

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

Re: New pigpio Python module

Thu Mar 12, 2015 12:35 pm

Here is an attempt at what I think you want. No error checking.

Code: Select all

#!/usr/bin/env python

# 2015-03-12
# matter45.py
# Public Domain

import sys
import time

import pigpio

NONE=0

GPIOS=[4, 5, 6]

FREQ=5000
PULSE_WIDTH=20

if len(sys.argv) > 1:
   f=int(sys.argv[1])
else:
   f = FREQ

if len(sys.argv) > 2:
   pw=int(sys.argv[2])
else:
   pw = PULSE_WIDTH

num_gpio = len(GPIOS)

freq_micros = int(1000000.0 / f)
gpio_micros = int(freq_micros / num_gpio)
gap_micros = gpio_micros - pw
start_micro = int(gap_micros / 2)

# Assume that each gpio has the same pulse width and frequency

wf=[]

for g in range(num_gpio):

   wf.append(pigpio.pulse(NONE,        NONE,        start_micro))
   wf.append(pigpio.pulse(1<<GPIOS[g], NONE,        pw))
   wf.append(pigpio.pulse(NONE,        1<<GPIOS[g], gpio_micros-start_micro-pw))

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

for g in GPIOS:
   pi.set_mode(g, pigpio.OUTPUT)

pi.wave_add_generic(wf)
wid = pi.wave_create()

if wid >= 0:
   pi.wave_send_repeat(wid)
   time.sleep(60)
   pi.wave_tx_stop()
   pi.wave_delete(wid)

pi.stop()
matter45.png
./matter45.py 10000 20
matter45.png (39.49 KiB) Viewed 2937 times

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

Re: New pigpio Python module

Thu Mar 12, 2015 8:15 pm

in the meantime you are doing your magic, have i understood right that ONE wave can contain waves sent on more than one gpio?
In your code i see you creating a wave (as we did for ir..) and adding it waveforms created on different gpios
That's wonderful! Ideally no limit at all..

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

Re: New pigpio Python module

Thu Mar 12, 2015 8:20 pm

pattagghiu wrote:in the meantime you are doing your magic, have i understood right that ONE wave can contain waves sent on more than one gpio?
In your code i see you creating a wave (as we did for ir..) and adding it waveforms created on different gpios
That's wonderful! Ideally no limit at all..
Yes, that's correct. For most uses you are only concerned with one gpio. That is why the examples invariably only show one gpio. You can switch on and switch off any of gpios 0 - 31 with each pulse. You select the gpios with a bit-mask, e.g. to select gpios 5, 9, 23 you'd use a mask of (1<<5)|(1<<9)|(1<<23).

By the way, man pig2vcd may have helped with pig2vcd.

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

Re: New pigpio Python module

Thu Mar 12, 2015 9:27 pm

joan wrote:By the way, man pig2vcd may have helped with pig2vcd.
Damn, why didn'y you put that on the pigpio page? :)

sorry, this could help me and i missed it..

Code: Select all

SYNOPSIS
       pig2vcd </dev/pigpioXX >file.VCD
While we are here, could you please explain - in noob language - the meaning of your whatchdog?

Is it "only" a way to monitor "timeout" between one edge and another? Because as far as i've understood it's not changing anything in callback managing (apart from passing a "level.timeout" when there's no edge within the timeout period passed to watchdog)

Is this right?

Thanks, master :)

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

Re: New pigpio Python module

Thu Mar 12, 2015 10:12 pm

pattagghiu wrote: ...
While we are here, could you please explain - in noob language - the meaning of your whatchdog?

Is it "only" a way to monitor "timeout" between one edge and another? Because as far as i've understood it's not changing anything in callback managing (apart from passing a "level.timeout" when there's no edge within the timeout period passed to watchdog)
...
The watchdog is a convenience function. It just makes it easier to write some types of code.

Think of a serial link and wanting to decode a byte. Each byte is typically transmitted as a start bit, eight data bits, and a stop bit. Suppose you send 0xFF then the line starts high, goes low for one bit time (startbit) and then goes high for 9 bit times (eight data bits and one stop bit) and stays high until the next byte, if any.

A callback will get a level change from 1 to 0 for the start bit, and then from 0 to 1 for the first data bit. No more level changes will be received unless another byte is transmitted (half an hour later say). Watchdogs are used to give the callback an escape. You simulate a level change by arranging for a watchdog to expire if more than 10 bit times have passed since the last level change. If you were receiving a byte you then know all the missing bits were at the previous level and so the completed byte can be delivered to the caller.

It's not easy to explain. Hope you get the idea.

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

Re: New pigpio Python module

Thu Mar 12, 2015 10:35 pm

joan wrote:It's not easy to explain. Hope you get the idea.
+1
thanks

matter45
Posts: 10
Joined: Wed Mar 11, 2015 11:34 pm

Re: New pigpio Python module

Fri Mar 13, 2015 1:19 am

Thanks Joan.

I was able to interpret the whole script except for 1<<GPIOS[g]

I am starting to like Python and its simplicity.

Here is my interpretation for wf.append(pigpio.pulse(NONE, NONE, start_micro)):
wf.append adds a value to the array wf in sequel order.
pigpio.pulse uses the 3 different variables provided in the brackets to produce data for use in the waveform.
The first variable in the bracket is to provide a starting point.
The second variable in the bracket is to provide a stopping point.
The Third variable in the bracket is to provide a delaying point till the pulse starts again?

Now if I am to try and interpret 1<<GPIOS[g]:
the 1 is an indication of true or false? 0 = false and 1 = true?
the << is like an equals sign.
the GPIOS is the GPIO pins to be used?


Code: Select all

# Assume that each gpio has the same pulse width and frequency

wf=[]					#wf with no values in the array

for g in range(num_gpio):  # this is obviously a loop counter up to int(num_gpio), and g is the counter.

   wf.append(pigpio.pulse(NONE, NONE, start_micro))  #add a variable to wf array. delay 23us.

   wf.append(pigpio.pulse(1<<GPIOS[g], NONE, pw))   #ok i need help understanding 1<<GPIOS[g]. delay 20

   wf.append(pigpio.pulse(NONE, 1<<GPIOS[g], gpio_micros-start_micro-pw))	# delay 23. again need help understanding 1<<GPIOS[g]

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

Re: New pigpio Python module

Fri Mar 13, 2015 9:06 am

matter45 wrote:pigpio.pulse uses the 3 different variables provided in the brackets to produce data for use in the waveform.
The first variable in the bracket is to provide a starting point.
The second variable in the bracket is to provide a stopping point.
The Third variable in the bracket is to provide a delaying point till the pulse starts again?
No.
The first var is what you are setting HIGH
The second var is what you are setting LOW
The third var is for how long this setting will last.

So, in your example, it's adding a pulse that is not changing any GPIO and is lasting for start_micro us
Now if I am to try and interpret 1<<GPIOS[g]:
the 1 is an indication of true or false? 0 = false and 1 = true?
the << is like an equals sign.
Ok, i try to explain what i understood :)
pigpio.pulse accepts as input a "bitmask" (sorry joan if the word is wrong..) that selects on which GPIOs we are working.
<< is binary shifting operator, so 1<<gpio is creating a bitmask that enables the selected gpio (1 only on selected gpio)
the GPIOS is the GPIO pins to be used?
well that's python.
There's a while loop that cycles on the list of gpio so GPIOS[g] is the single element of the array, aka the single gpio

matter45
Posts: 10
Joined: Wed Mar 11, 2015 11:34 pm

Re: New pigpio Python module

Sat Mar 14, 2015 7:15 am

Thanks a ton for helping me interpret. Now I can try to write up a script myself.

Thank you Joan and pattagghiu

Happy as Larry.

:D

spac3-monkey
Posts: 2
Joined: Sat Mar 28, 2015 4:02 pm

Re: New pigpio Python module

Sat Mar 28, 2015 4:05 pm

hi:
how install this module in virtual environment?

thanks in advance

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

Re: New pigpio Python module

Thu Sep 24, 2015 8:44 am

I hope i'm not going to be too much off topic (is there a dedicated topic to pigpio? this is only for python module? DAMN WE HAVE TO CREATE ONE! :lol: ), but i have a question for joan.
I'd like to start some testing on the c side of the moon, but as far as i can remember you said (somewhere..) that if pigpiod is running, there can be some problems running c code using pigpio.
Since i have many python script running and using pigpiod, what have i to do to have also c programs running and using pigpio?

thanks

(this c part will be much pain LOL)

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

Re: New pigpio Python module

Thu Sep 24, 2015 9:31 am

Massi wrote:I hope i'm not going to be too much off topic (is there a dedicated topic to pigpio? this is only for python module? DAMN WE HAVE TO CREATE ONE! :lol: ), but i have a question for joan.
I'd like to start some testing on the c side of the moon, but as far as i can remember you said (somewhere..) that if pigpiod is running, there can be some problems running c code using pigpio.
Since i have many python script running and using pigpiod, what have i to do to have also c programs running and using pigpio?

thanks

(this c part will be much pain LOL)
There can only be one copy of the library running at a time.

sudo pigpiod starts the library and makes its services available via the socket and pipe interfaces.

The Python module, pigs, and piscope use the socket interface to talk to the library. It's preferable to use the socket interface rather than the pipe interface if you want to run your program on a remote machine. The pipe interface is only accessible on the Pi itself.

If possible I recommend you use the socket interface from C. There is a C library pigpiod_if to help in doing so. It has a very similar interface to the Python module. The only functional difference is that the C version (currently) only allows control of one Pi at a time (you can connect to many simultaneously from the Python module).

If you really need to squeeze the last ounce of performance out of the system you will need to use the C library directly.

In this mode pigpiod becomes redundant. Your C linked program (foobar say) in effect becomes pigpiod. While foobar is running anything which normally connects via the socket or pipe interface won't see any difference. They will transparently connect to foobar rather than pigpiod. You don't have to do anything special in foobar. Once you call gpioInitialise within foobar the socket and pipe interfaces are automatically started (unless you explicitly say not to with gpioCfgInterfaces before calling gpioInitialise).

My own take would be try Python first. If the performance isn't there convert to C using pigpiod_if. Only if that is still not good enough go to the raw C I/F.

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

Re: New pigpio Python module

Thu Sep 24, 2015 9:53 am

joan wrote:If possible I recommend you use the socket interface from C. There is a C library pigpiod_if to help in doing so. It has a very similar interface to the Python module. The only functional difference is that the C version (currently) only allows control of one Pi at a time (you can connect to many simultaneously from the Python module).
Very clear. so my remembrance was correct. I'll try with the c module
My own take would be try Python first. If the performance isn't there convert to C using pigpiod_if. Only if that is still not good enough go to the raw C I/F.
i want to try to decode rf signals. If you remember, i already tried (some months ago..) with python and i got a full 100% cpu usage. I expect much less with a c program.
And i expect to find much more performance gain going from python to C (module) than going from C module to direct C.. am i wrong?

Thanks :)
I'll do my best to avoid the need of further help :)

Return to “Python”