ashbashmash
Posts: 8
Joined: Tue Aug 14, 2018 7:36 pm

sleepy python

Sun Aug 19, 2018 5:15 pm

I have an array of <bit><int>, where <int> is the number of microseconds I want to send a signal for.
So, it looks something like this:
1,481 (on for 481 microsecs)
0,2894 (off for 2894 microsecs)
1,425 (you get the gist)
etc.

I'm using pigpio to set the output high or low, and then sleep to wait. I didn't expect it to work because I'd read that time.sleep has no chance of being accurate., and I wasn't wrong :)
I'm currently doing this:

Code: Select all

for sig in signal_arr:
    pi.write(gpioWrite,sig.sigLevel)
    time.sleep(sig.sigLengthSeconds);
When I record the signal getting sent, the times are way out (by about 200 microseconds, but not a constant amount I can adjust for).
Is there an alternative to time.sleep which is more reliable, or a way with pigpio to send it a list of states/lengths?

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

Re: sleepy python

Sun Aug 19, 2018 5:35 pm

You need to use pigpio waves.

I'd play with pigs to get the idea and view the output with piscope.

http://abyz.me.uk/rpi/pigpio/pigs.html
http://abyz.me.uk/rpi/pigpio/piscope.html

For instance to send pulses to GPIO4 (1<<4 == 16)

Code: Select all

$ pigs m 4 w
$ pigs wvclr
$ pigs wvag 16 0 481 0 16 2894 16 0 425 0 16 3000
4
$ pigs wvcre
0
$ pigs wvtxr 0
9
$ pigs wvhlt

ashbashmash
Posts: 8
Joined: Tue Aug 14, 2018 7:36 pm

Re: sleepy python

Sun Aug 19, 2018 6:04 pm

Thank you Joan.
I'm new to python and pi (and linux!), so am struggling a little.
Can I call pigs from python directly, or is it a case of shelling out to a command line? or build up a string, which is the 'wave' and then pipe it?
Or..should I be looking at gpioWaveAddGeneric? It's looking like that might do the trick. From the example here http://abyz.me.uk/rpi/pigpio/cif.html#g ... AddGeneric

I think maybe I need to:

gpioWaveAddNew
loop over my data, create a new pulse for each entry, call gpioWaveAddGeneric with that
gpioWaveCreate
gpioWaveTxSend

is that in the right ballpark?
If so...what is this doing?

Code: Select all

pulse[0].gpioOn = (1<<gpio);
pulse[0].gpioOff = 0;
I just want to say "on for x microseconds"

ashbashmash
Posts: 8
Joined: Tue Aug 14, 2018 7:36 pm

Re: sleepy python

Sun Aug 19, 2018 6:31 pm

ok, so I've confused the c and the python libs...
For python, I need to:

myWave = wave_create()
loop over my data, create a 'pulse'
pulse will have gpioOn=myGpioPin,gpioOff=0 if I want to send a 1, or gpioOff=myGpioPin,gpioOn=0, and the delay in microseconds
call wave_add_generic with my pulse object
wave_send_once(myWave)

i think...

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

Re: sleepy python

Sun Aug 19, 2018 7:43 pm

Yes, that seems correct. I suggested pigs as that is more interactive (as long as you can visualise the signals).

There should be several examples at http://abyz.me.uk/rpi/pigpio/examples.html

In particular

http://abyz.me.uk/rpi/pigpio/examples.h ... on__433_py (tx side)

http://abyz.me.uk/rpi/pigpio/examples.h ... on_irrp_py

http://abyz.me.uk/rpi/pigpio/examples.h ... layback_py

http://abyz.me.uk/rpi/pigpio/examples.h ... ave_PWM_py

ashbashmash
Posts: 8
Joined: Tue Aug 14, 2018 7:36 pm

Re: sleepy python

Mon Aug 20, 2018 10:49 am

Joan, you are awesome!
I can now turn my light on :)
Thank you so much for your help.

Return to “Python”

Who is online

Users browsing this forum: Stefanmz7 and 12 guests