From a100f174f98dc4294654da6f3145530fa05813c4 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 12 Sep 2017 12:47:40 -0400 Subject: [PATCH] mcu: Pass print_time directly to MCU calls Now that the print_time is always synchronized with the mcu_time, there is no longer a need to track mcu_time as a separate quantity. Eliminate references to mcu_time from the code and pass print_time directly in its place. Signed-off-by: Kevin O'Connor --- klippy/cartesian.py | 24 ++++++------ klippy/corexy.py | 28 +++++++------- klippy/delta.py | 24 ++++++------ klippy/extruder.py | 25 ++++++------ klippy/fan.py | 16 +++----- klippy/homing.py | 2 +- klippy/mcu.py | 92 ++++++++++++++++++++++----------------------- klippy/stepper.py | 16 +++----- 8 files changed, 108 insertions(+), 119 deletions(-) diff --git a/klippy/cartesian.py b/klippy/cartesian.py index 10c5b5f2..bc5ead70 100644 --- a/klippy/cartesian.py +++ b/klippy/cartesian.py @@ -62,16 +62,16 @@ class CartKinematics: # Set final homed position coord[axis] = s.position_endstop + s.get_homed_offset() homing_state.set_homed_position(coord) - def motor_off(self, move_time): + def motor_off(self, print_time): self.limits = [(1.0, -1.0)] * 3 for stepper in self.steppers: - stepper.motor_enable(move_time, 0) + stepper.motor_enable(print_time, 0) self.need_motor_enable = True - def _check_motor_enable(self, move_time, move): + def _check_motor_enable(self, print_time, move): need_motor_enable = False for i in StepList: if move.axes_d[i]: - self.steppers[i].motor_enable(move_time, 1) + self.steppers[i].motor_enable(print_time, 1) need_motor_enable |= self.steppers[i].need_motor_enable self.need_motor_enable = need_motor_enable def query_endstops(self, print_time): @@ -101,15 +101,15 @@ class CartKinematics: z_ratio = move.move_d / abs(move.axes_d[2]) move.limit_speed( self.max_z_velocity * z_ratio, self.max_z_accel * z_ratio) - def move(self, move_time, move): + def move(self, print_time, move): if self.need_motor_enable: - self._check_motor_enable(move_time, move) + self._check_motor_enable(print_time, move) for i in StepList: axis_d = move.axes_d[i] if not axis_d: continue mcu_stepper = self.steppers[i].mcu_stepper - mcu_time = mcu_stepper.print_to_mcu_time(move_time) + move_time = print_time start_pos = move.start_pos[i] axis_r = abs(axis_d) / move.move_d accel = move.accel * axis_r @@ -119,18 +119,18 @@ class CartKinematics: if move.accel_r: accel_d = move.accel_r * axis_d mcu_stepper.step_const( - mcu_time, start_pos, accel_d, move.start_v * axis_r, accel) + move_time, start_pos, accel_d, move.start_v * axis_r, accel) start_pos += accel_d - mcu_time += move.accel_t + move_time += move.accel_t # Cruising steps if move.cruise_r: cruise_d = move.cruise_r * axis_d mcu_stepper.step_const( - mcu_time, start_pos, cruise_d, cruise_v, 0.) + move_time, start_pos, cruise_d, cruise_v, 0.) start_pos += cruise_d - mcu_time += move.cruise_t + move_time += move.cruise_t # Deceleration steps if move.decel_r: decel_d = move.decel_r * axis_d mcu_stepper.step_const( - mcu_time, start_pos, decel_d, cruise_v, -accel) + move_time, start_pos, decel_d, cruise_v, -accel) diff --git a/klippy/corexy.py b/klippy/corexy.py index 0bbbf910..b92e2672 100644 --- a/klippy/corexy.py +++ b/klippy/corexy.py @@ -67,17 +67,17 @@ class CoreXYKinematics: # Support endstop phase detection on Z axis coord[axis] = s.position_endstop + s.get_homed_offset() homing_state.set_homed_position(coord) - def motor_off(self, move_time): + def motor_off(self, print_time): self.limits = [(1.0, -1.0)] * 3 for stepper in self.steppers: - stepper.motor_enable(move_time, 0) + stepper.motor_enable(print_time, 0) self.need_motor_enable = True - def _check_motor_enable(self, move_time, move): + def _check_motor_enable(self, print_time, move): if move.axes_d[0] or move.axes_d[1]: - self.steppers[0].motor_enable(move_time, 1) - self.steppers[1].motor_enable(move_time, 1) + self.steppers[0].motor_enable(print_time, 1) + self.steppers[1].motor_enable(print_time, 1) if move.axes_d[2]: - self.steppers[2].motor_enable(move_time, 1) + self.steppers[2].motor_enable(print_time, 1) need_motor_enable = False for i in StepList: need_motor_enable |= self.steppers[i].need_motor_enable @@ -109,9 +109,9 @@ class CoreXYKinematics: z_ratio = move.move_d / abs(move.axes_d[2]) move.limit_speed( self.max_z_velocity * z_ratio, self.max_z_accel * z_ratio) - def move(self, move_time, move): + def move(self, print_time, move): if self.need_motor_enable: - self._check_motor_enable(move_time, move) + self._check_motor_enable(print_time, move) sxp = move.start_pos[0] syp = move.start_pos[1] move_start_pos = (sxp + syp, sxp - syp, move.start_pos[2]) @@ -124,7 +124,7 @@ class CoreXYKinematics: if not axis_d: continue mcu_stepper = self.steppers[i].mcu_stepper - mcu_time = mcu_stepper.print_to_mcu_time(move_time) + move_time = print_time start_pos = move_start_pos[i] axis_r = abs(axis_d) / move.move_d accel = move.accel * axis_r @@ -134,18 +134,18 @@ class CoreXYKinematics: if move.accel_r: accel_d = move.accel_r * axis_d mcu_stepper.step_const( - mcu_time, start_pos, accel_d, move.start_v * axis_r, accel) + move_time, start_pos, accel_d, move.start_v * axis_r, accel) start_pos += accel_d - mcu_time += move.accel_t + move_time += move.accel_t # Cruising steps if move.cruise_r: cruise_d = move.cruise_r * axis_d mcu_stepper.step_const( - mcu_time, start_pos, cruise_d, cruise_v, 0.) + move_time, start_pos, cruise_d, cruise_v, 0.) start_pos += cruise_d - mcu_time += move.cruise_t + move_time += move.cruise_t # Deceleration steps if move.decel_r: decel_d = move.decel_r * axis_d mcu_stepper.step_const( - mcu_time, start_pos, decel_d, cruise_v, -accel) + move_time, start_pos, decel_d, cruise_v, -accel) diff --git a/klippy/delta.py b/klippy/delta.py index 5533e83c..0c523d71 100644 --- a/klippy/delta.py +++ b/klippy/delta.py @@ -123,14 +123,14 @@ class DeltaKinematics: + self.steppers[i].get_homed_offset() for i in StepList] homing_state.set_homed_position(self._actuator_to_cartesian(spos)) - def motor_off(self, move_time): + def motor_off(self, print_time): self.limit_xy2 = -1. for stepper in self.steppers: - stepper.motor_enable(move_time, 0) + stepper.motor_enable(print_time, 0) self.need_motor_enable = self.need_home = True - def _check_motor_enable(self, move_time): + def _check_motor_enable(self, print_time): for i in StepList: - self.steppers[i].motor_enable(move_time, 1) + self.steppers[i].motor_enable(print_time, 1) self.need_motor_enable = False def query_endstops(self, print_time): endstops = [(s, s.query_endstop(print_time)) for s in self.steppers] @@ -164,9 +164,9 @@ class DeltaKinematics: move.limit_speed(max_velocity * r, self.max_accel * r) limit_xy2 = -1. self.limit_xy2 = min(limit_xy2, self.slow_xy2) - def move(self, move_time, move): + def move(self, print_time, move): if self.need_motor_enable: - self._check_motor_enable(move_time) + self._check_motor_enable(print_time) axes_d = move.axes_d move_d = move.move_d movexy_r = 1. @@ -203,24 +203,24 @@ class DeltaKinematics: # Generate steps mcu_stepper = self.steppers[i].mcu_stepper - mcu_time = mcu_stepper.print_to_mcu_time(move_time) + move_time = print_time if accel_d: mcu_stepper.step_delta( - mcu_time, accel_d, move.start_v, accel, + move_time, accel_d, move.start_v, accel, vt_startz, vt_startxy_d, vt_arm_d, movez_r) vt_startz += accel_d * movez_r vt_startxy_d -= accel_d * movexy_r - mcu_time += move.accel_t + move_time += move.accel_t if cruise_d: mcu_stepper.step_delta( - mcu_time, cruise_d, cruise_v, 0., + move_time, cruise_d, cruise_v, 0., vt_startz, vt_startxy_d, vt_arm_d, movez_r) vt_startz += cruise_d * movez_r vt_startxy_d -= cruise_d * movexy_r - mcu_time += move.cruise_t + move_time += move.cruise_t if decel_d: mcu_stepper.step_delta( - mcu_time, decel_d, cruise_v, -accel, + move_time, decel_d, cruise_v, -accel, vt_startz, vt_startxy_d, vt_arm_d, movez_r) diff --git a/klippy/extruder.py b/klippy/extruder.py index d6eff720..f86af765 100644 --- a/klippy/extruder.py +++ b/klippy/extruder.py @@ -50,8 +50,8 @@ class PrinterExtruder: if is_active: return self.activate_gcode return self.deactivate_gcode - def motor_off(self, move_time): - self.stepper.motor_enable(move_time, 0) + def motor_off(self, print_time): + self.stepper.motor_enable(print_time, 0) self.need_motor_enable = True def check_move(self, move): move.extrude_r = move.axes_d[3] / move.move_d @@ -131,9 +131,9 @@ class PrinterExtruder: return i move.extrude_max_corner_v = max_corner_v return flush_count - def move(self, move_time, move): + def move(self, print_time, move): if self.need_motor_enable: - self.stepper.motor_enable(move_time, 1) + self.stepper.motor_enable(print_time, 1) self.need_motor_enable = False axis_d = move.axes_d[3] axis_r = abs(axis_d) / move.move_d @@ -190,27 +190,28 @@ class PrinterExtruder: # Prepare for steps mcu_stepper = self.stepper.mcu_stepper - mcu_time = mcu_stepper.print_to_mcu_time(move_time) + move_time = print_time # Acceleration steps if accel_d: - mcu_stepper.step_const(mcu_time, start_pos, accel_d, start_v, accel) + mcu_stepper.step_const(move_time, start_pos, accel_d, start_v, accel) start_pos += accel_d - mcu_time += accel_t + move_time += accel_t # Cruising steps if cruise_d: - mcu_stepper.step_const(mcu_time, start_pos, cruise_d, cruise_v, 0.) + mcu_stepper.step_const(move_time, start_pos, cruise_d, cruise_v, 0.) start_pos += cruise_d - mcu_time += cruise_t + move_time += cruise_t # Deceleration steps if decel_d: - mcu_stepper.step_const(mcu_time, start_pos, decel_d, decel_v, -accel) + mcu_stepper.step_const( + move_time, start_pos, decel_d, decel_v, -accel) start_pos += decel_d - mcu_time += decel_t + move_time += decel_t # Retraction steps if retract_d: mcu_stepper.step_const( - mcu_time, start_pos, -retract_d, retract_v, accel) + move_time, start_pos, -retract_d, retract_v, accel) start_pos -= retract_d self.extrude_pos = start_pos diff --git a/klippy/fan.py b/klippy/fan.py index 8fa253bc..112fb34c 100644 --- a/klippy/fan.py +++ b/klippy/fan.py @@ -18,23 +18,19 @@ class PrinterFan: self.mcu_fan.setup_max_duration(0.) self.mcu_fan.setup_cycle_time(PWM_CYCLE_TIME) self.mcu_fan.setup_hard_pwm(config.getint('hard_pwm', 0)) - def set_pwm(self, mcu_time, value): + def set_speed(self, print_time, value): value = max(0., min(self.max_power, value)) if value == self.last_fan_value: return - mcu_time = max(self.last_fan_time + FAN_MIN_TIME, mcu_time) + print_time = max(self.last_fan_time + FAN_MIN_TIME, print_time) if (value and value < self.max_power and not self.last_fan_value and self.kick_start_time): # Run fan at full speed for specified kick_start_time - self.mcu_fan.set_pwm(mcu_time, self.max_power) - mcu_time += self.kick_start_time - self.mcu_fan.set_pwm(mcu_time, value) - self.last_fan_time = mcu_time + self.mcu_fan.set_pwm(print_time, self.max_power) + print_time += self.kick_start_time + self.mcu_fan.set_pwm(print_time, value) + self.last_fan_time = print_time self.last_fan_value = value - # External commands - def set_speed(self, print_time, value): - mcu_time = self.mcu_fan.get_mcu().print_to_mcu_time(print_time) - self.set_pwm(mcu_time, value) class PrinterHeaterFan: def __init__(self, printer, config): diff --git a/klippy/homing.py b/klippy/homing.py index c939c978..c3b9723b 100644 --- a/klippy/homing.py +++ b/klippy/homing.py @@ -42,7 +42,7 @@ class Homing: move_end_print_time = self.toolhead.get_last_move_time() self.toolhead.reset_print_time() for s, es, last_pos in endstops: - es.home_finalize(es.get_mcu().print_to_mcu_time(move_end_print_time)) + es.home_finalize(move_end_print_time) # Wait for endstops to trigger for s, es, last_pos in endstops: try: diff --git a/klippy/mcu.py b/klippy/mcu.py index 7f9f1d01..0bdf0b5e 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -25,7 +25,6 @@ class MCU_stepper: self._mcu_freq = self._min_stop_interval = 0. self._reset_cmd = self._get_position_cmd = None self._ffi_lib = self._stepqueue = None - self.print_to_mcu_time = mcu.print_to_mcu_time def get_mcu(self): return self._mcu def setup_dir_pin(self, pin_params): @@ -48,7 +47,8 @@ class MCU_stepper: "config_stepper oid=%d step_pin=%s dir_pin=%s" " min_stop_interval=%d invert_step=%d" % ( self._oid, self._step_pin, self._dir_pin, - min_stop_interval * self._mcu_freq, self._invert_step)) + self._mcu.seconds_to_clock(min_stop_interval), + self._invert_step)) step_cmd = self._mcu.lookup_command( "queue_step oid=%c interval=%u count=%hu add=%hi") dir_cmd = self._mcu.lookup_command( @@ -58,9 +58,8 @@ class MCU_stepper: self._get_position_cmd = self._mcu.lookup_command( "stepper_get_position oid=%c") ffi_main, self._ffi_lib = chelper.get_ffi() - max_error = int(max_error * self._mcu_freq) self._stepqueue = ffi_main.gc(self._ffi_lib.stepcompress_alloc( - max_error, step_cmd.msgid, dir_cmd.msgid, + self._mcu.seconds_to_clock(max_error), step_cmd.msgid, dir_cmd.msgid, self._invert_dir, self._oid), self._ffi_lib.stepcompress_free) self._mcu.register_stepqueue(self._stepqueue) @@ -97,8 +96,8 @@ class MCU_stepper: if self._invert_dir: pos = -pos self._mcu_position_offset = pos - self._commanded_pos - def reset_step_clock(self, mcu_time): - clock = int(mcu_time * self._mcu_freq) + def reset_step_clock(self, print_time): + clock = self._mcu.print_time_to_clock(print_time) ret = self._ffi_lib.stepcompress_reset(self._stepqueue, clock) if ret: raise error("Internal error in stepcompress") @@ -107,28 +106,29 @@ class MCU_stepper: self._stepqueue, data, len(data)) if ret: raise error("Internal error in stepcompress") - def step(self, mcu_time, sdir): - count = self._ffi_lib.stepcompress_push( - self._stepqueue, mcu_time * self._mcu_freq, sdir) + def step(self, print_time, sdir): + clock = print_time * self._mcu_freq + count = self._ffi_lib.stepcompress_push(self._stepqueue, clock, sdir) if count == STEPCOMPRESS_ERROR_RET: raise error("Internal error in stepcompress") self._commanded_pos += count - def step_const(self, mcu_time, start_pos, dist, start_v, accel): + def step_const(self, print_time, start_pos, dist, start_v, accel): + clock = print_time * self._mcu_freq inv_step_dist = self._inv_step_dist step_offset = self._commanded_pos - start_pos * inv_step_dist count = self._ffi_lib.stepcompress_push_const( - self._stepqueue, mcu_time * self._mcu_freq, step_offset, - dist * inv_step_dist, start_v * self._velocity_factor, - accel * self._accel_factor) + self._stepqueue, clock, step_offset, dist * inv_step_dist, + start_v * self._velocity_factor, accel * self._accel_factor) if count == STEPCOMPRESS_ERROR_RET: raise error("Internal error in stepcompress") self._commanded_pos += count - def step_delta(self, mcu_time, dist, start_v, accel + def step_delta(self, print_time, dist, start_v, accel , height_base, startxy_d, arm_d, movez_r): + clock = print_time * self._mcu_freq inv_step_dist = self._inv_step_dist height = self._commanded_pos - height_base * inv_step_dist count = self._ffi_lib.stepcompress_push_delta( - self._stepqueue, mcu_time * self._mcu_freq, dist * inv_step_dist, + self._stepqueue, clock, dist * inv_step_dist, start_v * self._velocity_factor, accel * self._accel_factor, height, startxy_d * inv_step_dist, arm_d * inv_step_dist, movez_r) if count == STEPCOMPRESS_ERROR_RET: @@ -147,7 +147,7 @@ class MCU_endstop: self._cmd_queue = mcu.alloc_command_queue() self._oid = self._home_cmd = self._query_cmd = None self._homing = False - self._min_query_time = self._mcu_freq = 0. + self._min_query_time = 0. self._next_query_clock = self._home_timeout_clock = 0 self._retry_query_ticks = 0 self._last_state = {} @@ -156,7 +156,6 @@ class MCU_endstop: def add_stepper(self, stepper): self._steppers.append(stepper) def build_config(self): - self._mcu_freq = self._mcu.get_mcu_freq() self._oid = self._mcu.create_oid() self._mcu.add_config_cmd( "config_end_stop oid=%d pin=%s pull_up=%d stepper_count=%d" % ( @@ -165,15 +164,15 @@ class MCU_endstop: self._mcu.add_config_cmd( "end_stop_set_stepper oid=%d pos=%d stepper_oid=%d" % ( self._oid, i, s.get_oid()), is_init=True) - self._retry_query_ticks = int(self._mcu_freq * self.RETRY_QUERY) + self._retry_query_ticks = self._mcu.seconds_to_clock(self.RETRY_QUERY) self._home_cmd = self._mcu.lookup_command( "end_stop_home oid=%c clock=%u rest_ticks=%u pin_value=%c") self._query_cmd = self._mcu.lookup_command("end_stop_query oid=%c") self._mcu.register_msg(self._handle_end_stop_state, "end_stop_state" , self._oid) - def home_start(self, mcu_time, rest_time): - clock = int(mcu_time * self._mcu_freq) - rest_ticks = int(rest_time * self._mcu_freq) + 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) self._homing = True self._min_query_time = self._mcu.monotonic() self._next_query_clock = clock + self._retry_query_ticks @@ -182,10 +181,10 @@ class MCU_endstop: self._mcu.send(msg, reqclock=clock, cq=self._cmd_queue) for s in self._steppers: s.note_homing_start(clock) - def home_finalize(self, mcu_time): + def home_finalize(self, print_time): for s in self._steppers: s.note_homing_finalized() - self._home_timeout_clock = int(mcu_time * self._mcu_freq) + self._home_timeout_clock = self._mcu.print_time_to_clock(print_time) def home_wait(self): eventtime = self._mcu.monotonic() while self._check_busy(eventtime): @@ -220,11 +219,10 @@ class MCU_endstop: msg = self._query_cmd.encode(self._oid) self._mcu.send(msg, cq=self._cmd_queue) return True - def query_endstop(self, mcu_time): - clock = int(mcu_time * self._mcu_freq) + def query_endstop(self, print_time): self._homing = False + self._next_query_clock = self._mcu.print_time_to_clock(print_time) self._min_query_time = self._mcu.monotonic() - self._next_query_clock = clock def query_endstop_wait(self): eventtime = self._mcu.monotonic() while self._check_busy(eventtime): @@ -241,7 +239,6 @@ class MCU_digital_out: self._max_duration = 2. self._last_clock = 0 self._last_value = None - self._mcu_freq = 0. self._cmd_queue = mcu.alloc_command_queue() self._set_cmd = None def get_mcu(self): @@ -251,7 +248,6 @@ class MCU_digital_out: def setup_static(self): self._static_value = not self._invert def build_config(self): - self._mcu_freq = self._mcu.get_mcu_freq() if self._static_value is not None: self._mcu.add_config_cmd("set_digital_out pin=%s value=%d" % ( self._pin, self._static_value)) @@ -261,11 +257,11 @@ class MCU_digital_out: "config_digital_out oid=%d pin=%s default_value=%d" " max_duration=%d" % ( self._oid, self._pin, self._invert, - self._max_duration * self._mcu_freq)) + self._mcu.seconds_to_clock(self._max_duration))) self._set_cmd = self._mcu.lookup_command( "schedule_digital_out oid=%c clock=%u value=%c") - def set_digital(self, mcu_time, value): - clock = int(mcu_time * self._mcu_freq) + def set_digital(self, print_time, value): + clock = self._mcu.print_time_to_clock(print_time) msg = self._set_cmd.encode( self._oid, clock, not not (value ^ self._invert)) self._mcu.send(msg, minclock=self._last_clock, reqclock=clock @@ -274,8 +270,8 @@ class MCU_digital_out: self._last_value = value def get_last_setting(self): return self._last_value - def set_pwm(self, mcu_time, value): - self.set_digital(mcu_time, value >= 0.5) + def set_pwm(self, print_time, value): + self.set_digital(print_time, value >= 0.5) class MCU_pwm: def __init__(self, mcu, pin_params): @@ -288,7 +284,6 @@ class MCU_pwm: self._pin = pin_params['pin'] self._invert = pin_params['invert'] self._last_clock = 0 - self._mcu_freq = 0. self._pwm_max = 0. self._cmd_queue = mcu.alloc_command_queue() self._set_cmd = None @@ -309,7 +304,6 @@ class MCU_pwm: value = 1. - value self._static_value = max(0., min(1., value)) def build_config(self): - self._mcu_freq = self._mcu.get_mcu_freq() if self._hard_pwm: self._pwm_max = self._mcu.serial.msgparser.get_constant_float( "PWM_MAX") @@ -324,7 +318,7 @@ class MCU_pwm: "config_pwm_out oid=%d pin=%s cycle_ticks=%d default_value=%d" " max_duration=%d" % ( self._oid, self._pin, self._cycle_time, self._invert, - self._max_duration * self._mcu_freq)) + self._mcu.seconds_to_clock(self._max_duration))) self._set_cmd = self._mcu.lookup_command( "schedule_pwm_out oid=%c clock=%u value=%hu") else: @@ -340,12 +334,14 @@ class MCU_pwm: self._mcu.add_config_cmd( "config_soft_pwm_out oid=%d pin=%s cycle_ticks=%d" " default_value=%d max_duration=%d" % ( - self._oid, self._pin, self._cycle_time * self._mcu_freq, - self._invert, self._max_duration * self._mcu_freq)) + self._oid, self._pin, + self._mcu.seconds_to_clock(self._cycle_time), + self._invert, + self._mcu.seconds_to_clock(self._max_duration))) self._set_cmd = self._mcu.lookup_command( "schedule_soft_pwm_out oid=%c clock=%u value=%hu") - def set_pwm(self, mcu_time, value): - clock = int(mcu_time * self._mcu_freq) + def set_pwm(self, print_time, value): + clock = self._mcu.print_time_to_clock(print_time) if self._invert: value = 1. - value value = int(max(0., min(1., value)) * self._pwm_max + 0.5) @@ -364,7 +360,6 @@ class MCU_adc: self._report_clock = 0 self._oid = self._callback = None self._inv_max_adc = 0. - self._mcu_freq = 0. self._cmd_queue = mcu.alloc_command_queue() def get_mcu(self): return self._mcu @@ -379,17 +374,17 @@ class MCU_adc: def build_config(self): if not self._sample_count: return - self._mcu_freq = self._mcu.get_mcu_freq() self._oid = self._mcu.create_oid() self._mcu.add_config_cmd("config_analog_in oid=%d pin=%s" % ( self._oid, self._pin)) last_clock, last_clock_time = self._mcu.get_last_clock() - clock = last_clock + int(self._mcu_freq * (1.0 + self._oid * 0.01)) # XXX - sample_ticks = int(self._sample_time * self._mcu_freq) + clock = last_clock + self._mcu.seconds_to_clock( + 1.0 + self._oid * 0.01) # XXX + sample_ticks = self._mcu.seconds_to_clock(self._sample_time) mcu_adc_max = self._mcu.serial.msgparser.get_constant_float("ADC_MAX") max_adc = self._sample_count * mcu_adc_max self._inv_max_adc = 1.0 / max_adc - self._report_clock = int(self._report_time * self._mcu_freq) + self._report_clock = self._mcu.seconds_to_clock(self._report_time) min_sample = max(0, min(0xffff, int(self._min_sample * max_adc))) max_sample = max(0, min(0xffff, int( math.ceil(self._max_sample * max_adc)))) @@ -403,7 +398,8 @@ class MCU_adc: def _handle_analog_in_state(self, params): last_value = params['value'] * self._inv_max_adc next_clock = self._mcu.serial.translate_clock(params['next_clock']) - last_read_time = (next_clock - self._report_clock) / self._mcu_freq + 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: self._callback(last_read_time, last_value) @@ -690,8 +686,6 @@ class MCU: def create_command(self, msg): return self.serial.msgparser.create_command(msg) # Clock syncing - def print_to_mcu_time(self, print_time): - return print_time def print_time_to_clock(self, print_time): return int(print_time * self._mcu_freq) def clock_to_print_time(self, clock): @@ -700,6 +694,8 @@ class MCU: return self.clock_to_print_time(self.serial.get_clock(eventtime)) def get_mcu_freq(self): return self._mcu_freq + def seconds_to_clock(self, time): + return int(time * self._mcu_freq) def get_last_clock(self): return self.serial.get_last_clock() def get_max_stepper_error(self): diff --git a/klippy/stepper.py b/klippy/stepper.py index ffafb395..6bec852a 100644 --- a/klippy/stepper.py +++ b/klippy/stepper.py @@ -38,14 +38,12 @@ class PrinterStepper: 2. * self.step_dist, max_halt_velocity, max_accel) min_stop_interval = second_last_step_time - last_step_time self.mcu_stepper.setup_min_stop_interval(min_stop_interval) - def motor_enable(self, move_time, enable=0): + def motor_enable(self, print_time, enable=0): if enable and self.need_motor_enable: - mcu_time = self.mcu_stepper.print_to_mcu_time(move_time) - self.mcu_stepper.reset_step_clock(mcu_time) + self.mcu_stepper.reset_step_clock(print_time) if (self.mcu_enable is not None and self.mcu_enable.get_last_setting() != enable): - mcu_time = self.mcu_enable.get_mcu().print_to_mcu_time(move_time) - self.mcu_enable.set_digital(mcu_time, enable) + self.mcu_enable.set_digital(print_time, enable) self.need_motor_enable = not enable class PrinterHomingStepper(PrinterStepper): @@ -97,13 +95,11 @@ class PrinterHomingStepper(PrinterStepper): self.homing_stepper_phases = None if self.mcu_endstop.get_mcu().is_fileoutput(): self.homing_endstop_accuracy = self.homing_stepper_phases - def enable_endstop_checking(self, move_time, step_time): - mcu_time = self.mcu_endstop.get_mcu().print_to_mcu_time(move_time) - self.mcu_endstop.home_start(mcu_time, step_time) + def enable_endstop_checking(self, print_time, step_time): + self.mcu_endstop.home_start(print_time, step_time) return self.mcu_endstop def query_endstop(self, print_time): - mcu_time = self.mcu_endstop.get_mcu().print_to_mcu_time(print_time) - self.mcu_endstop.query_endstop(mcu_time) + self.mcu_endstop.query_endstop(print_time) return self.mcu_endstop def get_homing_speed(self): # Round the configured homing speed so that it is an even