Skip to content

Commit ed8089b

Browse files
committed
Merge branch 'master' of https://github.com/IridiumIO/PolyCut
2 parents 1b889f1 + 3113094 commit ed8089b

File tree

3 files changed

+868
-1
lines changed

3 files changed

+868
-1
lines changed

Klipper Setup.md

Lines changed: 391 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,391 @@
1+
# Klipper Setup
2+
We're going to set up Klipper to have two separate profiles that will allow us to easily switch between 3D Printing and Plotting/Cutting. After all, we've gone through the effort of making the toolhead hotswappable, so it would be nice to set up Klipper to do the same.
3+
4+
I'm also using Mainsail for the Web interface. You can use Fluidd if you want, but you'll have to figure out those specifics yourself.
5+
6+
This looks complicated, but it really isn't; it's just a lot of copy-and-pasting.
7+
8+
> This is *not* a guide on setting up Klipper from scratch. It assumes you already have your Ender 3 S1 set up in Klipper the way you want for 3D Printing.
9+
10+
## Step 1 - Separating out Printer.cfg
11+
12+
The first thing to do is to **move** everything (except the `SAVE_CONFIG` section!) from the `Printer.cfg` file into its own `3DPrint.cfg` file.
13+
14+
Then we add an `[include 3DPrint.cfg]` line in the `Printer.cfg` and you should be left with *only* that line and the `SAVE_CONFIG` stuff.
15+
16+
**Before (Printer.cfg):**
17+
```yml
18+
19+
[include macros.cfg]
20+
[include mainsail.cfg]
21+
22+
[stepper_x]
23+
step_pin: PC2
24+
dir_pin: PB9
25+
enable_pin: !PC3
26+
microsteps: 16
27+
rotation_distance: 40
28+
...
29+
...other stuff here...
30+
...
31+
32+
33+
34+
#*# <---------------------- SAVE_CONFIG ---------------------->
35+
#*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
36+
#*#
37+
#*# [bltouch]
38+
#*# z_offset = -0.000
39+
#*#
40+
41+
```
42+
43+
**After (Printer.cfg):**
44+
```yml
45+
46+
[include 3DPrint.cfg]
47+
48+
#*# <---------------------- SAVE_CONFIG ---------------------->
49+
#*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
50+
#*#
51+
#*# [bltouch]
52+
#*# z_offset = -0.000
53+
#*#
54+
55+
```
56+
57+
Note: You'll see that my Z_Offset is set to 0. This is **intentional**. You can mess with the Z offset later but it is important to make sure it's set to zero for now, because that offset will persist between both the 3D Print mode and the Cutting Mode.
58+
59+
What we've done now is simply moved everything from the Printer.cfg to its own file, freeing up the ability to make swaps easier.
60+
61+
62+
63+
## Step 2 - Setting up PolyCut.cfg
64+
65+
Duplicate the `3DPrint.cfg` file and call it `PolyCut.cfg`. You should have two files now both with the same content. Not to worry, we'll fix that now. Open up `PolyCut.cfg`
66+
67+
### Deleting Useless Sections
68+
69+
**Delete** the following sections entirely:
70+
71+
- `[include macros.cfg]`
72+
- `[extruder]`
73+
- `[heater_fan hotend_fan]`
74+
- `[fan]`
75+
- `[safe_z_home]`
76+
77+
These are all things you don't need running while in cutting mode. Technically you can delete the `[heater_bed]` section as well but it's not necessary.
78+
> **Make sure you delete `[include macros.cfg]`! We don't need all those printing macros and we'll use our own here**
79+
80+
### Bypass BLTouch
81+
Ideally you would delete the `[bltouch]` section too, but it doesn't like it when you do that. Instead, go to the `[bltouch]` section and change the following line:
82+
```yml
83+
[bltouch]
84+
sensor_pin: ^PC14
85+
...
86+
```
87+
to:
88+
```yml
89+
[bltouch]
90+
sensor_pin: ^!PC14
91+
...
92+
```
93+
Note the exclamation mark, which inverts the default signal and stops it making a fuss when you're not using it.
94+
95+
### Set Manual Homing
96+
97+
Now, we've disabled automatic homing so we have to rig together our own Z-homing mechanism. The new way to home will simply be to lower the Z gantry by hand until the blade touches the cutting mat.
98+
99+
Copy and paste the following into the file:
100+
101+
```yml
102+
[homing_override]
103+
gcode:
104+
{% if printer.toolhead.homed_axes == "xyz" %}
105+
RESPOND TYPE=command MSG="Already Homed"
106+
{% else %}
107+
G91
108+
G1 Z10
109+
G28.1 X Y
110+
G90
111+
G1 Z0
112+
G92 Z0
113+
{% endif %}
114+
115+
axes: xyz
116+
set_position_z: 0.0
117+
118+
[gcode_macro G28]
119+
# Only home if needed.
120+
rename_existing: G28.1
121+
gcode:
122+
{% if printer.toolhead.homed_axes != "xyz" %}
123+
G28.1
124+
{% endif %}
125+
126+
[gcode_macro G00]
127+
gcode:
128+
G0 {rawparams}
129+
130+
[gcode_macro G01]
131+
gcode:
132+
G1 {rawparams}
133+
```
134+
135+
What each part does:
136+
137+
- **`[homing_override]`** - This section bypasses homing on the Z axis since we've removed the BLTouch. **The Z position is set to wherever the print head's height (cutting head?) was when homing was run, so make sure you move the Z gantry down so the blade is touching the cutting surface before you home!** If the blade is 20mm above the bed when you home the printer, that 20mm height becomes the new "zero".
138+
- **`[gcode_macro G28]`** - This bypasses homing entirely if the axes are already homed, so you don't have to keep re-homing between every cutout.
139+
- **`[gcode_macro G00]` and `[gcode_macro G01]`**- These just tell Klipper to interpret all G00 and G01 codes as G0 and G1 instead. Inskcape likes to output double-digits which Klipper can't understand.
140+
141+
142+
### Fixing the Pause and Cancel Buttons
143+
By default, the pause and cancel buttons in Klipper contain code that depends on the extruder being present. Copy and paste the following instead (If you already have these sections in your `PolyCut.cfg` file, overwrite them. I'm assuming most of your macros would be in `macros.cfg` however).
144+
145+
```yml
146+
147+
[gcode_macro CANCEL_PRINT]
148+
description: Cancel the actual running print
149+
rename_existing: CANCEL_PRINT_BASE
150+
gcode:
151+
##### get user parameters or use default #####
152+
{% set allow_park = False if printer['gcode_macro _CLIENT_VARIABLE'] is not defined
153+
else False if printer['gcode_macro _CLIENT_VARIABLE'].park_at_cancel is not defined
154+
else True if printer['gcode_macro _CLIENT_VARIABLE'].park_at_cancel|lower == 'true'
155+
else False %}
156+
{% set retract = 5.0 if not macro_found else client.cancel_retract|default(5.0)|abs %}
157+
{% set sp_retract = 2100 if not macro_found else client.speed_retract|default(35) * 60 %}
158+
##### end of definitions #####
159+
{% if not printer.pause_resume.is_paused and allow_park %} _TOOLHEAD_PARK_PAUSE_CANCEL {% endif %}
160+
161+
TURN_OFF_HEATERS
162+
M106 S0
163+
CANCEL_PRINT_BASE
164+
165+
[gcode_macro PAUSE]
166+
description: Pause the actual running print
167+
rename_existing: PAUSE_BASE
168+
gcode:
169+
PAUSE_BASE
170+
_TOOLHEAD_PARK_PAUSE_CANCEL {rawparams}
171+
172+
[gcode_macro RESUME]
173+
description: Resume the actual running print
174+
rename_existing: RESUME_BASE
175+
gcode:
176+
##### get user parameters or use default #####
177+
{% set macro_found = True if printer['gcode_macro _CLIENT_VARIABLE'] is defined else False %}
178+
{% set client = printer['gcode_macro _CLIENT_VARIABLE'] %}
179+
{% set velocity = printer.configfile.settings.pause_resume.recover_velocity %}
180+
{% set use_fw_retract = False if not macro_found
181+
else False if client.use_fw_retract is not defined
182+
else True if client.use_fw_retract|lower == 'true' and printer.firmware_retraction is defined
183+
else False %}
184+
{% set unretract = 1.0 if not macro_found else client.unretract|default(1.0)|abs %}
185+
{% set sp_unretract = 2100 if not macro_found else client.speed_unretract|default(35) * 60 %}
186+
{% set sp_move = velocity if not macro_found else client.speed_move|default(velocity) %}
187+
##### end of definitions #####
188+
189+
RESUME_BASE VELOCITY={params.VELOCITY|default(sp_move)}
190+
191+
[gcode_macro _TOOLHEAD_PARK_PAUSE_CANCEL]
192+
description: Helper: park toolhead used in PAUSE and CANCEL_PRINT
193+
gcode:
194+
##### get user parameters or use default #####
195+
{% set macro_found = True if printer['gcode_macro _CLIENT_VARIABLE'] is defined else False %}
196+
{% set client = printer['gcode_macro _CLIENT_VARIABLE'] %}
197+
{% set velocity = printer.configfile.settings.pause_resume.recover_velocity %}
198+
{% set use_custom = False if not macro_found
199+
else False if client.use_custom_pos is not defined
200+
else True if client.use_custom_pos|lower == 'true'
201+
else False %}
202+
{% set custom_park_x = 0.0 if not macro_found else client.custom_park_x|default(0.0) %}
203+
{% set custom_park_y = 0.0 if not macro_found else client.custom_park_y|default(0.0) %}
204+
{% set park_dz = 12.0 if not macro_found else client.custom_park_dz|default(2.0)|abs %}
205+
{% set use_fw_retract = False if not macro_found
206+
else False if client.use_fw_retract is not defined
207+
else True if client.use_fw_retract|lower == 'true' and printer.firmware_retraction is defined
208+
else False %}
209+
{% set retract = 1.0 if not macro_found else client.retract|default(1.0)|abs %}
210+
{% set sp_retract = 2100 if not macro_found else client.speed_retract|default(35) * 60 %}
211+
{% set sp_hop = 900 if not macro_found else client.speed_hop|default(15) * 60 %}
212+
{% set sp_move = velocity * 60 if not macro_found else client.speed_move|default(velocity) * 60 %}
213+
##### get config and toolhead values #####
214+
{% set act = printer.toolhead.position %}
215+
{% set max = printer.toolhead.axis_maximum %}
216+
{% set cone = printer.toolhead.cone_start_z|default(max.z) %} ; hight as long the toolhead can reach max and min of an delta
217+
{% set round_bed = True if printer.configfile.settings.printer.kinematics is in ['delta','polar','rotary_delta','winch']
218+
else False %}
219+
##### define park position #####
220+
{% set z_min = params.Z_MIN|default(0)|float %}
221+
{% set z_park = [[(act.z + park_dz), z_min]|max, max.z]|min %}
222+
{% set x_park = params.X if params.X is defined
223+
else custom_park_x if use_custom
224+
else 0.0 if round_bed
225+
else (max.x - 5.0) %}
226+
{% set y_park = params.Y if params.Y is defined
227+
else custom_park_y if use_custom
228+
else (max.y - 5.0) if round_bed and z_park < cone
229+
else 0.0 if round_bed
230+
else (max.y - 5.0) %}
231+
##### end of definitions #####
232+
233+
{% if "xyz" in printer.toolhead.homed_axes %}
234+
G90
235+
G1 Z{z_park} F{sp_hop}
236+
G1 X{x_park} Y{y_park} F{sp_move}
237+
{% if not printer.gcode_move.absolute_coordinates %} G91 {% endif %}
238+
{% else %}
239+
{action_respond_info("Printer not homed")}
240+
{% endif %}
241+
```
242+
243+
### Adding PolyCut.cfg to the main Printer.cfg file
244+
245+
Let's add `PolyCut.cfg` to `Printer.cfg` but we'll leave it commented out for now:
246+
247+
Printer.cfg:
248+
```yml
249+
250+
[include 3DPrint.cfg]
251+
#[include PolyCut.cfg]
252+
253+
#*# <---------------------- SAVE_CONFIG ---------------------->
254+
#*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
255+
#*#
256+
#*# [bltouch]
257+
#*# z_offset = -0.000
258+
#*#
259+
260+
```
261+
You could stop here actually. When you want to switch to cutting mode, you comment out the `[include 3DPrint.cfg]` line and uncomment the `[include PolyCut.cfg]` line, then do a Firmware restart. Vice-versa to switch back to 3D Printing mode.
262+
263+
But we can do better.
264+
265+
## Step 3 - Macros to switch modes
266+
Commenting/Uncommenting is tedious. Let's set up some macros to do this for us.
267+
268+
### Installing G-Code Shell Command
269+
The first thing we need is the ability to run our own scripts. This is the only way I know of to actually modify the files.
270+
The easiest way to get this extension is to get [KIAUH](https://github.com/dw-0/kiauh) which you hopefully had as part of installing Klipper in the first place. If you didn't use it, just install it anyway, no big deal.
271+
272+
SSH into your Raspberry PI (Other boards are available), then run the following to install and run KIAUH:
273+
```bash
274+
sudo apt-get update && sudo apt-get install git -y
275+
```
276+
```bash
277+
cd ~ && git clone https://github.com/dw-0/kiauh.git
278+
```
279+
```bash
280+
./kiauh/kiauh.sh
281+
```
282+
You'll see the KIAUH interface come up.
283+
Press `4` to enter the `Advanced Menu`
284+
Then press `8` to install G-Code Shell Command.
285+
286+
Done!
287+
288+
>Note! Be careful from hereon out. You have enabled a way to run arbitrary system code from within GCode - which means don't download and run random GCode files because they could contain malicious code that can now run.
289+
290+
291+
### ShellCommand File
292+
Open up `printer.cfg` and at the very top, add a new line
293+
```
294+
[include shellcommand.cfg]
295+
```
296+
You guessed it, create a new file called `shellcommand.cfg` and open it.
297+
298+
Paste the following into that file:
299+
300+
```yaml
301+
[gcode_shell_command set_mode]
302+
command: python /home/pi/printer_data/config/SwitchMode.py
303+
timeout: 2.
304+
verbose: False
305+
306+
[gcode_macro SET]
307+
gcode:
308+
RUN_SHELL_COMMAND CMD=set_mode PARAMS={params.MODE}
309+
RESPOND PREFIX=! MSG="Mode set to {params.MODE}"
310+
RESTART
311+
312+
[gcode_macro CUTTING_MODE]
313+
gcode:
314+
SET MODE=CUT
315+
316+
[gcode_macro POLYCUT_MODE]
317+
gcode:
318+
SET MODE=PRINT
319+
```
320+
321+
What did I *just* tell you about downloading random G code and sticking it into your files all willy-nilly? You see that second line?
322+
```yaml
323+
command: python /home/pi/printer_data/config/SwitchMode.py
324+
```
325+
*Any* shell command can be run in that line. I could have entered a command to delete all your files instead. Be careful.
326+
327+
But what we're actually doing is telling it to run a certain python file to switch the mode (We'll create that file in a second).
328+
> Note: If your username is not creatively called "pi" like mine is, make sure you change it in the filepath above, e.g.
329+
> `command: python /home/[YOUR_USERNAME_HERE]/printer_data/config/SwitchMode.py`
330+
331+
- `[gcode_macro SET]` - This is a macro that lets you type `SET MODE=CUT` or `SET MODE=PRINT` into your console, and it passes the `MODE` variable to the python script. It then automatically restarts the firmware.
332+
- `[gcode_macro CUTTING_MODE]` and `[gcode_macro POLYCUT_MODE]` - Shortcuts to the above. We'll use these for creating the interface buttons in Mainsail later.
333+
334+
### SwitchMode File
335+
Let's create that python file now and call it `SwitchMode.py` (create it in the same place your `printer.cfg` and other similar files are)
336+
337+
Open `SwitchMode.py` and paste the following in:
338+
339+
```py
340+
import sys
341+
342+
config_file_path = '/home/pi/printer_data/config/printer.cfg'
343+
344+
def modifyLine(mode):
345+
346+
with open(config_file_path, 'r') as f:
347+
lines = f.readlines()
348+
349+
include_lines = ''
350+
351+
if mode =="CUT" or mode =="C":
352+
include_lines = ['[include 3DPrint.cfg]\n', '#[include PolyCut.cfg]\n']
353+
print("Mode changed to CUT")
354+
elif mode =="PRINT" or mode =="P":
355+
include_lines = ['#[include 3DPrint.cfg]\n', '[include PolyCut.cfg]\n']
356+
print("Mode changed to PRINT")
357+
358+
with open(config_file_path, 'w') as f:
359+
for line in lines:
360+
if line in include_lines:
361+
line = line.lstrip('#') if line.startswith('#') else '#' + line
362+
f.write(line)
363+
364+
if __name__ == "__main__":
365+
# Check if at least one command-line argument is provided
366+
if len(sys.argv) < 2:
367+
print("Usage: python myscript.py <your_variable>")
368+
sys.exit(1)
369+
370+
mode:str = sys.argv[1]
371+
372+
modifyLine(mode.upper())
373+
374+
```
375+
376+
All this is doing is exactly what we could've done manually: Commenting out one line in `printer.cfg` and uncommenting out the other line, depending on what mode you want.
377+
378+
Again, if your username is not "pi", make sure you change it in the `config_file_path` variable at the start of the file.
379+
380+
### Mainsail Buttons
381+
We'll now add buttons to the Mainsail interface so you don't have to keep typing `SET MODE=CUT` and `SET MODE=PRINT`
382+
383+
384+
1. Go to the Dashboard in Mainsail, and in the top right corner click on the cogs icon to open the Interface Settings.
385+
2. Under the `Macros` section, click on `ADD GROUP` at the bottom
386+
3. Call it whatever you want (`Printer Mode` will do) then under `Status`, make sure only the "zzz" icon is enabled (disable the other two options for showing the macro when the printer is paused or printing. You probbaly don't want to switch modes while halfway through something)
387+
4. Under the `Available Macros`, add `CUTTING_MODE` and `PRINTING_MODE`
388+
5. Go to the `Dashboard` settings (Still in the Interface Settings window!) and drag the `Printer Mode` section wherever you want. I like to keep it near the top.
389+
390+
391+

0 commit comments

Comments
 (0)