From 11ecac626d754d83c03be6ca15225008ad0e504c Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 24 Aug 2016 15:16:02 -0400 Subject: [PATCH] mcu: Convert PWM and ADC objects to take mcu_time instead of clock Signed-off-by: Kevin O'Connor --- klippy/fan.py | 24 +++++------ klippy/heater.py | 104 +++++++++++++++++++++-------------------------- klippy/mcu.py | 53 +++++++++++++----------- 3 files changed, 85 insertions(+), 96 deletions(-) diff --git a/klippy/fan.py b/klippy/fan.py index 52374844..98e73ba6 100644 --- a/klippy/fan.py +++ b/klippy/fan.py @@ -11,29 +11,25 @@ class PrinterFan: self.printer = printer self.config = config self.mcu_fan = None - self.last_fan_clock = self.last_fan_value = 0 - self.min_fan_clock = 0 - self.kick_start_clock = 0 + self.last_fan_value = 0 + self.last_fan_time = 0. + self.kick_start_time = config.getfloat('kick_start_time', 0.1) def build_config(self): pin = self.config.get('pin') hard_pwm = self.config.getint('hard_pwm', 128) - mcu_freq = self.printer.mcu.get_mcu_freq() - self.min_fan_clock = int(FAN_MIN_TIME * mcu_freq) - kst = self.config.getfloat('kick_start_time', 0.1) - self.kick_start_clock = int(kst * mcu_freq) self.mcu_fan = self.printer.mcu.create_pwm(pin, hard_pwm, 0) # External commands def set_speed(self, print_time, value): value = max(0, min(255, int(value*255. + 0.5))) if value == self.last_fan_value: return - pc = int(self.mcu_fan.get_print_clock(print_time)) - pc = max(self.last_fan_clock + self.min_fan_clock, pc) + mcu_time = self.mcu_fan.print_to_mcu_time(print_time) + mcu_time = max(self.last_fan_time + FAN_MIN_TIME, mcu_time) if (value and value < 255 - and not self.last_fan_value and self.kick_start_clock): + 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(pc, 255) - pc += self.kick_start_clock - self.mcu_fan.set_pwm(pc, value) - self.last_fan_clock = pc + self.mcu_fan.set_pwm(mcu_time, 255) + mcu_time += self.kick_start_time + self.mcu_fan.set_pwm(mcu_time, value) + self.last_fan_time = mcu_time self.last_fan_value = value diff --git a/klippy/heater.py b/klippy/heater.py index d05035a0..29f29232 100644 --- a/klippy/heater.py +++ b/klippy/heater.py @@ -28,15 +28,12 @@ class PrinterHeater: self.pullup_r = config.getfloat('pullup_resistor', 4700.) self.lock = threading.Lock() self.last_temp = 0. - self.last_temp_clock = 0 + self.last_temp_time = 0. self.target_temp = 0. - self.report_clock = 0 self.control = None # pwm caching - self.next_pwm_clock = 0 + self.next_pwm_time = 0. self.last_pwm_value = 0 - self.resend_clock = 0 - self.pwm_offset_clock = 0 def build_config(self): heater_pin = self.config.get('heater_pin') thermistor_pin = self.config.get('thermistor_pin') @@ -44,35 +41,28 @@ class PrinterHeater: self.mcu_adc = self.printer.mcu.create_adc(thermistor_pin) min_adc = self.calc_adc(self.config.getfloat('max_temp')) max_adc = self.calc_adc(self.config.getfloat('min_temp')) - freq = self.printer.mcu.get_mcu_freq() - sample_clock = int(SAMPLE_TIME*freq) self.mcu_adc.set_minmax( - sample_clock, SAMPLE_COUNT, minval=min_adc, maxval=max_adc) + SAMPLE_TIME, SAMPLE_COUNT, minval=min_adc, maxval=max_adc) self.mcu_adc.set_adc_callback(self.adc_callback) - self.report_clock = int(REPORT_TIME*freq) control_algo = self.config.get('control', 'watermark') algos = {'watermark': ControlBangBang, 'pid': ControlPID} self.control = algos[control_algo](self, self.config) - self.next_pwm_clock = 0 - self.last_pwm_value = 0 - self.resend_clock = int(MAX_HEAT_TIME * freq * 3. / 4.) - self.pwm_offset_clock = sample_clock*SAMPLE_COUNT + self.report_clock def run(self): - self.mcu_adc.query_analog_in(self.report_clock) - def set_pwm(self, read_clock, value): + self.mcu_adc.query_analog_in(REPORT_TIME) + def set_pwm(self, read_time, value): if value: if self.target_temp <= 0.: return - if (read_clock < self.next_pwm_clock + if (read_time < self.next_pwm_time and abs(value - self.last_pwm_value) < 15): return elif not self.last_pwm_value: return - pwm_clock = read_clock + self.pwm_offset_clock - self.next_pwm_clock = pwm_clock + self.resend_clock + pwm_time = read_time + REPORT_TIME + SAMPLE_TIME*SAMPLE_COUNT + self.next_pwm_time = pwm_time + 0.75 * MAX_HEAT_TIME self.last_pwm_value = value - logging.debug("pwm=%d@%d (%d)" % (value, read_clock, pwm_clock)) - self.mcu_pwm.set_pwm(pwm_clock, value) + logging.debug("pwm=%d@%.3f (%.3f)" % (value, read_time, pwm_time)) + self.mcu_pwm.set_pwm(pwm_time, value) # Temperature calculation def calc_temp(self, adc): r = self.pullup_r * adc / (1.0 - adc) @@ -90,14 +80,13 @@ class PrinterHeater: x = math.sqrt(math.pow(c2 / (3.*c3), 3.) + math.pow(y, 2.)) r = math.exp(math.pow(x-y, 1./3.) - math.pow(x+y, 1./3.)) return r / (self.pullup_r + r) - def adc_callback(self, read_clock, read_value): + def adc_callback(self, read_time, read_value): temp = self.calc_temp(float(read_value)) with self.lock: self.last_temp = temp - self.last_temp_clock = read_clock - self.control.adc_callback(read_clock, temp) - #logging.debug("temp: %d(%d) %f = %f" % ( - # read_clock, read_clock & 0xffffffff, read_value, temp)) + self.last_temp_time = read_time + self.control.adc_callback(read_time, temp) + #logging.debug("temp: %.3f %f = %f" % (read_time, read_value, temp)) # External commands def set_temp(self, print_time, degrees): with self.lock: @@ -122,15 +111,15 @@ class ControlBangBang: self.heater = heater self.max_delta = config.getfloat('max_delta', 2.0) self.heating = False - def adc_callback(self, read_clock, temp): + def adc_callback(self, read_time, temp): if self.heating and temp >= self.heater.target_temp+self.max_delta: self.heating = False elif not self.heating and temp <= self.heater.target_temp-self.max_delta: self.heating = True if self.heating: - self.heater.set_pwm(read_clock, PWM_MAX) + self.heater.set_pwm(read_time, PWM_MAX) else: - self.heater.set_pwm(read_clock, 0) + self.heater.set_pwm(read_time, 0) def check_busy(self, eventtime): return self.heater.last_temp < self.heater.target_temp-self.max_delta @@ -149,12 +138,11 @@ class ControlPID: imax = config.getint('pid_integral_max', PWM_MAX) self.temp_integ_max = imax / self.Ki self.prev_temp = AMBIENT_TEMP - self.prev_temp_clock = 0 + self.prev_temp_time = 0. self.prev_temp_deriv = 0. self.prev_temp_integ = 0. - self.inv_mcu_freq = 1. / self.heater.printer.mcu.get_mcu_freq() - def adc_callback(self, read_clock, temp): - time_diff = (read_clock - self.prev_temp_clock) * self.inv_mcu_freq + def adc_callback(self, read_time, temp): + time_diff = read_time - self.prev_temp_time # Calculate change of temperature temp_diff = temp - self.prev_temp if time_diff >= self.min_deriv_time: @@ -168,13 +156,13 @@ class ControlPID: temp_integ = max(0., min(self.temp_integ_max, temp_integ)) # Calculate output co = int(self.Kp*temp_err + self.Ki*temp_integ - self.Kd*temp_deriv) - #logging.debug("pid: %f@%d -> diff=%f deriv=%f err=%f integ=%f co=%d" % ( - # temp, read_clock, temp_diff, temp_deriv, temp_err, temp_integ, co)) + #logging.debug("pid: %f@%.3f -> diff=%f deriv=%f err=%f integ=%f co=%d" % ( + # temp, read_time, temp_diff, temp_deriv, temp_err, temp_integ, co)) bounded_co = max(0, min(PWM_MAX, co)) - self.heater.set_pwm(read_clock, bounded_co) + self.heater.set_pwm(read_time, bounded_co) # Store state for next measurement self.prev_temp = temp - self.prev_temp_clock = read_clock + self.prev_temp_time = read_time self.prev_temp_deriv = temp_deriv if co == bounded_co: self.prev_temp_integ = temp_integ @@ -197,8 +185,8 @@ class ControlAutoTune: self.heating = False self.peaks = [] self.peak = 0. - self.peak_clock = 0 - def adc_callback(self, read_clock, temp): + self.peak_time = 0. + def adc_callback(self, read_time, temp): if self.heating and temp >= self.target_temp: self.heating = False self.check_peaks() @@ -206,17 +194,17 @@ class ControlAutoTune: self.heating = True self.check_peaks() if self.heating: - self.heater.set_pwm(read_clock, PWM_MAX) + self.heater.set_pwm(read_time, PWM_MAX) if temp < self.peak: self.peak = temp - self.peak_clock = read_clock + self.peak_time = read_time else: - self.heater.set_pwm(read_clock, 0) + self.heater.set_pwm(read_time, 0) if temp > self.peak: self.peak = temp - self.peak_clock = read_clock + self.peak_time = read_time def check_peaks(self): - self.peaks.append((self.peak, self.peak_clock)) + self.peaks.append((self.peak, self.peak_time)) if self.heating: self.peak = 9999999. else: @@ -224,18 +212,18 @@ class ControlAutoTune: if len(self.peaks) < 4: return temp_diff = self.peaks[-1][0] - self.peaks[-2][0] - clock_diff = self.peaks[-1][1] - self.peaks[-3][1] + time_diff = self.peaks[-1][1] - self.peaks[-3][1] pwm_diff = PWM_MAX - 0 Ku = 4. * (2. * pwm_diff) / (abs(temp_diff) * math.pi) - Tu = clock_diff / self.heater.printer.mcu.get_mcu_freq() + Tu = time_diff Kp = 0.6 * Ku Ti = 0.5 * Tu Td = 0.125 * Tu Ki = Kp / Ti Kd = Kp * Td - logging.info("Autotune: raw=%f/%d/%d Ku=%f Tu=%f Kp=%f Ki=%f Kd=%f" % ( - temp_diff, clock_diff, pwm_diff, Ku, Tu, Kp, Ki, Kd)) + logging.info("Autotune: raw=%f/%d Ku=%f Tu=%f Kp=%f Ki=%f Kd=%f" % ( + temp_diff, pwm_diff, Ku, Tu, Kp, Ki, Kd)) def check_busy(self, eventtime): if self.heating or len(self.peaks) < 12: return True @@ -255,29 +243,29 @@ class ControlBumpTest: self.temp_samples = {} self.pwm_samples = {} self.state = 0 - def set_pwm(self, read_clock, value): - self.pwm_samples[read_clock + 2*self.heater.report_clock] = value - self.heater.set_pwm(read_clock, value) - def adc_callback(self, read_clock, temp): - self.temp_samples[read_clock] = temp + def set_pwm(self, read_time, value): + self.pwm_samples[read_time + 2*REPORT_TIME] = value + self.heater.set_pwm(read_time, value) + def adc_callback(self, read_time, temp): + self.temp_samples[read_time] = temp if not self.state: - self.set_pwm(read_clock, 0) + self.set_pwm(read_time, 0) if len(self.temp_samples) >= 20: self.state += 1 elif self.state == 1: if temp < self.target_temp: - self.set_pwm(read_clock, PWM_MAX) + self.set_pwm(read_time, PWM_MAX) return - self.set_pwm(read_clock, 0) + self.set_pwm(read_time, 0) self.state += 1 elif self.state == 2: - self.set_pwm(read_clock, 0) + self.set_pwm(read_time, 0) if temp <= (self.target_temp + AMBIENT_TEMP) / 2.: self.dump_stats() self.state += 1 def dump_stats(self): - out = ["%d %.1f %d" % (clock, temp, self.pwm_samples.get(clock, -1)) - for clock, temp in sorted(self.temp_samples.items())] + out = ["%.3f %.1f %d" % (time, temp, self.pwm_samples.get(time, -1.)) + for time, temp in sorted(self.temp_samples.items())] f = open("/tmp/heattest.txt", "wb") f.write('\n'.join(out)) f.close() diff --git a/klippy/mcu.py b/klippy/mcu.py index 864ebbfb..2c7dcbd3 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -134,6 +134,7 @@ class MCU_digital_out: pin, pullup, self._invert = parse_pin_extras(pin) self._last_clock = 0 self._last_value = None + self._mcu_freq = mcu.get_mcu_freq() self._cmd_queue = mcu.alloc_command_queue() mcu.add_config_cmd( "config_digital_out oid=%d pin=%s default_value=%d" @@ -148,7 +149,8 @@ class MCU_digital_out: self._last_value = value def get_last_setting(self): return self._last_value - def set_pwm(self, clock, value): + def set_pwm(self, mcu_time, value): + clock = int(mcu_time * self._mcu_freq) dval = 0 if value > 127: dval = 1 @@ -161,6 +163,7 @@ class MCU_pwm: self._mcu = mcu self._oid = mcu.create_oid() self._last_clock = 0 + self._mcu_freq = mcu.get_mcu_freq() self._cmd_queue = mcu.alloc_command_queue() if hard_pwm: mcu.add_config_cmd( @@ -175,13 +178,13 @@ class MCU_pwm: self._oid, pin, cycle_ticks, max_duration)) self._set_cmd = mcu.lookup_command( "schedule_soft_pwm_out oid=%c clock=%u value=%c") - def set_pwm(self, clock, value): + self.print_to_mcu_time = mcu.print_to_mcu_time + def set_pwm(self, mcu_time, value): + clock = int(mcu_time * self._mcu_freq) msg = self._set_cmd.encode(self._oid, clock, value) self._mcu.send(msg, minclock=self._last_clock, reqclock=clock , cq=self._cmd_queue) self._last_clock = clock - def get_print_clock(self, print_time): - return self._mcu.get_print_clock(print_time) class MCU_adc: ADC_MAX = 1024 # 10bit adc @@ -193,10 +196,9 @@ class MCU_adc: self._sample_ticks = 0 self._sample_count = 1 self._report_clock = 0 - self._last_value = 0 - self._last_read_clock = 0 self._callback = None - self._max_adc_inv = 0. + self._inv_max_adc = 0. + self._mcu_freq = mcu.get_mcu_freq() self._cmd_queue = mcu.alloc_command_queue() mcu.add_config_cmd("config_analog_in oid=%d pin=%s" % (self._oid, pin)) mcu.register_msg(self._handle_analog_in_state, "analog_in_state" @@ -204,36 +206,33 @@ class MCU_adc: self._query_cmd = mcu.lookup_command( "query_analog_in oid=%c clock=%u sample_ticks=%u sample_count=%c" " rest_ticks=%u min_value=%hu max_value=%hu") - def set_minmax(self, sample_ticks, sample_count, minval=None, maxval=None): + def set_minmax(self, sample_time, sample_count, minval=None, maxval=None): + self._sample_ticks = int(sample_time * self._mcu_freq) + self._sample_count = sample_count if minval is None: minval = 0 if maxval is None: maxval = 0xffff - self._sample_ticks = sample_ticks - self._sample_count = sample_count max_adc = sample_count * self.ADC_MAX self._min_sample = int(minval * max_adc) self._max_sample = min(0xffff, int(math.ceil(maxval * max_adc))) - self._max_adc_inv = 1.0 / max_adc - def query_analog_in(self, report_clock): - self._report_clock = report_clock - mcu_freq = self._mcu.get_mcu_freq() + self._inv_max_adc = 1.0 / max_adc + def query_analog_in(self, report_time): + self._report_clock = int(report_time * self._mcu_freq) cur_clock = self._mcu.get_last_clock() - clock = cur_clock + int(mcu_freq * (1.0 + self._oid * 0.01)) # XXX + clock = cur_clock + int(self._mcu_freq * (1.0 + self._oid * 0.01)) # XXX msg = self._query_cmd.encode( self._oid, clock, self._sample_ticks, self._sample_count - , report_clock, self._min_sample, self._max_sample) + , self._report_clock, self._min_sample, self._max_sample) self._mcu.send(msg, reqclock=clock, cq=self._cmd_queue) def _handle_analog_in_state(self, params): - self._last_value = params['value'] * self._max_adc_inv + last_value = params['value'] * self._inv_max_adc next_clock = self._mcu.serial.translate_clock(params['next_clock']) - self._last_read_clock = next_clock - self._report_clock + last_read_time = (next_clock - self._report_clock) / self._mcu_freq if self._callback is not None: - self._callback(self._last_read_clock, self._last_value) + self._callback(last_read_time, last_value) def set_adc_callback(self, cb): self._callback = cb - def get_print_clock(self, print_time): - return self._mcu.get_print_clock(print_time) class MCU: def __init__(self, printer, config): @@ -419,6 +418,8 @@ class MCU: def get_print_buffer_time(self, eventtime, last_move_end): clock_diff = self.serial.get_clock(eventtime) - self._print_start_clock return last_move_end - (float(clock_diff) / self._clock_freq) + def print_to_mcu_time(self, print_time): + return print_time + self._print_start_clock / self._clock_freq def get_print_clock(self, print_time): return print_time * self._clock_freq + self._print_start_clock def get_mcu_freq(self): @@ -469,14 +470,16 @@ class Dummy_MCU_obj: return False def home_finalize(self): pass - def set_pwm(self, print_time, value): + def set_pwm(self, mcu_time, value): pass - def set_minmax(self, sample_ticks, sample_count, minval=None, maxval=None): + def set_minmax(self, sample_time, sample_count, minval=None, maxval=None): pass - def query_analog_in(self, report_clock): + def query_analog_in(self, report_time): pass def set_adc_callback(self, cb): pass + def print_to_mcu_time(self, print_time): + return self._mcu.print_to_mcu_time(print_time) def get_print_clock(self, print_time): return self._mcu.get_print_clock(print_time) @@ -510,6 +513,8 @@ class DummyMCU: pass def get_print_buffer_time(self, eventtime, last_move_end): return 0.250 + def print_to_mcu_time(self, print_time): + return print_time + self._print_start_clock / self._clock_freq def get_print_clock(self, print_time): return print_time * self._clock_freq + self._print_start_clock def get_mcu_freq(self):