| 
									
										
										
										
											2017-09-15 13:37:00 -04:00
										 |  |  | # Interface to Klipper micro-controller code | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 |  |  | # Copyright (C) 2016,2017  Kevin O'Connor <kevin@koconnor.net> | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | # | 
					
						
							|  |  |  | # This file may be distributed under the terms of the GNU GPLv3 license. | 
					
						
							| 
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 |  |  | import sys, os, zlib, logging, math | 
					
						
							| 
									
										
										
										
											2017-09-18 21:41:00 -04:00
										 |  |  | import serialhdl, pins, chelper, clocksync | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-29 18:27:48 -05:00
										 |  |  | class error(Exception): | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 |  |  | STEPCOMPRESS_ERROR_RET = -989898989 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | class MCU_stepper: | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def __init__(self, mcu, pin_params): | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._mcu = mcu | 
					
						
							| 
									
										
										
										
											2017-08-29 17:56:19 -04:00
										 |  |  |         self._oid = self._mcu.create_oid() | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |         self._step_pin = pin_params['pin'] | 
					
						
							|  |  |  |         self._invert_step = pin_params['invert'] | 
					
						
							|  |  |  |         self._dir_pin = self._invert_dir = None | 
					
						
							| 
									
										
										
										
											2017-12-07 18:12:06 -05:00
										 |  |  |         self._commanded_pos = self._mcu_position_offset = 0. | 
					
						
							| 
									
										
										
										
											2017-04-04 12:31:03 -04:00
										 |  |  |         self._step_dist = self._inv_step_dist = 1. | 
					
						
							| 
									
										
										
										
											2017-09-13 08:59:26 -04:00
										 |  |  |         self._min_stop_interval = 0. | 
					
						
							| 
									
										
										
										
											2017-08-29 17:56:19 -04:00
										 |  |  |         self._reset_cmd = self._get_position_cmd = None | 
					
						
							| 
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 |  |  |         self._ffi_lib = self._stepqueue = None | 
					
						
							| 
									
										
										
										
											2017-08-29 17:20:26 -04:00
										 |  |  |     def get_mcu(self): | 
					
						
							|  |  |  |         return self._mcu | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def setup_dir_pin(self, pin_params): | 
					
						
							|  |  |  |         if pin_params['chip'] is not self._mcu: | 
					
						
							|  |  |  |             raise pins.error("Stepper dir pin must be on same mcu as step pin") | 
					
						
							|  |  |  |         self._dir_pin = pin_params['pin'] | 
					
						
							|  |  |  |         self._invert_dir = pin_params['invert'] | 
					
						
							|  |  |  |     def setup_min_stop_interval(self, min_stop_interval): | 
					
						
							| 
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 |  |  |         self._min_stop_interval = min_stop_interval | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def setup_step_distance(self, step_dist): | 
					
						
							| 
									
										
										
										
											2017-04-04 12:31:03 -04:00
										 |  |  |         self._step_dist = step_dist | 
					
						
							|  |  |  |         self._inv_step_dist = 1. / step_dist | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |     def build_config(self): | 
					
						
							| 
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 |  |  |         max_error = self._mcu.get_max_stepper_error() | 
					
						
							|  |  |  |         min_stop_interval = max(0., self._min_stop_interval - max_error) | 
					
						
							|  |  |  |         self._mcu.add_config_cmd( | 
					
						
							|  |  |  |             "config_stepper oid=%d step_pin=%s dir_pin=%s" | 
					
						
							| 
									
										
										
										
											2017-08-25 23:21:55 -04:00
										 |  |  |             " min_stop_interval=%d invert_step=%d" % ( | 
					
						
							| 
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 |  |  |                 self._oid, self._step_pin, self._dir_pin, | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |                 self._mcu.seconds_to_clock(min_stop_interval), | 
					
						
							|  |  |  |                 self._invert_step)) | 
					
						
							| 
									
										
										
										
											2017-12-05 21:03:49 -05:00
										 |  |  |         self._mcu.add_config_cmd( | 
					
						
							|  |  |  |             "reset_step_clock oid=%d clock=0" % (self._oid,), is_init=True) | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         step_cmd = self._mcu.lookup_command( | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |             "queue_step oid=%c interval=%u count=%hu add=%hi") | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         dir_cmd = self._mcu.lookup_command( | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |             "set_next_step_dir oid=%c dir=%c") | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         self._reset_cmd = self._mcu.lookup_command( | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |             "reset_step_clock oid=%c clock=%u") | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         self._get_position_cmd = self._mcu.lookup_command( | 
					
						
							| 
									
										
										
										
											2017-03-05 15:00:15 -05:00
										 |  |  |             "stepper_get_position oid=%c") | 
					
						
							| 
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 |  |  |         ffi_main, self._ffi_lib = chelper.get_ffi() | 
					
						
							|  |  |  |         self._stepqueue = ffi_main.gc(self._ffi_lib.stepcompress_alloc( | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |             self._mcu.seconds_to_clock(max_error), step_cmd.msgid, dir_cmd.msgid, | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |             self._invert_dir, self._oid), | 
					
						
							| 
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 |  |  |                                       self._ffi_lib.stepcompress_free) | 
					
						
							| 
									
										
										
										
											2017-08-23 18:59:25 -04:00
										 |  |  |         self._mcu.register_stepqueue(self._stepqueue) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     def get_oid(self): | 
					
						
							|  |  |  |         return self._oid | 
					
						
							| 
									
										
										
										
											2017-11-07 12:29:51 -05:00
										 |  |  |     def get_step_dist(self): | 
					
						
							|  |  |  |         return self._step_dist | 
					
						
							| 
									
										
										
										
											2016-11-19 11:34:42 -05:00
										 |  |  |     def set_position(self, pos): | 
					
						
							| 
									
										
										
										
											2017-12-07 18:12:06 -05:00
										 |  |  |         steppos = pos * self._inv_step_dist | 
					
						
							| 
									
										
										
										
											2017-04-04 19:20:54 -04:00
										 |  |  |         self._mcu_position_offset += self._commanded_pos - steppos | 
					
						
							|  |  |  |         self._commanded_pos = steppos | 
					
						
							| 
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 |  |  |     def get_commanded_position(self): | 
					
						
							| 
									
										
										
										
											2017-04-04 19:20:54 -04:00
										 |  |  |         return self._commanded_pos * self._step_dist | 
					
						
							| 
									
										
										
										
											2016-11-28 11:22:01 -05:00
										 |  |  |     def get_mcu_position(self): | 
					
						
							| 
									
										
										
										
											2017-12-07 18:12:06 -05:00
										 |  |  |         mcu_pos = self._commanded_pos + self._mcu_position_offset | 
					
						
							|  |  |  |         if mcu_pos >= 0.: | 
					
						
							|  |  |  |             return int(mcu_pos + 0.5) | 
					
						
							|  |  |  |         return int(mcu_pos - 0.5) | 
					
						
							| 
									
										
										
										
											2016-12-30 17:02:28 -05:00
										 |  |  |     def note_homing_start(self, homing_clock): | 
					
						
							| 
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 |  |  |         ret = self._ffi_lib.stepcompress_set_homing( | 
					
						
							|  |  |  |             self._stepqueue, homing_clock) | 
					
						
							| 
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 |  |  |         if ret: | 
					
						
							|  |  |  |             raise error("Internal error in stepcompress") | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |     def note_homing_end(self, did_trigger=False): | 
					
						
							| 
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 |  |  |         ret = self._ffi_lib.stepcompress_set_homing(self._stepqueue, 0) | 
					
						
							| 
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 |  |  |         if ret: | 
					
						
							|  |  |  |             raise error("Internal error in stepcompress") | 
					
						
							| 
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 |  |  |         ret = self._ffi_lib.stepcompress_reset(self._stepqueue, 0) | 
					
						
							| 
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 |  |  |         if ret: | 
					
						
							|  |  |  |             raise error("Internal error in stepcompress") | 
					
						
							| 
									
										
										
										
											2017-12-05 21:03:49 -05:00
										 |  |  |         data = (self._reset_cmd.msgid, 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(): | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |         cmd = self._get_position_cmd.encode(self._oid) | 
					
						
							|  |  |  |         params = self._mcu.send_with_response(cmd, 'stepper_position', self._oid) | 
					
						
							| 
									
										
										
										
											2017-03-05 15:00:15 -05:00
										 |  |  |         pos = params['pos'] | 
					
						
							|  |  |  |         if self._invert_dir: | 
					
						
							|  |  |  |             pos = -pos | 
					
						
							| 
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 |  |  |         self._mcu_position_offset = pos - self._commanded_pos | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |     def step(self, print_time, sdir): | 
					
						
							| 
									
										
										
										
											2017-09-13 08:59:26 -04:00
										 |  |  |         count = self._ffi_lib.stepcompress_push( | 
					
						
							|  |  |  |             self._stepqueue, print_time, sdir) | 
					
						
							| 
									
										
										
										
											2017-09-13 09:01:13 -04:00
										 |  |  |         if count == STEPCOMPRESS_ERROR_RET: | 
					
						
							| 
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 |  |  |             raise error("Internal error in stepcompress") | 
					
						
							| 
									
										
										
										
											2017-09-13 09:01:13 -04:00
										 |  |  |         self._commanded_pos += count | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |     def step_const(self, print_time, start_pos, dist, start_v, accel): | 
					
						
							| 
									
										
										
										
											2017-04-04 12:31:03 -04:00
										 |  |  |         inv_step_dist = self._inv_step_dist | 
					
						
							| 
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 |  |  |         step_offset = self._commanded_pos - start_pos * inv_step_dist | 
					
						
							| 
									
										
										
										
											2017-04-04 19:54:32 -04:00
										 |  |  |         count = self._ffi_lib.stepcompress_push_const( | 
					
						
							| 
									
										
										
										
											2017-09-13 08:59:26 -04:00
										 |  |  |             self._stepqueue, print_time, step_offset, dist * inv_step_dist, | 
					
						
							|  |  |  |             start_v * inv_step_dist, accel * inv_step_dist) | 
					
						
							| 
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 |  |  |         if count == STEPCOMPRESS_ERROR_RET: | 
					
						
							|  |  |  |             raise error("Internal error in stepcompress") | 
					
						
							| 
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 |  |  |         self._commanded_pos += count | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |     def step_delta(self, print_time, dist, start_v, accel | 
					
						
							| 
									
										
										
										
											2017-04-06 11:09:08 -04:00
										 |  |  |                    , height_base, startxy_d, arm_d, movez_r): | 
					
						
							| 
									
										
										
										
											2017-04-04 21:59:28 -04:00
										 |  |  |         inv_step_dist = self._inv_step_dist | 
					
						
							|  |  |  |         height = self._commanded_pos - height_base * inv_step_dist | 
					
						
							| 
									
										
										
										
											2017-04-07 10:49:14 -04:00
										 |  |  |         count = self._ffi_lib.stepcompress_push_delta( | 
					
						
							| 
									
										
										
										
											2017-09-13 08:59:26 -04:00
										 |  |  |             self._stepqueue, print_time, dist * inv_step_dist, | 
					
						
							|  |  |  |             start_v * inv_step_dist, accel * inv_step_dist, | 
					
						
							| 
									
										
										
										
											2017-04-06 11:09:08 -04:00
										 |  |  |             height, startxy_d * inv_step_dist, arm_d * inv_step_dist, movez_r) | 
					
						
							| 
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 |  |  |         if count == STEPCOMPRESS_ERROR_RET: | 
					
						
							|  |  |  |             raise error("Internal error in stepcompress") | 
					
						
							| 
									
										
										
										
											2017-04-04 19:07:34 -04:00
										 |  |  |         self._commanded_pos += count | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class MCU_endstop: | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |     class TimeoutError(Exception): | 
					
						
							|  |  |  |         pass | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     RETRY_QUERY = 1.000 | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def __init__(self, mcu, pin_params): | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._mcu = mcu | 
					
						
							| 
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 |  |  |         self._steppers = [] | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |         self._pin = pin_params['pin'] | 
					
						
							|  |  |  |         self._pullup = pin_params['pullup'] | 
					
						
							|  |  |  |         self._invert = pin_params['invert'] | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._cmd_queue = mcu.alloc_command_queue() | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |         self._oid = self._home_cmd = self._query_cmd = None | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._homing = False | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |         self._min_query_time = self._next_query_time = 0. | 
					
						
							| 
									
										
										
										
											2016-10-10 23:41:40 -04:00
										 |  |  |         self._last_state = {} | 
					
						
							| 
									
										
										
										
											2017-08-29 17:20:26 -04:00
										 |  |  |     def get_mcu(self): | 
					
						
							|  |  |  |         return self._mcu | 
					
						
							| 
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 |  |  |     def add_stepper(self, stepper): | 
					
						
							| 
									
										
										
										
											2017-12-05 17:51:54 -05:00
										 |  |  |         if stepper.get_mcu() is not self._mcu: | 
					
						
							|  |  |  |             raise pins.error("Endstop and stepper must be on the same mcu") | 
					
						
							| 
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 |  |  |         self._steppers.append(stepper) | 
					
						
							| 
									
										
										
										
											2017-12-05 21:51:44 -05:00
										 |  |  |     def get_steppers(self): | 
					
						
							|  |  |  |         return list(self._steppers) | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |     def build_config(self): | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |         self._oid = self._mcu.create_oid() | 
					
						
							| 
									
										
										
										
											2017-03-12 22:23:10 -04:00
										 |  |  |         self._mcu.add_config_cmd( | 
					
						
							| 
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 |  |  |             "config_end_stop oid=%d pin=%s pull_up=%d stepper_count=%d" % ( | 
					
						
							|  |  |  |                 self._oid, self._pin, self._pullup, len(self._steppers))) | 
					
						
							| 
									
										
										
										
											2017-12-05 12:13:09 -05:00
										 |  |  |         self._mcu.add_config_cmd( | 
					
						
							|  |  |  |             "end_stop_home oid=%d clock=0 sample_ticks=0 sample_count=0" | 
					
						
							|  |  |  |             " rest_ticks=0 pin_value=0" % (self._oid,), is_init=True) | 
					
						
							| 
									
										
										
										
											2017-08-21 21:42:00 -04:00
										 |  |  |         for i, s in enumerate(self._steppers): | 
					
						
							|  |  |  |             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) | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         self._home_cmd = self._mcu.lookup_command( | 
					
						
							| 
									
										
										
										
											2017-10-12 10:39:46 -04:00
										 |  |  |             "end_stop_home oid=%c clock=%u sample_ticks=%u sample_count=%c" | 
					
						
							|  |  |  |             " rest_ticks=%u pin_value=%c") | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         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) | 
					
						
							| 
									
										
										
										
											2017-10-12 10:39:46 -04:00
										 |  |  |     def home_start(self, print_time, sample_time, sample_count, rest_time): | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |         clock = self._mcu.print_time_to_clock(print_time) | 
					
						
							| 
									
										
										
										
											2017-09-15 12:28:55 -04:00
										 |  |  |         rest_ticks = int(rest_time * self._mcu.get_adjusted_freq()) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._homing = True | 
					
						
							| 
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 |  |  |         self._min_query_time = self._mcu.monotonic() | 
					
						
							| 
									
										
										
										
											2017-12-05 23:50:29 -05:00
										 |  |  |         self._next_query_time = self._min_query_time + self.RETRY_QUERY | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         msg = self._home_cmd.encode( | 
					
						
							| 
									
										
										
										
											2017-10-12 10:39:46 -04:00
										 |  |  |             self._oid, clock, self._mcu.seconds_to_clock(sample_time), | 
					
						
							|  |  |  |             sample_count, rest_ticks, 1 ^ self._invert) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._mcu.send(msg, reqclock=clock, cq=self._cmd_queue) | 
					
						
							| 
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 |  |  |         for s in self._steppers: | 
					
						
							|  |  |  |             s.note_homing_start(clock) | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |     def home_wait(self, home_end_time): | 
					
						
							| 
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 |  |  |         eventtime = self._mcu.monotonic() | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |         while self._check_busy(eventtime, home_end_time): | 
					
						
							| 
									
										
										
										
											2016-12-08 18:12:20 -05:00
										 |  |  |             eventtime = self._mcu.pause(eventtime + 0.1) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     def _handle_end_stop_state(self, params): | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         logging.debug("end_stop_state %s", params) | 
					
						
							| 
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 |  |  |         self._last_state = params | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |     def _check_busy(self, eventtime, home_end_time=0.): | 
					
						
							| 
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 |  |  |         # Check if need to send an end_stop_query command | 
					
						
							| 
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 |  |  |         last_sent_time = self._last_state.get('#sent_time', -1.) | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |         if last_sent_time >= self._min_query_time or self._mcu.is_fileoutput(): | 
					
						
							| 
									
										
										
										
											2016-11-28 11:22:01 -05:00
										 |  |  |             if not self._homing: | 
					
						
							|  |  |  |                 return False | 
					
						
							|  |  |  |             if not self._last_state.get('homing', 0): | 
					
						
							| 
									
										
										
										
											2017-03-05 15:30:04 -05:00
										 |  |  |                 for s in self._steppers: | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |                     s.note_homing_end(did_trigger=True) | 
					
						
							| 
									
										
										
										
											2016-11-28 11:22:01 -05:00
										 |  |  |                 self._homing = False | 
					
						
							| 
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 |  |  |                 return False | 
					
						
							| 
									
										
										
										
											2017-12-05 23:50:29 -05:00
										 |  |  |             last_sent_print_time = self._mcu.estimated_print_time(last_sent_time) | 
					
						
							|  |  |  |             if last_sent_print_time > home_end_time: | 
					
						
							| 
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 |  |  |                 # Timeout - disable endstop checking | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |                 for s in self._steppers: | 
					
						
							|  |  |  |                     s.note_homing_end() | 
					
						
							|  |  |  |                 self._homing = False | 
					
						
							| 
									
										
										
										
											2017-10-12 10:39:46 -04:00
										 |  |  |                 msg = self._home_cmd.encode(self._oid, 0, 0, 0, 0, 0) | 
					
						
							| 
									
										
										
										
											2016-11-18 13:03:40 -05:00
										 |  |  |                 self._mcu.send(msg, reqclock=0, cq=self._cmd_queue) | 
					
						
							| 
									
										
										
										
											2017-12-05 23:33:23 -05:00
										 |  |  |                 raise self.TimeoutError("Timeout during endstop homing") | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |         if self._mcu.is_shutdown(): | 
					
						
							| 
									
										
										
										
											2016-11-29 18:27:48 -05:00
										 |  |  |             raise error("MCU is shutdown") | 
					
						
							| 
									
										
										
										
											2017-12-05 23:50:29 -05:00
										 |  |  |         if eventtime >= self._next_query_time: | 
					
						
							|  |  |  |             self._next_query_time = eventtime + self.RETRY_QUERY | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |             msg = self._query_cmd.encode(self._oid) | 
					
						
							|  |  |  |             self._mcu.send(msg, cq=self._cmd_queue) | 
					
						
							| 
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 |  |  |         return True | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |     def query_endstop(self, print_time): | 
					
						
							| 
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 |  |  |         self._homing = False | 
					
						
							| 
									
										
										
										
											2017-12-05 23:50:29 -05:00
										 |  |  |         self._min_query_time = self._next_query_time = self._mcu.monotonic() | 
					
						
							| 
									
										
										
										
											2016-12-08 12:42:12 -05:00
										 |  |  |     def query_endstop_wait(self): | 
					
						
							| 
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 |  |  |         eventtime = self._mcu.monotonic() | 
					
						
							| 
									
										
										
										
											2016-12-08 18:12:20 -05:00
										 |  |  |         while self._check_busy(eventtime): | 
					
						
							| 
									
										
										
										
											2016-12-08 12:42:12 -05:00
										 |  |  |             eventtime = self._mcu.pause(eventtime + 0.1) | 
					
						
							| 
									
										
										
										
											2016-09-22 11:09:20 -04:00
										 |  |  |         return self._last_state.get('pin', self._invert) ^ self._invert | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class MCU_digital_out: | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def __init__(self, mcu, pin_params): | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._mcu = mcu | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |         self._oid = None | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |         self._pin = pin_params['pin'] | 
					
						
							| 
									
										
										
										
											2017-12-18 13:07:41 -05:00
										 |  |  |         self._invert = pin_params['invert'] | 
					
						
							|  |  |  |         self._start_value = self._shutdown_value = self._invert | 
					
						
							|  |  |  |         self._static_value = None | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |         self._max_duration = 2. | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._last_clock = 0 | 
					
						
							|  |  |  |         self._cmd_queue = mcu.alloc_command_queue() | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         self._set_cmd = None | 
					
						
							| 
									
										
										
										
											2017-08-29 17:20:26 -04:00
										 |  |  |     def get_mcu(self): | 
					
						
							|  |  |  |         return self._mcu | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def setup_max_duration(self, max_duration): | 
					
						
							|  |  |  |         self._max_duration = max_duration | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |     def setup_static(self): | 
					
						
							|  |  |  |         self._static_value = not self._invert | 
					
						
							| 
									
										
										
										
											2017-12-18 13:07:41 -05:00
										 |  |  |     def setup_start_value(self, start_value, shutdown_value): | 
					
						
							|  |  |  |         self._start_value = (not not start_value) ^ self._invert | 
					
						
							|  |  |  |         self._shutdown_value = (not not shutdown_value) ^ self._invert | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |     def build_config(self): | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |         if self._static_value is not None: | 
					
						
							|  |  |  |             self._mcu.add_config_cmd("set_digital_out pin=%s value=%d" % ( | 
					
						
							|  |  |  |                 self._pin, self._static_value)) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         self._oid = self._mcu.create_oid() | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |         self._mcu.add_config_cmd( | 
					
						
							| 
									
										
										
										
											2017-10-11 22:50:29 -04:00
										 |  |  |             "config_digital_out oid=%d pin=%s value=%d default_value=%d" | 
					
						
							| 
									
										
										
										
											2017-08-25 23:21:55 -04:00
										 |  |  |             " max_duration=%d" % ( | 
					
						
							| 
									
										
										
										
											2017-10-11 22:50:29 -04:00
										 |  |  |                 self._oid, self._pin, self._invert, self._shutdown_value, | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |                 self._mcu.seconds_to_clock(self._max_duration))) | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         self._set_cmd = self._mcu.lookup_command( | 
					
						
							|  |  |  |             "schedule_digital_out oid=%c clock=%u value=%c") | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |     def set_digital(self, print_time, value): | 
					
						
							|  |  |  |         clock = self._mcu.print_time_to_clock(print_time) | 
					
						
							| 
									
										
										
										
											2017-09-06 11:55:34 -04:00
										 |  |  |         msg = self._set_cmd.encode( | 
					
						
							| 
									
										
										
										
											2017-10-11 22:50:29 -04:00
										 |  |  |             self._oid, clock, (not not value) ^ self._invert) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._mcu.send(msg, minclock=self._last_clock, reqclock=clock | 
					
						
							|  |  |  |                       , cq=self._cmd_queue) | 
					
						
							|  |  |  |         self._last_clock = clock | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |     def set_pwm(self, print_time, value): | 
					
						
							|  |  |  |         self.set_digital(print_time, value >= 0.5) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class MCU_pwm: | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def __init__(self, mcu, pin_params): | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._mcu = mcu | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |         self._hard_pwm = False | 
					
						
							|  |  |  |         self._cycle_time = 0.100 | 
					
						
							|  |  |  |         self._max_duration = 2. | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |         self._oid = None | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |         self._pin = pin_params['pin'] | 
					
						
							|  |  |  |         self._invert = pin_params['invert'] | 
					
						
							| 
									
										
										
										
											2017-12-18 13:07:41 -05:00
										 |  |  |         self._start_value = self._shutdown_value = float(self._invert) | 
					
						
							|  |  |  |         self._static_value = None | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._last_clock = 0 | 
					
						
							| 
									
										
										
										
											2017-05-07 17:33:45 -04:00
										 |  |  |         self._pwm_max = 0. | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._cmd_queue = mcu.alloc_command_queue() | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         self._set_cmd = None | 
					
						
							| 
									
										
										
										
											2017-08-29 17:20:26 -04:00
										 |  |  |     def get_mcu(self): | 
					
						
							|  |  |  |         return self._mcu | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def setup_max_duration(self, max_duration): | 
					
						
							|  |  |  |         self._max_duration = max_duration | 
					
						
							|  |  |  |     def setup_cycle_time(self, cycle_time): | 
					
						
							|  |  |  |         self._cycle_time = cycle_time | 
					
						
							|  |  |  |         self._hard_pwm = False | 
					
						
							|  |  |  |     def setup_hard_pwm(self, hard_cycle_ticks): | 
					
						
							|  |  |  |         if not hard_cycle_ticks: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         self._cycle_time = hard_cycle_ticks | 
					
						
							|  |  |  |         self._hard_pwm = True | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |     def setup_static_pwm(self, value): | 
					
						
							|  |  |  |         if self._invert: | 
					
						
							| 
									
										
										
										
											2017-09-06 11:55:34 -04:00
										 |  |  |             value = 1. - value | 
					
						
							|  |  |  |         self._static_value = max(0., min(1., value)) | 
					
						
							| 
									
										
										
										
											2017-12-18 13:07:41 -05:00
										 |  |  |     def setup_start_value(self, start_value, shutdown_value): | 
					
						
							| 
									
										
										
										
											2017-10-11 22:50:29 -04:00
										 |  |  |         if self._invert: | 
					
						
							| 
									
										
										
										
											2017-12-18 13:07:41 -05:00
										 |  |  |             start_value = 1. - start_value | 
					
						
							|  |  |  |             shutdown_value = 1. - shutdown_value | 
					
						
							|  |  |  |         self._start_value = max(0., min(1., start_value)) | 
					
						
							|  |  |  |         self._shutdown_value = max(0., min(1., shutdown_value)) | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |     def build_config(self): | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |         if self._hard_pwm: | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |             self._pwm_max = self._mcu.get_constant_float("PWM_MAX") | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |             if self._static_value is not None: | 
					
						
							|  |  |  |                 value = int(self._static_value * self._pwm_max + 0.5) | 
					
						
							|  |  |  |                 self._mcu.add_config_cmd( | 
					
						
							|  |  |  |                     "set_pwm_out pin=%s cycle_ticks=%d value=%d" % ( | 
					
						
							|  |  |  |                         self._pin, self._cycle_time, value)) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |             self._oid = self._mcu.create_oid() | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |             self._mcu.add_config_cmd( | 
					
						
							| 
									
										
										
										
											2017-10-11 22:50:29 -04:00
										 |  |  |                 "config_pwm_out oid=%d pin=%s cycle_ticks=%d value=%d" | 
					
						
							|  |  |  |                 " default_value=%d max_duration=%d" % ( | 
					
						
							|  |  |  |                     self._oid, self._pin, self._cycle_time, | 
					
						
							| 
									
										
										
										
											2017-12-18 13:07:41 -05:00
										 |  |  |                     self._start_value * self._pwm_max, | 
					
						
							| 
									
										
										
										
											2017-10-11 22:50:29 -04:00
										 |  |  |                     self._shutdown_value * self._pwm_max, | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |                     self._mcu.seconds_to_clock(self._max_duration))) | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |             self._set_cmd = self._mcu.lookup_command( | 
					
						
							| 
									
										
										
										
											2017-05-07 17:33:45 -04:00
										 |  |  |                 "schedule_pwm_out oid=%c clock=%u value=%hu") | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |             self._pwm_max = self._mcu.get_constant_float("SOFT_PWM_MAX") | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |             if self._static_value is not None: | 
					
						
							| 
									
										
										
										
											2017-10-11 22:50:29 -04:00
										 |  |  |                 if self._static_value not in [0., 1.]: | 
					
						
							|  |  |  |                     raise pins.error( | 
					
						
							|  |  |  |                         "static value must be 0.0 or 1.0 on soft pwm") | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |                 self._mcu.add_config_cmd("set_digital_out pin=%s value=%d" % ( | 
					
						
							| 
									
										
										
										
											2017-09-06 11:55:34 -04:00
										 |  |  |                     self._pin, self._static_value >= 0.5)) | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |                 return | 
					
						
							| 
									
										
										
										
											2017-12-18 13:07:41 -05:00
										 |  |  |             if (self._start_value not in [0., 1.] | 
					
						
							|  |  |  |                 or self._shutdown_value not in [0., 1.]): | 
					
						
							| 
									
										
										
										
											2017-10-11 22:50:29 -04:00
										 |  |  |                 raise pins.error( | 
					
						
							| 
									
										
										
										
											2017-12-18 13:07:41 -05:00
										 |  |  |                     "start and shutdown values must be 0.0 or 1.0 on soft pwm") | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |             self._oid = self._mcu.create_oid() | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |             self._mcu.add_config_cmd( | 
					
						
							| 
									
										
										
										
											2017-10-11 22:50:29 -04:00
										 |  |  |                 "config_soft_pwm_out oid=%d pin=%s cycle_ticks=%d value=%d" | 
					
						
							| 
									
										
										
										
											2017-08-25 23:21:55 -04:00
										 |  |  |                 " default_value=%d max_duration=%d" % ( | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |                     self._oid, self._pin, | 
					
						
							|  |  |  |                     self._mcu.seconds_to_clock(self._cycle_time), | 
					
						
							| 
									
										
										
										
											2017-12-18 13:07:41 -05:00
										 |  |  |                     self._start_value >= 0.5, self._shutdown_value >= 0.5, | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |                     self._mcu.seconds_to_clock(self._max_duration))) | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |             self._set_cmd = self._mcu.lookup_command( | 
					
						
							| 
									
										
										
										
											2017-05-07 17:33:45 -04:00
										 |  |  |                 "schedule_soft_pwm_out oid=%c clock=%u value=%hu") | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |     def set_pwm(self, print_time, value): | 
					
						
							|  |  |  |         clock = self._mcu.print_time_to_clock(print_time) | 
					
						
							| 
									
										
										
										
											2017-03-09 00:44:25 -05:00
										 |  |  |         if self._invert: | 
					
						
							|  |  |  |             value = 1. - value | 
					
						
							| 
									
										
										
										
											2017-09-06 11:55:34 -04:00
										 |  |  |         value = int(max(0., min(1., value)) * self._pwm_max + 0.5) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MCU_adc: | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def __init__(self, mcu, pin_params): | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._mcu = mcu | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |         self._pin = pin_params['pin'] | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         self._min_sample = self._max_sample = 0. | 
					
						
							|  |  |  |         self._sample_time = self._report_time = 0. | 
					
						
							|  |  |  |         self._sample_count = 0 | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._report_clock = 0 | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |         self._oid = self._callback = None | 
					
						
							| 
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 |  |  |         self._inv_max_adc = 0. | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._cmd_queue = mcu.alloc_command_queue() | 
					
						
							| 
									
										
										
										
											2017-08-29 17:20:26 -04:00
										 |  |  |     def get_mcu(self): | 
					
						
							|  |  |  |         return self._mcu | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def setup_minmax(self, sample_time, sample_count, minval=0., maxval=1.): | 
					
						
							|  |  |  |         self._sample_time = sample_time | 
					
						
							|  |  |  |         self._sample_count = sample_count | 
					
						
							|  |  |  |         self._min_sample = minval | 
					
						
							|  |  |  |         self._max_sample = maxval | 
					
						
							|  |  |  |     def setup_adc_callback(self, report_time, callback): | 
					
						
							|  |  |  |         self._report_time = report_time | 
					
						
							|  |  |  |         self._callback = callback | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |     def build_config(self): | 
					
						
							| 
									
										
										
										
											2017-08-21 21:42:00 -04:00
										 |  |  |         if not self._sample_count: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-08-24 09:34:31 -04:00
										 |  |  |         self._oid = self._mcu.create_oid() | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |         self._mcu.add_config_cmd("config_analog_in oid=%d pin=%s" % ( | 
					
						
							|  |  |  |             self._oid, self._pin)) | 
					
						
							| 
									
										
										
										
											2017-09-18 22:53:12 -04:00
										 |  |  |         clock = self._mcu.get_query_slot(self._oid) | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |         sample_ticks = self._mcu.seconds_to_clock(self._sample_time) | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |         mcu_adc_max = self._mcu.get_constant_float("ADC_MAX") | 
					
						
							| 
									
										
										
										
											2017-03-12 21:30:18 -04:00
										 |  |  |         max_adc = self._sample_count * mcu_adc_max | 
					
						
							|  |  |  |         self._inv_max_adc = 1.0 / max_adc | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |         self._report_clock = self._mcu.seconds_to_clock(self._report_time) | 
					
						
							| 
									
										
										
										
											2017-09-05 18:19:36 -04:00
										 |  |  |         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)))) | 
					
						
							| 
									
										
										
										
											2017-08-21 21:42:00 -04:00
										 |  |  |         self._mcu.add_config_cmd( | 
					
						
							|  |  |  |             "query_analog_in oid=%d clock=%d sample_ticks=%d sample_count=%d" | 
					
						
							|  |  |  |             " rest_ticks=%d min_value=%d max_value=%d" % ( | 
					
						
							|  |  |  |                 self._oid, clock, sample_ticks, self._sample_count, | 
					
						
							|  |  |  |                 self._report_clock, min_sample, max_sample), is_init=True) | 
					
						
							|  |  |  |         self._mcu.register_msg(self._handle_analog_in_state, "analog_in_state" | 
					
						
							|  |  |  |                                , self._oid) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     def _handle_analog_in_state(self, params): | 
					
						
							| 
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 |  |  |         last_value = params['value'] * self._inv_max_adc | 
					
						
							| 
									
										
										
										
											2017-09-15 12:28:55 -04:00
										 |  |  |         next_clock = self._mcu.clock32_to_clock64(params['next_clock']) | 
					
						
							| 
									
										
										
										
											2017-09-12 12:47:40 -04:00
										 |  |  |         last_read_clock = next_clock - self._report_clock | 
					
						
							|  |  |  |         last_read_time = self._mcu.clock_to_print_time(last_read_clock) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         if self._callback is not None: | 
					
						
							| 
									
										
										
										
											2016-08-24 15:16:02 -04:00
										 |  |  |             self._callback(last_read_time, last_value) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class MCU: | 
					
						
							| 
									
										
										
										
											2016-12-08 12:42:12 -05:00
										 |  |  |     error = error | 
					
						
							| 
									
										
										
										
											2017-09-18 21:41:00 -04:00
										 |  |  |     def __init__(self, printer, config, clocksync): | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._printer = printer | 
					
						
							| 
									
										
										
										
											2017-09-18 21:41:00 -04:00
										 |  |  |         self._clocksync = clocksync | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         self._name = config.section | 
					
						
							|  |  |  |         if self._name.startswith('mcu '): | 
					
						
							|  |  |  |             self._name = self._name[4:] | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         # Serial port | 
					
						
							| 
									
										
										
										
											2017-03-08 22:26:10 -05:00
										 |  |  |         self._serialport = config.get('serial', '/dev/ttyS0') | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |         baud = 0 | 
					
						
							| 
									
										
										
										
											2017-07-20 00:02:43 -04:00
										 |  |  |         if not (self._serialport.startswith("/dev/rpmsg_") | 
					
						
							|  |  |  |                 or self._serialport.startswith("/tmp/klipper_host_")): | 
					
						
							| 
									
										
										
										
											2017-05-08 10:29:59 -04:00
										 |  |  |             baud = config.getint('baud', 250000, minval=2400) | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |         self._serial = serialhdl.SerialReader( | 
					
						
							| 
									
										
										
										
											2017-03-08 22:26:10 -05:00
										 |  |  |             printer.reactor, self._serialport, baud) | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |         # Restarts | 
					
						
							| 
									
										
										
										
											2017-09-03 22:02:48 -04:00
										 |  |  |         self._restart_method = 'command' | 
					
						
							|  |  |  |         if baud: | 
					
						
							|  |  |  |             rmethods = {m: m for m in ['arduino', 'command', 'rpi_usb']} | 
					
						
							|  |  |  |             self._restart_method = config.getchoice( | 
					
						
							|  |  |  |                 'restart_method', rmethods, 'arduino') | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |         self._reset_cmd = self._config_reset_cmd = None | 
					
						
							|  |  |  |         self._emergency_stop_cmd = None | 
					
						
							| 
									
										
										
										
											2018-01-08 10:55:37 -05:00
										 |  |  |         self._is_shutdown = self._is_timeout = False | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |         self._shutdown_msg = "" | 
					
						
							| 
									
										
										
										
											2017-05-01 13:44:06 -04:00
										 |  |  |         if printer.bglogger is not None: | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |             printer.bglogger.set_rollover_info(self._name, None) | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |         # Config building | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         pins.get_printer_pins(printer).register_chip(self._name, self) | 
					
						
							| 
									
										
										
										
											2017-08-23 18:55:04 -04:00
										 |  |  |         self._oid_count = 0 | 
					
						
							|  |  |  |         self._config_objects = [] | 
					
						
							| 
									
										
										
										
											2017-08-21 21:42:00 -04:00
										 |  |  |         self._init_cmds = [] | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._config_cmds = [] | 
					
						
							|  |  |  |         self._config_crc = None | 
					
						
							| 
									
										
										
										
											2017-03-12 23:05:01 -04:00
										 |  |  |         self._pin_map = config.get('pin_map', None) | 
					
						
							| 
									
										
										
										
											2017-03-12 22:43:05 -04:00
										 |  |  |         self._custom = config.get('custom', '') | 
					
						
							| 
									
										
										
										
											2017-09-12 12:24:44 -04:00
										 |  |  |         self._mcu_freq = 0. | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         # Move command queuing | 
					
						
							| 
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 |  |  |         ffi_main, self._ffi_lib = chelper.get_ffi() | 
					
						
							| 
									
										
										
										
											2017-04-11 11:37:09 -04:00
										 |  |  |         self._max_stepper_error = config.getfloat( | 
					
						
							|  |  |  |             'max_stepper_error', 0.000025, minval=0.) | 
					
						
							| 
									
										
										
										
											2017-08-23 18:59:25 -04:00
										 |  |  |         self._stepqueues = [] | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._steppersync = None | 
					
						
							|  |  |  |         # Stats | 
					
						
							| 
									
										
										
										
											2017-01-09 23:08:23 -05:00
										 |  |  |         self._stats_sumsq_base = 0. | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._mcu_tick_avg = 0. | 
					
						
							|  |  |  |         self._mcu_tick_stddev = 0. | 
					
						
							| 
									
										
										
										
											2017-07-12 22:16:16 -04:00
										 |  |  |         self._mcu_tick_awake = 0. | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |     # Serial callbacks | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     def handle_mcu_stats(self, params): | 
					
						
							|  |  |  |         count = params['count'] | 
					
						
							|  |  |  |         tick_sum = params['sum'] | 
					
						
							| 
									
										
										
										
											2016-08-25 12:09:37 -04:00
										 |  |  |         c = 1.0 / (count * self._mcu_freq) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._mcu_tick_avg = tick_sum * c | 
					
						
							| 
									
										
										
										
											2017-01-09 23:08:23 -05:00
										 |  |  |         tick_sumsq = params['sumsq'] * self._stats_sumsq_base | 
					
						
							|  |  |  |         self._mcu_tick_stddev = c * math.sqrt(count*tick_sumsq - tick_sum**2) | 
					
						
							| 
									
										
										
										
											2017-07-12 22:16:16 -04:00
										 |  |  |         self._mcu_tick_awake = tick_sum / self._mcu_freq | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     def handle_shutdown(self, params): | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |         if self._is_shutdown: | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |         self._is_shutdown = True | 
					
						
							| 
									
										
										
										
											2017-09-05 21:15:24 -04:00
										 |  |  |         self._shutdown_msg = msg = params['#msg'] | 
					
						
							| 
									
										
										
										
											2017-09-27 13:13:38 -04:00
										 |  |  |         logging.info("MCU '%s' %s: %s\n%s\n%s", self._name, params['#name'], | 
					
						
							|  |  |  |                      self._shutdown_msg, self._clocksync.dump_debug(), | 
					
						
							|  |  |  |                      self._serial.dump_debug()) | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         prefix = "MCU '%s' shutdown: " % (self._name,) | 
					
						
							| 
									
										
										
										
											2017-09-05 21:15:24 -04:00
										 |  |  |         if params['#name'] == 'is_shutdown': | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |             prefix = "Previous MCU '%s' shutdown: " % (self._name,) | 
					
						
							| 
									
										
										
										
											2017-10-12 15:15:14 -04:00
										 |  |  |         self._printer.invoke_async_shutdown(prefix + msg + error_help(msg)) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     # Connection phase | 
					
						
							| 
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 |  |  |     def _check_restart(self, reason): | 
					
						
							| 
									
										
										
										
											2017-08-21 17:19:43 -04:00
										 |  |  |         start_reason = self._printer.get_start_args().get("start_reason") | 
					
						
							|  |  |  |         if start_reason == 'firmware_restart': | 
					
						
							| 
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         logging.info("Attempting automated MCU '%s' restart: %s", | 
					
						
							|  |  |  |                      self._name, reason) | 
					
						
							| 
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 |  |  |         self._printer.request_exit('firmware_restart') | 
					
						
							|  |  |  |         self._printer.reactor.pause(self._printer.reactor.monotonic() + 2.000) | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         raise error("Attempt MCU '%s' restart failed" % (self._name,)) | 
					
						
							| 
									
										
										
										
											2017-08-21 17:19:43 -04:00
										 |  |  |     def _connect_file(self, pace=False): | 
					
						
							|  |  |  |         # In a debugging mode.  Open debug output file and read data dictionary | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         start_args = self._printer.get_start_args() | 
					
						
							|  |  |  |         if self._name == 'mcu': | 
					
						
							|  |  |  |             out_fname = start_args.get('debugoutput') | 
					
						
							|  |  |  |             dict_fname = start_args.get('dictionary') | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             out_fname = start_args.get('debugoutput') + "-" + self._name | 
					
						
							|  |  |  |             dict_fname = start_args.get('dictionary_' + self._name) | 
					
						
							| 
									
										
										
										
											2017-08-21 17:19:43 -04:00
										 |  |  |         outfile = open(out_fname, 'wb') | 
					
						
							|  |  |  |         dfile = open(dict_fname, 'rb') | 
					
						
							|  |  |  |         dict_data = dfile.read() | 
					
						
							|  |  |  |         dfile.close() | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |         self._serial.connect_file(outfile, dict_data) | 
					
						
							|  |  |  |         self._clocksync.connect_file(self._serial, pace) | 
					
						
							| 
									
										
										
										
											2017-08-21 17:19:43 -04:00
										 |  |  |         # Handle pacing | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         if not pace: | 
					
						
							| 
									
										
										
										
											2017-09-12 12:24:44 -04:00
										 |  |  |             def dummy_estimated_print_time(eventtime): | 
					
						
							|  |  |  |                 return 0. | 
					
						
							|  |  |  |             self.estimated_print_time = dummy_estimated_print_time | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     def _add_custom(self): | 
					
						
							| 
									
										
										
										
											2017-03-12 22:43:05 -04:00
										 |  |  |         for line in self._custom.split('\n'): | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |             line = line.strip() | 
					
						
							|  |  |  |             cpos = line.find('#') | 
					
						
							|  |  |  |             if cpos >= 0: | 
					
						
							|  |  |  |                 line = line[:cpos].strip() | 
					
						
							|  |  |  |             if not line: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             self.add_config_cmd(line) | 
					
						
							| 
									
										
										
										
											2017-03-12 22:43:05 -04:00
										 |  |  |     def _build_config(self): | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         # Build config commands | 
					
						
							| 
									
										
										
										
											2017-08-23 18:55:04 -04:00
										 |  |  |         for co in self._config_objects: | 
					
						
							|  |  |  |             co.build_config() | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |         self._add_custom() | 
					
						
							|  |  |  |         self._config_cmds.insert(0, "allocate_oids count=%d" % ( | 
					
						
							| 
									
										
										
										
											2017-08-23 18:55:04 -04:00
										 |  |  |             self._oid_count,)) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Resolve pin names | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |         mcu = self._serial.msgparser.get_constant('MCU') | 
					
						
							| 
									
										
										
										
											2017-03-12 23:05:01 -04:00
										 |  |  |         pnames = pins.get_pin_map(mcu, self._pin_map) | 
					
						
							| 
									
										
										
										
											2016-11-30 15:05:26 -05:00
										 |  |  |         updated_cmds = [] | 
					
						
							|  |  |  |         for cmd in self._config_cmds: | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2017-09-12 13:54:53 -04:00
										 |  |  |                 updated_cmds.append(pins.update_command(cmd, pnames)) | 
					
						
							| 
									
										
										
										
											2016-11-30 15:05:26 -05:00
										 |  |  |             except: | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |                 raise pins.error("Unable to translate pin name: %s" % (cmd,)) | 
					
						
							| 
									
										
										
										
											2016-11-30 15:05:26 -05:00
										 |  |  |         self._config_cmds = updated_cmds | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Calculate config CRC | 
					
						
							|  |  |  |         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") | 
					
						
							| 
									
										
										
										
											2017-08-21 17:19:43 -04:00
										 |  |  |         if self.is_fileoutput(): | 
					
						
							| 
									
										
										
										
											2016-11-28 14:52:22 -05:00
										 |  |  |             config_params = { | 
					
						
							|  |  |  |                 'is_config': 0, 'move_count': 500, 'crc': self._config_crc} | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |             config_params = self.send_with_response(msg, 'config') | 
					
						
							| 
									
										
										
										
											2016-11-28 14:52:22 -05:00
										 |  |  |         if not config_params['is_config']: | 
					
						
							| 
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 |  |  |             if self._restart_method == 'rpi_usb': | 
					
						
							|  |  |  |                 # Only configure mcu after usb power reset | 
					
						
							|  |  |  |                 self._check_restart("full reset before config") | 
					
						
							| 
									
										
										
										
											2016-11-28 14:52:22 -05:00
										 |  |  |             # Send config commands | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |             logging.info("Sending MCU '%s' printer configuration...", | 
					
						
							|  |  |  |                          self._name) | 
					
						
							| 
									
										
										
										
											2016-11-28 14:52:22 -05:00
										 |  |  |             for c in self._config_cmds: | 
					
						
							|  |  |  |                 self.send(self.create_command(c)) | 
					
						
							| 
									
										
										
										
											2017-08-21 17:19:43 -04:00
										 |  |  |             if not self.is_fileoutput(): | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |                 config_params = self.send_with_response(msg, 'config') | 
					
						
							| 
									
										
										
										
											2017-03-08 21:42:51 -05:00
										 |  |  |                 if not config_params['is_config']: | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |                     if self._is_shutdown: | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |                         raise error("MCU '%s' error during config: %s" % ( | 
					
						
							|  |  |  |                             self._name, self._shutdown_msg)) | 
					
						
							|  |  |  |                     raise error("Unable to configure MCU '%s'" % (self._name,)) | 
					
						
							| 
									
										
										
										
											2017-08-21 17:19:43 -04:00
										 |  |  |         else: | 
					
						
							|  |  |  |             start_reason = self._printer.get_start_args().get("start_reason") | 
					
						
							|  |  |  |             if start_reason == 'firmware_restart': | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |                 raise error("Failed automated reset of MCU '%s'" % (self._name,)) | 
					
						
							| 
									
										
										
										
											2016-11-28 14:52:22 -05:00
										 |  |  |         if self._config_crc != config_params['crc']: | 
					
						
							| 
									
										
										
										
											2017-04-13 12:09:37 -04:00
										 |  |  |             self._check_restart("CRC mismatch") | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |             raise error("MCU '%s' CRC does not match config" % (self._name,)) | 
					
						
							| 
									
										
										
										
											2016-12-24 12:33:56 -05:00
										 |  |  |         move_count = config_params['move_count'] | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         logging.info("Configured MCU '%s' (%d moves)", self._name, move_count) | 
					
						
							| 
									
										
										
										
											2017-05-01 13:44:06 -04:00
										 |  |  |         if self._printer.bglogger is not None: | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |             msgparser = self._serial.msgparser | 
					
						
							| 
									
										
										
										
											2017-05-01 13:44:06 -04:00
										 |  |  |             info = [ | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |                 "Configured MCU '%s' (%d moves)" % (self._name, move_count), | 
					
						
							| 
									
										
										
										
											2017-12-21 18:18:18 -05:00
										 |  |  |                 "Loaded MCU '%s' %d commands (%s / %s)" % ( | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |                     self._name, len(msgparser.messages_by_id), | 
					
						
							| 
									
										
										
										
											2017-12-21 18:18:18 -05:00
										 |  |  |                     msgparser.version, msgparser.build_versions), | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |                 "MCU '%s' config: %s" % (self._name, " ".join( | 
					
						
							| 
									
										
										
										
											2017-05-01 13:44:06 -04:00
										 |  |  |                     ["%s=%s" % (k, v) for k, v in msgparser.config.items()]))] | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |             self._printer.bglogger.set_rollover_info(self._name, "\n".join(info)) | 
					
						
							| 
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 |  |  |         self._steppersync = self._ffi_lib.steppersync_alloc( | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |             self._serial.serialqueue, self._stepqueues, len(self._stepqueues), | 
					
						
							| 
									
										
										
										
											2017-08-23 18:59:25 -04:00
										 |  |  |             move_count) | 
					
						
							| 
									
										
										
										
											2017-09-13 08:59:26 -04:00
										 |  |  |         self._ffi_lib.steppersync_set_time(self._steppersync, 0., self._mcu_freq) | 
					
						
							| 
									
										
										
										
											2017-08-21 21:42:00 -04:00
										 |  |  |         for c in self._init_cmds: | 
					
						
							|  |  |  |             self.send(self.create_command(c)) | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |     def connect(self): | 
					
						
							|  |  |  |         if self.is_fileoutput(): | 
					
						
							|  |  |  |             self._connect_file() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             if (self._restart_method == 'rpi_usb' | 
					
						
							|  |  |  |                 and not os.path.exists(self._serialport)): | 
					
						
							|  |  |  |                 # Try toggling usb power | 
					
						
							|  |  |  |                 self._check_restart("enable power") | 
					
						
							|  |  |  |             self._serial.connect() | 
					
						
							|  |  |  |             self._clocksync.connect(self._serial) | 
					
						
							|  |  |  |         self._mcu_freq = self.get_constant_float('CLOCK_FREQ') | 
					
						
							|  |  |  |         self._stats_sumsq_base = self.get_constant_float('STATS_SUMSQ_BASE') | 
					
						
							|  |  |  |         self._emergency_stop_cmd = self.lookup_command("emergency_stop") | 
					
						
							|  |  |  |         self._reset_cmd = self.try_lookup_command("reset") | 
					
						
							|  |  |  |         self._config_reset_cmd = self.try_lookup_command("config_reset") | 
					
						
							|  |  |  |         self.register_msg(self.handle_shutdown, 'shutdown') | 
					
						
							|  |  |  |         self.register_msg(self.handle_shutdown, 'is_shutdown') | 
					
						
							|  |  |  |         self.register_msg(self.handle_mcu_stats, 'stats') | 
					
						
							|  |  |  |         self._build_config() | 
					
						
							|  |  |  |         self._send_config() | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     # Config creation helpers | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  |     def setup_pin(self, pin_params): | 
					
						
							|  |  |  |         pcs = {'stepper': MCU_stepper, 'endstop': MCU_endstop, | 
					
						
							|  |  |  |                'digital_out': MCU_digital_out, 'pwm': MCU_pwm, 'adc': MCU_adc} | 
					
						
							|  |  |  |         pin_type = pin_params['type'] | 
					
						
							|  |  |  |         if pin_type not in pcs: | 
					
						
							|  |  |  |             raise pins.error("pin type %s not supported on mcu" % (pin_type,)) | 
					
						
							| 
									
										
										
										
											2017-08-23 18:55:04 -04:00
										 |  |  |         co = pcs[pin_type](self, pin_params) | 
					
						
							|  |  |  |         self.add_config_object(co) | 
					
						
							|  |  |  |         return co | 
					
						
							|  |  |  |     def create_oid(self): | 
					
						
							|  |  |  |         self._oid_count += 1 | 
					
						
							|  |  |  |         return self._oid_count - 1 | 
					
						
							|  |  |  |     def add_config_object(self, co): | 
					
						
							|  |  |  |         self._config_objects.append(co) | 
					
						
							| 
									
										
										
										
											2017-08-21 21:42:00 -04:00
										 |  |  |     def add_config_cmd(self, cmd, is_init=False): | 
					
						
							|  |  |  |         if is_init: | 
					
						
							|  |  |  |             self._init_cmds.append(cmd) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self._config_cmds.append(cmd) | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |     def get_query_slot(self, oid): | 
					
						
							|  |  |  |         slot = self.seconds_to_clock(oid * .01) | 
					
						
							|  |  |  |         t = int(self.estimated_print_time(self.monotonic()) + 1.5) | 
					
						
							|  |  |  |         return self.print_time_to_clock(t) + slot | 
					
						
							| 
									
										
										
										
											2017-08-23 18:59:25 -04:00
										 |  |  |     def register_stepqueue(self, stepqueue): | 
					
						
							|  |  |  |         self._stepqueues.append(stepqueue) | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |     def seconds_to_clock(self, time): | 
					
						
							|  |  |  |         return int(time * self._mcu_freq) | 
					
						
							|  |  |  |     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) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     def alloc_command_queue(self): | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |         return self._serial.alloc_command_queue() | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |     def create_command(self, msg): | 
					
						
							|  |  |  |         return self._serial.msgparser.create_command(msg) | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     def lookup_command(self, msgformat): | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |         return self._serial.msgparser.lookup_command(msgformat) | 
					
						
							| 
									
										
										
										
											2017-09-03 22:02:48 -04:00
										 |  |  |     def try_lookup_command(self, msgformat): | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |             return self._serial.msgparser.lookup_command(msgformat) | 
					
						
							|  |  |  |         except self._serial.msgparser.error as e: | 
					
						
							| 
									
										
										
										
											2017-09-03 22:02:48 -04:00
										 |  |  |             return None | 
					
						
							| 
									
										
										
										
											2017-09-19 11:13:44 -04:00
										 |  |  |     def get_constant_float(self, name): | 
					
						
							|  |  |  |         return self._serial.msgparser.get_constant_float(name) | 
					
						
							| 
									
										
										
										
											2017-09-12 12:24:44 -04:00
										 |  |  |     def print_time_to_clock(self, print_time): | 
					
						
							| 
									
										
										
										
											2017-09-15 12:28:55 -04:00
										 |  |  |         return self._clocksync.print_time_to_clock(print_time) | 
					
						
							| 
									
										
										
										
											2017-09-12 12:24:44 -04:00
										 |  |  |     def clock_to_print_time(self, clock): | 
					
						
							| 
									
										
										
										
											2017-09-15 12:28:55 -04:00
										 |  |  |         return self._clocksync.clock_to_print_time(clock) | 
					
						
							| 
									
										
										
										
											2017-09-12 12:24:44 -04:00
										 |  |  |     def estimated_print_time(self, eventtime): | 
					
						
							| 
									
										
										
										
											2017-09-15 12:28:55 -04:00
										 |  |  |         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) | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |     def pause(self, waketime): | 
					
						
							|  |  |  |         return self._printer.reactor.pause(waketime) | 
					
						
							|  |  |  |     def monotonic(self): | 
					
						
							|  |  |  |         return self._printer.reactor.monotonic() | 
					
						
							| 
									
										
										
										
											2017-09-20 16:02:43 -04:00
										 |  |  |     # Restarts | 
					
						
							|  |  |  |     def _restart_arduino(self): | 
					
						
							|  |  |  |         logging.info("Attempting MCU '%s' reset", self._name) | 
					
						
							|  |  |  |         self.disconnect() | 
					
						
							|  |  |  |         serialhdl.arduino_reset(self._serialport, self._printer.reactor) | 
					
						
							|  |  |  |     def _restart_via_command(self): | 
					
						
							|  |  |  |         reactor = self._printer.reactor | 
					
						
							|  |  |  |         if ((self._reset_cmd is None and self._config_reset_cmd is None) | 
					
						
							|  |  |  |             or not self._clocksync.is_active(reactor.monotonic())): | 
					
						
							|  |  |  |             logging.info("Unable to issue reset command on MCU '%s'", self._name) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         if self._reset_cmd is None: | 
					
						
							|  |  |  |             # Attempt reset via config_reset command | 
					
						
							|  |  |  |             logging.info("Attempting MCU '%s' config_reset command", self._name) | 
					
						
							|  |  |  |             self._is_shutdown = True | 
					
						
							| 
									
										
										
										
											2017-10-12 21:29:54 -04:00
										 |  |  |             self.do_shutdown(force=True) | 
					
						
							| 
									
										
										
										
											2017-09-20 16:02:43 -04:00
										 |  |  |             reactor.pause(reactor.monotonic() + 0.015) | 
					
						
							|  |  |  |             self.send(self._config_reset_cmd.encode()) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # Attempt reset via reset command | 
					
						
							|  |  |  |             logging.info("Attempting MCU '%s' reset command", self._name) | 
					
						
							|  |  |  |             self.send(self._reset_cmd.encode()) | 
					
						
							|  |  |  |         reactor.pause(reactor.monotonic() + 0.015) | 
					
						
							|  |  |  |         self.disconnect() | 
					
						
							|  |  |  |     def _restart_rpi_usb(self): | 
					
						
							|  |  |  |         logging.info("Attempting MCU '%s' reset via rpi usb power", self._name) | 
					
						
							|  |  |  |         self.disconnect() | 
					
						
							|  |  |  |         chelper.run_hub_ctrl(0) | 
					
						
							|  |  |  |         self._printer.reactor.pause(self._printer.reactor.monotonic() + 2.) | 
					
						
							|  |  |  |         chelper.run_hub_ctrl(1) | 
					
						
							|  |  |  |     def microcontroller_restart(self): | 
					
						
							|  |  |  |         if self._restart_method == 'rpi_usb': | 
					
						
							|  |  |  |             self._restart_rpi_usb() | 
					
						
							|  |  |  |         elif self._restart_method == 'command': | 
					
						
							|  |  |  |             self._restart_via_command() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self._restart_arduino() | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |     # Misc external commands | 
					
						
							|  |  |  |     def is_fileoutput(self): | 
					
						
							|  |  |  |         return self._printer.get_start_args().get('debugoutput') is not None | 
					
						
							|  |  |  |     def is_shutdown(self): | 
					
						
							|  |  |  |         return self._is_shutdown | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  |     def flush_moves(self, print_time): | 
					
						
							| 
									
										
										
										
											2016-12-28 22:17:49 -05:00
										 |  |  |         if self._steppersync is None: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-09-12 12:24:44 -04:00
										 |  |  |         clock = self.print_time_to_clock(print_time) | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         if clock < 0: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-04-04 10:13:02 -04:00
										 |  |  |         ret = self._ffi_lib.steppersync_flush(self._steppersync, clock) | 
					
						
							| 
									
										
										
										
											2017-02-06 11:37:03 -05:00
										 |  |  |         if ret: | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |             raise error("Internal error in MCU '%s' stepcompress" % ( | 
					
						
							|  |  |  |                 self._name,)) | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |     def check_active(self, print_time, eventtime): | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         if self._steppersync is None: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         offset, freq = self._clocksync.calibrate_clock(print_time, eventtime) | 
					
						
							|  |  |  |         self._ffi_lib.steppersync_set_time(self._steppersync, offset, freq) | 
					
						
							| 
									
										
										
										
											2018-01-08 10:55:37 -05:00
										 |  |  |         if (self._clocksync.is_active(eventtime) or self.is_fileoutput() | 
					
						
							|  |  |  |             or self._is_timeout): | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2018-01-08 10:55:37 -05:00
										 |  |  |         self._is_timeout = True | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         logging.info("Timeout with MCU '%s' (eventtime=%f)", | 
					
						
							|  |  |  |                      self._name, eventtime) | 
					
						
							| 
									
										
										
										
											2017-10-12 15:15:14 -04:00
										 |  |  |         self._printer.invoke_shutdown("Lost communication with MCU '%s'" % ( | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |             self._name,)) | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |     def stats(self, eventtime): | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |         msg = "%s: mcu_awake=%.03f mcu_task_avg=%.06f mcu_task_stddev=%.06f" % ( | 
					
						
							|  |  |  |             self._name, self._mcu_tick_awake, self._mcu_tick_avg, | 
					
						
							|  |  |  |             self._mcu_tick_stddev) | 
					
						
							|  |  |  |         return ' '.join([msg, self._serial.stats(eventtime), | 
					
						
							|  |  |  |                          self._clocksync.stats(eventtime)]) | 
					
						
							| 
									
										
										
										
											2017-10-12 21:29:54 -04:00
										 |  |  |     def do_shutdown(self, force=False): | 
					
						
							|  |  |  |         if self._emergency_stop_cmd is None or (self._is_shutdown and not force): | 
					
						
							| 
									
										
										
										
											2017-10-12 15:15:14 -04:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-09-19 12:10:06 -04:00
										 |  |  |         self.send(self._emergency_stop_cmd.encode()) | 
					
						
							|  |  |  |     def disconnect(self): | 
					
						
							|  |  |  |         self._serial.disconnect() | 
					
						
							|  |  |  |         if self._steppersync is not None: | 
					
						
							|  |  |  |             self._ffi_lib.steppersync_free(self._steppersync) | 
					
						
							|  |  |  |             self._steppersync = None | 
					
						
							| 
									
										
										
										
											2016-11-30 01:58:45 -05:00
										 |  |  |     def __del__(self): | 
					
						
							|  |  |  |         self.disconnect() | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-05 21:15:24 -04:00
										 |  |  | Common_MCU_errors = { | 
					
						
							|  |  |  |     ("Timer too close", "No next step", "Missed scheduling of next "): """
 | 
					
						
							|  |  |  | This is generally indicative of an intermittent | 
					
						
							| 
									
										
										
										
											2017-09-05 23:07:45 -04:00
										 |  |  | communication failure between micro-controller and host.""",
 | 
					
						
							| 
									
										
										
										
											2017-09-05 21:15:24 -04:00
										 |  |  |     ("ADC out of range",): """
 | 
					
						
							|  |  |  | This generally occurs when a heater temperature exceeds | 
					
						
							| 
									
										
										
										
											2017-09-05 23:07:45 -04:00
										 |  |  | its configured min_temp or max_temp.""",
 | 
					
						
							| 
									
										
										
										
											2017-09-05 21:15:24 -04:00
										 |  |  |     ("Rescheduled timer in the past", "Stepper too far in past"): """
 | 
					
						
							|  |  |  | This generally occurs when the micro-controller has been | 
					
						
							|  |  |  | requested to step at a rate higher than it is capable of | 
					
						
							|  |  |  | obtaining.""",
 | 
					
						
							|  |  |  |     ("Command request",): """
 | 
					
						
							|  |  |  | This generally occurs in response to an M112 G-Code command | 
					
						
							|  |  |  | or in response to an internal error in the host software.""",
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def error_help(msg): | 
					
						
							|  |  |  |     for prefixes, help_msg in Common_MCU_errors.items(): | 
					
						
							|  |  |  |         for prefix in prefixes: | 
					
						
							|  |  |  |             if msg.startswith(prefix): | 
					
						
							|  |  |  |                 return help_msg | 
					
						
							|  |  |  |     return "" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 11:25:26 -04:00
										 |  |  | def add_printer_objects(printer, config): | 
					
						
							| 
									
										
										
										
											2017-09-18 21:41:00 -04:00
										 |  |  |     mainsync = clocksync.ClockSync(printer.reactor) | 
					
						
							|  |  |  |     printer.add_object('mcu', MCU(printer, config.getsection('mcu'), mainsync)) | 
					
						
							| 
									
										
										
										
											2017-08-14 11:46:35 -04:00
										 |  |  |     for s in config.get_prefix_sections('mcu '): | 
					
						
							|  |  |  |         printer.add_object(s.section, MCU( | 
					
						
							|  |  |  |             printer, s, clocksync.SecondarySync(printer.reactor, mainsync))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_printer_mcus(printer): | 
					
						
							|  |  |  |     return [printer.objects[n] for n in sorted(printer.objects) | 
					
						
							|  |  |  |             if n.startswith('mcu')] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_printer_mcu(printer, name): | 
					
						
							|  |  |  |     mcu_name = name | 
					
						
							|  |  |  |     if name != 'mcu': | 
					
						
							|  |  |  |         mcu_name = 'mcu ' + name | 
					
						
							|  |  |  |     if mcu_name not in printer.objects: | 
					
						
							|  |  |  |         raise printer.config_error("Unknown MCU %s" % (name,)) | 
					
						
							|  |  |  |     return printer.objects[mcu_name] |