Issue
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!
Solution
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.
Answered By - Tirso J. Bello Ponce
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.