Skip to content

Commit

Permalink
axis_twist_compensation: Apply suggestions from review
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Apaza Merma <[email protected]>
  • Loading branch information
yochiwarez committed Oct 17, 2024
1 parent ac34a01 commit e93f077
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
4 changes: 0 additions & 4 deletions docs/Config_Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ All dates in this document are approximate.

## Changes

20240709: The `z_compensations` parameter in the `[axis_twist_compensation]`
config section has been renamed to `zx_compensations`. If you don't want
to recalibrate your x_axis_twist_compensation, simply rename the parameter.

20240415: The `on_error_gcode` parameter in the `[virtual_sdcard]`
config section now has a default. If this parameter is not specified
it now defaults to `TURN_OFF_HEATERS`. If the previous behavior is
Expand Down
4 changes: 2 additions & 2 deletions docs/G-Codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ section](Config_Reference.md#axis_twist_compensation) is enabled.
#### AXIS_TWIST_COMPENSATION_CALIBRATE
`AXIS_TWIST_COMPENSATION_CALIBRATE [SAMPLE_COUNT=<value>] [AXIS=<X or Y, default X>]`: Initiates the X or Y
twist calibration wizard. `SAMPLE_COUNT` specifies the number of points along
the X axis to calibrate at and defaults to 3.
`axis` can either be X or Y
the X or Y axis to calibrate at and defaults to 3.

#### AXIS_TWIST_COMPENSATION_AUTOCALIBRATE
`AXIS_TWIST_COMPENSATION_AUTOCALIBRATE` performs automatic calibration to calculate the twist of the X and Y axes without manual measurement.

### [bed_mesh]
Expand Down
42 changes: 20 additions & 22 deletions klippy/extras/axis_twist_compensation.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, config):
self.calibrate_start_x = config.getfloat('calibrate_start_x')
self.calibrate_end_x = config.getfloat('calibrate_end_x')
self.calibrate_y = config.getfloat('calibrate_y')
self.zx_compensations = config.getlists('zx_compensations',
self.z_compensations = config.getlists('z_compensations',
default=[], parser=float)
self.compensation_start_x = config.getfloat('compensation_start_x',
default=None)
Expand Down Expand Up @@ -54,9 +54,9 @@ def __init__(self, config):
self._update_z_compensation_value)

def _update_z_compensation_value(self, pos):
if self.zx_compensations:
if self.z_compensations:
pos[2] += self._get_interpolated_z_compensation(
pos[0], self.zx_compensations,
pos[0], self.z_compensations,
self.compensation_start_x,
self.compensation_end_x
)
Expand Down Expand Up @@ -87,7 +87,7 @@ def _get_interpolated_z_compensation(
return interpolated_z_compensation

def clear_compensations(self):
self.zx_compensations = []
self.z_compensations = []
self.zy_compensations = []
self.m = None
self.b = None
Expand All @@ -111,8 +111,8 @@ def __init__(self, compensation, config):
compensation.calibrate_y)
self.x_end_point = (compensation.calibrate_end_x,
compensation.calibrate_y)
self.y_start_point = (compensation.calibrate_start_y,
compensation.calibrate_x)
self.y_start_point = (compensation.calibrate_x,
compensation.calibrate_start_y)
self.y_end_point = (compensation.calibrate_end_y,
compensation.calibrate_x)
self.results = None
Expand Down Expand Up @@ -157,7 +157,7 @@ def cmd_AXIS_TWIST_COMPENSATION_CALIBRATE(self, gcmd):
axis = gcmd.get('AXIS', 'X')

# check for valid sample_count
if sample_count is None or sample_count < 2:
if sample_count < 2:
raise self.gcmd.error(
"SAMPLE_COUNT to probe must be at least 2")

Expand All @@ -182,13 +182,12 @@ def cmd_AXIS_TWIST_COMPENSATION_CALIBRATE(self, gcmd):
"""
)

start_point = self.y_start_point
start_point = (self.y_start_point[1], self.y_start_point[0])
end_point = self.y_end_point
else:
raise self.gcmd.error(
"AXIS_TWIST_COMPENSATION_CALIBRATE: "
"Invalid axis.")
return

axis_range = end_point[0] - start_point[0]

Expand Down Expand Up @@ -284,6 +283,9 @@ def cmd_AXIS_TWIST_COMPENSATION_AUTOCALIBRATE(self, gcmd):
raise self.gcmd.error(
"SAMPLE_COUNT to probe must be at least 2")

# verify no other manual probe is in progress
manual_probe.verify_no_manual_probe(self.printer)

# clear the current config
self.compensation.clear_compensations()

Expand All @@ -292,12 +294,12 @@ def cmd_AXIS_TWIST_COMPENSATION_AUTOCALIBRATE(self, gcmd):

min_x = self.x_start_point[0]
max_x = self.x_end_point[0]
min_y = self.y_start_point[0]
min_y = self.y_start_point[1]
max_y = self.y_end_point[0]

# calculate x positions
spcx = (max_x - min_x) / (sample_count - 1)
xps = [min_x + spcx * i for i in range(sample_count)]
interval_x = (max_x - min_x) / (sample_count - 1)
xps = [min_x + interval_x * i for i in range(sample_count)]

# Calculate points array
spcy = (max_y - min_y) / (sample_count - 1)
Expand All @@ -313,9 +315,6 @@ def cmd_AXIS_TWIST_COMPENSATION_AUTOCALIBRATE(self, gcmd):
points.append([xps[i], min_y + spcy * idx ])
flip = not flip

# verify no other manual probe is in progress
manual_probe.verify_no_manual_probe(self.printer)


# calculate the points to put the nozzle at, and probe
probe_points = []
Expand All @@ -336,7 +335,7 @@ def cmd_AXIS_TWIST_COMPENSATION_AUTOCALIBRATE(self, gcmd):

# finalize
configfile = self.printer.lookup_object('configfile')
configfile.set(self.configname, 'zx_compensations', x_corr_str)
configfile.set(self.configname, 'z_compensations', x_corr_str)
configfile.set(self.configname, 'compensation_start_x',
self.x_start_point[0])
configfile.set(self.configname, 'compensation_end_x',
Expand All @@ -345,7 +344,7 @@ def cmd_AXIS_TWIST_COMPENSATION_AUTOCALIBRATE(self, gcmd):

configfile.set(self.configname, 'zy_compensations', y_corr_str)
configfile.set(self.configname, 'compensation_start_y',
self.y_start_point[0])
self.y_start_point[1])
configfile.set(self.configname, 'compensation_end_y',
self.y_end_point[0])

Expand All @@ -365,7 +364,6 @@ def _auto_calibration(self, probe_point):

# probe the point
pos = probe.run_single_probe(self.probe, self.gcmd)
#self.current_measured_z = pos[2]

# horizontal_move_z (to prevent probe trigger or hitting bed)
self._move_helper((None, None, self.horizontal_move_z))
Expand Down Expand Up @@ -461,26 +459,26 @@ def _finalize_calibration(self):

if(self.current_axis == 'X'):

configfile.set(self.configname, 'zx_compensations', values_as_str)
configfile.set(self.configname, 'z_compensations', values_as_str)
configfile.set(self.configname, 'compensation_start_x',
self.x_start_point[0])
configfile.set(self.configname, 'compensation_end_x',
self.x_end_point[0])

self.compensation.zx_compensations = self.results
self.compensation.z_compensations = self.results
self.compensation.compensation_start_x = self.x_start_point[0]
self.compensation.compensation_end_x = self.x_end_point[0]

elif(self.current_axis == 'Y'):

configfile.set(self.configname, 'zy_compensations', values_as_str)
configfile.set(self.configname, 'compensation_start_y',
self.y_start_point[0])
self.y_start_point[1])
configfile.set(self.configname, 'compensation_end_y',
self.y_end_point[0])

self.compensation.zy_compensations = self.results
self.compensation.compensation_start_y = self.y_start_point[0]
self.compensation.compensation_start_y = self.y_start_point[1]
self.compensation.compensation_end_y = self.y_end_point[0]

self.gcode.respond_info(
Expand Down

0 comments on commit e93f077

Please sign in to comment.