Skip to content

Instantly share code, notes, and snippets.

@t-nissie
Last active March 20, 2024 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-nissie/81bc471cd543e92d5cd1c3d36474c8b1 to your computer and use it in GitHub Desktop.
Save t-nissie/81bc471cd543e92d5cd1c3d36474c8b1 to your computer and use it in GitHub Desktop.
Arduino UNO に Klipper をインストールして multi-MCU する

Arduino UNO に Klipper をインストールして multi-MCU する

電源電圧の違いなどでKlipperな3Dプリンターを複数のメインボードでコントロールしたい場合がある。 メインボードの1つとしてATmega328pなArduino UNOとCNC Shieldを使用する方法のメモ。 2024年3月現在Ubuntu 22.04 LTSやDebian 12 Bookwormに入っている avr-gcc 5.4.0を使うと遭遇するエラーの回避方法を書いた。 完全無保証

この文章のオリジナルは https://gist.github.com/t-nissie/81bc471cd543e92d5cd1c3d36474c8b1 にある。

Klipper, Moonraker, Mainsailの詳細については解説していない。 それらについては https://gist.github.com/t-nissie/9256acaf29cc901fa1504d7ace79a2e3 が参考になるかもしれない。

Klipperのmake時のエラーの回避方法

将来、Ubuntu 24.04 LTSやDebian 13であればこの問題は解決されているはずなので、本節の作業はスキップしてよい。

Ubuntu 22.04 LTSやDebian 12 Bookwormに KIAUH (Klipper Installation And Update Helper) などを使ってKlipperといっしょにインストールされるavr-gccは5.4.0。 Klipperのmake時に

/usr/lib/avr/bin/ld: address 0x800931 of out/klipper.elf section `.bss' is not within region `data'

などというエラーになる。 Developing for the ATmega328P under Linuxを参考にして、 avr-gcc 5.4.0などをアンインストールして、 avr-gcc 13.2.0をインストールして回避する。

binutils-2.42, gcc-13.2.0, avr-libc-2.1.0 ソースパッケージをダウンロードし、展開し、buildディレクトリを作っておく。 make -j12とコア数の3倍くらいを設定しておくと吉。 gccのコンパイルは特に時間がかかる。 高速で複数のCPUコアを持ったLinux PCで作業をしてしまうのも吉。

$ sudo apt remove gcc-avr avr-libc binutils-avr
$ sudo apt install libgmp3-dev libmpfr-dev libmpc-dev texinfo   # Install prerequests.

$ cd SOMEWHERE/binutils-2.42/build-avr
$ ../configure --prefix=/usr/local --target=avr
$ make -j12
$ sudo make install

$ cd SOMEWHERE/gcc-13.2.0/build-avr
$ ../configure --prefix=/usr/local --target=avr --enable-languages=c,c++ --disable-nls --disable-libssp
$ make -j12   # It takes a few hours.
$ sudo make install

$ cd SOMEWHERE/avr-libc-2.1.0/build
$ ../configure --prefix=/usr/local --host=avr
$ make -j12   # It takes a few hours.
$ sudo make install

Klipperの再makeとflash

Klipperを再度makeしてUSBケーブル経由で Arduino UNO に書き込む。

$ avr-gcc --version
avr-gcc (GCC) 13.2.0   ← versionを確認
$ cd klipper
$ make menuconfig
$ make -j12
$ ls /dev/serial/by-id
$ make flash FLASH_DEVICE=/dev/serial/by-id/usb-Arduino__www.arduino.cc__XXXX_xxxxxxxxxxxxxxxxxxxx-if00

printer.cfgの設定

klipper/config/sample-multi-mcu.cfg を参考に printer.cfg を書く。 Arduino UNO + CNC Shield のピン配置などは https://github.com/gwisp2/klipper-drawbot を参考にする。

例として本Gistに osiad_black_arduino_printer.cfg を置いた。

Klipperのコントローラーに /home/pi/printer_data/config/printer.cfg などとして置く。

# printer.cfg for ET4000+ board in EasyThreeD X3
# Original file is https://github.com/makerbase-mks/Klipper-for-MKS-Boards/blob/main/MKS%20Robin%20Lite%20V1.1/generic-mks-robin-lite-v1.cfg
# This file contains common pin mappings for MKS Robin Lite V1.1
# boards. To use this config, the firmware should be compiled for the
# STM32F103RCT6. When running "make menuconfig", enable "extra low-level
# configuration setup", select the 28KiB bootloader, and serial (on
# USART1 PA10/PA9) communication.
# Note that the "make flash" command does not work with MKS Robin
# boards. After running "make", run the following command:
# ./scripts/update_mks_robin.py out/klipper.bin out/mksLite.bin
# Copy the file out/mksLite.bin to an SD card and then restart the
# printer with that SD card.
# See docs/Config_Reference.md for a description of parameters.
# See klipper/config/sample-multi-mcu.cfg
[mcu]
serial: /dev/serial/by-id/usb-1a86_USB_Serial-if00-port0
restart_method: command
[mcu uno]
serial: /dev/serial/by-id/usb-Arduino__www.arduino.cc__0043_7583033353835121B0A2-if00
[printer]
kinematics: corexy
max_velocity: 80
max_accel: 2000
max_z_velocity: 25
max_z_accel: 100
[virtual_sdcard]
path: /home/pi/printer_data/gcodes
[stepper_x]
step_pin: PC6
dir_pin: !PB12
enable_pin: !PB10
microsteps: 16
rotation_distance: 40.0
endstop_pin: !PC13
position_min: -3
position_endstop: -3
position_max: 145
homing_speed: 30
[stepper_y]
step_pin: PB11
dir_pin: !PB2
enable_pin: !PB10
microsteps: 16
rotation_distance: 40.0
endstop_pin: !PC0
position_min: -63
position_endstop: -63
position_max: 150
homing_speed: 30
[safe_z_home]
home_xy_position: 0.0,0.0
speed: 50.0
z_hop: 0 #move up 10mm
z_hop_speed: 5
[stepper_z]
step_pin: PB1
dir_pin: !PC5
enable_pin: !PB10
microsteps: 16
rotation_distance: 2
#rotation_distance: 8 # for 8mm lead
endstop_pin: !PC12 #Zmin, Zmax use PB9
#position_endstop: 0.0
position_max: 227
[extruder]
step_pin: uno:PD2
dir_pin: uno:PD5
enable_pin: !uno:PB0
microsteps: 16
gear_ratio: 210:100
rotation_distance: 23.132
full_steps_per_rotation: 2048
nozzle_diameter: 0.400
filament_diameter: 1.750
heater_pin: PC9
sensor_type: EPCOS 100K B57560G104F
sensor_pin: PA0
control: pid
pid_Kp: 20.375
pid_Ki: 0.844
pid_Kd: 123.016
min_temp: 0
max_temp: 250
# [heater_bed]
# heater_pin: PC8
# sensor_type: EPCOS 100K B57560G104F
# sensor_pin: PA1
# control: pid
# pid_Kp: 325.10
# pid_Ki: 63.35
# pid_Kd: 417.10
# min_temp: 0
# max_temp: 130
[fan]
pin: PA8
[pause_resume]
[gcode_macro CANCEL_PRINT]
rename_existing: BASE_CANCEL_PRINT
gcode:
TURN_OFF_HEATERS
CLEAR_PAUSE
SDCARD_RESET_FILE
BASE_CANCEL_PRINT
[gcode_macro PAUSE]
rename_existing: BASE_PAUSE
# change this if you need more or less extrusion
variable_extrude: 1.0
gcode:
##### read E from pause macro #####
{% set E = printer["gcode_macro PAUSE"].extrude|float %}
##### set park positon for x and y #####
# default is your max posion from your printer.cfg
{% set x_park = printer.toolhead.axis_maximum.x|float - 5.0 %}
{% set y_park = printer.toolhead.axis_maximum.y|float - 5.0 %}
##### calculate save lift position #####
{% set max_z = printer.toolhead.axis_maximum.z|float %}
{% set act_z = printer.toolhead.position.z|float %}
{% if act_z < (max_z - 2.0) %}
{% set z_safe = 2.0 %}
{% else %}
{% set z_safe = max_z - act_z %}
{% endif %}
##### end of definitions #####
SAVE_GCODE_STATE NAME=PAUSE_state
BASE_PAUSE
G91
G1 E-{E} F2100
G1 Z{z_safe} F900
G90
G1 X{x_park} Y{y_park} F6000
[gcode_macro RESUME]
rename_existing: BASE_RESUME
gcode:
##### read E from pause macro #####
{% set E = printer["gcode_macro PAUSE"].extrude|float %}
##### end of definitions #####
G91
G1 E{E} F2100
RESTORE_GCODE_STATE NAME=PAUSE_state
BASE_RESUME
[display_status]
########################################
# EXP1 (display) pins
########################################
######################################################################
# Beeper
# M300 : Play tone. Beeper support, as commonly found on usual LCD
# displays (i.e. RepRapDiscount 2004 Smart Controller, RepRapDiscount
# 12864 Full Graphic). This defines a custom I/O pin and a custom
# GCODE macro. Usage:
# M300 [P<ms>] [S<Hz>]
# P is the tone duration, S the tone frequency.
# The frequency won't be pitch perfect.
[output_pin BEEPER_pin]
pin: PD2
# Beeper pin. This parameter must be provided.
pwm: True
# A piezo beeper needs a PWM signal, a DC buzzer doesn't.
value: 0
# Silent at power on, set to 1 if active low.
shutdown_value: 0
# Disable at emergency shutdown (no PWM would be available anyway).
cycle_time: 0.001
# Default PWM frequency : 0.001 = 1ms will give a tone of 1kHz
# Although not pitch perfect.
[gcode_macro M300]
gcode:
# Use a default 1kHz tone if S is omitted.
{% set S = params.S|default(1000)|int %}
# Use a 10ms duration is P is omitted.
{% set P = params.P|default(100)|int %}
SET_PIN PIN=BEEPER_pin VALUE=0.5 CYCLE_TIME={ 1.0/S if S > 0 else 1 }
G4 P{P}
SET_PIN PIN=BEEPER_pin VALUE=0
[board_pins]
aliases:
# EXP1 header
EXP1_2=<5V>, EXP1_4=<RST>, EXP1_6=PB4, EXP1_8=PB5, EXP1_10=PB15,EXP1_12=<3V3>,EXP1_14=PD2,
EXP1_1=PC3, EXP1_3=PC2, EXP1_5=PB3, EXP1_7=PC1, EXP1_9=PB13,EXP1_11=<GND>,EXP1_13=PA11,
# Pins PB14, EXP1_10, EXP1_9 are also MISO, MOSI, SCK of bus "ssp2"
# See the MKS Lcd Config path file for definitions of common LCD displays.
#[include mks_robin_lite_lcd_12864.cfg]
#[include mks_robin_lite_mini_12864.cfg]
#*# <---------------------- SAVE_CONFIG ---------------------->
#*# DO NOT EDIT THIS BLOCK OR BELOW. The contents are auto-generated.
#*#
#*# [stepper_z]
#*# position_endstop = 0.010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment