# michaelpenner/greifarm architecture

> Architecture diagram of the GitHub repository michaelpenner/greifarm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/michaelpenner/greifarm
- Repository: https://github.com/michaelpenner/greifarm

## Overview

The product is a DIY CoreXY arcade claw machine: an ESP32 runs the hardware, a Raspberry Pi backend exposes APIs and bridges the ESP32 to browser clients, and a tablet UI provides the operator/player surface. The principal workflow is a browser command routed through the backend to the machine, with telemetry relayed back. Firmware also covers game control, motion, sensing, safety, configuration, and lighting. Some firmware subsystems and frontend wiring are represented from the source index or README rather than inspected excerpts, so uncertain links are omitted.

Main entry points: [`firmware/src/main.cpp`](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/main.cpp) (Firmware runtime), [`backend/main.py`](https://github.com/michaelpenner/greifarm/blob/main/backend/main.py) (FastAPI application), [`frontend/js/websocket-client.js`](https://github.com/michaelpenner/greifarm/blob/main/frontend/js/websocket-client.js) (WebSocket client), [`backend/services/esp_client.py`](https://github.com/michaelpenner/greifarm/blob/main/backend/services/esp_client.py) (ESP WebSocket client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Tablet Interface"]
  node_browser_ui["Browser UI<br/>[state-manager.js]"]
  node_browser_ws["WebSocket client"]
end

subgraph group_backend["Backend Services"]
  node_backend_app["FastAPI application<br/>[main.py]"]
  node_machine_api["Machine controls<br/>[routes_machine.py]"]
  node_game_api["Game API<br/>[routes_game.py]"]
  node_stats_api["Statistics API<br/>[routes_stats.py]"]
  node_admin_api["Admin API<br/>[routes_admin.py]"]
  node_db[("Game database<br/>[models.py]")]
  node_esp_client["ESP WebSocket client<br/>[esp_client.py]"]
end

subgraph group_firmware["Firmware Control"]
  node_firmware_main["Firmware runtime<br/>[main.cpp]"]
  node_ws_server["WebSocket server"]
  node_protocol["Command protocol<br/>[protocol.h]"]
  node_game_machine["Game state machine"]
  node_config["Runtime configuration<br/>[config_manager.cpp]"]
end

subgraph group_hardware["Machine Systems"]
  node_motion["Motion planner<br/>[motion_planner.cpp]"]
  node_homing["Homing control<br/>[homing.cpp]"]
  node_steppers["Stepper control"]
  node_claw["Claw control"]
  node_sensors["Sensor manager<br/>[sensor_manager.cpp]"]
  node_safety["Safety control"]
  node_joystick["Joystick input"]
  node_lighting["LED control<br/>[led_controller.cpp]"]
end

node_tablet(("Tablet user"))

node_tablet -->|"uses"| node_browser_ui
node_browser_ui -.->|"sends commands"| node_browser_ws
node_browser_ws -->|"WebSocket commands"| node_backend_app
node_backend_app -->|"broadcasts telemetry"| node_browser_ws
node_backend_app -->|"starts and listens"| node_esp_client
node_backend_app -->|"mounts router"| node_machine_api
node_backend_app -->|"mounts router"| node_game_api
node_backend_app -->|"mounts router"| node_stats_api
node_backend_app -->|"mounts router"| node_admin_api
node_machine_api -->|"sends commands"| node_esp_client
node_backend_app -->|"initializes"| node_db
node_esp_client -->|"WebSocket link"| node_ws_server
node_ws_server -.->|"uses messages"| node_protocol
node_firmware_main -.->|"runs firmware service"| node_ws_server
node_firmware_main -.->|"runs game control"| node_game_machine
node_firmware_main -.->|"runs motion control"| node_motion
node_firmware_main -.->|"runs homing"| node_homing
node_motion -.->|"controls steps"| node_steppers
node_firmware_main -.->|"runs claw control"| node_claw
node_firmware_main -.->|"updates sensors"| node_sensors
node_firmware_main -.->|"updates safety"| node_safety
node_firmware_main -.->|"reads input"| node_joystick
node_firmware_main -.->|"runs lighting"| node_lighting
node_firmware_main -.->|"loads settings"| node_config

click node_browser_ui "https://github.com/michaelpenner/greifarm/blob/main/frontend/js/state-manager.js"
click node_browser_ws "https://github.com/michaelpenner/greifarm/blob/main/frontend/js/websocket-client.js"
click node_backend_app "https://github.com/michaelpenner/greifarm/blob/main/backend/main.py"
click node_machine_api "https://github.com/michaelpenner/greifarm/blob/main/backend/api/routes_machine.py"
click node_game_api "https://github.com/michaelpenner/greifarm/blob/main/backend/api/routes_game.py"
click node_stats_api "https://github.com/michaelpenner/greifarm/blob/main/backend/api/routes_stats.py"
click node_admin_api "https://github.com/michaelpenner/greifarm/blob/main/backend/api/routes_admin.py"
click node_db "https://github.com/michaelpenner/greifarm/blob/main/backend/database/models.py"
click node_esp_client "https://github.com/michaelpenner/greifarm/blob/main/backend/services/esp_client.py"
click node_firmware_main "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/main.cpp"
click node_ws_server "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/communication/websocket_server.cpp"
click node_protocol "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/communication/protocol.h"
click node_game_machine "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/game/game_state_machine.cpp"
click node_motion "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/motion/motion_planner.cpp"
click node_homing "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/motion/homing.cpp"
click node_steppers "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/motion/stepper_controller.cpp"
click node_claw "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/claw/claw_controller.cpp"
click node_sensors "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/sensors/sensor_manager.cpp"
click node_safety "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/safety/safety_controller.cpp"
click node_joystick "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/joystick/joystick_reader.cpp"
click node_lighting "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/lighting/led_controller.cpp"
click node_config "https://github.com/michaelpenner/greifarm/blob/main/firmware/src/config/config_manager.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser_ui,node_browser_ws,node_tablet toneBlue
class node_backend_app,node_machine_api,node_game_api,node_stats_api,node_admin_api,node_db,node_esp_client toneAmber
class node_firmware_main,node_ws_server,node_protocol,node_game_machine,node_config toneMint
class node_motion,node_homing,node_steppers,node_claw,node_sensors,node_safety,node_joystick,node_lighting toneRose
```

## Components

### Tablet Interface

- [Browser UI](https://github.com/michaelpenner/greifarm/blob/main/frontend/js/state-manager.js): `frontend/js/state-manager.js`
- [WebSocket client](https://github.com/michaelpenner/greifarm/blob/main/frontend/js/websocket-client.js): `frontend/js/websocket-client.js`

### Backend Services

- [FastAPI application](https://github.com/michaelpenner/greifarm/blob/main/backend/main.py): `backend/main.py`
- [Machine controls](https://github.com/michaelpenner/greifarm/blob/main/backend/api/routes_machine.py): `backend/api/routes_machine.py`
- [Game API](https://github.com/michaelpenner/greifarm/blob/main/backend/api/routes_game.py): `backend/api/routes_game.py`
- [Statistics API](https://github.com/michaelpenner/greifarm/blob/main/backend/api/routes_stats.py): `backend/api/routes_stats.py`
- [Admin API](https://github.com/michaelpenner/greifarm/blob/main/backend/api/routes_admin.py): `backend/api/routes_admin.py`
- [Game database](https://github.com/michaelpenner/greifarm/blob/main/backend/database/models.py): `backend/database/models.py`
- [ESP WebSocket client](https://github.com/michaelpenner/greifarm/blob/main/backend/services/esp_client.py): `backend/services/esp_client.py`

### Firmware Control

- [Firmware runtime](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/main.cpp): `firmware/src/main.cpp`
- [WebSocket server](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/communication/websocket_server.cpp): `firmware/src/communication/websocket_server.cpp`
- [Command protocol](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/communication/protocol.h): `firmware/src/communication/protocol.h`
- [Game state machine](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/game/game_state_machine.cpp): `firmware/src/game/game_state_machine.cpp`
- [Runtime configuration](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/config/config_manager.cpp): `firmware/src/config/config_manager.cpp`

### Machine Systems

- [Motion planner](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/motion/motion_planner.cpp): `firmware/src/motion/motion_planner.cpp`
- [Homing control](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/motion/homing.cpp): `firmware/src/motion/homing.cpp`
- [Stepper control](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/motion/stepper_controller.cpp): `firmware/src/motion/stepper_controller.cpp`
- [Claw control](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/claw/claw_controller.cpp): `firmware/src/claw/claw_controller.cpp`
- [Sensor manager](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/sensors/sensor_manager.cpp): `firmware/src/sensors/sensor_manager.cpp`
- [Safety control](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/safety/safety_controller.cpp): `firmware/src/safety/safety_controller.cpp`
- [Joystick input](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/joystick/joystick_reader.cpp): `firmware/src/joystick/joystick_reader.cpp`
- [LED control](https://github.com/michaelpenner/greifarm/blob/main/firmware/src/lighting/led_controller.cpp): `firmware/src/lighting/led_controller.cpp`

### Other components

- **Tablet user**

## Connections

- Tablet user → Browser UI: uses
- Browser UI → WebSocket client: sends commands
- WebSocket client → FastAPI application: WebSocket commands (evidence: `backend/main.py`)
- FastAPI application → WebSocket client: broadcasts telemetry (evidence: `backend/main.py`)
- FastAPI application → ESP WebSocket client: starts and listens (evidence: `backend/main.py`)
- FastAPI application → Machine controls: mounts router (evidence: `backend/main.py`)
- FastAPI application → Game API: mounts router (evidence: `backend/main.py`)
- FastAPI application → Statistics API: mounts router (evidence: `backend/main.py`)
- FastAPI application → Admin API: mounts router (evidence: `backend/main.py`)
- Machine controls → ESP WebSocket client: sends commands (evidence: `backend/api/routes_machine.py`)
- FastAPI application → Game database: initializes (evidence: `backend/main.py`)
- ESP WebSocket client → WebSocket server: WebSocket link
- WebSocket server → Command protocol: uses messages
- Firmware runtime → WebSocket server: runs firmware service
- Firmware runtime → Game state machine: runs game control
- Firmware runtime → Motion planner: runs motion control
- Firmware runtime → Homing control: runs homing
- Motion planner → Stepper control: controls steps
- Firmware runtime → Claw control: runs claw control
- Firmware runtime → Sensor manager: updates sensors
- Firmware runtime → Safety control: updates safety
- Firmware runtime → Joystick input: reads input
- Firmware runtime → LED control: runs lighting
- Firmware runtime → Runtime configuration: loads settings
