1

Faced an issue while trying to execute function at specific time.

The idea is that I've got a while loop for a random amount of time, where during that period of time I need to execute a beep sound for 50ms at randomly generated time and then reassign it again (so that in theory it can be called 1-3 times).

How can I ensure accurate timing? For now I used:

random_time_loop = np.round(np.random.uniform(4,7), 3)
random_time_tone = np.round(np.random.uniform(2,3), 3)

print("Random_time: {}; random_time_tone: {}".format(random_time_loop, random_time_tone))

t_end = (datetime.datetime.now() + datetime.timedelta(seconds=random_time_loop)).time()
t_tone = (datetime.datetime.now() + datetime.timedelta(seconds=random_time_tone)).time()

print("current: "+str(datetime.datetime.now().time()))
print("t_end: "+ str(t_end))
print("t_ring: "+ str(t_tone))

while datetime.datetime.now().time() < t_end:
    if(datetime.datetime.now().time() == t_tone):
        print("Ring ring")
        print(str(datetime.datetime.now().time()) + "==" + str(t_tone))
        random_time_tone = np.round(np.random.uniform(2,3), 3)
        t_tone = (datetime.datetime.now() + datetime.timedelta(seconds=random_time_tone)).time()
        print("New tone random time: {} ringing at: {}".format(random_time_tone, t_tone))
print("ended: {}".format(datetime.datetime.now().time()))

The output of this implementation:

Random_time: 5.684; random_time_tone: 2.909
current: 12:32:36.713071
t_end: 12:32:42.397056
t_ring: 12:32:39.622069
Ring ring
12:32:39.622101==12:32:39.622069
New tone random time: 2.663 ringing at: 12:32:42.285198
ended: 12:32:42.397059

As it can be seen current time is not exactly the same at time for function execution. Moreover, it was planned to ring at *42.285198, but it did not happen even though the process ended at *42.397059. Is it possible to ensure accurate execution with milliseconds precision? I've checked some other solutions, but most of them are trying to execute something once in a while and do not operate with such fluctuating timings.

PS. I understand that there's a small delay when executing code, so that datetime.now() may differ a bit especially when declaring t_end and t_tone (could be a bad implementation and rather use single "current_time" variable with the same current time assigned to it). Nevertheless, how can I ensure millisecond accuracy when comparing times.

Thank you in advance!

1 Answers1

2

Its impossible to do that in an computer "common" operating system, you need some type of OS called RTOS, it's used in embedded systems programming to tackle this types of accuracy.

If you research more on this, you will notice that exist something called "Timer interruption" it's a hardware which main functionality is this types of things.

You need another way of implement this more "near" the hardware.

When you program microcontrollers, its very common work with this types of counters for timer events, basically you set a counter which will do something when it set to 0, and you can set millisecond, microseconds, etc., that's why this types of things are used for robotics and another special applications, like telecommunications.

It's a different world very distant from the common OS, you need to know much about C and hardware programming.

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
  • What would be my best options to acquire nearly milliseconds precision on a regular OS then? If there are any? :/ – Anonymous User Aug 12 '20 at 10:55
  • In a basic computer, and for human times, you dont know if one event differ miliseconds from another event, so in theory, i am confortable with compare to 2 decimal from the seconds unit. 12:32:39.622101==12:32:39.622069 are practically the same time in human sensation, There is something used in physics and another main areas that is called rounding, its for this cases where you have some error but you assume this numbers are equal, so you discard part of number. – Tirso J. Bello Ponce Aug 12 '20 at 11:00
  • True in terms of human, but I need to run an experiment using eyetracker, so ms precision is very desirable. – Anonymous User Aug 12 '20 at 11:04
  • For that cases its common to use a buffer, where you store data and make processing in real time, know something about multithreading? – Tirso J. Bello Ponce Aug 12 '20 at 11:07
  • I found this, if it works for you https://stackoverflow.com/questions/18169099/comparing-times-with-sub-second-accuracy – Tirso J. Bello Ponce Aug 12 '20 at 11:11