picandies
Posts: 192
Joined: Wed Nov 26, 2014 5:13 pm

make one-shot

Tue Jan 01, 2019 10:01 pm

Hello:

I'm doing some RPI3 Python math operations & when I get a certain result , need to make ONE 30ms (approx.) pulse on an IO pin (A one-shot)
I'm using import RPi.GPIO as GPIO library for all my IO. I need the pulse to end without using a sleep command, since the program is very busy doing other things & can't sit around for 30ms. Is there something associated with the library to easily make such a pulse? Another pulse request will never occur before the first one is finished. Seems like this is a common & often used function that is avail somewhere.

User avatar
scruss
Posts: 2419
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: make one-shot

Wed Jan 02, 2019 12:26 am

pigpio's gpio_trigger function looks like it does what you need.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

picandies
Posts: 192
Joined: Wed Nov 26, 2014 5:13 pm

Re: make one-shot

Wed Jan 02, 2019 2:10 am

pigpio's gpio_trigger function looks like it does what you need.
Thanks for the suggestion, however the limit is 100us [pulse_len:= 1-100] & I need a pulse about 30ms long, upon demand. Also everything else in my program uses rpi.gpio, so not sure if it is ok to mix such things together. Is there a way to genwerate this using a timwer or some io callback?

Again, I want to request one 30ms pulse on, say gpio 11, then execute other code & not wait around for 30ms. Any future pulse requests will occur long after the 30ms pulse is finished (maybe around 500-600ms).

pfletch101
Posts: 500
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: make one-shot

Wed Jan 02, 2019 3:45 pm

picandies wrote:
Wed Jan 02, 2019 2:10 am
pigpio's gpio_trigger function looks like it does what you need.
Thanks for the suggestion, however the limit is 100us [pulse_len:= 1-100] & I need a pulse about 30ms long, upon demand. Also everything else in my program uses rpi.gpio, so not sure if it is ok to mix such things together. Is there a way to genwerate this using a timwer or some io callback?

Again, I want to request one 30ms pulse on, say gpio 11, then execute other code & not wait around for 30ms. Any future pulse requests will occur long after the 30ms pulse is finished (maybe around 500-600ms).
Spawn a thread to generate the pulse via rpi.gpio. The thread can sleep during the pulse on time without holding up execution of the main program.

picandies
Posts: 192
Joined: Wed Nov 26, 2014 5:13 pm

Re: make one-shot

Wed Jan 02, 2019 4:32 pm

Spawn a thread to generate the pulse via rpi.gpio. The thread can sleep during the pulse on time without holding up execution of the main program.

Do you have an example of doing it this way? I'm not sure I follow your statement, as I have never seen such a procedure.

gordon77
Posts: 4116
Joined: Sun Aug 05, 2012 3:12 pm

Re: make one-shot

Wed Jan 02, 2019 4:44 pm

I just use time.time(). Set a start time and then keep checking against the current time eg. time.time() - start > 0.03. I am not sure of the accuracy.

Will5455
Posts: 126
Joined: Sat Jul 21, 2018 8:37 pm
Location: harrisonville mo

Re: make one-shot

Wed Jan 02, 2019 4:50 pm

turn the pin on and use time.sleep(30) then turn pin off
I do strange things and am sometimes the techhead stereotype.
deal with it!

picandies
Posts: 192
Joined: Wed Nov 26, 2014 5:13 pm

Re: make one-shot

Wed Jan 02, 2019 7:48 pm

I just use time.time(). Set a start time and then keep checking against the current time eg. time.time() - start > 0.03. I am not sure of the accuracy.
We cannot sit still in the code just checking the time, many other things need to be happening. At the time for stopping the pulse, we may be anywhere in the large code.

[/turn the pin on and use time.sleep(30) then turn pin offi]...same thing, the codes must keep running..it cannot stop for 30ms

Will5455
Posts: 126
Joined: Sat Jul 21, 2018 8:37 pm
Location: harrisonville mo

Re: make one-shot

Wed Jan 02, 2019 9:33 pm

ooohhhhh! you need other things to happen at the same time hmm maybe separate programs would be more appropriate?
I do strange things and am sometimes the techhead stereotype.
deal with it!

pfletch101
Posts: 500
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: make one-shot

Wed Jan 02, 2019 10:03 pm

picandies wrote:
Wed Jan 02, 2019 4:32 pm
Spawn a thread to generate the pulse via rpi.gpio. The thread can sleep during the pulse on time without holding up execution of the main program.

Do you have an example of doing it this way? I'm not sure I follow your statement, as I have never seen such a procedure.
I haven't used threads much in Python for the Pi, but I have used them much more extensively in .NET languages under Windows. They are ideal for situations like the one you have here, where you want to trigger a process that takes a significant time to complete and then go on to do something else without waiting on the process you have triggered. See https://docs.python.org/2/library/threading.html for Python 2.x or https://docs.python.org/3/library/threading.html for Python 3.x. You need to define a function that generates your pulse (call it 'myPulse()'), then proceed as follows (Python 3 version - untested):

Code: Select all

import threading

# Your working code
myThread = threading.Thread(target=myPulse)
myThread.start()
# carry on with the rest of your working code
Last edited by pfletch101 on Wed Jan 02, 2019 11:49 pm, edited 1 time in total.

gordon77
Posts: 4116
Joined: Sun Aug 05, 2012 3:12 pm

Re: make one-shot

Wed Jan 02, 2019 10:18 pm

picandies wrote:
Wed Jan 02, 2019 7:48 pm
I just use time.time(). Set a start time and then keep checking against the current time eg. time.time() - start > 0.03. I am not sure of the accuracy.
We cannot sit still in the code just checking the time, many other things need to be happening. At the time for stopping the pulse, we may be anywhere in the large code.

[/turn the pin on and use time.sleep(30) then turn pin offi]...same thing, the codes must keep running..it cannot stop for 30ms


I check it at various points in a large code, not just wait at one point.

picandies
Posts: 192
Joined: Wed Nov 26, 2014 5:13 pm

Re: make one-shot

Thu Jan 03, 2019 7:13 pm


I check it at various points in a large code, not just wait at one point.
That might not be practical if the code is large..it might not be read often enough..then the timing would be off. Also, would the pin name definition be known everywhere in the program (such as in different classes).

Seems like the threading might be an answer...but surprised I haven't seen anyone doing it to make a pulse-on-demand.

pfletch101
Posts: 500
Joined: Sat Feb 24, 2018 4:09 am
Location: Illinois, USA

Re: make one-shot

Thu Jan 03, 2019 7:22 pm

picandies wrote:
Thu Jan 03, 2019 7:13 pm

I check it at various points in a large code, not just wait at one point.
That might not be practical if the code is large..it might not be read often enough..then the timing would be off. Also, would the pin name definition be known everywhere in the program (such as in different classes).

Seems like the threading might be an answer...but surprised I haven't seen anyone doing it to make a pulse-on-demand.
Many people seem to be afraid of threading (if they know about it at all). Writing threaded code to do more complex things can be more than a little bit tricky, and there is also always a potential issue with library code used by the subsidiary threads being 'thread safe' (not getting confused if more than one instance of a function is active at once), but neither of these issues should be a problem here.

Return to “General discussion”