mcu: Move print_time to clock conversion code to clocksync.py

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-09-15 12:28:55 -04:00
parent 94bcd9735a
commit f1b315e04f
3 changed files with 27 additions and 16 deletions

View File

@ -14,6 +14,7 @@ class ClockSync:
self.queries_pending = 0
self.status_timer = self.reactor.register_timer(self._status_event)
self.status_cmd = None
self.mcu_freq = 0.
self.lock = threading.Lock()
self.last_clock = 0
self.last_clock_time = self.last_clock_time_min = 0.
@ -27,18 +28,19 @@ class ClockSync:
self.last_clock = (params['high'] << 32) | params['clock']
self.last_clock_time = params['#receive_time']
self.last_clock_time_min = params['#sent_time']
clock_freq = msgparser.get_constant_float('CLOCK_FREQ')
self.min_freq = clock_freq * (1. - MAX_CLOCK_DRIFT)
self.max_freq = clock_freq * (1. + MAX_CLOCK_DRIFT)
self.mcu_freq = msgparser.get_constant_float('CLOCK_FREQ')
self.min_freq = self.mcu_freq * (1. - MAX_CLOCK_DRIFT)
self.max_freq = self.mcu_freq * (1. + MAX_CLOCK_DRIFT)
# Enable periodic get_status timer
serial.register_callback(self._handle_status, 'status')
self.status_cmd = msgparser.create_command('get_status')
self.reactor.update_timer(self.status_timer, self.reactor.NOW)
def connect_file(self, serial, pace=False):
self.serial = serial
self.mcu_freq = serial.msgparser.get_constant_float('CLOCK_FREQ')
est_freq = 1000000000000.
if pace:
est_freq = float(self.msgparser.config['CLOCK_FREQ'])
est_freq = self.mcu_freq
self.min_freq = self.max_freq = est_freq
self.last_clock = 0
self.last_clock_time = self.reactor.monotonic()
@ -55,13 +57,21 @@ class ClockSync:
last_clock_time = self.last_clock_time
min_freq = self.min_freq
return int(last_clock + (eventtime - last_clock_time) * min_freq)
def translate_clock(self, raw_clock):
def clock32_to_clock64(self, clock32):
with self.lock:
last_clock = self.last_clock
clock_diff = (last_clock - raw_clock) & 0xffffffff
clock_diff = (last_clock - clock32) & 0xffffffff
if clock_diff & 0x80000000:
return last_clock + 0x100000000 - clock_diff
return last_clock - clock_diff
def print_time_to_clock(self, print_time):
return int(print_time * self.mcu_freq)
def clock_to_print_time(self, clock):
return clock / self.mcu_freq
def estimated_print_time(self, eventtime):
return self.clock_to_print_time(self.get_clock(eventtime))
def get_adjusted_freq(self):
return self.mcu_freq
def _status_event(self, eventtime):
self.queries_pending += 1
self.serial.send(self.status_cmd)

View File

@ -162,7 +162,7 @@ class MCU_endstop:
, self._oid)
def home_start(self, print_time, rest_time):
clock = self._mcu.print_time_to_clock(print_time)
rest_ticks = self._mcu.seconds_to_clock(rest_time)
rest_ticks = int(rest_time * self._mcu.get_adjusted_freq())
self._homing = True
self._min_query_time = self._mcu.monotonic()
self._next_query_time = print_time + self.RETRY_QUERY
@ -382,7 +382,7 @@ class MCU_adc:
, self._oid)
def _handle_analog_in_state(self, params):
last_value = params['value'] * self._inv_max_adc
next_clock = self._mcu.translate_clock(params['next_clock'])
next_clock = self._mcu.clock32_to_clock64(params['next_clock'])
last_read_clock = next_clock - self._report_clock
last_read_time = self._mcu.clock_to_print_time(last_read_clock)
if self._callback is not None:
@ -674,17 +674,17 @@ class MCU:
return self._serial.msgparser.get_constant_float(name)
# Clock syncing
def print_time_to_clock(self, print_time):
return int(print_time * self._mcu_freq)
return self._clocksync.print_time_to_clock(print_time)
def clock_to_print_time(self, clock):
return clock / self._mcu_freq
return self._clocksync.clock_to_print_time(clock)
def estimated_print_time(self, eventtime):
return self.clock_to_print_time(self._clocksync.get_clock(eventtime))
def get_mcu_freq(self):
return self._mcu_freq
return self._clocksync.estimated_print_time(eventtime)
def get_adjusted_freq(self):
return self._clocksync.get_adjusted_freq()
def clock32_to_clock64(self, clock32):
return self._clocksync.clock32_to_clock64(clock32)
def seconds_to_clock(self, time):
return int(time * self._mcu_freq)
def translate_clock(self, clock):
return self._clocksync.translate_clock(clock)
def get_max_stepper_error(self):
return self._max_stepper_error
# Move command queuing

View File

@ -98,7 +98,8 @@ class PrinterHomingStepper(PrinterStepper):
def get_homing_speed(self):
# Round the configured homing speed so that it is an even
# number of ticks per step.
dist_ticks = self.mcu_stepper.get_mcu().get_mcu_freq() * self.step_dist
adjusted_freq = self.mcu_stepper.get_mcu().get_adjusted_freq()
dist_ticks = adjusted_freq * self.step_dist
ticks_per_step = round(dist_ticks / self.homing_speed)
return dist_ticks / ticks_per_step
def get_homed_offset(self):