| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | # Printer stepper support | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2021-02-04 16:33:03 -05:00
										 |  |  | # Copyright (C) 2016-2021  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. | 
					
						
							| 
									
										
										
										
											2018-06-21 18:48:33 -04:00
										 |  |  | import math, logging, collections | 
					
						
							| 
									
										
										
										
											2020-09-04 11:49:43 -04:00
										 |  |  | import chelper | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | class error(Exception): | 
					
						
							|  |  |  |     pass | 
					
						
							| 
									
										
										
										
											2016-05-25 11:37:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 11:44:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ###################################################################### | 
					
						
							|  |  |  | # Steppers | 
					
						
							|  |  |  | ###################################################################### | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 16:41:48 -05:00
										 |  |  | MIN_BOTH_EDGE_DURATION = 0.000000200 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-12 14:12:33 -05:00
										 |  |  | # Interface to low-level mcu and chelper code | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  | class MCU_stepper: | 
					
						
							| 
									
										
										
										
											2022-01-29 14:13:26 -05:00
										 |  |  |     def __init__(self, name, step_pin_params, dir_pin_params, | 
					
						
							|  |  |  |                  rotation_dist, steps_per_rotation, | 
					
						
							| 
									
										
										
										
											2021-10-27 19:10:36 -04:00
										 |  |  |                  step_pulse_duration=None, units_in_radians=False): | 
					
						
							| 
									
										
										
										
											2019-11-12 14:12:33 -05:00
										 |  |  |         self._name = name | 
					
						
							| 
									
										
										
										
											2022-01-29 14:13:26 -05:00
										 |  |  |         self._rotation_dist = rotation_dist | 
					
						
							|  |  |  |         self._steps_per_rotation = steps_per_rotation | 
					
						
							| 
									
										
										
										
											2021-10-27 19:10:36 -04:00
										 |  |  |         self._step_pulse_duration = step_pulse_duration | 
					
						
							| 
									
										
										
										
											2019-12-04 18:42:59 -05:00
										 |  |  |         self._units_in_radians = units_in_radians | 
					
						
							| 
									
										
										
										
											2022-01-29 14:13:26 -05:00
										 |  |  |         self._step_dist = rotation_dist / steps_per_rotation | 
					
						
							| 
									
										
										
										
											2019-11-12 14:12:33 -05:00
										 |  |  |         self._mcu = step_pin_params['chip'] | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         self._oid = oid = self._mcu.create_oid() | 
					
						
							|  |  |  |         self._mcu.register_config_callback(self._build_config) | 
					
						
							| 
									
										
										
										
											2019-11-12 14:12:33 -05:00
										 |  |  |         self._step_pin = step_pin_params['pin'] | 
					
						
							|  |  |  |         self._invert_step = step_pin_params['invert'] | 
					
						
							|  |  |  |         if dir_pin_params['chip'] is not self._mcu: | 
					
						
							|  |  |  |             raise self._mcu.get_printer().config_error( | 
					
						
							|  |  |  |                 "Stepper dir pin must be on same mcu as step pin") | 
					
						
							|  |  |  |         self._dir_pin = dir_pin_params['pin'] | 
					
						
							| 
									
										
										
										
											2022-01-29 19:13:31 -05:00
										 |  |  |         self._invert_dir = self._orig_invert_dir = dir_pin_params['invert'] | 
					
						
							| 
									
										
										
										
											2021-10-28 17:10:10 -04:00
										 |  |  |         self._step_both_edge = self._req_step_both_edge = False | 
					
						
							| 
									
										
										
										
											2021-05-01 00:27:43 -04:00
										 |  |  |         self._mcu_position_offset = 0. | 
					
						
							| 
									
										
										
										
											2021-02-04 16:33:03 -05:00
										 |  |  |         self._reset_cmd_tag = self._get_position_cmd = None | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         self._active_callbacks = [] | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							|  |  |  |         self._stepqueue = ffi_main.gc(ffi_lib.stepcompress_alloc(oid), | 
					
						
							|  |  |  |                                       ffi_lib.stepcompress_free) | 
					
						
							| 
									
										
										
										
											2022-01-29 19:13:31 -05:00
										 |  |  |         ffi_lib.stepcompress_set_invert_sdir(self._stepqueue, self._invert_dir) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         self._mcu.register_stepqueue(self._stepqueue) | 
					
						
							|  |  |  |         self._stepper_kinematics = None | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         self._itersolve_generate_steps = ffi_lib.itersolve_generate_steps | 
					
						
							|  |  |  |         self._itersolve_check_active = ffi_lib.itersolve_check_active | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         self._trapq = ffi_main.NULL | 
					
						
							| 
									
										
										
										
											2021-04-02 12:18:05 -04:00
										 |  |  |         self._mcu.get_printer().register_event_handler('klippy:connect', | 
					
						
							|  |  |  |                                                        self._query_mcu_position) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |     def get_mcu(self): | 
					
						
							|  |  |  |         return self._mcu | 
					
						
							| 
									
										
										
										
											2019-11-12 14:12:33 -05:00
										 |  |  |     def get_name(self, short=False): | 
					
						
							|  |  |  |         if short and self._name.startswith('stepper_'): | 
					
						
							|  |  |  |             return self._name[8:] | 
					
						
							|  |  |  |         return self._name | 
					
						
							| 
									
										
										
										
											2019-12-04 18:42:59 -05:00
										 |  |  |     def units_in_radians(self): | 
					
						
							|  |  |  |         # Returns true if distances are in radians instead of millimeters | 
					
						
							|  |  |  |         return self._units_in_radians | 
					
						
							| 
									
										
										
										
											2021-10-28 17:10:10 -04:00
										 |  |  |     def get_pulse_duration(self): | 
					
						
							|  |  |  |         return self._step_pulse_duration, self._step_both_edge | 
					
						
							|  |  |  |     def setup_default_pulse_duration(self, pulse_duration, step_both_edge): | 
					
						
							|  |  |  |         if self._step_pulse_duration is None: | 
					
						
							|  |  |  |             self._step_pulse_duration = pulse_duration | 
					
						
							|  |  |  |         self._req_step_both_edge = step_both_edge | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |     def setup_itersolve(self, alloc_func, *params): | 
					
						
							|  |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							|  |  |  |         sk = ffi_main.gc(getattr(ffi_lib, alloc_func)(*params), ffi_lib.free) | 
					
						
							|  |  |  |         self.set_stepper_kinematics(sk) | 
					
						
							|  |  |  |     def _build_config(self): | 
					
						
							| 
									
										
										
										
											2021-10-27 19:10:36 -04:00
										 |  |  |         if self._step_pulse_duration is None: | 
					
						
							|  |  |  |             self._step_pulse_duration = .000002 | 
					
						
							| 
									
										
										
										
											2021-10-28 17:10:10 -04:00
										 |  |  |         invert_step = self._invert_step | 
					
						
							|  |  |  |         sbe = int(self._mcu.get_constants().get('STEPPER_BOTH_EDGE', '0')) | 
					
						
							| 
									
										
										
										
											2021-11-29 16:41:48 -05:00
										 |  |  |         if (self._req_step_both_edge and sbe | 
					
						
							|  |  |  |             and self._step_pulse_duration <= MIN_BOTH_EDGE_DURATION): | 
					
						
							| 
									
										
										
										
											2021-10-28 17:10:10 -04:00
										 |  |  |             # Enable stepper optimized step on both edges | 
					
						
							|  |  |  |             self._step_both_edge = True | 
					
						
							|  |  |  |             self._step_pulse_duration = 0. | 
					
						
							|  |  |  |             invert_step = -1 | 
					
						
							| 
									
										
										
										
											2021-10-27 19:10:36 -04:00
										 |  |  |         step_pulse_ticks = self._mcu.seconds_to_clock(self._step_pulse_duration) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         self._mcu.add_config_cmd( | 
					
						
							| 
									
										
										
										
											2021-07-03 21:27:31 -04:00
										 |  |  |             "config_stepper oid=%d step_pin=%s dir_pin=%s invert_step=%d" | 
					
						
							| 
									
										
										
										
											2021-10-27 19:10:36 -04:00
										 |  |  |             " step_pulse_ticks=%u" % (self._oid, self._step_pin, self._dir_pin, | 
					
						
							| 
									
										
										
										
											2021-10-28 17:10:10 -04:00
										 |  |  |                                       invert_step, step_pulse_ticks)) | 
					
						
							| 
									
										
										
										
											2020-07-19 12:26:31 -04:00
										 |  |  |         self._mcu.add_config_cmd("reset_step_clock oid=%d clock=0" | 
					
						
							|  |  |  |                                  % (self._oid,), on_restart=True) | 
					
						
							| 
									
										
										
										
											2021-02-04 16:33:03 -05:00
										 |  |  |         step_cmd_tag = self._mcu.lookup_command_tag( | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |             "queue_step oid=%c interval=%u count=%hu add=%hi") | 
					
						
							| 
									
										
										
										
											2021-02-04 16:33:03 -05:00
										 |  |  |         dir_cmd_tag = self._mcu.lookup_command_tag( | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |             "set_next_step_dir oid=%c dir=%c") | 
					
						
							| 
									
										
										
										
											2021-02-04 16:33:03 -05:00
										 |  |  |         self._reset_cmd_tag = self._mcu.lookup_command_tag( | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |             "reset_step_clock oid=%c clock=%u") | 
					
						
							| 
									
										
										
										
											2020-02-19 16:46:06 -05:00
										 |  |  |         self._get_position_cmd = self._mcu.lookup_query_command( | 
					
						
							|  |  |  |             "stepper_get_position oid=%c", | 
					
						
							|  |  |  |             "stepper_position oid=%c pos=%i", oid=self._oid) | 
					
						
							| 
									
										
										
										
											2021-04-25 14:53:50 -04:00
										 |  |  |         max_error = self._mcu.get_max_stepper_error() | 
					
						
							| 
									
										
										
										
											2021-10-27 19:10:36 -04:00
										 |  |  |         max_error_ticks = self._mcu.seconds_to_clock(max_error) | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							| 
									
										
										
										
											2021-10-27 19:10:36 -04:00
										 |  |  |         ffi_lib.stepcompress_fill(self._stepqueue, max_error_ticks, | 
					
						
							| 
									
										
										
										
											2022-01-29 19:13:31 -05:00
										 |  |  |                                   step_cmd_tag, dir_cmd_tag) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |     def get_oid(self): | 
					
						
							|  |  |  |         return self._oid | 
					
						
							|  |  |  |     def get_step_dist(self): | 
					
						
							|  |  |  |         return self._step_dist | 
					
						
							| 
									
										
										
										
											2022-01-29 14:13:26 -05:00
										 |  |  |     def get_rotation_distance(self): | 
					
						
							|  |  |  |         return self._rotation_dist, self._steps_per_rotation | 
					
						
							|  |  |  |     def set_rotation_distance(self, rotation_dist): | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |         mcu_pos = self.get_mcu_position() | 
					
						
							| 
									
										
										
										
											2022-01-29 14:13:26 -05:00
										 |  |  |         self._rotation_dist = rotation_dist | 
					
						
							|  |  |  |         self._step_dist = rotation_dist / self._steps_per_rotation | 
					
						
							| 
									
										
										
										
											2020-03-28 09:56:07 -04:00
										 |  |  |         self.set_stepper_kinematics(self._stepper_kinematics) | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |         self._set_mcu_position(mcu_pos) | 
					
						
							| 
									
										
										
										
											2022-01-29 19:13:31 -05:00
										 |  |  |     def get_dir_inverted(self): | 
					
						
							|  |  |  |         return self._invert_dir, self._orig_invert_dir | 
					
						
							|  |  |  |     def set_dir_inverted(self, invert_dir): | 
					
						
							|  |  |  |         invert_dir = not not invert_dir | 
					
						
							|  |  |  |         if invert_dir == self._invert_dir: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         self._invert_dir = invert_dir | 
					
						
							|  |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							|  |  |  |         ffi_lib.stepcompress_set_invert_sdir(self._stepqueue, invert_dir) | 
					
						
							|  |  |  |         self._mcu.get_printer().send_event("stepper:set_dir_inverted", self) | 
					
						
							| 
									
										
										
										
											2019-11-19 12:27:33 -05:00
										 |  |  |     def calc_position_from_coord(self, coord): | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							|  |  |  |         return ffi_lib.itersolve_calc_position_from_coord( | 
					
						
							| 
									
										
										
										
											2019-11-19 12:27:33 -05:00
										 |  |  |             self._stepper_kinematics, coord[0], coord[1], coord[2]) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |     def set_position(self, coord): | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |         mcu_pos = self.get_mcu_position() | 
					
						
							| 
									
										
										
										
											2019-11-13 18:58:51 -05:00
										 |  |  |         sk = self._stepper_kinematics | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							|  |  |  |         ffi_lib.itersolve_set_position(sk, coord[0], coord[1], coord[2]) | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |         self._set_mcu_position(mcu_pos) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |     def get_commanded_position(self): | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |         return ffi_lib.itersolve_get_commanded_pos(self._stepper_kinematics) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |     def get_mcu_position(self): | 
					
						
							|  |  |  |         mcu_pos_dist = self.get_commanded_position() + self._mcu_position_offset | 
					
						
							|  |  |  |         mcu_pos = mcu_pos_dist / self._step_dist | 
					
						
							|  |  |  |         if mcu_pos >= 0.: | 
					
						
							|  |  |  |             return int(mcu_pos + 0.5) | 
					
						
							|  |  |  |         return int(mcu_pos - 0.5) | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |     def _set_mcu_position(self, mcu_pos): | 
					
						
							|  |  |  |         mcu_pos_dist = mcu_pos * self._step_dist | 
					
						
							|  |  |  |         self._mcu_position_offset = mcu_pos_dist - self.get_commanded_position() | 
					
						
							| 
									
										
										
										
											2021-05-29 21:29:00 -04:00
										 |  |  |     def get_past_mcu_position(self, print_time): | 
					
						
							|  |  |  |         clock = self._mcu.print_time_to_clock(print_time) | 
					
						
							| 
									
										
										
										
											2021-02-22 17:57:36 -05:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							| 
									
										
										
										
											2021-04-05 10:59:47 -04:00
										 |  |  |         pos = ffi_lib.stepcompress_find_past_position(self._stepqueue, clock) | 
					
						
							|  |  |  |         return int(pos) | 
					
						
							| 
									
										
										
										
											2021-07-31 20:23:48 -04:00
										 |  |  |     def mcu_to_commanded_position(self, mcu_pos): | 
					
						
							| 
									
										
										
										
											2021-02-22 17:57:36 -05:00
										 |  |  |         return mcu_pos * self._step_dist - self._mcu_position_offset | 
					
						
							| 
									
										
										
										
											2021-07-19 14:51:05 -04:00
										 |  |  |     def dump_steps(self, count, start_clock, end_clock): | 
					
						
							|  |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							|  |  |  |         data = ffi_main.new('struct pull_history_steps[]', count) | 
					
						
							|  |  |  |         count = ffi_lib.stepcompress_extract_old(self._stepqueue, data, count, | 
					
						
							|  |  |  |                                                  start_clock, end_clock) | 
					
						
							|  |  |  |         return (data, count) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |     def set_stepper_kinematics(self, sk): | 
					
						
							|  |  |  |         old_sk = self._stepper_kinematics | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |         mcu_pos = 0 | 
					
						
							|  |  |  |         if old_sk is not None: | 
					
						
							|  |  |  |             mcu_pos = self.get_mcu_position() | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         self._stepper_kinematics = sk | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							|  |  |  |         ffi_lib.itersolve_set_stepcompress(sk, self._stepqueue, self._step_dist) | 
					
						
							|  |  |  |         self.set_trapq(self._trapq) | 
					
						
							|  |  |  |         self._set_mcu_position(mcu_pos) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         return old_sk | 
					
						
							| 
									
										
										
										
											2021-04-02 12:18:05 -04:00
										 |  |  |     def note_homing_end(self): | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							|  |  |  |         ret = ffi_lib.stepcompress_reset(self._stepqueue, 0) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         if ret: | 
					
						
							|  |  |  |             raise error("Internal error in stepcompress") | 
					
						
							| 
									
										
										
										
											2021-02-04 16:33:03 -05:00
										 |  |  |         data = (self._reset_cmd_tag, self._oid, 0) | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         ret = ffi_lib.stepcompress_queue_msg(self._stepqueue, data, len(data)) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         if ret: | 
					
						
							|  |  |  |             raise error("Internal error in stepcompress") | 
					
						
							| 
									
										
										
										
											2021-04-02 12:18:05 -04:00
										 |  |  |         self._query_mcu_position() | 
					
						
							|  |  |  |     def _query_mcu_position(self): | 
					
						
							|  |  |  |         if self._mcu.is_fileoutput(): | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2020-02-19 16:46:06 -05:00
										 |  |  |         params = self._get_position_cmd.send([self._oid]) | 
					
						
							| 
									
										
										
										
											2021-02-22 17:57:36 -05:00
										 |  |  |         last_pos = params['pos'] | 
					
						
							| 
									
										
										
										
											2021-05-29 21:29:00 -04:00
										 |  |  |         if self._invert_dir: | 
					
						
							|  |  |  |             last_pos = -last_pos | 
					
						
							| 
									
										
										
										
											2021-08-06 09:56:06 -04:00
										 |  |  |         print_time = self._mcu.estimated_print_time(params['#receive_time']) | 
					
						
							|  |  |  |         clock = self._mcu.print_time_to_clock(print_time) | 
					
						
							| 
									
										
										
										
											2021-04-02 12:18:05 -04:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							| 
									
										
										
										
											2021-08-06 09:56:06 -04:00
										 |  |  |         ret = ffi_lib.stepcompress_set_last_position(self._stepqueue, clock, | 
					
						
							|  |  |  |                                                      last_pos) | 
					
						
							| 
									
										
										
										
											2021-02-22 17:57:36 -05:00
										 |  |  |         if ret: | 
					
						
							|  |  |  |             raise error("Internal error in stepcompress") | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |         self._set_mcu_position(last_pos) | 
					
						
							| 
									
										
										
										
											2021-08-05 23:21:31 -04:00
										 |  |  |         self._mcu.get_printer().send_event("stepper:sync_mcu_position", self) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |     def set_trapq(self, tq): | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         if tq is None: | 
					
						
							|  |  |  |             tq = ffi_main.NULL | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         ffi_lib.itersolve_set_trapq(self._stepper_kinematics, tq) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         old_tq = self._trapq | 
					
						
							|  |  |  |         self._trapq = tq | 
					
						
							|  |  |  |         return old_tq | 
					
						
							|  |  |  |     def add_active_callback(self, cb): | 
					
						
							|  |  |  |         self._active_callbacks.append(cb) | 
					
						
							|  |  |  |     def generate_steps(self, flush_time): | 
					
						
							|  |  |  |         # Check for activity if necessary | 
					
						
							|  |  |  |         if self._active_callbacks: | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |             sk = self._stepper_kinematics | 
					
						
							|  |  |  |             ret = self._itersolve_check_active(sk, flush_time) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |             if ret: | 
					
						
							|  |  |  |                 cbs = self._active_callbacks | 
					
						
							|  |  |  |                 self._active_callbacks = [] | 
					
						
							|  |  |  |                 for cb in cbs: | 
					
						
							|  |  |  |                     cb(ret) | 
					
						
							|  |  |  |         # Generate steps | 
					
						
							| 
									
										
										
										
											2021-06-11 21:38:09 -04:00
										 |  |  |         sk = self._stepper_kinematics | 
					
						
							|  |  |  |         ret = self._itersolve_generate_steps(sk, flush_time) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  |         if ret: | 
					
						
							|  |  |  |             raise error("Internal error in stepcompress") | 
					
						
							| 
									
										
										
										
											2020-01-13 21:30:32 -05:00
										 |  |  |     def is_active_axis(self, axis): | 
					
						
							| 
									
										
										
										
											2021-02-18 15:39:55 -05:00
										 |  |  |         ffi_main, ffi_lib = chelper.get_ffi() | 
					
						
							| 
									
										
										
										
											2021-10-01 19:09:37 -04:00
										 |  |  |         a = axis.encode() | 
					
						
							|  |  |  |         return ffi_lib.itersolve_is_active_axis(self._stepper_kinematics, a) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:55:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-12 14:12:33 -05:00
										 |  |  | # Helper code to build a stepper object from a config section | 
					
						
							| 
									
										
										
										
											2019-12-04 18:42:59 -05:00
										 |  |  | def PrinterStepper(config, units_in_radians=False): | 
					
						
							| 
									
										
										
										
											2019-11-12 14:12:33 -05:00
										 |  |  |     printer = config.get_printer() | 
					
						
							|  |  |  |     name = config.get_name() | 
					
						
							|  |  |  |     # Stepper definition | 
					
						
							|  |  |  |     ppins = printer.lookup_object('pins') | 
					
						
							|  |  |  |     step_pin = config.get('step_pin') | 
					
						
							|  |  |  |     step_pin_params = ppins.lookup_pin(step_pin, can_invert=True) | 
					
						
							|  |  |  |     dir_pin = config.get('dir_pin') | 
					
						
							|  |  |  |     dir_pin_params = ppins.lookup_pin(dir_pin, can_invert=True) | 
					
						
							| 
									
										
										
										
											2022-01-29 14:13:26 -05:00
										 |  |  |     rotation_dist, steps_per_rotation = parse_step_distance( | 
					
						
							|  |  |  |         config, units_in_radians, True) | 
					
						
							| 
									
										
										
										
											2021-10-27 19:10:36 -04:00
										 |  |  |     step_pulse_duration = config.getfloat('step_pulse_duration', None, | 
					
						
							|  |  |  |                                           minval=0., maxval=.001) | 
					
						
							| 
									
										
										
										
											2022-01-29 14:13:26 -05:00
										 |  |  |     mcu_stepper = MCU_stepper(name, step_pin_params, dir_pin_params, | 
					
						
							|  |  |  |                               rotation_dist, steps_per_rotation, | 
					
						
							| 
									
										
										
										
											2021-10-27 19:10:36 -04:00
										 |  |  |                               step_pulse_duration, units_in_radians) | 
					
						
							| 
									
										
										
										
											2021-07-19 13:22:40 -04:00
										 |  |  |     # Register with helper modules | 
					
						
							| 
									
										
										
										
											2021-07-19 13:11:36 -04:00
										 |  |  |     for mname in ['stepper_enable', 'force_move', 'motion_report']: | 
					
						
							| 
									
										
										
										
											2021-07-19 13:22:40 -04:00
										 |  |  |         m = printer.load_object(config, mname) | 
					
						
							|  |  |  |         m.register_stepper(config, mcu_stepper) | 
					
						
							| 
									
										
										
										
											2019-11-12 14:12:33 -05:00
										 |  |  |     return mcu_stepper | 
					
						
							| 
									
										
										
										
											2017-07-24 13:54:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-05 20:19:43 -05:00
										 |  |  | # Parse stepper gear_ratio config parameter | 
					
						
							|  |  |  | def parse_gear_ratio(config, note_valid): | 
					
						
							| 
									
										
										
										
											2021-08-19 13:03:59 -04:00
										 |  |  |     gear_ratio = config.getlists('gear_ratio', (), seps=(':', ','), count=2, | 
					
						
							|  |  |  |                                  parser=float, note_valid=note_valid) | 
					
						
							| 
									
										
										
										
											2020-01-05 20:19:43 -05:00
										 |  |  |     result = 1. | 
					
						
							| 
									
										
										
										
											2021-08-19 13:03:59 -04:00
										 |  |  |     for g1, g2 in gear_ratio: | 
					
						
							|  |  |  |         result *= g1 / g2 | 
					
						
							| 
									
										
										
										
											2020-01-05 20:19:43 -05:00
										 |  |  |     return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Obtain "step distance" information from a config section | 
					
						
							|  |  |  | def parse_step_distance(config, units_in_radians=None, note_valid=False): | 
					
						
							|  |  |  |     if units_in_radians is None: | 
					
						
							|  |  |  |         # Caller doesn't know if units are in radians - infer it | 
					
						
							|  |  |  |         rd = config.get('rotation_distance', None, note_valid=False) | 
					
						
							|  |  |  |         gr = config.get('gear_ratio', None, note_valid=False) | 
					
						
							|  |  |  |         units_in_radians = rd is None and gr is not None | 
					
						
							|  |  |  |     if units_in_radians: | 
					
						
							|  |  |  |         rotation_dist = 2. * math.pi | 
					
						
							|  |  |  |         config.get('gear_ratio', note_valid=note_valid) | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2021-02-23 11:51:06 -05:00
										 |  |  |         rotation_dist = config.getfloat('rotation_distance', above=0., | 
					
						
							|  |  |  |                                         note_valid=note_valid) | 
					
						
							| 
									
										
										
										
											2020-01-05 20:19:43 -05:00
										 |  |  |     # Newer config format with rotation_distance | 
					
						
							|  |  |  |     microsteps = config.getint('microsteps', minval=1, note_valid=note_valid) | 
					
						
							|  |  |  |     full_steps = config.getint('full_steps_per_rotation', 200, minval=1, | 
					
						
							|  |  |  |                                note_valid=note_valid) | 
					
						
							|  |  |  |     if full_steps % 4: | 
					
						
							|  |  |  |         raise config.error("full_steps_per_rotation invalid in section '%s'" | 
					
						
							|  |  |  |                            % (config.get_name(),)) | 
					
						
							|  |  |  |     gearing = parse_gear_ratio(config, note_valid) | 
					
						
							| 
									
										
										
										
											2022-01-29 14:13:26 -05:00
										 |  |  |     return rotation_dist, full_steps * microsteps * gearing | 
					
						
							| 
									
										
										
										
											2020-01-05 20:19:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 11:44:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ###################################################################### | 
					
						
							|  |  |  | # Stepper controlled rails | 
					
						
							|  |  |  | ###################################################################### | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # A motor control "rail" with one (or more) steppers and one (or more) | 
					
						
							|  |  |  | # endstops. | 
					
						
							|  |  |  | class PrinterRail: | 
					
						
							| 
									
										
										
										
											2018-06-21 13:47:39 -04:00
										 |  |  |     def __init__(self, config, need_position_minmax=True, | 
					
						
							| 
									
										
										
										
											2019-12-04 18:42:59 -05:00
										 |  |  |                  default_position_endstop=None, units_in_radians=False): | 
					
						
							| 
									
										
										
										
											2019-11-12 13:53:07 -05:00
										 |  |  |         # Primary stepper and endstop | 
					
						
							| 
									
										
										
										
											2019-12-04 18:42:59 -05:00
										 |  |  |         self.stepper_units_in_radians = units_in_radians | 
					
						
							| 
									
										
										
										
											2019-11-12 13:53:07 -05:00
										 |  |  |         self.steppers = [] | 
					
						
							|  |  |  |         self.endstops = [] | 
					
						
							| 
									
										
										
										
											2021-08-24 17:07:58 +02:00
										 |  |  |         self.endstop_map = {} | 
					
						
							| 
									
										
										
										
											2019-11-12 13:53:07 -05:00
										 |  |  |         self.add_extra_stepper(config) | 
					
						
							| 
									
										
										
										
											2019-11-19 12:27:33 -05:00
										 |  |  |         mcu_stepper = self.steppers[0] | 
					
						
							| 
									
										
										
										
											2021-05-01 00:27:43 -04:00
										 |  |  |         self.get_name = mcu_stepper.get_name | 
					
						
							| 
									
										
										
										
											2019-11-19 12:27:33 -05:00
										 |  |  |         self.get_commanded_position = mcu_stepper.get_commanded_position | 
					
						
							|  |  |  |         self.calc_position_from_coord = mcu_stepper.calc_position_from_coord | 
					
						
							| 
									
										
										
										
											2019-11-12 13:53:07 -05:00
										 |  |  |         # Primary endstop position | 
					
						
							|  |  |  |         mcu_endstop = self.endstops[0][0] | 
					
						
							| 
									
										
										
										
											2018-09-26 19:29:58 -04:00
										 |  |  |         if hasattr(mcu_endstop, "get_position_endstop"): | 
					
						
							|  |  |  |             self.position_endstop = mcu_endstop.get_position_endstop() | 
					
						
							|  |  |  |         elif default_position_endstop is None: | 
					
						
							| 
									
										
										
										
											2017-11-01 21:21:37 -04:00
										 |  |  |             self.position_endstop = config.getfloat('position_endstop') | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.position_endstop = config.getfloat( | 
					
						
							| 
									
										
										
										
											2018-05-18 19:34:17 -04:00
										 |  |  |                 'position_endstop', default_position_endstop) | 
					
						
							| 
									
										
										
										
											2017-11-06 18:37:35 -05:00
										 |  |  |         # Axis range | 
					
						
							| 
									
										
										
										
											2018-05-18 19:34:17 -04:00
										 |  |  |         if need_position_minmax: | 
					
						
							|  |  |  |             self.position_min = config.getfloat('position_min', 0.) | 
					
						
							|  |  |  |             self.position_max = config.getfloat( | 
					
						
							|  |  |  |                 'position_max', above=self.position_min) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.position_min = 0. | 
					
						
							|  |  |  |             self.position_max = self.position_endstop | 
					
						
							|  |  |  |         if (self.position_endstop < self.position_min | 
					
						
							|  |  |  |             or self.position_endstop > self.position_max): | 
					
						
							|  |  |  |             raise config.error( | 
					
						
							|  |  |  |                 "position_endstop in section '%s' must be between" | 
					
						
							|  |  |  |                 " position_min and position_max" % config.get_name()) | 
					
						
							| 
									
										
										
										
											2017-11-06 18:37:35 -05:00
										 |  |  |         # Homing mechanics | 
					
						
							| 
									
										
										
										
											2017-04-11 11:37:09 -04:00
										 |  |  |         self.homing_speed = config.getfloat('homing_speed', 5.0, above=0.) | 
					
						
							| 
									
										
										
										
											2018-10-08 12:54:33 -04:00
										 |  |  |         self.second_homing_speed = config.getfloat( | 
					
						
							|  |  |  |             'second_homing_speed', self.homing_speed/2., above=0.) | 
					
						
							| 
									
										
										
										
											2020-02-12 16:09:39 +01:00
										 |  |  |         self.homing_retract_speed = config.getfloat( | 
					
						
							|  |  |  |             'homing_retract_speed', self.homing_speed, above=0.) | 
					
						
							| 
									
										
										
										
											2017-11-06 18:37:35 -05:00
										 |  |  |         self.homing_retract_dist = config.getfloat( | 
					
						
							| 
									
										
										
										
											2018-03-06 08:59:13 +02:00
										 |  |  |             'homing_retract_dist', 5., minval=0.) | 
					
						
							| 
									
										
										
										
											2018-07-26 10:11:56 -04:00
										 |  |  |         self.homing_positive_dir = config.getboolean( | 
					
						
							|  |  |  |             'homing_positive_dir', None) | 
					
						
							| 
									
										
										
										
											2017-07-24 14:12:17 -04:00
										 |  |  |         if self.homing_positive_dir is None: | 
					
						
							|  |  |  |             axis_len = self.position_max - self.position_min | 
					
						
							|  |  |  |             if self.position_endstop <= self.position_min + axis_len / 4.: | 
					
						
							|  |  |  |                 self.homing_positive_dir = False | 
					
						
							|  |  |  |             elif self.position_endstop >= self.position_max - axis_len / 4.: | 
					
						
							|  |  |  |                 self.homing_positive_dir = True | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 raise config.error( | 
					
						
							| 
									
										
										
										
											2021-07-03 21:27:31 -04:00
										 |  |  |                     "Unable to infer homing_positive_dir in section '%s'" | 
					
						
							|  |  |  |                     % (config.get_name(),)) | 
					
						
							| 
									
										
										
										
											2021-04-16 11:42:39 -04:00
										 |  |  |             config.getboolean('homing_positive_dir', self.homing_positive_dir) | 
					
						
							| 
									
										
										
										
											2020-03-24 15:48:49 -04:00
										 |  |  |         elif ((self.homing_positive_dir | 
					
						
							|  |  |  |                and self.position_endstop == self.position_min) | 
					
						
							|  |  |  |               or (not self.homing_positive_dir | 
					
						
							|  |  |  |                   and self.position_endstop == self.position_max)): | 
					
						
							|  |  |  |             raise config.error( | 
					
						
							|  |  |  |                 "Invalid homing_positive_dir / position_endstop in '%s'" | 
					
						
							|  |  |  |                 % (config.get_name(),)) | 
					
						
							| 
									
										
										
										
											2018-06-22 11:44:25 -04:00
										 |  |  |     def get_range(self): | 
					
						
							|  |  |  |         return self.position_min, self.position_max | 
					
						
							|  |  |  |     def get_homing_info(self): | 
					
						
							|  |  |  |         homing_info = collections.namedtuple('homing_info', [ | 
					
						
							| 
									
										
										
										
											2020-02-12 16:09:39 +01:00
										 |  |  |             'speed', 'position_endstop', 'retract_speed', 'retract_dist', | 
					
						
							|  |  |  |             'positive_dir', 'second_homing_speed'])( | 
					
						
							| 
									
										
										
										
											2018-06-22 11:44:25 -04:00
										 |  |  |                 self.homing_speed, self.position_endstop, | 
					
						
							| 
									
										
										
										
											2020-02-12 16:09:39 +01:00
										 |  |  |                 self.homing_retract_speed, self.homing_retract_dist, | 
					
						
							|  |  |  |                 self.homing_positive_dir, self.second_homing_speed) | 
					
						
							| 
									
										
										
										
											2018-06-22 11:44:25 -04:00
										 |  |  |         return homing_info | 
					
						
							|  |  |  |     def get_steppers(self): | 
					
						
							|  |  |  |         return list(self.steppers) | 
					
						
							|  |  |  |     def get_endstops(self): | 
					
						
							|  |  |  |         return list(self.endstops) | 
					
						
							|  |  |  |     def add_extra_stepper(self, config): | 
					
						
							| 
									
										
										
										
											2019-12-04 18:42:59 -05:00
										 |  |  |         stepper = PrinterStepper(config, self.stepper_units_in_radians) | 
					
						
							| 
									
										
										
										
											2018-06-22 11:44:25 -04:00
										 |  |  |         self.steppers.append(stepper) | 
					
						
							| 
									
										
										
										
											2021-09-29 20:10:57 -04:00
										 |  |  |         if self.endstops and config.get('endstop_pin', None) is None: | 
					
						
							| 
									
										
										
										
											2019-11-12 13:53:07 -05:00
										 |  |  |             # No endstop defined - use primary endstop | 
					
						
							| 
									
										
										
										
											2019-11-12 14:21:15 -05:00
										 |  |  |             self.endstops[0][0].add_stepper(stepper) | 
					
						
							| 
									
										
										
										
											2019-11-12 13:53:07 -05:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2021-09-29 20:10:57 -04:00
										 |  |  |         endstop_pin = config.get('endstop_pin') | 
					
						
							| 
									
										
										
										
											2019-11-12 13:53:07 -05:00
										 |  |  |         printer = config.get_printer() | 
					
						
							|  |  |  |         ppins = printer.lookup_object('pins') | 
					
						
							| 
									
										
										
										
											2021-08-24 17:07:58 +02:00
										 |  |  |         pin_params = ppins.parse_pin(endstop_pin, True, True) | 
					
						
							|  |  |  |         # Normalize pin name | 
					
						
							|  |  |  |         pin_name = "%s:%s" % (pin_params['chip_name'], pin_params['pin']) | 
					
						
							|  |  |  |         # Look for already-registered endstop | 
					
						
							|  |  |  |         endstop = self.endstop_map.get(pin_name, None) | 
					
						
							|  |  |  |         if endstop is None: | 
					
						
							|  |  |  |             # New endstop, register it | 
					
						
							|  |  |  |             mcu_endstop = ppins.setup_pin('endstop', endstop_pin) | 
					
						
							|  |  |  |             self.endstop_map[pin_name] = {'endstop': mcu_endstop, | 
					
						
							|  |  |  |                                           'invert': pin_params['invert'], | 
					
						
							|  |  |  |                                           'pullup': pin_params['pullup']} | 
					
						
							|  |  |  |             name = stepper.get_name(short=True) | 
					
						
							|  |  |  |             self.endstops.append((mcu_endstop, name)) | 
					
						
							|  |  |  |             query_endstops = printer.load_object(config, 'query_endstops') | 
					
						
							|  |  |  |             query_endstops.register_endstop(mcu_endstop, name) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             mcu_endstop = endstop['endstop'] | 
					
						
							|  |  |  |             changed_invert = pin_params['invert'] != endstop['invert'] | 
					
						
							|  |  |  |             changed_pullup = pin_params['pullup'] != endstop['pullup'] | 
					
						
							|  |  |  |             if changed_invert or changed_pullup: | 
					
						
							|  |  |  |                 raise error("Pinter rail %s shared endstop pin %s " | 
					
						
							|  |  |  |                             "must specify the same pullup/invert settings" % ( | 
					
						
							|  |  |  |                                 self.get_name(), pin_name)) | 
					
						
							| 
									
										
										
										
											2019-11-12 14:21:15 -05:00
										 |  |  |         mcu_endstop.add_stepper(stepper) | 
					
						
							| 
									
										
										
										
											2018-07-13 11:24:36 -04:00
										 |  |  |     def setup_itersolve(self, alloc_func, *params): | 
					
						
							| 
									
										
										
										
											2018-06-22 11:44:25 -04:00
										 |  |  |         for stepper in self.steppers: | 
					
						
							| 
									
										
										
										
											2018-07-13 11:24:36 -04:00
										 |  |  |             stepper.setup_itersolve(alloc_func, *params) | 
					
						
							| 
									
										
										
										
											2019-10-27 21:05:57 -04:00
										 |  |  |     def generate_steps(self, flush_time): | 
					
						
							|  |  |  |         for stepper in self.steppers: | 
					
						
							|  |  |  |             stepper.generate_steps(flush_time) | 
					
						
							|  |  |  |     def set_trapq(self, trapq): | 
					
						
							|  |  |  |         for stepper in self.steppers: | 
					
						
							|  |  |  |             stepper.set_trapq(trapq) | 
					
						
							| 
									
										
										
										
											2018-10-08 20:50:45 -04:00
										 |  |  |     def set_position(self, coord): | 
					
						
							| 
									
										
										
										
											2018-06-22 11:44:25 -04:00
										 |  |  |         for stepper in self.steppers: | 
					
						
							| 
									
										
										
										
											2018-10-08 20:50:45 -04:00
										 |  |  |             stepper.set_position(coord) | 
					
						
							| 
									
										
										
										
											2017-11-07 13:10:08 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 11:44:25 -04:00
										 |  |  | # Wrapper for dual stepper motor support | 
					
						
							|  |  |  | def LookupMultiRail(config): | 
					
						
							|  |  |  |     rail = PrinterRail(config) | 
					
						
							|  |  |  |     for i in range(1, 99): | 
					
						
							|  |  |  |         if not config.has_section(config.get_name() + str(i)): | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         rail.add_extra_stepper(config.getsection(config.get_name() + str(i))) | 
					
						
							|  |  |  |     return rail |