Skip to content

Commit 7a08a9a

Browse files
committed
Refactor custom bed boudary module
1 parent f764efd commit 7a08a9a

1 file changed

Lines changed: 76 additions & 45 deletions

File tree

‎klippy/extras/bed_custom_bound.py‎

Lines changed: 76 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import typing
22

33

4+
###############################
5+
# Example configuration
6+
#
7+
# [bed_custom_bound]
8+
# custom_boundary_x: 0.0, 500.0
9+
# custom_boundary_y: 0.0, 500.0
10+
# travel_speed: 50.0
11+
# park_xy: 0., 500
12+
###############################
13+
414
class BedCustomBound:
515
def __init__(self, config):
616
self.printer = config.get_printer()
@@ -9,10 +19,14 @@ def __init__(self, config):
919
self.printer.register_event_handler("klippy:ready", self.handle_ready)
1020
self.custom_boundary_x = None
1121
if config.getfloatlist("custom_boundary_x", None, count=2) is not None:
12-
self.custom_boundary_x = config.getfloatlist("custom_boundary_x", count=2)
22+
self.custom_boundary_x = config.getfloatlist(
23+
"custom_boundary_x", count=2
24+
)
1325
self.custom_boundary_y = None
1426
if config.getfloatlist("custom_boundary_y", None, count=2) is not None:
15-
self.custom_boundary_y = config.getfloatlist("custom_boundary_y", count=2)
27+
self.custom_boundary_y = config.getfloatlist(
28+
"custom_boundary_y", count=2
29+
)
1630
self.park = None
1731
if config.getfloatlist("park_xy", None, count=2) is not None:
1832
self.park = config.getfloatlist("park_xy", count=2)
@@ -44,9 +58,14 @@ def cmd_SET_CUSTOM_BOUNDARY(self, gcmd):
4458
return
4559
move_to_custom_pos = gcmd.get("MOVE_TO_PARK", False, parser=bool)
4660
self.set_custom_boundary()
61+
4762
if move_to_custom_pos and self.park:
63+
self.gcode.respond_info(
64+
f" PARK POSITION FLOAT CONVERTED {float(self.park[0])} {float(self.park[1])} "
65+
)
66+
4867
self.toolhead.manual_move(
49-
[self.park[0], self.park[1]],
68+
[float(self.park[0]), float(self.park[1])],
5069
self.travel_speed,
5170
)
5271

@@ -58,17 +77,20 @@ def restore_default_boundary(self, eventtime=None):
5877
return
5978
if not self.default_limits_x or not self.default_limits_y:
6079
return
80+
6181
self.gcode.respond_info(
62-
"[CUSTOM BED BOUNDARY] Restoring printer boundary limits."
82+
f"[CUSTOM BED BOUNDARY] Restoring printer boundary limits. {self.default_limits_x} {self.default_limits_y}"
6383
)
84+
6485
kin = self.toolhead.get_kinematics()
86+
self.gcode.respond_info(f"kinematics limits {kin.limits}")
6587
kin.limits[0] = (
66-
self.default_limits_x[0],
67-
self.default_limits_y[1],
88+
float(self.default_limits_x[0]),
89+
float(self.default_limits_x[1]),
6890
) # X min, X max
6991
kin.limits[1] = (
70-
self.default_limits_y[0],
71-
self.default_limits_y[1],
92+
float(self.default_limits_y[0]),
93+
float(self.default_limits_y[1]),
7294
) # Y min , Y max
7395
self.current_boundary = "default"
7496
return
@@ -82,65 +104,74 @@ def set_custom_boundary(self, eventtime=None):
82104
"[CUSTOM BED BOUNDARY] Setting specified custom boundary"
83105
)
84106
kin = self.toolhead.get_kinematics()
107+
108+
self.gcode.respond_info(f"kinematics limits {kin.limits}")
109+
85110
self.default_limits_x, self.default_limits_y = (
86111
kin.limits[0],
87112
kin.limits[1],
88113
)
89114

90115
kin.limits[0] = (
91-
self.custom_boundary_x[0],
92-
self.custom_boundary_x[1],
116+
float(self.custom_boundary_x[0]),
117+
float(self.custom_boundary_x[1]),
93118
) # X min, X max
94119
kin.limits[1] = (
95-
self.custom_boundary_y[0],
96-
self.custom_boundary_y[1],
120+
float(self.custom_boundary_y[0]),
121+
float(self.custom_boundary_y[1]),
97122
) # Y min , Y max
123+
124+
self.gcode.respond_info(
125+
f"Custom boundaries set to X=[{self.custom_boundary_x}] Y=[{self.custom_boundary_y}]"
126+
)
98127
self.current_boundary = "custom"
99128

100129
def move_to_park(self):
101130
if self.park:
102-
self.toolhead.manual_move([self.park[0], self.park[1]], self.travel_speed)
131+
self.toolhead.manual_move(
132+
[self.park[0], self.park[1]], self.travel_speed
133+
)
103134

104135
def check_boundary_limits(
105136
self, position: typing.Tuple[float, float], bound_type: str = "default"
106137
):
107138
if not self.toolhead or not position:
108139
return
109140

110-
_limits = {
111-
"x": True,
112-
"y": True,
113-
}
114-
115-
if bound_type == "default" and self.default_limits_x and self.default_limits_y:
116-
min_limit_x, max_limit_x = (
117-
self.default_limits_x[0],
118-
self.default_limits_x[1],
119-
)
120-
min_limit_y, max_limit_y = (
121-
self.default_limits_y[0],
122-
self.default_limits_y[1],
123-
)
124-
125-
if bound_type == "current":
141+
min_limit_x = max_limit_x = min_limit_y = max_limit_y = None
142+
143+
if (
144+
bound_type == "default"
145+
and self.default_limits_x
146+
and self.default_limits_y
147+
):
148+
min_limit_x = float(self.default_limits_x[0])
149+
max_limit_x = float(self.default_limits_x[1])
150+
min_limit_y = float(self.default_limits_y[0])
151+
max_limit_y = float(self.default_limits_y[1])
152+
elif bound_type == "current":
126153
kin = self.toolhead.get_kinematics()
127-
min_limit_x, max_limit_x = kin.limits[0][0], kin.limits[0][1]
128-
min_limit_y, max_limit_y = kin.limits[1][0], kin.limits[1][1]
129-
if bound_type == "custom" and self.custom_boundary_x and self.custom_boundary_y:
130-
min_limit_x, max_limit_x = (
131-
self.custom_boundary_x[0],
132-
self.custom_boundary_x[1],
133-
)
134-
min_limit_y, max_limit_y = (
135-
self.custom_boundary_y[0],
136-
self.custom_boundary_y[1],
137-
)
154+
min_limit_x = float(kin.limits[0][0])
155+
max_limit_x = float(kin.limits[0][1])
156+
min_limit_y = float(kin.limits[1][0])
157+
max_limit_y = float(kin.limits[1][1])
158+
elif (
159+
bound_type == "custom"
160+
and self.custom_boundary_x
161+
and self.custom_boundary_y
162+
):
163+
min_limit_x = float(self.custom_boundary_x[0])
164+
max_limit_x = float(self.custom_boundary_x[1])
165+
min_limit_y = float(self.custom_boundary_y[0])
166+
max_limit_y = float(self.custom_boundary_y[1])
167+
168+
if None in (min_limit_x, max_limit_x, min_limit_y, max_limit_y):
169+
return None
138170

139-
if min_limit_x < position[0] or max_limit_x < position[0]:
140-
_limits.update({"x": False})
141-
142-
if min_limit_y < position[1] or max_limit_y < position[1]:
143-
_limits.update({"y": False})
171+
_limits = {
172+
"x": min_limit_x <= position[0] and position[0] <= max_limit_x,
173+
"y": min_limit_y <= position[1] and position[1] <= max_limit_y,
174+
}
144175

145176
return _limits
146177

0 commit comments

Comments
 (0)