Given that the RPi runs a Linux system and that I'll be programming in Python, both of which introduce potential delays to time-sensitive code, how accurately can I expect to get timing on the RPi? I'm replacing an existing system which is capable of millisecond-level timing, but for the purposes of the application anything within about 5ms should be acceptable.
The GPIO pin will be connected to a relay, which will actuate a pneumatic device. I need to actuate the device for a precise number of milliseconds (within a few ms tolerance).
Example code below, for actuating my device for ~1000ms.
Code: Select all
import time
#returns ms since the epoch
def millis():
return time.time() * 1000
def switchTheThing(howlong):
#set GPIO pin high
start = millis()
while ( (start + howlong) > millis() ):
pass
#set GPIO pin low
def main():
#bunch of code
switchTheThing(1000) #actuate the pneumatics for 1000ms
#bunch more code
If not, what might I be able to expect as maximum delay (I should be able to work around larger delays, up to a point)?
If not, what's the cheapest way of achieving such precise timings (add-on board or such)?
If this is an OK way to go, are there any improvements to the way I'm performing the timing calculations? It feels a little clumsy.


