StackChan — firmware and control from Claude
Personal project: firmware and tooling for the M5Stack StackChan (K151) desktop robot — CoreS3 (ESP32-S3) + motorized body (2 Feetech SCS0009 servos, 12 LEDs, head touch, NFC, IR). Repository docs and code are in English; talk to the user in French.
Architecture
Claude sessions ──MCP HTTP 127.0.0.1:8765/mcp──> mcp/ (Python, shared stackchan-mcp server) ──USB serial, JSON-lines──> firmware/ (Arduino, CoreS3)
firmware/— PlatformIO, Arduino framework.src/main.cpp: protocol + gestures;src/face.{h,cpp}: face drawn into a PSRAM sprite.mcp/— MCP server (mcpSDK 2.x:MCPServerinmcp.server.mcpserver, notFastMCP).robot.py: serial link;sessions.py: control lease and per-session event cursors;server.py: tools.- The server is registered at user scope (
claude mcp add --transport http --scope user stackchan http://127.0.0.1:8765/mcp), so every session sees it; it is started manually withtools/stackchan-server.sh start|stop|status|logs. tools/serial_cmd.py,tools/serial_read.py— raw serial access for debugging without MCP.docs/hardware-reference.md(hardware, pinout),docs/protocol.md(firmware ↔ server contract, keep it up to date with every new command).
Commands
cd firmware && pio run # build
cd firmware && pio run -t upload --upload-port /dev/cu.usbmodem101 # flash
cd mcp && uv sync # MCP server dependencies
tools/stackchan-server.sh start # shared MCP server (logs: ~/Library/Logs/stackchan-mcp.log)
~/.local/share/uv/tools/platformio/bin/python -I tools/serial_cmd.py /dev/cu.usbmodem101 3 '{"id":1,"cmd":"status"}'
piois installed withuv tool(~/.local/bin/pio). Platform pioarduino 55.03.313 (Arduino core 3.3.13): the official espressif32 platform is too old for theM5StackChanlib. Do not move to core 4.x without validating M5Unified/M5GFX.- USB port:
/dev/cu.usbmodem101(Espressif VID303A). Flashing works without manual download mode.
Important rules
- Only one process on the USB port at a time. The shared server keeps it open. To flash:
flash_firmwaretool; for a manualpio run -t uploadortools/serial_cmd.py: runtools/stackchan-server.sh stopfirst. - Control lease between sessions: a robot command grants control for 10 s (extended by each command); other sessions are refused.
get_status,stopand touch stay free. A "robot in use by another session" refusal is expected: wait, or ask the user. - If the robot stops answering entirely (even to
esptool) while the USB port is listed: ask the user to unplug/replug the USB cable. - Opening the port reboots the robot (macOS / USB-Serial-JTAG), even with DTR/RTS low: wait for the
{"event":"boot"}event before sending commands. Face and LEDs go back to their initial state. - Servo limits (BSP units = 1/10°): yaw −90…+90°, pitch 5…85° (beyond: risk of stalling and breakage, M5Stack recommendation). Clamping is done in the firmware (
kYawMin/Max,kPitchMin/Max) — do not loosen it. - Verified directions: positive yaw = head turns towards the robot's left; larger pitch = looks up; (0, 0) = calibrated position, screen perpendicular to the base.
- Zero calibration lives in NVS (namespace
servo, keyszero_pos_1/2) and must be redone after a third-party firmware. It requires the user to position the head by hand (torque off) — never callset_homewithout them. - Do not force the head by hand while the motors are powered.
- Servo bus: UART1 1 Mbaud, TX=G6 / RX=G7. Body I2C (G11/G12): INA226 0x41, NFC 0x50, Si12T 0x68, PY32L020 IO expander 0x6F (servo power + LEDs).
- The servo
movingflag can stay true at rest: judge the end of a move on position stability (see_wait_stillinserver.py).
Validation
No automated tests. After any change: build, flash, then exercise the affected commands via tools/serial_cmd.py or an MCP client (restart the server after changing mcp/: tools/stackchan-server.sh restart), and ask the user what they see (screen, LEDs, movements) — I cannot observe it.
Validated versions
M5StackChan 1.1.1, M5Unified 0.3.0, M5GFX 0.3.0, ArduinoJson 7.x, mcp 2.3, custom firmware 0.1.0. The original M5Stack firmware (XiaoZhi) has been overwritten; it can be restored with M5Burner.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|