Page 1 of 1

make one-shot

Posted: Tue Jan 01, 2019 10:01 pm
by picandies
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.

Re: make one-shot

Posted: Wed Jan 02, 2019 12:26 am
by scruss
pigpio's gpio_trigger function looks like it does what you need.

Re: make one-shot

Posted: Wed Jan 02, 2019 2:10 am
by picandies
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).

Re: make one-shot

Posted: Wed Jan 02, 2019 3:45 pm
by pfletch101
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.

Re: make one-shot

Posted: Wed Jan 02, 2019 4:32 pm
by picandies
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.

Re: make one-shot

Posted: Wed Jan 02, 2019 4:44 pm
by gordon77
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.

Re: make one-shot

Posted: Wed Jan 02, 2019 4:50 pm
by Will5455
turn the pin on and use time.sleep(30) then turn pin off

Re: make one-shot

Posted: Wed Jan 02, 2019 7:48 pm
by picandies
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

Re: make one-shot

Posted: Wed Jan 02, 2019 9:33 pm
by Will5455
ooohhhhh! you need other things to happen at the same time hmm maybe separate programs would be more appropriate?

Re: make one-shot

Posted: Wed Jan 02, 2019 10:03 pm
by pfletch101
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

Re: make one-shot

Posted: Wed Jan 02, 2019 10:18 pm
by gordon77
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.

Re: make one-shot

Posted: Thu Jan 03, 2019 7:13 pm
by picandies

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.

Re: make one-shot

Posted: Thu Jan 03, 2019 7:22 pm
by pfletch101
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.