def probe_finalize(self, offsets, positions): # Setup for coordinate descent analysis z_offset = offsets[2] logging.info("Calculating bed tilt with: %s", positions) params = { 'x_adjust': 0., 'y_adjust': 0., 'z_adjust': z_offset } # Perform coordinate descent def adjusted_height(pos, params): x, y, z = pos return (z - x*params['x_adjust'] - y*params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent( params.keys(), params, errorfunc) # Apply results logging.info("Calculated bed tilt parameters: %s", new_params) x_adjust = new_params['x_adjust'] y_adjust = new_params['y_adjust'] z_adjust = (new_params['z_adjust'] - z_offset - x_adjust * offsets[0] - y_adjust * offsets[1]) try: self.adjust_steppers(x_adjust, y_adjust, z_adjust) except: logging.exception("z_tilt adjust_steppers") for s in self.z_steppers: s.set_ignore_move(False) raise
def finalize(self, positions): kin = self.printer.lookup_object('toolhead').get_kinematics() logging.info("Calculating delta_calibrate with: %s", positions) params = kin.get_calibrate_params() logging.info("Initial delta_calibrate parameters: %s", params) adj_params = ('endstop_a', 'endstop_b', 'endstop_c', 'radius', 'angle_a', 'angle_b') def delta_errorfunc(params): total_error = 0. for spos in positions: x, y, z = delta.get_position_from_stable(spos, params) total_error += (z - self.probe_z_offset)**2 return total_error new_params = mathutil.coordinate_descent(adj_params, params, delta_errorfunc) logging.info("Calculated delta_calibrate parameters: %s", new_params) for spos in positions: logging.info("orig: %s new: %s", delta.get_position_from_stable(spos, params), delta.get_position_from_stable(spos, new_params)) self.gcode.respond_info( "stepper_a: position_endstop: %.6f angle: %.6f\n" "stepper_b: position_endstop: %.6f angle: %.6f\n" "stepper_c: position_endstop: %.6f angle: %.6f\n" "radius: %.6f\n" "To use these parameters, update the printer config file with\n" "the above and then issue a RESTART command" % (new_params['endstop_a'], new_params['angle_a'], new_params['endstop_b'], new_params['angle_b'], new_params['endstop_c'], new_params['angle_c'], new_params['radius']))
def finalize(self, z_offset, positions): logging.info("Calculating bed tilt with: %s", positions) params = {'x_adjust': 0., 'y_adjust': 0., 'z_adjust': z_offset} def adjusted_height(pos, params): x, y, z = pos return (z - x * params['x_adjust'] - y * params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent(params.keys(), params, errorfunc) logging.info("Calculated bed tilt parameters: %s", new_params) try: self.adjust_steppers(new_params['x_adjust'], new_params['y_adjust'], new_params['z_adjust'], z_offset) except: logging.exception("z_tilt adjust_steppers") for s in self.z_steppers: z.set_ignore_move(False) raise
def probe_finalize(self, offsets, positions): # Setup for coordinate descent analysis z_offset = offsets[2] logging.info("Calculating bed tilt with: %s", positions) params = { 'x_adjust': 0., 'y_adjust': 0., 'z_adjust': z_offset } # Perform coordinate descent def adjusted_height(pos, params): x, y, z = pos return (z - x*params['x_adjust'] - y*params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent( params.keys(), params, errorfunc) # Apply results logging.info("Calculated bed tilt parameters: %s", new_params) x_adjust = new_params['x_adjust'] y_adjust = new_params['y_adjust'] z_adjust = (new_params['z_adjust'] - z_offset - x_adjust * offsets[0] - y_adjust * offsets[1]) try: self.adjust_steppers(x_adjust, y_adjust, z_adjust) except: logging.exception("z_tilt adjust_steppers") for s in self.z_steppers: s.set_ignore_move(False) raise
def probe_finalize(self, offsets, positions): # Setup for coordinate descent analysis z_offset = offsets[2] logging.info("Calculating bed tilt with: %s", positions) params = { 'x_adjust': 0., 'y_adjust': 0., 'z_adjust': z_offset } # Perform coordinate descent def adjusted_height(pos, params): x, y, z = pos return (z - x*params['x_adjust'] - y*params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent( params.keys(), params, errorfunc) # Apply results speed = self.probe_helper.get_lift_speed() logging.info("Calculated bed tilt parameters: %s", new_params) x_adjust = new_params['x_adjust'] y_adjust = new_params['y_adjust'] z_adjust = (new_params['z_adjust'] - z_offset - x_adjust * offsets[0] - y_adjust * offsets[1]) adjustments = [x*x_adjust + y*y_adjust + z_adjust for x, y in self.z_positions] self.z_helper.adjust_steppers(adjustments, speed) return self.retry_helper.check_retry([p[2] for p in positions])
def finalize(self, offsets, positions): z_offset = offsets[2] kin = self.printer.lookup_object('toolhead').get_kinematics() logging.info("Calculating delta_calibrate with: %s", positions) params = kin.get_calibrate_params() logging.info("Initial delta_calibrate parameters: %s", params) adj_params = ('endstop_a', 'endstop_b', 'endstop_c', 'radius', 'angle_a', 'angle_b') def delta_errorfunc(params): total_error = 0. for x, y, z in kin.get_positions_from_stable(positions, params): total_error += (z - z_offset)**2 return total_error new_params = mathutil.coordinate_descent( adj_params, params, delta_errorfunc) logging.info("Calculated delta_calibrate parameters: %s", new_params) old_positions = kin.get_positions_from_stable(positions, params) new_positions = kin.get_positions_from_stable(positions, new_params) for oldpos, newpos in zip(old_positions, new_positions): logging.info("orig: %s new: %s", oldpos, newpos) self.gcode.respond_info( "stepper_a: position_endstop: %.6f angle: %.6f\n" "stepper_b: position_endstop: %.6f angle: %.6f\n" "stepper_c: position_endstop: %.6f angle: %.6f\n" "delta_radius: %.6f\n" "To use these parameters, update the printer config file with\n" "the above and then issue a RESTART command" % ( new_params['endstop_a'], new_params['angle_a'], new_params['endstop_b'], new_params['angle_b'], new_params['endstop_c'], new_params['angle_c'], new_params['radius']))
def finalize(self, positions): logging.info("Calculating bed_tilt with: %s", positions) params = { 'x_adjust': self.bedtilt.x_adjust, 'y_adjust': self.bedtilt.y_adjust, 'z_adjust': self.probe_z_offset } logging.info("Initial bed_tilt parameters: %s", params) def adjusted_height(pos, params): x, y, z = pos return (z - x * params['x_adjust'] - y * params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent(params.keys(), params, errorfunc) logging.info("Calculated bed_tilt parameters: %s", new_params) for pos in positions: logging.info("orig: %s new: %s", adjusted_height(pos, params), adjusted_height(pos, new_params)) z_warn = "" z_diff = new_params['z_adjust'] - self.probe_z_offset if abs(z_diff) > .010: z_warn = "Note: Z offset was %.6f\n" % (z_diff, ) self.gcode.respond_info( "%sx_adjust: %.6f y_adjust: %.6f\n" "To use these parameters, update the printer config file with\n" "the above and then issue a RESTART command" % (z_warn, new_params['x_adjust'], new_params['y_adjust']))
def finalize(self, offsets, positions): z_offset = offsets[2] logging.info("Calculating bed_tilt with: %s", positions) params = { 'x_adjust': self.bedtilt.x_adjust, 'y_adjust': self.bedtilt.y_adjust, 'z_adjust': z_offset } logging.info("Initial bed_tilt parameters: %s", params) def adjusted_height(pos, params): x, y, z = pos return (z - x * params['x_adjust'] - y * params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent(params.keys(), params, errorfunc) logging.info("Calculated bed_tilt parameters: %s", new_params) for pos in positions: logging.info("orig: %s new: %s", adjusted_height(pos, params), adjusted_height(pos, new_params)) # Update current bed_tilt calculations bed_tilt = self.printer.lookup_object('bed_tilt') bed_tilt.x_adjust = new_params['x_adjust'] bed_tilt.y_adjust = new_params['y_adjust'] z_diff = new_params['z_adjust'] - z_offset bed_tilt.z_adjust = z_diff self.gcode.reset_last_position() # Report results back to user if self.z_position_endstop is not None: # Cartesian style robot z_extra = "" probe = self.printer.lookup_object('probe', None) if probe is not None: last_home_position = probe.last_home_position() if last_home_position is not None: # Using z_virtual_endstop home_x, home_y = last_home_position[:2] z_diff -= home_x * new_params['x_adjust'] z_diff -= home_y * new_params['y_adjust'] z_extra = " (when Z homing at %.3f,%.3f)" % (home_x, home_y) z_adjust = "stepper_z position_endstop: %.6f%s\n" % ( self.z_position_endstop - z_diff, z_extra) else: # Delta (or other) style robot z_adjust = "Add %.6f to endstop position\n" % (-z_diff, ) msg = "%sx_adjust: %.6f y_adjust: %.6f" % ( z_adjust, new_params['x_adjust'], new_params['y_adjust']) self.printer.set_rollover_info("bed_tilt", "bed_tilt: %s" % (msg, )) self.gcode.respond_info( "%s\nThe above parameters have been applied to the current\n" "session. Update the printer config file with the above to\n" "use these settings in future sessions." % (msg, ))
def finalize(self, offsets, positions): z_offset = offsets[2] logging.info("Calculating bed_tilt with: %s", positions) params = { 'x_adjust': self.bedtilt.x_adjust, 'y_adjust': self.bedtilt.y_adjust, 'z_adjust': z_offset } logging.info("Initial bed_tilt parameters: %s", params) def adjusted_height(pos, params): x, y, z = pos return (z - x*params['x_adjust'] - y*params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent( params.keys(), params, errorfunc) logging.info("Calculated bed_tilt parameters: %s", new_params) for pos in positions: logging.info("orig: %s new: %s", adjusted_height(pos, params), adjusted_height(pos, new_params)) # Update current bed_tilt calculations bed_tilt = self.printer.lookup_object('bed_tilt') bed_tilt.x_adjust = new_params['x_adjust'] bed_tilt.y_adjust = new_params['y_adjust'] z_diff = new_params['z_adjust'] - z_offset bed_tilt.z_adjust = z_diff self.gcode.reset_last_position() # Report results back to user if self.z_position_endstop is not None: # Cartesian style robot z_extra = "" probe = self.printer.lookup_object('probe', None) if probe is not None: last_home_position = probe.last_home_position() if last_home_position is not None: # Using z_virtual_endstop home_x, home_y = last_home_position[:2] z_diff -= home_x * new_params['x_adjust'] z_diff -= home_y * new_params['y_adjust'] z_extra = " (when Z homing at %.3f,%.3f)" % (home_x, home_y) z_adjust = "stepper_z position_endstop: %.6f%s\n" % ( self.z_position_endstop - z_diff, z_extra) else: # Delta (or other) style robot z_adjust = "Add %.6f to endstop position\n" % (-z_diff,) msg = "%sx_adjust: %.6f y_adjust: %.6f" % ( z_adjust, new_params['x_adjust'], new_params['y_adjust']) self.printer.set_rollover_info("bed_tilt", "bed_tilt: %s" % (msg,)) self.gcode.respond_info( "%s\nThe above parameters have been applied to the current\n" "session. Update the printer config file with the above to\n" "use these settings in future sessions." % (msg,))
def finalize(self, z_offset, positions): logging.info("Calculating bed_tilt with: %s", positions) params = { 'x_adjust': self.bedtilt.x_adjust, 'y_adjust': self.bedtilt.y_adjust, 'z_adjust': z_offset } logging.info("Initial bed_tilt parameters: %s", params) def adjusted_height(pos, params): x, y, z = pos return (z - x * params['x_adjust'] - y * params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent(params.keys(), params, errorfunc) logging.info("Calculated bed_tilt parameters: %s", new_params) for pos in positions: logging.info("orig: %s new: %s", adjusted_height(pos, params), adjusted_height(pos, new_params)) z_diff = new_params['z_adjust'] - z_offset if self.z_position_endstop is not None: # Cartesian style robot z_extra = "" probe = self.printer.lookup_object('probe', None) if probe is not None: last_home_position = probe.last_home_position() if last_home_position is not None: # Using z_virtual_endstop home_x, home_y = last_home_position[:2] z_diff -= home_x * new_params['x_adjust'] z_diff -= home_y * new_params['y_adjust'] z_extra = " (when Z homing at %.3f,%.3f)" % (home_x, home_y) z_adjust = "stepper_z position_endstop: %.6f%s\n" % ( self.z_position_endstop - z_diff, z_extra) else: # Delta (or other) style robot z_adjust = "Add %.6f to endstop position\n" % (-z_diff, ) msg = "%sx_adjust: %.6f y_adjust: %.6f" % ( z_adjust, new_params['x_adjust'], new_params['y_adjust']) logging.info("bed_tilt_calibrate: %s", msg) self.gcode.respond_info( "%s\nTo use these parameters, update the printer config file with\n" "the above and then issue a RESTART command" % (msg, ))
def probe_finalize(self, offsets, positions): # Setup for coordinate descent analysis z_offset = offsets[2] logging.info("Calculating bed_tilt with: %s", positions) params = { 'x_adjust': self.bedtilt.x_adjust, 'y_adjust': self.bedtilt.y_adjust, 'z_adjust': z_offset } logging.info("Initial bed_tilt parameters: %s", params) # Perform coordinate descent def adjusted_height(pos, params): x, y, z = pos return (z - x * params['x_adjust'] - y * params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent(params.keys(), params, errorfunc) # Update current bed_tilt calculations x_adjust = new_params['x_adjust'] y_adjust = new_params['y_adjust'] z_adjust = (new_params['z_adjust'] - z_offset - x_adjust * offsets[0] - y_adjust * offsets[1]) self.bedtilt.update_adjust(x_adjust, y_adjust, z_adjust) self.gcode.reset_last_position() # Log and report results logging.info("Calculated bed_tilt parameters: %s", new_params) for pos in positions: logging.info("orig: %s new: %s", adjusted_height(pos, params), adjusted_height(pos, new_params)) msg = "x_adjust: %.6f y_adjust: %.6f z_adjust: %.6f" % ( x_adjust, y_adjust, z_adjust) self.printer.set_rollover_info("bed_tilt", "bed_tilt: %s" % (msg, )) self.gcode.respond_info( "%s\nThe above parameters have been applied to the current\n" "session. The SAVE_CONFIG command will update the printer\n" "config file and restart the printer." % (msg, ))
def probe_finalize(self, offsets, positions): # Setup for coordinate descent analysis z_offset = offsets[2] logging.info("Calculating bed_tilt with: %s", positions) params = { 'x_adjust': self.bedtilt.x_adjust, 'y_adjust': self.bedtilt.y_adjust, 'z_adjust': z_offset } logging.info("Initial bed_tilt parameters: %s", params) # Perform coordinate descent def adjusted_height(pos, params): x, y, z = pos return (z - x*params['x_adjust'] - y*params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent( params.keys(), params, errorfunc) # Update current bed_tilt calculations x_adjust = new_params['x_adjust'] y_adjust = new_params['y_adjust'] z_adjust = (new_params['z_adjust'] - z_offset - x_adjust * offsets[0] - y_adjust * offsets[1]) self.bedtilt.update_adjust(x_adjust, y_adjust, z_adjust) self.gcode.reset_last_position() # Log and report results logging.info("Calculated bed_tilt parameters: %s", new_params) for pos in positions: logging.info("orig: %s new: %s", adjusted_height(pos, params), adjusted_height(pos, new_params)) msg = "x_adjust: %.6f y_adjust: %.6f z_adjust: %.6f" % ( x_adjust, y_adjust, z_adjust) self.printer.set_rollover_info("bed_tilt", "bed_tilt: %s" % (msg,)) self.gcode.respond_info( "%s\nThe above parameters have been applied to the current\n" "session. The SAVE_CONFIG command will update the printer\n" "config file and restart the printer." % (msg,))
def finalize(self, z_offset, positions): logging.info("Calculating bed tilt with: %s", positions) params = { 'x_adjust': 0., 'y_adjust': 0., 'z_adjust': z_offset } def adjusted_height(pos, params): x, y, z = pos return (z - x*params['x_adjust'] - y*params['y_adjust'] - params['z_adjust']) def errorfunc(params): total_error = 0. for pos in positions: total_error += adjusted_height(pos, params)**2 return total_error new_params = mathutil.coordinate_descent( params.keys(), params, errorfunc) logging.info("Calculated bed tilt parameters: %s", new_params) try: self.adjust_steppers(new_params['x_adjust'], new_params['y_adjust'], new_params['z_adjust'], z_offset) except: logging.exception("z_tilt adjust_steppers") for s in self.z_steppers: z.set_ignore_move(False) raise
def calculate_params(self, probe_positions, distances): # Setup for coordinate descent analysis kin = self.printer.lookup_object('toolhead').get_kinematics() params = kin.get_calibrate_params() orig_delta_params = build_delta_params(params) logging.info( "Calculating delta_calibrate with:\n%s\n%s\n" "Initial delta_calibrate parameters: %s", probe_positions, distances, params) adj_params = ('radius', 'angle_a', 'angle_b', 'endstop_a', 'endstop_b', 'endstop_c') z_weight = 1. if distances: adj_params += ('arm_a', 'arm_b', 'arm_c') z_weight = len(distances) / (MEASURE_WEIGHT * len(probe_positions)) # Perform coordinate descent call_count = [0] def delta_errorfunc(params): call_count[0] += 1 if not call_count[0] % 1000: self.gcode.respond_info("Working on calibration...") self.printer.get_reactor().pause(0.) # Build new delta_params for params under test delta_params = build_delta_params(params) # Calculate z height errors total_error = 0. for z_offset, stable_pos in probe_positions: x, y, z = get_position_from_stable(stable_pos, delta_params) total_error += (z - z_offset)**2 total_error *= z_weight # Calculate distance errors for dist, stable_pos1, stable_pos2 in distances: x1, y1, z1 = get_position_from_stable(stable_pos1, delta_params) x2, y2, z2 = get_position_from_stable(stable_pos2, delta_params) d = math.sqrt((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2) total_error += (d - dist)**2 return total_error new_params = mathutil.coordinate_descent(adj_params, params, delta_errorfunc) # Log and report results logging.info("Calculated delta_calibrate parameters: %s", new_params) new_delta_params = build_delta_params(new_params) for z_offset, spos in probe_positions: logging.info("height orig: %.6f new: %.6f goal: %.6f", get_position_from_stable(spos, orig_delta_params)[2], get_position_from_stable(spos, new_delta_params)[2], z_offset) for dist, spos1, spos2 in distances: x1, y1, z1 = get_position_from_stable(spos1, orig_delta_params) x2, y2, z2 = get_position_from_stable(spos2, orig_delta_params) orig_dist = math.sqrt((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2) x1, y1, z1 = get_position_from_stable(spos1, new_delta_params) x2, y2, z2 = get_position_from_stable(spos2, new_delta_params) new_dist = math.sqrt((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2) logging.info("distance orig: %.6f new: %.6f goal: %.6f", orig_dist, new_dist, dist) self.gcode.respond_info( "stepper_a: position_endstop: %.6f angle: %.6f arm: %.6f\n" "stepper_b: position_endstop: %.6f angle: %.6f arm: %.6f\n" "stepper_c: position_endstop: %.6f angle: %.6f arm: %.6f\n" "delta_radius: %.6f\n" "The SAVE_CONFIG command will update the printer config file\n" "with these parameters and restart the printer." % (new_params['endstop_a'], new_params['angle_a'], new_params['arm_a'], new_params['endstop_b'], new_params['angle_b'], new_params['arm_b'], new_params['endstop_c'], new_params['angle_c'], new_params['arm_c'], new_params['radius'])) # Store results for SAVE_CONFIG self.save_state(probe_positions, distances, new_params)