# robotarium/gternal architecture

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

- Interactive diagram: https://gitdiagram.com/robotarium/gternal
- Repository: https://github.com/robotarium/gternal

## Overview

1. This repository is the deployment and firmware stack for a GTernal robot, not a single app. The system is split into three runtime domains: a host computer used for provisioning, a Raspberry Pi running the robot-side software, and a Teensy microcontroller running the low-level embedded firmware.

2. The host-side tooling lives in `setup/` and `interfacing/`. These scripts discover robots on the LAN, map MAC addresses to robot IDs, push configuration over SSH/SCP, and trigger setup or shutdown actions. The key identity file is `config/mac_list.json`, which is the source of truth for “which physical robot is which ID” during setup.

3. `config/env_variables.sh` is the deployment configuration boundary. It likely holds registry/repo names, image tags, and environment values used by the Docker build/run scripts. Architecture-wise, this file controls how the same code is packaged for base-image provisioning versus runtime execution.

4. Docker is a first-class subsystem. `docker/base_image/` builds and publishes the Raspberry Pi base image, while `docker/` builds and publishes the runtime firmware image and runs a local registry if needed. The repo is designed so robots pull prebuilt images instead of compiling on-device, which is important for scaling setup across multiple robots.

5. The base-image workflow exists to create a reusable SD-card image for Raspberry Pi OS. The host creates the image once, clones it to multiple SD cards, and then uses the robot provisioning scripts to specialize each robot. This is a provisioning layer, separate from the runtime firmware layer.

6. On the Raspberry Pi, the runtime is containerized. The Pi boots from the base SD image, then the setup scripts install or launch the firmware container. The README also references `v2tec/watchtower`, indicating that the Pi can automatically refresh to the newest firmware image from Docker Hub or a local registry.

7. The Pi-side firmware code is in `firmware/RPi/` and is Python-based. `pi_firmware.py`, `gritsbotserial.py`, and `detect_serial.py` form the bridge between the Pi and the Teensy over serial/UART. Architecturally, this layer is the robot’s runtime coordinator: it is the boundary between network-facing control and embedded hardware control.

8. The Teensy-side firmware is in `firmware/Teensy/`. `defaultOperation/defaultOperation.ino` is the main embedded program, with `debug/debug.ino` as a secondary diagnostic entry point. This is the lowest-level control plane for the robot, handling hardware behavior directly on the microcontroller.

9. The Teensy firmware depends on a large vendored library set under `firmware/Teensy/libraries/`. These include sensor and device libraries such as Adafruit GFX, BNO055, INA260, NeoPixel, VL53L0X, ArduinoJson, and several SparkFun/ST sensor libraries. That means the embedded boundary includes display, sensor, and peripheral drivers, not just a single custom sketch.

10. The main data flow is: host computer discovers robot MACs, assigns IDs, and provisions the Pi; the Pi container runs robot software and communicates upward to the network and downward to the Teensy over serial; the Teensy drives sensors/actuators. The Pi is the bridge between Wi‑Fi/network control and deterministic embedded control.

11. External services are part of the architecture. The README’s test flow uses an MQTT broker and the `vizier` client to query robot status and publish motor commands. So the operational control path is network client → MQTT broker → robot-side software, with the Pi/Teensy chain executing the command locally.

12. `GTernal_design_files/` is not runtime code, but it matters to system identity: it contains mechanical/laser-cut assets for robot marking such as Vicon hats and ArUco-related ID assignment. In an architecture diagram, this is a supporting physical-design artifact tied to robot identity management.

13. The important boundaries to draw are: provisioning host vs. robot runtime; Raspberry Pi Linux/container layer vs. Teensy embedded layer; local serial link vs. network/MQTT link; and image build/publish infrastructure vs. on-robot execution. Those are the core seams in this repository.

Main entry points: [`firmware/RPi/pi_firmware.py`](https://github.com/robotarium/gternal/blob/main/firmware/RPi/pi_firmware.py) (Pi firmware), [`setup`](https://github.com/robotarium/gternal/tree/main/setup) (Setup tools), [`docker`](https://github.com/robotarium/gternal/tree/main/docker) (Runtime image), [`firmware/Teensy/defaultOperation/defaultOperation.ino`](https://github.com/robotarium/gternal/blob/main/firmware/Teensy/defaultOperation/defaultOperation.ino) (Main sketch)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_host["Host provisioning"]
  node_setup_tools["Setup tools<br/>host provisioning scripts"]
  node_interfacing_tools["Robot ops<br/>host LAN control"]
  node_mac_identity["MAC map<br/>identity source<br/>[mac_list.json]"]
  node_env_config["Env config<br/>deploy settings<br/>[env_variables.sh]"]
end

subgraph group_docker["Container build"]
  node_base_image["Base image<br/>pi OS image build"]
  node_runtime_image["Runtime image<br/>robot container build"]
  node_registry_ops["Registry ops<br/>local registry helper<br/>[docker_registry.sh]"]
end

subgraph group_robot["Robot runtime"]
  node_pi_firmware["Pi firmware<br/>robot coordinator<br/>[pi_firmware.py]"]
  node_serial_bridge["Serial bridge<br/>uart protocol<br/>[gritsbotserial.py]"]
  node_serial_detect["Serial detect<br/>device discovery<br/>[detect_serial.py]"]
end

subgraph group_teensy["Embedded firmware"]
  node_teensy_main["Main sketch<br/>embedded control"]
  node_teensy_debug["Debug sketch<br/>embedded debug<br/>[debug.ino]"]
  node_teensy_libs["Sensor libs<br/>vendored drivers"]
end

subgraph group_external["External systems"]
  node_watchtower(("Watchtower<br/>auto-update agent"))
  node_mqtt_broker[("MQTT broker<br/>control bus")]
  node_vizier["Vizier client<br/>operator client"]
end

subgraph group_assets["Physical assets"]
  node_design_files["Design files<br/>physical assets"]
end

node_setup_tools -->|"reads IDs"| node_mac_identity
node_setup_tools -->|"loads config"| node_env_config
node_setup_tools -->|"prepares SD"| node_base_image
node_setup_tools -->|"deploys runtime"| node_runtime_image
node_interfacing_tools -->|"controls robots"| node_pi_firmware
node_interfacing_tools -->|"publishes cmds"| node_mqtt_broker
node_base_image -->|"uses settings"| node_env_config
node_runtime_image -->|"uses settings"| node_env_config
node_runtime_image -->|"push/pull"| node_registry_ops
node_pi_firmware -->|"uses"| node_serial_bridge
node_pi_firmware -->|"finds device"| node_serial_detect
node_pi_firmware -->|"subscribes"| node_mqtt_broker
node_pi_firmware -->|"updates via"| node_watchtower
node_vizier -->|"talks to"| node_mqtt_broker
node_mqtt_broker -->|"delivers"| node_pi_firmware
node_pi_firmware -->|"serial cmds"| node_teensy_main
node_teensy_main -->|"depends on"| node_teensy_libs
node_teensy_debug -->|"depends on"| node_teensy_libs
node_design_files -.->|"supports ID"| node_mac_identity
node_design_files -.->|"hardware context"| node_teensy_main

click node_setup_tools "https://github.com/robotarium/gternal/tree/main/setup"
click node_interfacing_tools "https://github.com/robotarium/gternal/tree/main/interfacing"
click node_mac_identity "https://github.com/robotarium/gternal/blob/main/config/mac_list.json"
click node_env_config "https://github.com/robotarium/gternal/blob/main/config/env_variables.sh"
click node_base_image "https://github.com/robotarium/gternal/tree/main/docker/base_image"
click node_runtime_image "https://github.com/robotarium/gternal/tree/main/docker"
click node_registry_ops "https://github.com/robotarium/gternal/blob/main/docker/docker_registry.sh"
click node_pi_firmware "https://github.com/robotarium/gternal/blob/main/firmware/RPi/pi_firmware.py"
click node_serial_bridge "https://github.com/robotarium/gternal/blob/main/firmware/RPi/gritsbotserial.py"
click node_serial_detect "https://github.com/robotarium/gternal/blob/main/firmware/RPi/detect_serial.py"
click node_teensy_main "https://github.com/robotarium/gternal/blob/main/firmware/Teensy/defaultOperation/defaultOperation.ino"
click node_teensy_debug "https://github.com/robotarium/gternal/blob/main/firmware/Teensy/debug/debug.ino"
click node_teensy_libs "https://github.com/robotarium/gternal/tree/main/firmware/Teensy/libraries"
click node_design_files "https://github.com/robotarium/gternal/tree/main/GTernal_design_files"

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_setup_tools,node_interfacing_tools,node_mac_identity,node_env_config toneBlue
class node_base_image,node_runtime_image,node_registry_ops toneAmber
class node_pi_firmware,node_serial_bridge,node_serial_detect toneMint
class node_teensy_main,node_teensy_debug,node_teensy_libs toneRose
class node_watchtower,node_mqtt_broker,node_vizier toneIndigo
class node_design_files toneTeal
```

## Components

### Host provisioning

Discovery, identity mapping, and setup workflows on the provisioning computer.

- [Setup tools](https://github.com/robotarium/gternal/tree/main/setup): host provisioning scripts · Host-side scripts that prepare robots, push config, and coordinate provisioning. · `setup`
- [Robot ops](https://github.com/robotarium/gternal/tree/main/interfacing): host LAN control · Scripts for robot discovery, restart, and shutdown over the network. · `interfacing`
- [MAC map](https://github.com/robotarium/gternal/blob/main/config/mac_list.json): identity source · Authoritative mapping from physical robot MAC addresses to robot IDs. · `config/mac_list.json`
- [Env config](https://github.com/robotarium/gternal/blob/main/config/env_variables.sh): deploy settings · Deployment boundary for image names, tags, registry, and runtime values. · `config/env_variables.sh`

### Container build

Image build, publish, and registry support for robot runtime and base images.

- [Base image](https://github.com/robotarium/gternal/tree/main/docker/base_image): pi OS image build · Workflow that builds a reusable Raspberry Pi base SD-card image. · `docker/base_image`
- [Runtime image](https://github.com/robotarium/gternal/tree/main/docker): robot container build · Build and publish flow for the robot firmware container image. · `docker`
- [Registry ops](https://github.com/robotarium/gternal/blob/main/docker/docker_registry.sh): local registry helper · Local registry and push/pull helpers used during deployment. · `docker/docker_registry.sh`

### Robot runtime

Raspberry Pi software layer that bridges network control to embedded hardware.

- [Pi firmware](https://github.com/robotarium/gternal/blob/main/firmware/RPi/pi_firmware.py): robot coordinator · Main Raspberry Pi-side runtime that coordinates robot behavior and serial control. · `firmware/RPi/pi_firmware.py`
- [Serial bridge](https://github.com/robotarium/gternal/blob/main/firmware/RPi/gritsbotserial.py): uart protocol · Bridge code between Pi software and the Teensy over serial/UART. · `firmware/RPi/gritsbotserial.py`
- [Serial detect](https://github.com/robotarium/gternal/blob/main/firmware/RPi/detect_serial.py): device discovery · Detects the active serial device for the attached Teensy. · `firmware/RPi/detect_serial.py`

### Embedded firmware

Teensy sketches and supporting libraries for low-level robot control.

- [Main sketch](https://github.com/robotarium/gternal/blob/main/firmware/Teensy/defaultOperation/defaultOperation.ino): embedded control · Primary Teensy firmware entry point for default robot operation. · `firmware/Teensy/defaultOperation/defaultOperation.ino`
- [Debug sketch](https://github.com/robotarium/gternal/blob/main/firmware/Teensy/debug/debug.ino): embedded debug · Secondary Teensy entry point for diagnostics and bring-up. · `firmware/Teensy/debug/debug.ino`
- [Sensor libs](https://github.com/robotarium/gternal/tree/main/firmware/Teensy/libraries): vendored drivers · Vendored device libraries for sensors, display, JSON, and peripherals. · `firmware/Teensy/libraries`

### External systems

Runtime dependencies outside the repository.

- **Watchtower**: auto-update agent · Container updater used to refresh the Pi runtime from an image registry.
- **MQTT broker**: control bus · External message broker used for operational control and status queries.
- **Vizier client**: operator client · Client used in tests and operations to query status and publish motor commands.

### Physical assets

Design files tied to robot identity and marking.

- [Design files](https://github.com/robotarium/gternal/tree/main/GTernal_design_files): physical assets · Mechanical and marking assets tied to robot identity and fabrication. · `GTernal_design_files`

## Connections

- Setup tools → MAC map: reads IDs · Uses the MAC-to-robot mapping as the source of truth during provisioning.
- Setup tools → Env config: loads config · Reads deployment settings that parameterize image and runtime setup.
- Setup tools → Base image: prepares SD · Starts or coordinates the Raspberry Pi base-image workflow.
- Setup tools → Runtime image: deploys runtime · Installs or launches the robot container on the Pi.
- Robot ops → Pi firmware: controls robots · Discovery and operational scripts target the robot-side software.
- Robot ops → MQTT broker: publishes cmds · Operational control flows through the network message bus.
- Base image → Env config: uses settings · Image build and provisioning are parameterized by deployment variables.
- Runtime image → Env config: uses settings · Runtime image build, push, and run behavior are driven by deployment variables.
- Runtime image → Registry ops: push/pull · Builds are published to or retrieved from a registry.
- Pi firmware → Serial bridge: uses · Pi runtime delegates Teensy communication to the serial bridge layer.
- Pi firmware → Serial detect: finds device · Locates the correct serial device before opening the bridge.
- Pi firmware → MQTT broker: subscribes · Receives network-facing commands and status traffic via MQTT.
- Pi firmware → Watchtower: updates via · Can be refreshed automatically by the container update agent.
- Vizier client → MQTT broker: talks to · The operator/test client interacts with robots through MQTT.
- MQTT broker → Pi firmware: delivers · Network commands reach the Pi runtime through the broker.
- Pi firmware → Main sketch: serial cmds · Robot coordination is forwarded from Pi to embedded firmware over serial.
- Main sketch → Sensor libs: depends on · Main firmware is built on top of vendored device and utility libraries.
- Debug sketch → Sensor libs: depends on · Diagnostic sketch also relies on the shared embedded library set.
- Design files → MAC map: supports ID · Physical identity/marking assets support robot labeling and identification.
- Design files → Main sketch: hardware context · Mechanical artifacts relate to the robot form factor controlled by the embedded firmware.
