0

I want to create trigger that runs something every 500 ms, and it should be without drifting over time. I have this code running to show something in pyopengl

def __init__(self):
    ...
    self.t_on = time.clock()
    ...
def display_gl(self):
    ...
    if (time.clock() - self.t_on) >= 0.500:
        self.t_on = time.clock()
        #do things
    ...

the clock is drifting over time, I think that's because the if condition is catching the 500 ms moment after a little time.

Is there any way to avoid this drifting issue?

Solved:
changed:

if (time.clock() - self.t_on) >= 0.500:
    self.t_on = time.clock()

to:

if (time.clock() - self.t_on) >= 0.500:
    self.t_on =time.clock() - ((time.clock() - self.t_on) - 0.500)
Cœur
  • 37,241
  • 25
  • 195
  • 267
zixmarkiz
  • 13
  • 5
  • check the time of your if statement then deduce if from 500ms, so the new time+ process time is exactly 500ms. – Mayeul sgc Mar 20 '17 at 11:20

1 Answers1

0

Solved:
thanks to Mayeul sgc
changed:

if (time.clock() - self.t_on) >= 0.500:
    self.t_on = time.clock()

to:

if (time.clock() - self.t_on) >= 0.500:
    self.t_on =time.clock() - ((time.clock() - self.t_on) - 0.500)
Community
  • 1
  • 1
zixmarkiz
  • 13
  • 5