patacongo commented on pull request #5424:
URL: https://github.com/apache/incubator-nuttx/pull/5424#issuecomment-1030712544


   Subtracting one is not always the right solution.  It only works in your 
case, again because your specific test case is running synchronously with the 
system time.  But that might not always be the case.  I do not recommend using 
this solution.
   
   I provided you with a better, adaptive solution in #5421 that will work in 
ALL cases.  this case, basically one:
   
   - given the last time and desired period, it gets the desired time of the 
next timer expiration:
   
   `clock_t next_time= last_time + period`
   
   - At the time of the timer expiration, it calculates the error between the 
desired time and the actual time:
   
   `clock_t error = next_time - clock_systime_ticks();`
   
   - Then it uses that error to correct the time for the next tick:
   
   `clock_t tick = period + error`
   
   This will give you perfect timing regardless of the clock phase.
   
   You cannot defeat quantization errors by adding or subtracting one.  That 
just doesn't work in a robust way.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to