Skip to content

Commit

Permalink
Merge branch 'STEAPP-872' of https://github.com/stereotech/klipper in…
Browse files Browse the repository at this point in the history
…to STEAPP-872
  • Loading branch information
SokolovJek committed Feb 13, 2024
2 parents 1dcff52 + b63577a commit 3656dc1
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 87 deletions.
73 changes: 45 additions & 28 deletions klippy/extras/a_axis_offset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Helper script to automate a axis offset calculation

import math
import logging


RAD_TO_DEG = 57.295779513

Expand All @@ -10,18 +12,24 @@ def __init__(self, config):
self.printer = config.get_printer()
self.gcode_move = self.printer.load_object(config, 'gcode_move')
self.gcode = self.printer.lookup_object('gcode')
self.calc_offset = 0.
self.max_repeat_probe = config.getint('max_repeat_probe', 5)
self.threshold_value = config.getfloat('threshold_value', 0.02)
self.point_coords = [
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]
]
self.gcode.register_command('CALC_A_AXIS_OFFSET',
self.cmd_CALC_A_AXIS_OFFSET,
desc=self.cmd_CALC_A_AXIS_OFFSET_help)
self.gcode.register_command(
'ALIGN_A_AXIS', self.cmd_ALIGN_A_AXIS,
desc=self.cmd_ALIGN_A_AXIS_help)
self.gcode.register_command(
'SAVE_A_AXIS_POINT', self.cmd_SAVE_A_AXIS_POINT,
desc=self.cmd_SAVE_A_AXIS_POINT_help)
self.gcode.register_command(
'MOVE_ALIGN_A_AXIS', self.cmd_MOVE_ALIGN_A_AXIS,
desc=self.cmd_MOVE_ALIGN_A_AXIS_help)

cmd_SAVE_A_AXIS_POINT_help = "Save point for A axis offset"
def cmd_SAVE_A_AXIS_POINT(self, gcmd):
Expand All @@ -41,37 +49,46 @@ def cmd_SAVE_A_AXIS_POINT(self, gcmd):
self.point_coords[point_idx][axis] = coord

def _calc_a_axis_offset(self, point_0, point_1):
offset = RAD_TO_DEG * math.asin((point_1[2] - point_0[2]) / math.hypot(
point_1[1] - point_0[1], point_1[2] - point_0[2]))
"""
. b
. .
. .
a . . . . c
"""
bc = point_1[2] - point_0[2]
ac = point_1[1] - point_0[1]
ab = math.hypot(ac, bc)
asin_a = math.asin(bc / ab)
offset = RAD_TO_DEG * asin_a
logging.info("calculate offset for the axis A: %f." % offset)
return offset

cmd_CALC_A_AXIS_OFFSET_help = "Calculate A axis offset"
def cmd_CALC_A_AXIS_OFFSET(self, gcmd):
offset = self._calc_a_axis_offset(
self.point_coords[0], self.point_coords[1])
def _apply_offset_a(self):
homing_origin_a = self.gcode_move.get_status()['homing_origin'].a
if homing_origin_a + offset > 0.0:
offset = 0.0
if (homing_origin_a + self.calc_offset) > 0.0:
logging.warning("A-axis offset is positive")
self.calc_offset = 0.0
offset_gcmd = self.gcode.create_gcode_command(
'SET_GCODE_OFFSET', 'SET_GCODE_OFFSET', {'A_ADJUST': offset})
'SET_GCODE_OFFSET', 'SET_GCODE_OFFSET', {'A_ADJUST': self.calc_offset})
self.gcode_move.cmd_SET_GCODE_OFFSET(offset_gcmd)
gcmd.respond_info("calculate offset for the axis A: %f." % offset)
logging.info("apply offset for the axis A: %f." % self.calc_offset)

# cmd_CALC_A_AXIS_OFFSET_TOOL_help = "Calculate A axis offset by tool"
# def cmd_CALC_A_AXIS_OFFSET_TOOL(self, gcmd):
# # get average value axis Z
# self.point_coords[0][2] = (self.point_coords[0][2] + self.point_coords[2][2]) / 2.
# self.point_coords[1][2] = (self.point_coords[1][2] + self.point_coords[3][2]) / 2.
# # calculate offset
# offset = self._calc_a_axis_offset(
# self.point_coords[0], self.point_coords[1])
# homing_origin_a = self.gcode_move.get_status()['homing_origin'].a
# if homing_origin_a + offset > 0.0:
# offset = 0.0
# offset_gcmd = self.gcode.create_gcode_command(
# 'SET_GCODE_OFFSET', 'SET_GCODE_OFFSET', {'A_ADJUST': offset})
# self.gcode_move.cmd_SET_GCODE_OFFSET(offset_gcmd)
# gcmd.respond_info("calculate offset for the axis A: %f." % offset)
cmd_ALIGN_A_AXIS_help = "Calculate A axis offset"
def cmd_ALIGN_A_AXIS(self, gcmd):
mode = gcmd.get('MODE')
for i in range(self.max_repeat_probe):
self.gcode.run_script_from_command("MOVE_ALIGN_A_AXIS MODE=%s" % mode)
self.calc_offset = self._calc_a_axis_offset(self.point_coords[0], self.point_coords[1])
if abs(self.calc_offset) <= self.threshold_value:
break
else:
self._apply_offset_a()

cmd_MOVE_ALIGN_A_AXIS_help = "By default, it returns the offset of the axis,\
if necessary, perform rename_existing of this command in the macro"
def cmd_MOVE_ALIGN_A_AXIS(self, gcmd):
homing_origin_a = self.gcode_move.get_status()['homing_origin'].a
gcmd.respond_info("offset A-axis= %s" % homing_origin_a)


def load_config(config):
Expand Down
41 changes: 32 additions & 9 deletions klippy/extras/c_axis_align.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Helper script to automate a axis offset calculation

import math
import logging


RAD_TO_DEG = 57.295779513

Expand All @@ -10,16 +12,23 @@ def __init__(self, config):
self.printer = config.get_printer()
self.gcode_move = self.printer.load_object(config, 'gcode_move')
self.gcode = self.printer.lookup_object('gcode')
self.max_repeat_probe = config.getint('max_repeat_probe', 5)
self.threshold_value = config.getfloat('threshold_value', 0.02)
self.point_coords = [
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]
]
self.gcode.register_command('CALC_C_AXIS_ALIGN',
self.cmd_CALC_C_AXIS_ALIGN,
desc=self.cmd_CALC_C_AXIS_ALIGN_help)
self.gcode.register_command(
'ALIGN_C_AXIS', self.cmd_ALIGN_C_AXIS,
desc=self.cmd_ALIGN_C_AXIS_help)
self.gcode.register_command(
'SAVE_C_AXIS_POINT', self.cmd_SAVE_C_AXIS_POINT,
desc=self.cmd_SAVE_C_AXIS_POINT_help)
self.gcode.register_command(
'MOVE_ALIGN_C_AXIS', self.cmd_MOVE_ALIGN_C_AXIS,
desc=self.cmd_MOVE_ALIGN_C_AXIS_help)

cmd_SAVE_C_AXIS_POINT_help = "Save point for C axis align"

def cmd_SAVE_C_AXIS_POINT(self, gcmd):
point_idx = gcmd.get_int('POINT', 0)
Expand All @@ -37,23 +46,37 @@ def cmd_SAVE_C_AXIS_POINT(self, gcmd):
for axis, coord in enumerate(coords):
self.point_coords[point_idx][axis] = coord

cmd_SAVE_C_AXIS_POINT_help = "Save point for C axis align"
cmd_ALIGN_C_AXIS_help = "Calculate and apply correction for align the C axis"

def cmd_ALIGN_C_AXIS(self, gcmd):
for i in range(self.max_repeat_probe):
self.gcode.run_script_from_command("MOVE_ALIGN_C_AXIS")
offset = self._calc_c_axis_align(
self.point_coords[0], self.point_coords[1])
if abs(offset) <= self.threshold_value:
logging.info("align the axis C completed")
break
else:
self._apply_offset_c(offset)

cmd_MOVE_ALIGN_C_AXIS_help = "By default, it returns the offset of the\
axis, if necessary, perform rename_existing of this command in the macro"
def cmd_MOVE_ALIGN_C_AXIS(self, gcmd):
gcmd.respond_info("the MOVE_ALIGN_C_AXIS command is not supported")

def _calc_c_axis_align(self, point_0, point_1):
offset = math.atan((point_1[1] - point_0[1]) / 90) * RAD_TO_DEG / 3
logging.info("calculate offset for the axis C: %f." % offset)
return offset

def cmd_CALC_C_AXIS_ALIGN(self, gcmd):
offset = self._calc_c_axis_align(
self.point_coords[0], self.point_coords[1])
def _apply_offset_c(self, offset):
logging.info("an offset has been applied to correct the C axis")
align_gcmd = self.gcode.create_gcode_command(
'G0', 'G0', {'C': offset})
self.gcode_move.cmd_G1(align_gcmd)
self.gcode_move.cmd_G92(self.gcode.create_gcode_command(
'G92', 'G92', {'C': 0}))

cmd_CALC_C_AXIS_ALIGN_help = "Calculate C axis align"


def load_config(config):
CAxisAlignCalculation(config)
4 changes: 2 additions & 2 deletions stereotech_config/750/calibrate/probe_v3.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ gcode:
; set approximate radius, length and measuring the wcs_1_z
CALC_TOOL_PARAMS LENGTH=1 APPROXIMATE_RADIUS=1
; checking and apply offset for axis A
ALIGN_A_AXIS_TOOL
ALIGN_A_AXIS MODE=tool
; move for measuring the wcs_2_y
PROBE_TOOL_POINT POINT=A_Y_A90
ADJUST_BASEMENT_WCS_V2 WCS=2
Expand Down Expand Up @@ -107,7 +107,7 @@ gcode:
{% set tool_length = printer['gcode_macro CALC_TOOL_PARAMS'].length|float %}
{% if radius < 5.0 and tool_length > 35.0 %}
; checking and apply offset for axis A
ALIGN_A_AXIS_TOOL
ALIGN_A_AXIS MODE=tool
; move for measuring the wcs_2_x
PROBE_TOOL_POINT POINT=A_X_A90
SET_POINT MACRO=SAVE_WCS_CALC_POINT POINT=0
Expand Down
17 changes: 17 additions & 0 deletions stereotech_config/calibrate/probe_5d.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,20 @@ gcode:
{% set wcs_4 = printer.gcode_move.wcs_offsets[4] %}
G10 L2 P2 X{wcs_3[0]} Y{wcs_3[1]} Z{wcs_3[2]}
G10 L2 P3 X{wcs_4[0]} Y{wcs_4[1]} Z{wcs_4[2]}

[gcode_macro MOVE_ALIGN_A_AXIS]
description: movements to obtain the points necessary to calculate the offset of the A axis
rename_existing: MOVE_ALIGN_A_AXIS_OLD
gcode:
{% set mode = params.MODE %}
{% if mode == 'template' %}
PROBE_TEMPLATE_POINT POINT=A_Z
SET_POINT MACRO=SAVE_A_AXIS_POINT POINT=0
PROBE_TEMPLATE_POINT POINT=B_Z
SET_POINT MACRO=SAVE_A_AXIS_POINT POINT=1
{% elif mode == 'tool' %}
PROBE_TOOL_POINT POINT=B_Z_A90
SET_POINT MACRO=SAVE_A_AXIS_POINT POINT=0
PROBE_TOOL_POINT POINT=A_Z_A90
SET_POINT MACRO=SAVE_A_AXIS_POINT POINT=1
{% endif %}
33 changes: 8 additions & 25 deletions stereotech_config/calibrate/probe_5d_template.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gcode:
G0 C0
ADJUST_TEMPLATE_HEIGHT A=0
ALIGN_C_AXIS
ALIGN_A_AXIS
ALIGN_A_AXIS MODE=template
ADJUST_TEMPLATE_HEIGHT A=0
ADJUST_TEMPLATE_HEIGHT A=90
AUTO_WCS
Expand All @@ -31,31 +31,14 @@ gcode:
{% set v = 'v_' if a == 90 else '' %}
SET_GCODE_VARIABLE MACRO=PROBE_TEMPLATE_POINT VARIABLE={"a_" ~ v ~"probe_z"} VALUE={printer.probe.last_result[2] - printer.gcode_move.homing_origin.z}

[gcode_macro ALIGN_C_AXIS]
description: align template along axis x
variable_repeat: 2
[gcode_macro MOVE_ALIGN_C_AXIS]
description: moves for align template along axis X
rename_existing: MOVE_ALIGN_C_AXIS_OLD
gcode:
{% set repeat = printer["gcode_macro ALIGN_C_AXIS"].repeat %}
{% for idx in range(repeat) %}
PROBE_TEMPLATE_POINT POINT=D_Y
SET_POINT MACRO=SAVE_C_AXIS_POINT POINT=1
PROBE_TEMPLATE_POINT POINT=C_Y
SET_POINT MACRO=SAVE_C_AXIS_POINT POINT=0
CALC_C_AXIS_ALIGN
{% endfor %}

[gcode_macro ALIGN_A_AXIS]
description: align template along horizontal plane
variable_repeat: 2
gcode:
{% set repeat = printer["gcode_macro ALIGN_A_AXIS"].repeat %}
{% for idx in range(repeat) %}
PROBE_TEMPLATE_POINT POINT=A_Z
SET_POINT MACRO=SAVE_A_AXIS_POINT POINT=0
PROBE_TEMPLATE_POINT POINT=B_Z
SET_POINT MACRO=SAVE_A_AXIS_POINT POINT=1
CALC_A_AXIS_OFFSET
{% endfor %}
PROBE_TEMPLATE_POINT POINT=D_Y
SET_POINT MACRO=SAVE_C_AXIS_POINT POINT=1
PROBE_TEMPLATE_POINT POINT=C_Y
SET_POINT MACRO=SAVE_C_AXIS_POINT POINT=0

[gcode_macro MOVE_TO_AUTO_WCS]
gcode:
Expand Down
18 changes: 0 additions & 18 deletions stereotech_config/calibrate/probe_5d_tool.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,6 @@ gcode:
G10 L2 P{wcs + 1} {axis_names[axis]}{value}
{% endif %}

[gcode_macro ALIGN_A_AXIS_TOOL]
description: align template along horizontal plane
variable_repeat: 2
gcode:
{% set repeat = printer["gcode_macro ALIGN_A_AXIS_TOOL"].repeat %}
{% for idx in range(repeat) %}
PROBE_TOOL_POINT POINT=B_Z_A90
SET_POINT MACRO=SAVE_A_AXIS_POINT POINT=0
PROBE_TOOL_POINT POINT=A_Z_A90
SET_POINT MACRO=SAVE_A_AXIS_POINT POINT=1
# PROBE_TOOL_POINT POINT=B_Z_A90_C60
# SET_POINT MACRO=SAVE_A_AXIS_POINT POINT=2
# PROBE_TOOL_POINT POINT=A_Z_A90_C60
# SET_POINT MACRO=SAVE_A_AXIS_POINT POINT=3
# CALC_A_AXIS_OFFSET_TOOL
CALC_A_AXIS_OFFSET
{% endfor %}

[gcode_macro CHECK_SKEW_TOOL]
description: This macro calculates the x-axis skew between four points using the average.
gcode:
Expand Down
4 changes: 2 additions & 2 deletions stereotech_config/calibrate/probe_v2.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gcode:
; set approximate radius, length and measuring the wcs_1_z
CALC_TOOL_PARAMS LENGTH=1 APPROXIMATE_RADIUS=1
; checking and apply offset for axis A
ALIGN_A_AXIS_TOOL
ALIGN_A_AXIS MODE=tool
; move for measuring the wcs_2_y
PROBE_TOOL_POINT POINT=A_Y_A90
ADJUST_BASEMENT_WCS_V2 WCS=2
Expand Down Expand Up @@ -100,7 +100,7 @@ gcode:
{% set tool_length = printer['gcode_macro CALC_TOOL_PARAMS'].length|float %}
{% if radius < 5.0 and tool_length > 35.0 %}
; checking and apply offset for axis A
ALIGN_A_AXIS_TOOL
ALIGN_A_AXIS MODE=tool
; move for measuring the wcs_2_x
PROBE_TOOL_POINT POINT=A_X_A90
SET_POINT MACRO=SAVE_WCS_CALC_POINT POINT=0
Expand Down
2 changes: 1 addition & 1 deletion test/klippy/five_axis.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ a: 0.0
c: 0.0
e: 0.0

#[a_axis_offset]
[a_axis_offset]

[wcs_1]
x: 160.7
Expand Down
4 changes: 2 additions & 2 deletions test/klippy/five_axis.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ CONFIG five_axis.cfg

G28
SAVE_A_AXIS_POINT POINT=0 COORDS=100.0,140.0,120.0
SAVE_A_AXIS_POINT POINT=1 COORDS=100.0,90.0,110.0
CALC_A_AXIS_OFFSET
SAVE_A_AXIS_POINT POINT=1 COORDS=100.0,90.0,119.5
ALIGN_A_AXIS MODE=tool
SAVE_CONFIG

0 comments on commit 3656dc1

Please sign in to comment.