avrsim: Rework pacing mechanism

Rework the pacing mechanism to make it more stable.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-02-04 11:57:22 -05:00
parent d1c209c689
commit 8121a4a29f
1 changed files with 11 additions and 13 deletions

View File

@ -1,10 +1,9 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# Script to interact with simulavr by simulating a serial port. # Script to interact with simulavr by simulating a serial port.
# #
# Copyright (C) 2015 Kevin O'Connor <kevin@koconnor.net> # Copyright (C) 2015-2018 Kevin O'Connor <kevin@koconnor.net>
# #
# This file may be distributed under the terms of the GNU GPLv3 license. # This file may be distributed under the terms of the GNU GPLv3 license.
import sys, optparse, time, os, pty, fcntl, termios, errno import sys, optparse, time, os, pty, fcntl, termios, errno
import pysimulavr import pysimulavr
@ -107,23 +106,22 @@ class Pacing(pysimulavr.PySimulationMember):
pysimulavr.PySimulationMember.__init__(self) pysimulavr.PySimulationMember.__init__(self)
self.sc = pysimulavr.SystemClock.Instance() self.sc = pysimulavr.SystemClock.Instance()
self.pacing_rate = 1. / (rate * SIMULAVR_FREQ) self.pacing_rate = 1. / (rate * SIMULAVR_FREQ)
self.rel_time = self.next_rel_time = time.time() self.next_check_clock = 0
self.rel_clock = self.next_rel_clock = self.sc.GetCurrentTime() self.rel_time = time.time()
self.best_offset = 0.
self.delay = SIMULAVR_FREQ / 10000 self.delay = SIMULAVR_FREQ / 10000
self.sc.Add(self) self.sc.Add(self)
def DoStep(self, trueHwStep): def DoStep(self, trueHwStep):
curtime = time.time() curtime = time.time()
clock = self.sc.GetCurrentTime() clock = self.sc.GetCurrentTime()
clock_diff = clock - self.rel_clock offset = clock * self.pacing_rate - (curtime - self.rel_time)
time_diff = curtime - self.rel_time self.best_offset = max(self.best_offset, offset)
offset = clock_diff * self.pacing_rate - time_diff
if offset > 0.000050: if offset > 0.000050:
time.sleep(offset) time.sleep(offset - 0.000040)
if clock_diff > self.delay * 20: if clock >= self.next_check_clock:
self.rel_clock = self.next_rel_clock self.rel_time -= min(self.best_offset, 0.)
self.rel_time = self.next_rel_time self.next_check_clock = clock + self.delay * 500
self.next_rel_clock = clock self.best_offset = -999999999.
self.next_rel_time = curtime
return self.delay return self.delay
# Forward data from a terminal device to the serial port pins # Forward data from a terminal device to the serial port pins