diff options
Diffstat (limited to 'klippy/mcu.py')
-rw-r--r-- | klippy/mcu.py | 89 |
1 files changed, 40 insertions, 49 deletions
diff --git a/klippy/mcu.py b/klippy/mcu.py index fed45f82..c00db3c7 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -21,7 +21,7 @@ class MCU_stepper: self._commanded_pos = self._mcu_position_offset = 0. self._step_dist = self._inv_step_dist = 1. self._min_stop_interval = 0. - self._reset_cmd = self._get_position_cmd = None + self._reset_cmd_id = self._get_position_cmd = None self._ffi_lib = self._stepqueue = None def get_mcu(self): return self._mcu @@ -46,17 +46,17 @@ class MCU_stepper: self._invert_step)) self._mcu.add_config_cmd( "reset_step_clock oid=%d clock=0" % (self._oid,), is_init=True) - step_cmd = self._mcu.lookup_command( + step_cmd_id = self._mcu.lookup_command_id( "queue_step oid=%c interval=%u count=%hu add=%hi") - dir_cmd = self._mcu.lookup_command( + dir_cmd_id = self._mcu.lookup_command_id( "set_next_step_dir oid=%c dir=%c") - self._reset_cmd = self._mcu.lookup_command( + self._reset_cmd_id = self._mcu.lookup_command_id( "reset_step_clock oid=%c clock=%u") self._get_position_cmd = self._mcu.lookup_command( "stepper_get_position oid=%c") ffi_main, self._ffi_lib = chelper.get_ffi() self._stepqueue = ffi_main.gc(self._ffi_lib.stepcompress_alloc( - self._mcu.seconds_to_clock(max_error), step_cmd.msgid, dir_cmd.msgid, + self._mcu.seconds_to_clock(max_error), step_cmd_id, dir_cmd_id, self._invert_dir, self._oid), self._ffi_lib.stepcompress_free) self._mcu.register_stepqueue(self._stepqueue) @@ -87,15 +87,15 @@ class MCU_stepper: ret = self._ffi_lib.stepcompress_reset(self._stepqueue, 0) if ret: raise error("Internal error in stepcompress") - data = (self._reset_cmd.msgid, self._oid, 0) + data = (self._reset_cmd_id, self._oid, 0) ret = self._ffi_lib.stepcompress_queue_msg( self._stepqueue, data, len(data)) if ret: raise error("Internal error in stepcompress") if not did_trigger or self._mcu.is_fileoutput(): return - cmd = self._get_position_cmd.encode(self._oid) - params = self._mcu.send_with_response(cmd, 'stepper_position', self._oid) + params = self._get_position_cmd.send_with_response( + [self._oid], response='stepper_position', response_oid=self._oid) pos = params['pos'] if self._invert_dir: pos = -pos @@ -137,7 +137,6 @@ class MCU_endstop: self._pin = pin_params['pin'] self._pullup = pin_params['pullup'] self._invert = pin_params['invert'] - self._cmd_queue = mcu.alloc_command_queue() self._oid = self._home_cmd = self._query_cmd = None self._homing = False self._min_query_time = self._next_query_time = 0. @@ -162,10 +161,12 @@ 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) + cmd_queue = self._mcu.alloc_command_queue() self._home_cmd = self._mcu.lookup_command( "end_stop_home oid=%c clock=%u sample_ticks=%u sample_count=%c" - " rest_ticks=%u pin_value=%c") - self._query_cmd = self._mcu.lookup_command("end_stop_query oid=%c") + " rest_ticks=%u pin_value=%c", cq=cmd_queue) + self._query_cmd = self._mcu.lookup_command("end_stop_query oid=%c", + cq=cmd_queue) self._mcu.register_msg(self._handle_end_stop_state, "end_stop_state" , self._oid) def home_prepare(self): @@ -176,10 +177,9 @@ class MCU_endstop: self._homing = True self._min_query_time = self._mcu.monotonic() self._next_query_time = self._min_query_time + self.RETRY_QUERY - msg = self._home_cmd.encode( - self._oid, clock, self._mcu.seconds_to_clock(sample_time), - sample_count, rest_ticks, 1 ^ self._invert) - self._mcu.send(msg, reqclock=clock, cq=self._cmd_queue) + self._home_cmd.send( + [self._oid, clock, self._mcu.seconds_to_clock(sample_time), + sample_count, rest_ticks, 1 ^ self._invert], reqclock=clock) for s in self._steppers: s.note_homing_start(clock) def home_wait(self, home_end_time): @@ -208,15 +208,13 @@ class MCU_endstop: for s in self._steppers: s.note_homing_end() self._homing = False - msg = self._home_cmd.encode(self._oid, 0, 0, 0, 0, 0) - self._mcu.send(msg, reqclock=0, cq=self._cmd_queue) + self._home_cmd.send([self._oid, 0, 0, 0, 0, 0]) raise self.TimeoutError("Timeout during endstop homing") if self._mcu.is_shutdown(): raise error("MCU is shutdown") if eventtime >= self._next_query_time: self._next_query_time = eventtime + self.RETRY_QUERY - msg = self._query_cmd.encode(self._oid) - self._mcu.send(msg, cq=self._cmd_queue) + self._query_cmd.send([self._oid]) return True def query_endstop(self, print_time): self._homing = False @@ -237,7 +235,6 @@ class MCU_digital_out: self._is_static = False self._max_duration = 2. self._last_clock = 0 - self._cmd_queue = mcu.alloc_command_queue() self._set_cmd = None def get_mcu(self): return self._mcu @@ -260,14 +257,13 @@ class MCU_digital_out: " max_duration=%d" % ( self._oid, self._pin, self._start_value, self._shutdown_value, self._mcu.seconds_to_clock(self._max_duration))) + cmd_queue = self._mcu.alloc_command_queue() self._set_cmd = self._mcu.lookup_command( - "schedule_digital_out oid=%c clock=%u value=%c") + "schedule_digital_out oid=%c clock=%u value=%c", cq=cmd_queue) 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 - , cq=self._cmd_queue) + self._set_cmd.send([self._oid, clock, (not not value) ^ self._invert], + minclock=self._last_clock, reqclock=clock) self._last_clock = clock def set_pwm(self, print_time, value): self.set_digital(print_time, value >= 0.5) @@ -285,7 +281,6 @@ class MCU_pwm: self._is_static = False self._last_clock = 0 self._pwm_max = 0. - self._cmd_queue = mcu.alloc_command_queue() self._set_cmd = None def get_mcu(self): return self._mcu @@ -304,6 +299,7 @@ class MCU_pwm: self._shutdown_value = max(0., min(1., shutdown_value)) self._is_static = is_static def build_config(self): + cmd_queue = self._mcu.alloc_command_queue() cycle_ticks = self._mcu.seconds_to_clock(self._cycle_time) if self._hardware_pwm: self._pwm_max = self._mcu.get_constant_float("PWM_MAX") @@ -322,7 +318,7 @@ class MCU_pwm: self._shutdown_value * self._pwm_max, self._mcu.seconds_to_clock(self._max_duration))) self._set_cmd = self._mcu.lookup_command( - "schedule_pwm_out oid=%c clock=%u value=%hu") + "schedule_pwm_out oid=%c clock=%u value=%hu", cq=cmd_queue) else: if (self._start_value not in [0., 1.] or self._shutdown_value not in [0., 1.]): @@ -341,15 +337,14 @@ class MCU_pwm: self._start_value >= 0.5, self._shutdown_value >= 0.5, 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") + "schedule_soft_pwm_out oid=%c clock=%u value=%hu", cq=cmd_queue) 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) - msg = self._set_cmd.encode(self._oid, clock, value) - self._mcu.send(msg, minclock=self._last_clock, reqclock=clock - , cq=self._cmd_queue) + self._set_cmd.send([self._oid, clock, value], + minclock=self._last_clock, reqclock=clock) self._last_clock = clock class MCU_adc: @@ -362,7 +357,6 @@ class MCU_adc: self._report_clock = 0 self._oid = self._callback = None self._inv_max_adc = 0. - self._cmd_queue = mcu.alloc_command_queue() def get_mcu(self): return self._mcu def setup_minmax(self, sample_time, sample_count, minval=0., maxval=1.): @@ -534,12 +528,12 @@ class MCU: self._config_crc = zlib.crc32('\n'.join(self._config_cmds)) & 0xffffffff self.add_config_cmd("finalize_config crc=%d" % (self._config_crc,)) def _send_config(self): - msg = self.create_command("get_config") + get_config_cmd = self.lookup_command("get_config") if self.is_fileoutput(): config_params = { 'is_config': 0, 'move_count': 500, 'crc': self._config_crc} else: - config_params = self.send_with_response(msg, 'config') + config_params = get_config_cmd.send_with_response(response='config') if not config_params['is_config']: if self._restart_method == 'rpi_usb': # Only configure mcu after usb power reset @@ -548,9 +542,10 @@ class MCU: logging.info("Sending MCU '%s' printer configuration...", self._name) for c in self._config_cmds: - self.send(self.create_command(c)) + self._serial.send(c) if not self.is_fileoutput(): - config_params = self.send_with_response(msg, 'config') + config_params = get_config_cmd.send_with_response( + response='config') if not config_params['is_config']: if self._is_shutdown: raise error("MCU '%s' error during config: %s" % ( @@ -579,7 +574,7 @@ class MCU: move_count) self._ffi_lib.steppersync_set_time(self._steppersync, 0., self._mcu_freq) for c in self._init_cmds: - self.send(self.create_command(c)) + self._serial.send(c) def _connect(self): if self.is_fileoutput(): self._connect_file() @@ -637,23 +632,19 @@ class MCU: def get_max_stepper_error(self): return self._max_stepper_error # Wrapper functions - def send(self, cmd, minclock=0, reqclock=0, cq=None): - self._serial.send(cmd, minclock, reqclock, cq=cq) - def send_with_response(self, cmd, name, oid=None): - return self._serial.send_with_response(cmd, name, oid) def register_msg(self, cb, msg, oid=None): self._serial.register_callback(cb, msg, oid) def alloc_command_queue(self): return self._serial.alloc_command_queue() - def create_command(self, msg): - return self._serial.msgparser.create_command(msg) - def lookup_command(self, msgformat): - return self._serial.msgparser.lookup_command(msgformat) + def lookup_command(self, msgformat, cq=None): + return self._serial.lookup_command(msgformat, cq) def try_lookup_command(self, msgformat): try: - return self._serial.msgparser.lookup_command(msgformat) + return self.lookup_command(msgformat) except self._serial.msgparser.error as e: return None + def lookup_command_id(self, msgformat): + return self._serial.msgparser.lookup_command(msgformat).msgid def get_constant_float(self, name): return self._serial.msgparser.get_constant_float(name) def print_time_to_clock(self, print_time): @@ -679,7 +670,7 @@ class MCU: def _shutdown(self, force=False): if self._emergency_stop_cmd is None or (self._is_shutdown and not force): return - self.send(self._emergency_stop_cmd.encode()) + self._emergency_stop_cmd.send() def _restart_arduino(self): logging.info("Attempting MCU '%s' reset", self._name) self._disconnect() @@ -695,11 +686,11 @@ class MCU: self._is_shutdown = True self._shutdown(force=True) self._reactor.pause(self._reactor.monotonic() + 0.015) - self.send(self._config_reset_cmd.encode()) + self._config_reset_cmd.send() else: # Attempt reset via reset command logging.info("Attempting MCU '%s' reset command", self._name) - self.send(self._reset_cmd.encode()) + self._reset_cmd.send() self._reactor.pause(self._reactor.monotonic() + 0.015) self._disconnect() def _restart_rpi_usb(self): |