| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  | # Code for coordinating events on the printer toolhead | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Copyright (C) 2016  Kevin O'Connor <kevin@koconnor.net> | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This file may be distributed under the terms of the GNU GPLv3 license. | 
					
						
							| 
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 |  |  | import math, logging | 
					
						
							| 
									
										
										
										
											2017-03-12 13:47:16 -04:00
										 |  |  | import cartesian, corexy, delta, extruder | 
					
						
							| 
									
										
										
										
											2016-07-12 12:04:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  | # Common suffixes: _d is distance (in mm), _v is velocity (in | 
					
						
							| 
									
										
										
										
											2017-01-14 13:35:27 -05:00
										 |  |  | #   mm/second), _v2 is velocity squared (mm^2/s^2), _t is time (in | 
					
						
							|  |  |  | #   seconds), _r is ratio (scalar between 0.0 and 1.0) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 16:01:58 -04:00
										 |  |  | # Class to track each move request | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  | class Move: | 
					
						
							| 
									
										
										
										
											2017-01-14 12:41:49 -05:00
										 |  |  |     def __init__(self, toolhead, start_pos, end_pos, speed): | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         self.toolhead = toolhead | 
					
						
							| 
									
										
										
										
											2016-10-25 19:11:05 -04:00
										 |  |  |         self.start_pos = tuple(start_pos) | 
					
						
							|  |  |  |         self.end_pos = tuple(end_pos) | 
					
						
							| 
									
										
										
										
											2017-01-14 12:41:49 -05:00
										 |  |  |         self.accel = toolhead.max_accel | 
					
						
							| 
									
										
										
										
											2017-01-31 11:04:49 -05:00
										 |  |  |         self.is_kinematic_move = True | 
					
						
							| 
									
										
										
										
											2016-10-25 19:11:05 -04:00
										 |  |  |         self.axes_d = axes_d = [end_pos[i] - start_pos[i] for i in (0, 1, 2, 3)] | 
					
						
							| 
									
										
										
										
											2017-01-31 11:04:49 -05:00
										 |  |  |         self.move_d = move_d = math.sqrt(sum([d*d for d in axes_d[:3]])) | 
					
						
							|  |  |  |         if not move_d: | 
					
						
							|  |  |  |             # Extrude only move | 
					
						
							|  |  |  |             self.move_d = move_d = abs(axes_d[3]) | 
					
						
							|  |  |  |             self.is_kinematic_move = False | 
					
						
							| 
									
										
										
										
											2017-02-11 12:29:19 -05:00
										 |  |  |         self.min_move_t = move_d / speed | 
					
						
							| 
									
										
										
										
											2017-01-14 13:35:27 -05:00
										 |  |  |         # Junction speeds are tracked in velocity squared.  The | 
					
						
							|  |  |  |         # delta_v2 is the maximum amount of this squared-velocity that | 
					
						
							|  |  |  |         # can change in this move. | 
					
						
							|  |  |  |         self.max_start_v2 = 0. | 
					
						
							|  |  |  |         self.max_cruise_v2 = speed**2 | 
					
						
							|  |  |  |         self.delta_v2 = 2.0 * move_d * self.accel | 
					
						
							| 
									
										
										
										
											2017-01-31 15:29:16 -05:00
										 |  |  |         self.max_smoothed_v2 = 0. | 
					
						
							|  |  |  |         self.smooth_delta_v2 = 2.0 * move_d * toolhead.max_accel_to_decel | 
					
						
							| 
									
										
										
										
											2016-09-30 14:47:45 -04:00
										 |  |  |     def limit_speed(self, speed, accel): | 
					
						
							| 
									
										
										
										
											2017-02-11 12:29:19 -05:00
										 |  |  |         speed2 = speed**2 | 
					
						
							|  |  |  |         if speed2 < self.max_cruise_v2: | 
					
						
							|  |  |  |             self.max_cruise_v2 = speed2 | 
					
						
							|  |  |  |             self.min_move_t = self.move_d / speed | 
					
						
							| 
									
										
										
										
											2017-01-31 11:04:49 -05:00
										 |  |  |         self.accel = min(self.accel, accel) | 
					
						
							|  |  |  |         self.delta_v2 = 2.0 * self.move_d * self.accel | 
					
						
							| 
									
										
										
										
											2017-01-31 15:29:16 -05:00
										 |  |  |         self.smooth_delta_v2 = min(self.smooth_delta_v2, self.delta_v2) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |     def calc_junction(self, prev_move): | 
					
						
							| 
									
										
										
										
											2017-01-31 11:04:49 -05:00
										 |  |  |         axes_d = self.axes_d | 
					
						
							|  |  |  |         prev_axes_d = prev_move.axes_d | 
					
						
							|  |  |  |         if (axes_d[2] or prev_axes_d[2] or self.accel != prev_move.accel | 
					
						
							|  |  |  |             or not self.is_kinematic_move or not prev_move.is_kinematic_move): | 
					
						
							| 
									
										
										
										
											2016-09-30 14:47:45 -04:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |         # Allow extruder to calculate its maximum junction | 
					
						
							|  |  |  |         extruder_v2 = self.toolhead.extruder.calc_junction(prev_move, self) | 
					
						
							| 
									
										
										
										
											2016-07-12 12:04:06 -04:00
										 |  |  |         # Find max velocity using approximated centripetal velocity as | 
					
						
							|  |  |  |         # described at: | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         # https://onehossshay.wordpress.com/2011/09/24/improving_grbl_cornering_algorithm/ | 
					
						
							| 
									
										
										
										
											2017-01-31 11:04:49 -05:00
										 |  |  |         junction_cos_theta = -((axes_d[0] * prev_axes_d[0] | 
					
						
							|  |  |  |                                 + axes_d[1] * prev_axes_d[1]) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |                                / (self.move_d * prev_move.move_d)) | 
					
						
							|  |  |  |         if junction_cos_theta > 0.999999: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         junction_cos_theta = max(junction_cos_theta, -0.999999) | 
					
						
							| 
									
										
										
										
											2016-07-12 12:04:06 -04:00
										 |  |  |         sin_theta_d2 = math.sqrt(0.5*(1.0-junction_cos_theta)) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         R = self.toolhead.junction_deviation * sin_theta_d2 / (1. - sin_theta_d2) | 
					
						
							| 
									
										
										
										
											2017-01-14 13:35:27 -05:00
										 |  |  |         self.max_start_v2 = min( | 
					
						
							|  |  |  |             R * self.accel, self.max_cruise_v2, prev_move.max_cruise_v2 | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |             , extruder_v2, prev_move.max_start_v2 + prev_move.delta_v2) | 
					
						
							| 
									
										
										
										
											2017-01-31 15:29:16 -05:00
										 |  |  |         self.max_smoothed_v2 = min( | 
					
						
							|  |  |  |             self.max_start_v2 | 
					
						
							|  |  |  |             , prev_move.max_smoothed_v2 + prev_move.smooth_delta_v2) | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |     def set_junction(self, start_v2, cruise_v2, end_v2): | 
					
						
							| 
									
										
										
										
											2016-07-11 13:21:27 -04:00
										 |  |  |         # Determine accel, cruise, and decel portions of the move distance | 
					
						
							| 
									
										
										
										
											2017-01-14 13:35:27 -05:00
										 |  |  |         inv_delta_v2 = 1. / self.delta_v2 | 
					
						
							|  |  |  |         self.accel_r = accel_r = (cruise_v2 - start_v2) * inv_delta_v2 | 
					
						
							|  |  |  |         self.decel_r = decel_r = (cruise_v2 - end_v2) * inv_delta_v2 | 
					
						
							|  |  |  |         self.cruise_r = cruise_r = 1. - accel_r - decel_r | 
					
						
							| 
									
										
										
										
											2016-07-07 16:01:58 -04:00
										 |  |  |         # Determine move velocities | 
					
						
							| 
									
										
										
										
											2017-01-14 13:35:27 -05:00
										 |  |  |         self.start_v = start_v = math.sqrt(start_v2) | 
					
						
							|  |  |  |         self.cruise_v = cruise_v = math.sqrt(cruise_v2) | 
					
						
							|  |  |  |         self.end_v = end_v = math.sqrt(end_v2) | 
					
						
							| 
									
										
										
										
											2016-07-11 13:21:27 -04:00
										 |  |  |         # Determine time spent in each portion of move (time is the | 
					
						
							|  |  |  |         # distance divided by average velocity) | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |         self.accel_t = accel_r * self.move_d / ((start_v + cruise_v) * 0.5) | 
					
						
							|  |  |  |         self.cruise_t = cruise_r * self.move_d / cruise_v | 
					
						
							|  |  |  |         self.decel_t = decel_r * self.move_d / ((end_v + cruise_v) * 0.5) | 
					
						
							|  |  |  |     def move(self): | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         # Generate step times for the move | 
					
						
							|  |  |  |         next_move_time = self.toolhead.get_next_move_time() | 
					
						
							| 
									
										
										
										
											2016-12-28 11:48:40 -05:00
										 |  |  |         if self.is_kinematic_move: | 
					
						
							|  |  |  |             self.toolhead.kin.move(next_move_time, self) | 
					
						
							| 
									
										
										
										
											2016-07-10 12:23:35 -04:00
										 |  |  |         if self.axes_d[3]: | 
					
						
							|  |  |  |             self.toolhead.extruder.move(next_move_time, self) | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |         self.toolhead.update_move_time( | 
					
						
							|  |  |  |             self.accel_t + self.cruise_t + self.decel_t) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-11 12:29:19 -05:00
										 |  |  | LOOKAHEAD_FLUSH_TIME = 0.250 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 16:01:58 -04:00
										 |  |  | # Class to track a list of pending move requests and to facilitate | 
					
						
							|  |  |  | # "look-ahead" across moves to reduce acceleration between moves. | 
					
						
							|  |  |  | class MoveQueue: | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |     def __init__(self, extruder_lookahead): | 
					
						
							|  |  |  |         self.extruder_lookahead = extruder_lookahead | 
					
						
							| 
									
										
										
										
											2016-07-07 16:01:58 -04:00
										 |  |  |         self.queue = [] | 
					
						
							| 
									
										
										
										
											2017-01-31 13:42:52 -05:00
										 |  |  |         self.leftover = 0 | 
					
						
							| 
									
										
										
										
											2017-02-11 12:29:19 -05:00
										 |  |  |         self.junction_flush = LOOKAHEAD_FLUSH_TIME | 
					
						
							| 
									
										
										
										
											2016-09-19 13:18:55 -04:00
										 |  |  |     def reset(self): | 
					
						
							|  |  |  |         del self.queue[:] | 
					
						
							| 
									
										
										
										
											2017-01-31 13:42:52 -05:00
										 |  |  |         self.leftover = 0 | 
					
						
							| 
									
										
										
										
											2017-02-11 12:29:19 -05:00
										 |  |  |         self.junction_flush = LOOKAHEAD_FLUSH_TIME | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |     def set_flush_time(self, flush_time): | 
					
						
							|  |  |  |         self.junction_flush = flush_time | 
					
						
							| 
									
										
										
										
											2016-07-07 16:01:58 -04:00
										 |  |  |     def flush(self, lazy=False): | 
					
						
							| 
									
										
										
										
											2017-02-11 12:29:19 -05:00
										 |  |  |         self.junction_flush = LOOKAHEAD_FLUSH_TIME | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |         update_flush_count = lazy | 
					
						
							|  |  |  |         queue = self.queue | 
					
						
							|  |  |  |         flush_count = len(queue) | 
					
						
							| 
									
										
										
										
											2016-07-11 13:21:27 -04:00
										 |  |  |         # Traverse queue from last to first move and determine maximum | 
					
						
							|  |  |  |         # junction speed assuming the robot comes to a complete stop | 
					
						
							|  |  |  |         # after the last move. | 
					
						
							| 
									
										
										
										
											2017-01-31 15:29:16 -05:00
										 |  |  |         delayed = [] | 
					
						
							|  |  |  |         next_end_v2 = next_smoothed_v2 = peak_cruise_v2 = 0. | 
					
						
							| 
									
										
										
										
											2017-01-31 13:42:52 -05:00
										 |  |  |         for i in range(flush_count-1, self.leftover-1, -1): | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |             move = queue[i] | 
					
						
							| 
									
										
										
										
											2017-01-14 13:35:27 -05:00
										 |  |  |             reachable_start_v2 = next_end_v2 + move.delta_v2 | 
					
						
							|  |  |  |             start_v2 = min(move.max_start_v2, reachable_start_v2) | 
					
						
							| 
									
										
										
										
											2017-01-31 15:29:16 -05:00
										 |  |  |             reachable_smoothed_v2 = next_smoothed_v2 + move.smooth_delta_v2 | 
					
						
							|  |  |  |             smoothed_v2 = min(move.max_smoothed_v2, reachable_smoothed_v2) | 
					
						
							|  |  |  |             if smoothed_v2 < reachable_smoothed_v2: | 
					
						
							|  |  |  |                 # It's possible for this move to accelerate | 
					
						
							|  |  |  |                 if (smoothed_v2 + move.smooth_delta_v2 > next_smoothed_v2 | 
					
						
							| 
									
										
										
										
											2017-02-20 12:37:43 -05:00
										 |  |  |                     or delayed): | 
					
						
							|  |  |  |                     # This move can decelerate or this is a full accel | 
					
						
							|  |  |  |                     # move after a full decel move | 
					
						
							| 
									
										
										
										
											2017-01-31 15:29:16 -05:00
										 |  |  |                     if update_flush_count and peak_cruise_v2: | 
					
						
							|  |  |  |                         flush_count = i | 
					
						
							|  |  |  |                         update_flush_count = False | 
					
						
							|  |  |  |                     peak_cruise_v2 = min(move.max_cruise_v2, ( | 
					
						
							|  |  |  |                         smoothed_v2 + reachable_smoothed_v2) * .5) | 
					
						
							|  |  |  |                     if delayed: | 
					
						
							|  |  |  |                         # Propagate peak_cruise_v2 to any delayed moves | 
					
						
							| 
									
										
										
										
											2017-02-20 12:37:43 -05:00
										 |  |  |                         if not update_flush_count and i < flush_count: | 
					
						
							|  |  |  |                             for m, ms_v2, me_v2 in delayed: | 
					
						
							|  |  |  |                                 mc_v2 = min(peak_cruise_v2, ms_v2) | 
					
						
							|  |  |  |                                 m.set_junction(min(ms_v2, mc_v2), mc_v2 | 
					
						
							|  |  |  |                                                , min(me_v2, mc_v2)) | 
					
						
							| 
									
										
										
										
											2017-01-31 15:29:16 -05:00
										 |  |  |                         del delayed[:] | 
					
						
							|  |  |  |                 if not update_flush_count and i < flush_count: | 
					
						
							| 
									
										
										
										
											2017-02-20 12:37:43 -05:00
										 |  |  |                     cruise_v2 = min((start_v2 + reachable_start_v2) * .5 | 
					
						
							|  |  |  |                                     , move.max_cruise_v2, peak_cruise_v2) | 
					
						
							| 
									
										
										
										
											2017-01-31 15:29:16 -05:00
										 |  |  |                     move.set_junction(min(start_v2, cruise_v2), cruise_v2 | 
					
						
							|  |  |  |                                       , min(next_end_v2, cruise_v2)) | 
					
						
							| 
									
										
										
										
											2017-02-20 12:37:43 -05:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2017-01-31 15:29:16 -05:00
										 |  |  |                 # Delay calculating this move until peak_cruise_v2 is known | 
					
						
							|  |  |  |                 delayed.append((move, start_v2, next_end_v2)) | 
					
						
							| 
									
										
										
										
											2017-01-14 13:35:27 -05:00
										 |  |  |             next_end_v2 = start_v2 | 
					
						
							| 
									
										
										
										
											2017-01-31 15:29:16 -05:00
										 |  |  |             next_smoothed_v2 = smoothed_v2 | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |         if update_flush_count: | 
					
						
							| 
									
										
										
										
											2017-02-11 12:29:19 -05:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |         # Allow extruder to do its lookahead | 
					
						
							| 
									
										
										
										
											2017-01-31 13:42:52 -05:00
										 |  |  |         move_count = self.extruder_lookahead(queue, flush_count, lazy) | 
					
						
							| 
									
										
										
										
											2016-07-11 13:21:27 -04:00
										 |  |  |         # Generate step times for all moves ready to be flushed | 
					
						
							| 
									
										
										
										
											2017-01-31 13:42:52 -05:00
										 |  |  |         for move in queue[:move_count]: | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |             move.move() | 
					
						
							| 
									
										
										
										
											2016-07-11 13:21:27 -04:00
										 |  |  |         # Remove processed moves from the queue | 
					
						
							| 
									
										
										
										
											2017-01-31 13:42:52 -05:00
										 |  |  |         self.leftover = flush_count - move_count | 
					
						
							|  |  |  |         del queue[:move_count] | 
					
						
							| 
									
										
										
										
											2016-07-07 16:01:58 -04:00
										 |  |  |     def add_move(self, move): | 
					
						
							|  |  |  |         self.queue.append(move) | 
					
						
							|  |  |  |         if len(self.queue) == 1: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2016-07-07 16:08:00 -04:00
										 |  |  |         move.calc_junction(self.queue[-2]) | 
					
						
							| 
									
										
										
										
											2017-02-11 12:29:19 -05:00
										 |  |  |         self.junction_flush -= move.min_move_t | 
					
						
							| 
									
										
										
										
											2016-07-07 16:01:58 -04:00
										 |  |  |         if self.junction_flush <= 0.: | 
					
						
							| 
									
										
										
										
											2016-07-11 13:21:27 -04:00
										 |  |  |             # There are enough queued moves to return to zero velocity | 
					
						
							|  |  |  |             # from the first move's maximum possible velocity, so at | 
					
						
							|  |  |  |             # least one move can be flushed. | 
					
						
							| 
									
										
										
										
											2016-07-07 16:01:58 -04:00
										 |  |  |             self.flush(lazy=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  | STALL_TIME = 0.100 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 16:01:58 -04:00
										 |  |  | # Main code to track events (and their timing) on the printer toolhead | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  | class ToolHead: | 
					
						
							|  |  |  |     def __init__(self, printer, config): | 
					
						
							|  |  |  |         self.printer = printer | 
					
						
							|  |  |  |         self.reactor = printer.reactor | 
					
						
							| 
									
										
										
										
											2016-07-10 12:23:35 -04:00
										 |  |  |         self.extruder = printer.objects.get('extruder') | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |         if self.extruder is None: | 
					
						
							|  |  |  |             self.extruder = extruder.DummyExtruder() | 
					
						
							| 
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 |  |  |         kintypes = {'cartesian': cartesian.CartKinematics, | 
					
						
							| 
									
										
										
										
											2017-03-12 13:47:16 -04:00
										 |  |  |                     'corexy': corexy.CoreXYKinematics, | 
					
						
							| 
									
										
										
										
											2016-09-15 12:20:49 -04:00
										 |  |  |                     'delta': delta.DeltaKinematics} | 
					
						
							| 
									
										
										
										
											2016-11-30 15:50:28 -05:00
										 |  |  |         self.kin = config.getchoice('kinematics', kintypes)(printer, config) | 
					
						
							| 
									
										
										
										
											2017-04-11 11:37:09 -04:00
										 |  |  |         self.max_speed = config.getfloat('max_velocity', above=0.) | 
					
						
							|  |  |  |         self.max_accel = config.getfloat('max_accel', above=0.) | 
					
						
							|  |  |  |         self.max_accel_to_decel = config.getfloat( | 
					
						
							|  |  |  |             'max_accel_to_decel', self.max_accel * 0.5 | 
					
						
							|  |  |  |             , above=0., maxval=self.max_accel) | 
					
						
							|  |  |  |         self.junction_deviation = config.getfloat( | 
					
						
							|  |  |  |             'junction_deviation', 0.02, above=0.) | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |         self.move_queue = MoveQueue(self.extruder.lookahead) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         self.commanded_pos = [0., 0., 0., 0.] | 
					
						
							|  |  |  |         # Print time tracking | 
					
						
							| 
									
										
										
										
											2017-04-11 11:37:09 -04:00
										 |  |  |         self.buffer_time_low = config.getfloat( | 
					
						
							|  |  |  |             'buffer_time_low', 1.000, above=0.) | 
					
						
							|  |  |  |         self.buffer_time_high = config.getfloat( | 
					
						
							|  |  |  |             'buffer_time_high', 2.000, above=self.buffer_time_low) | 
					
						
							|  |  |  |         self.buffer_time_start = config.getfloat( | 
					
						
							|  |  |  |             'buffer_time_start', 0.250, above=0.) | 
					
						
							|  |  |  |         self.move_flush_time = config.getfloat( | 
					
						
							|  |  |  |             'move_flush_time', 0.050, above=0.) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         self.print_time = 0. | 
					
						
							| 
									
										
										
										
											2016-12-09 09:41:46 -05:00
										 |  |  |         self.need_check_stall = -1. | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |         self.print_stall = 0 | 
					
						
							|  |  |  |         self.synch_print_time = True | 
					
						
							|  |  |  |         self.forced_synch = False | 
					
						
							| 
									
										
										
										
											2016-12-09 09:41:46 -05:00
										 |  |  |         self.flush_timer = self.reactor.register_timer(self._flush_handler) | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |         self.move_queue.set_flush_time(self.buffer_time_high) | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |         # Motor off tracking | 
					
						
							| 
									
										
										
										
											2017-04-11 11:37:09 -04:00
										 |  |  |         self.motor_off_time = config.getfloat( | 
					
						
							|  |  |  |             'motor_off_time', 600.000, minval=0.) | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |         self.motor_off_timer = self.reactor.register_timer( | 
					
						
							|  |  |  |             self._motor_off_handler) | 
					
						
							| 
									
										
										
										
											2017-02-12 14:33:06 -05:00
										 |  |  |         # Determine the maximum velocity a cartesian axis could have | 
					
						
							|  |  |  |         # before cornering.  The 8. was determined experimentally. | 
					
						
							|  |  |  |         xy_halt = math.sqrt(8. * self.junction_deviation * self.max_accel) | 
					
						
							| 
									
										
										
										
											2017-01-02 18:58:45 -05:00
										 |  |  |         self.kin.set_max_jerk(xy_halt, self.max_speed, self.max_accel) | 
					
						
							| 
									
										
										
										
											2017-01-27 22:06:54 -05:00
										 |  |  |         self.extruder.set_max_jerk(xy_halt, self.max_speed, self.max_accel) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |     # Print time tracking | 
					
						
							|  |  |  |     def update_move_time(self, movetime): | 
					
						
							|  |  |  |         self.print_time += movetime | 
					
						
							|  |  |  |         flush_to_time = self.print_time - self.move_flush_time | 
					
						
							|  |  |  |         self.printer.mcu.flush_moves(flush_to_time) | 
					
						
							|  |  |  |     def get_next_move_time(self): | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |         if self.synch_print_time: | 
					
						
							| 
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 |  |  |             curtime = self.reactor.monotonic() | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |             if self.print_time: | 
					
						
							|  |  |  |                 buffer_time = self.printer.mcu.get_print_buffer_time( | 
					
						
							|  |  |  |                     curtime, self.print_time) | 
					
						
							|  |  |  |                 self.print_time += max(self.buffer_time_start - buffer_time, 0.) | 
					
						
							|  |  |  |                 if self.forced_synch: | 
					
						
							|  |  |  |                     self.print_stall += 1 | 
					
						
							|  |  |  |                     self.forced_synch = False | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self.printer.mcu.set_print_start_time(curtime) | 
					
						
							|  |  |  |                 self.print_time = self.buffer_time_start | 
					
						
							|  |  |  |                 self._reset_motor_off() | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |             self.reactor.update_timer(self.flush_timer, self.reactor.NOW) | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |             self.synch_print_time = False | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         return self.print_time | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |     def _flush_lookahead(self, must_synch=False): | 
					
						
							|  |  |  |         synch_print_time = self.synch_print_time | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         self.move_queue.flush() | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |         if synch_print_time or must_synch: | 
					
						
							|  |  |  |             self.synch_print_time = True | 
					
						
							|  |  |  |             self.move_queue.set_flush_time(self.buffer_time_high) | 
					
						
							|  |  |  |             self.printer.mcu.flush_moves(self.print_time) | 
					
						
							|  |  |  |     def get_last_move_time(self): | 
					
						
							|  |  |  |         self._flush_lookahead() | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         return self.get_next_move_time() | 
					
						
							|  |  |  |     def reset_print_time(self): | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |         self._flush_lookahead(must_synch=True) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         self.print_time = 0. | 
					
						
							| 
									
										
										
										
											2016-12-09 09:41:46 -05:00
										 |  |  |         self.need_check_stall = -1. | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |         self.forced_synch = False | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |         self._reset_motor_off() | 
					
						
							| 
									
										
										
										
											2016-12-09 09:41:46 -05:00
										 |  |  |     def _check_stall(self): | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |         eventtime = self.reactor.monotonic() | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         if not self.print_time: | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |             # Building initial queue - make sure to flush on idle input | 
					
						
							|  |  |  |             self.reactor.update_timer(self.flush_timer, eventtime + 0.100) | 
					
						
							| 
									
										
										
										
											2016-12-09 09:41:46 -05:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |         # Check if there are lots of queued moves and stall if so | 
					
						
							| 
									
										
										
										
											2016-12-09 09:41:46 -05:00
										 |  |  |         while 1: | 
					
						
							|  |  |  |             buffer_time = self.printer.mcu.get_print_buffer_time( | 
					
						
							|  |  |  |                 eventtime, self.print_time) | 
					
						
							|  |  |  |             stall_time = buffer_time - self.buffer_time_high | 
					
						
							|  |  |  |             if stall_time <= 0.: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             eventtime = self.reactor.pause(eventtime + stall_time) | 
					
						
							|  |  |  |             if not self.print_time: | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |         self.need_check_stall = self.print_time - stall_time + 0.100 | 
					
						
							|  |  |  |     def _flush_handler(self, eventtime): | 
					
						
							| 
									
										
										
										
											2016-09-19 13:18:55 -04:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |             if not self.print_time: | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |                 # Input idled before filling lookahead queue - flush it | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |                 self._flush_lookahead() | 
					
						
							| 
									
										
										
										
											2016-09-19 13:18:55 -04:00
										 |  |  |                 if not self.print_time: | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |                     return self.reactor.NEVER | 
					
						
							| 
									
										
										
										
											2016-09-19 13:18:55 -04:00
										 |  |  |             print_time = self.print_time | 
					
						
							|  |  |  |             buffer_time = self.printer.mcu.get_print_buffer_time( | 
					
						
							|  |  |  |                 eventtime, print_time) | 
					
						
							|  |  |  |             if buffer_time > self.buffer_time_low: | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |                 # Running normally - reschedule check | 
					
						
							| 
									
										
										
										
											2016-09-19 13:18:55 -04:00
										 |  |  |                 return eventtime + buffer_time - self.buffer_time_low | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |             # Under ran low buffer mark - flush lookahead queue | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |             self._flush_lookahead(must_synch=True) | 
					
						
							| 
									
										
										
										
											2016-09-19 13:18:55 -04:00
										 |  |  |             if print_time != self.print_time: | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |                 # Flushed something - retry | 
					
						
							|  |  |  |                 self.forced_synch = True | 
					
						
							| 
									
										
										
										
											2016-09-19 13:18:55 -04:00
										 |  |  |                 return self.reactor.NOW | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |             if buffer_time > 0.: | 
					
						
							|  |  |  |                 # Wait for buffer to fully empty | 
					
						
							|  |  |  |                 return eventtime + buffer_time | 
					
						
							| 
									
										
										
										
											2016-09-19 13:18:55 -04:00
										 |  |  |             self.reset_print_time() | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             logging.exception("Exception in flush_handler") | 
					
						
							|  |  |  |             self.force_shutdown() | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |         return self.reactor.NEVER | 
					
						
							|  |  |  |     # Motor off timer | 
					
						
							|  |  |  |     def _reset_motor_off(self): | 
					
						
							|  |  |  |         if not self.print_time: | 
					
						
							|  |  |  |             waketime = self.reactor.monotonic() + self.motor_off_time | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             waketime = self.reactor.NEVER | 
					
						
							|  |  |  |         self.reactor.update_timer(self.motor_off_timer, waketime) | 
					
						
							|  |  |  |     def _motor_off_handler(self, eventtime): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             self.motor_off() | 
					
						
							|  |  |  |             self.reset_print_time() | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             logging.exception("Exception in motor_off_handler") | 
					
						
							|  |  |  |             self.force_shutdown() | 
					
						
							|  |  |  |         return self.reactor.NEVER | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |     # Movement commands | 
					
						
							|  |  |  |     def get_position(self): | 
					
						
							|  |  |  |         return list(self.commanded_pos) | 
					
						
							|  |  |  |     def set_position(self, newpos): | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |         self._flush_lookahead() | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         self.commanded_pos[:] = newpos | 
					
						
							|  |  |  |         self.kin.set_position(newpos) | 
					
						
							| 
									
										
										
										
											2016-11-30 22:34:38 -05:00
										 |  |  |     def move(self, newpos, speed): | 
					
						
							| 
									
										
										
										
											2016-09-30 14:47:45 -04:00
										 |  |  |         speed = min(speed, self.max_speed) | 
					
						
							| 
									
										
										
										
											2017-01-14 12:41:49 -05:00
										 |  |  |         move = Move(self, self.commanded_pos, newpos, speed) | 
					
						
							| 
									
										
										
										
											2016-10-25 19:11:05 -04:00
										 |  |  |         if not move.move_d: | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2016-12-28 11:48:40 -05:00
										 |  |  |         if move.is_kinematic_move: | 
					
						
							|  |  |  |             self.kin.check_move(move) | 
					
						
							| 
									
										
										
										
											2016-10-25 19:11:05 -04:00
										 |  |  |         if move.axes_d[3]: | 
					
						
							| 
									
										
										
										
											2016-09-30 14:47:45 -04:00
										 |  |  |             self.extruder.check_move(move) | 
					
						
							|  |  |  |         self.commanded_pos[:] = newpos | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         self.move_queue.add_move(move) | 
					
						
							| 
									
										
										
										
											2016-12-09 09:41:46 -05:00
										 |  |  |         if self.print_time > self.need_check_stall: | 
					
						
							|  |  |  |             self._check_stall() | 
					
						
							| 
									
										
										
										
											2016-11-18 11:27:16 -05:00
										 |  |  |     def home(self, homing_state): | 
					
						
							|  |  |  |         self.kin.home(homing_state) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |     def dwell(self, delay): | 
					
						
							|  |  |  |         self.get_last_move_time() | 
					
						
							|  |  |  |         self.update_move_time(delay) | 
					
						
							| 
									
										
										
										
											2016-12-09 09:41:46 -05:00
										 |  |  |         self._check_stall() | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |     def motor_off(self): | 
					
						
							|  |  |  |         self.dwell(STALL_TIME) | 
					
						
							|  |  |  |         last_move_time = self.get_last_move_time() | 
					
						
							|  |  |  |         self.kin.motor_off(last_move_time) | 
					
						
							| 
									
										
										
										
											2016-07-10 12:23:35 -04:00
										 |  |  |         self.extruder.motor_off(last_move_time) | 
					
						
							| 
									
										
										
										
											2016-07-07 15:52:44 -04:00
										 |  |  |         self.dwell(STALL_TIME) | 
					
						
							|  |  |  |         logging.debug('; Max time of %f' % (last_move_time,)) | 
					
						
							| 
									
										
										
										
											2016-12-28 21:24:12 -05:00
										 |  |  |     def wait_moves(self): | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |         self._flush_lookahead() | 
					
						
							| 
									
										
										
										
											2017-02-06 13:31:34 -05:00
										 |  |  |         eventtime = self.reactor.monotonic() | 
					
						
							| 
									
										
										
										
											2016-12-28 21:24:12 -05:00
										 |  |  |         while self.print_time: | 
					
						
							|  |  |  |             eventtime = self.reactor.pause(eventtime + 0.100) | 
					
						
							| 
									
										
										
										
											2016-12-08 12:42:12 -05:00
										 |  |  |     def query_endstops(self): | 
					
						
							|  |  |  |         last_move_time = self.get_last_move_time() | 
					
						
							|  |  |  |         return self.kin.query_endstops(last_move_time) | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |     # Misc commands | 
					
						
							|  |  |  |     def stats(self, eventtime): | 
					
						
							|  |  |  |         buffer_time = 0. | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |         print_time = self.print_time | 
					
						
							|  |  |  |         if print_time: | 
					
						
							| 
									
										
										
										
											2017-02-06 14:07:55 -05:00
										 |  |  |             buffer_time = self.printer.mcu.get_print_buffer_time( | 
					
						
							| 
									
										
										
										
											2017-02-11 14:43:36 -05:00
										 |  |  |                 eventtime, print_time) | 
					
						
							|  |  |  |             if buffer_time < 0.: | 
					
						
							|  |  |  |                 buffer_time = 0. | 
					
						
							|  |  |  |         return "print_time=%.3f buffer_time=%.3f print_stall=%d" % ( | 
					
						
							|  |  |  |             print_time, buffer_time, self.print_stall) | 
					
						
							| 
									
										
										
										
											2016-09-19 13:18:55 -04:00
										 |  |  |     def force_shutdown(self): | 
					
						
							| 
									
										
										
										
											2017-03-16 01:08:26 -04:00
										 |  |  |         try: | 
					
						
							|  |  |  |             self.printer.mcu.force_shutdown() | 
					
						
							|  |  |  |             self.move_queue.reset() | 
					
						
							|  |  |  |             self.reset_print_time() | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             logging.exception("Exception in force_shutdown") |