-2

How to generate precise pulses with python?

from threading import Thread
import time

def runA():
    while True:
        print "a"
        sendSignal(1)
        time.sleep(0.0001)
        sendSignal(0)
        time.sleep(0.005)


if __name__ == "__main__":
    t1 = Thread(target = runA)
    t1.setDaemon(True)
    t1.start()
    while True:
        sleep(10)

time management in python is not precise. specially to create multi-threaded PWM generators (assuming each thread to send data at specific rate) . is there any better way to generate pulses? precise ones!

[The solution is language-independent]

Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
  • `This code does not print "a" 10 times every second` - What does it do? – thefourtheye May 30 '15 at 15:00
  • it prints "a" on random intervals, but it should be printing a 10 times each second – Rami Dabain May 30 '15 at 15:01
  • well, it would print it about 10 times per second of that thread's execution time. If the CPU is really busy you might end up with it being less regular than that – Eric Renouf May 30 '15 at 15:09
  • running the function `runA()` directly (not within a thread) does not have any problems. CPU load is 0% (running linux without a UI - just command line + SSH) – Rami Dabain May 30 '15 at 15:16
  • 2
    The CPU shouldn't be zero, since your main thread appears to run a continuous loop and should therefore be getting much more CPU time than the sleeping sub-thread. – holdenweb May 30 '15 at 15:24
  • I ran the code exactly as is, unchanged, and it works exactly the same under python3 and python2-- python2 behaves exactly the same as python3. – snapshoe May 31 '15 at 01:50
  • Try this on ARM processor (raspi 2) – Rami Dabain Jun 05 '15 at 15:53
  • @holdenweb yes you are right. Does python support timers? – Rami Dabain Jun 05 '15 at 15:54
  • 1
    You can use time.sleep() to sleep a thread. So you might try a `sleep(0.1)` instead of `pass` in your main thread's loop – holdenweb Jun 05 '15 at 16:22
  • @holdenweb I understand this frees up some time for other code to run in the processor, but still it will not be precise right? i need it to be precise to 1ms for PWM generation. and this won't fix it. ... i guess i will change my question. – Rami Dabain Jun 05 '15 at 16:35
  • I suspect you may be struggling to achieve that level of precision without using a specialized OS (such as one for embedded systems). I would also point out the 0.0001sec is one-tenth of a millisecond, not that I understand your issues fully. – holdenweb Jun 05 '15 at 21:11
  • @holdenweb with a single loop in a single thread i tried a 5 millisecond delays and it worked, but had +- 1 millisecond from time to time. whemever there's another thread running the error margin increases. what specialised OS can achive that? will itnrun python? – Rami Dabain Jun 06 '15 at 09:51
  • 1
    I'm not an embedded systems expert, I'm afraid. You should be looking at QNX and similar systems. [This question](http://stackoverflow.com/questions/1402933/python-on-an-real-time-operation-system-rtos) might shed more light on the subject for you. – holdenweb Jun 06 '15 at 10:03
  • Sounds exactly what i need. can you please post this as an answer? – Rami Dabain Jun 10 '15 at 12:02

2 Answers2

0

My Solution was to find a real-time OS Actually raspberry pi have a real-time patch for the raspbian OS

For more information : https://www.raspberrypi.org/forums/viewtopic.php?t=39951

Was a little of a challenge to enable it. but it is working and the error rate dropped dramatically.

Thank you @holdenweb for the tip with the real-time OS

Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
-1

Your output is probably buffered so you'll get a big block of them printed all at once. You could try flushing stdout after each print like

sys.stdout.flush()
Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • It is not about the `stdout`. the `print "a"` is just an example of whatever function I need. the original is a function to send data through a socket ... another function is PWM generation – Rami Dabain May 30 '15 at 15:04