Adding three extra axes to Marlin
A robot arm with six joints needs six stepper motors, moving together, from one stream of commands.
Code: marlin-firmware on GitHub · Watch: 4DOF arm test rig
A robot arm with six joints needs six stepper motors, moving together, from one stream of commands. A 3D printer board already does that for three or four motors, and current Marlin can drive up to nine axes. So instead of writing my own motion controller, I forked Marlin and turned a printer board into an arm controller.
Why not write it myself?
For the same reason you don’t write your own HTTP server. Marlin already has the hard parts: an acceleration planner, step pulses generated from hardware timers, endstop handling, a G-code parser, and code to configure TMC drivers over UART. All of it has run on millions of printers.
It also gives the arm a simple API. G-code is just lines of text over USB:
G28 ; home every joint
G1 X90 A45 F600 ; move two joints
M114 ; report where every joint is
Anything that can write to a serial port can drive it: Pronterface, a Python script, and later a ROS 2 node.
The board
The BTT Octopus Pro V1.1 has eight stepper driver slots, labelled MOTOR 0 to MOTOR 7, and an STM32H723 chip. In Marlin’s stock pin file, X, Y and Z use slots 0 to 2 (with a second Z motor on slot 3), and slots 4 to 7 are for extruders.
The fork starts from Marlin’s bugfix-2.1.x branch, and builds with PlatformIO:
pio run -e STM32H723ZE_btt
The result is firmware.bin in .pio/build/STM32H723ZE_btt/. Copy it to the root of an SD card, put the card in the board and power it on. The bootloader flashes it and renames the file to FIRMWARE.CUR, which is how you know it worked.
Step 1: turn on the extra axes
Marlin calls axes 4 to 6 I, J and K. You enable them by giving each one a driver type:
// Configuration.h #define MOTHERBOARD BOARD_BTT_OCTOPUS_PRO_V1_1
#define I_DRIVER_TYPE TMC2209 #define J_DRIVER_TYPE TMC2209 #define K_DRIVER_TYPE TMC2209
#ifdef I_DRIVER_TYPE #define AXIS4_NAME ‘A’ #define AXIS4_ROTATES #endif #ifdef J_DRIVER_TYPE #define AXIS5_NAME ‘B’ #define AXIS5_ROTATES #endif #ifdef K_DRIVER_TYPE #define AXIS6_NAME ‘C’ #define AXIS6_ROTATES #endif
The naming is the confusing part. In the config files the axes are always I, J and K: I_MAX_POS, J_CURRENT, K_STALL_SENSITIVITY. In G-code they use whatever AXIS4_NAME and friends say, here A, B and C. It’s like a database column called created_at that your API exposes as createdAt. Same value, two names, and you need to know which side you’re on.
AXIS4_ROTATES tells Marlin the axis is measured in degrees rather than millimetres.
Step 2: remove the printer
A robot arm has no hotend, no heated bed and no extruder, and if Marlin expects a heater but finds no thermistor, it halts with a MINTEMP error. So all of that goes:
#define EXTRUDERS 0
#define TEMP_SENSOR_0 0
#define TEMP_SENSOR_BED 0
The extruder driver types are commented out too, which frees motor slots 4 to 6 for the new axes.
Step 3: tell Marlin it’s an arm
#define ARTICULATED_ROBOT_ARM
This kinematics option maps every joint straight to an axis, with no maths in between. G1 X90 means “turn joint X to 90”, not “move the gripper to x = 90 mm”.
It also changes what F, the feedrate, means. On a printer, F is the speed of the nozzle in millimetres per minute. On an arm there’s no single tool position to measure without kinematics, so Marlin treats each joint as if it were its own axis in a 6-dimensional space, and the move’s length is the length of that 6D vector. For example:
G1 X30 A40 F600
from all zeros moves X by 30° and A by 40°. The length is √(30² + 40²) = 50, and F600 is 600 units per minute, or 10 per second. So the move takes 5 seconds, and both joints start and finish together. That’s a sensible behaviour, but it’s worth knowing when a move takes longer than you expected.
Step 4: wire the new axes to the board
The pin file for the board needs to know where I, J and K live. In the fork, the three extruder slots are simply renamed:
// Marlin/src/pins/stm32h7/pins_BTT_OCTOPUS_PRO_V1_common.h #define I_STEP_PIN PF9 // MOTOR 4, was E0 #define J_STEP_PIN PC13 // MOTOR 5, was E1 #define K_STEP_PIN PE2 // MOTOR 6, was E2
(along with the matching direction, enable and UART pins).
Homing needs an input pin too. The TMC2209 can detect a stall, when the motor hits a hard stop and the load spikes, and report it on its DIAG output. On the Octopus, a jumper under each driver connects DIAG to an endstop input, and for slots 4 to 6 those are the inputs labelled E0DET, E1DET and E2DET, normally used for filament sensors. With no extruders, they’re free:
#define I_DIAG_PIN PG12 // E0DET #define J_DIAG_PIN PG13 // E1DET #define K_DIAG_PIN PG14 // E2DET
Step 5: home without switches
// Configuration_adv.h #define SENSORLESS_HOMING #define I_STALL_SENSITIVITY 8 #define J_STALL_SENSITIVITY 8 #define K_STALL_SENSITIVITY 8
For a TMC2209 the threshold runs from 0 to 255, and higher means more sensitive. Too high and the joint “finds home” in mid-air. Too low and it grinds against the stop. M914 changes the value without reflashing, which saves a lot of SD card swaps while you look for the right number.
X, Y and Z don’t home this way. Their stall settings stay commented out, and they home against ordinary endstop switches, which is what the repo’s endstop_homing_for_xyz_and_sensorless_homing_for_abc branch name spells out. The big joints near the base carry the most load, and a switch gives a repeatable home position whatever the load is.
Testing it
Before asking the arm to do anything clever, I’d check each joint on its own:
M119 ; show endstop states
G28 A ; home just joint A
G1 A45 F600 ; move it to 45°
M114 ; does Marlin agree it's at 45°?
If a joint moves the wrong way or the wrong distance, that’s tuning, not wiring, and it’s the subject of the next post. The aim at this stage is smaller: six motors, each answering to its own letter.