6

I work on a python script designed to control multiple actuator at the same time. To simplify, let's say I need to control 2 electric motors.

With the multiprocessing module I create a process for each motors, and a process to save the data on a sheet.

This part of the script works fine, however I need to command my motors at precise times, every milliseconds, and the time.time() or the time.clock() functions seems to be unreliable (triggs between 0.05 and 30 millisecond!)

Is it "normal" for these functions to be so erratic, or is this caused by another part of my script?

EDIT: I used the datetime function (see below) to improve the precision, but I still have several discrete level of error. For example, if I want 1ms, I get also 1.25, 0.75,1.5... So IMO this is due to computer hardware(as Serge Ballesta said).

Martin Thurau
  • 7,564
  • 7
  • 43
  • 80
CoMartel
  • 3,521
  • 4
  • 25
  • 48
  • Your ability to do this is going to be highly dependent on your hardware and OS. If `time.clock` isn't giving you the accuracy you need then the only real advice is to try different hardware. – Dunes Jul 15 '14 at 11:55

3 Answers3

4

As I "only" need a relative time (1ms between each command), do you know a way to do that precisely?

The best you can hope for is datetime.datetime.now(), which will give you microsecond resolution:

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2014, 7, 15, 14, 31, 1, 521368)
>>> i = datetime.datetime.now()
>>> q = datetime.datetime.now()
>>> (i-q).seconds
86389
>>> (i-q).microseconds
648299
>>> i.microsecond
513160
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
2

IMHO the problem is not in your script but more probably in your machine. I assume you are using a standard computer under Linux or Windows. And even if the computer is able to do many things in one single milliseconds, it constantly does with :

  • network
  • mouse or keyboard events
  • screen (including screen savers ...)
  • antivirus software (mainly on Windows)
  • system or daemon processes (and there are plenty of them)
  • multi-task management

You cannot reliably hope to have a one millisecond accuracy without a dedicated hardware equipement or you must use a real time system.

Edit:

As a complement, here is a quote from an answer from Ulrich Eckhardt to this other post Inconsistent Python Performance on Windows :

You have code that performs serial IO, which will block your process and possibly invoke the scheduler. If that happens, it will first give control to a different process and only when that one yield or exceeds its timeslice it will re-schedule your process.

The question for you is: What is the size of the scheduler timeslice of the systems you are running? I believe that this will give you an insight into what is happening.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • that's what I was afraid of... As I "only" need a relative time (1ms between each command), do you know a way to do that precisely? – CoMartel Jul 15 '14 at 11:32
  • Seven years is a long time. In the present a good solution would be to managing the timing with low cost micro controller. Something like the Raspberry Pi Pico could be used to handle the control of the motors. At 100+mhz, with no operating system to get in the way, and the ability to program using python it should easily do the trick. And all that for only $4. – codingCat Apr 28 '22 at 11:43
0

I can't give You a comment yet (reputation), so a give you an anwser: It's just a clue: Try to use cProfile module.

https://docs.python.org/2/library/profile.html

It lets you check how long your script is executed, and every function in the script. Function .run() in the cProfile module returns precise statistics about your script. Maybe it can help you..

bartekch
  • 603
  • 8
  • 15