# adcuelloa/rovelink architecture

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

- Interactive diagram: https://gitdiagram.com/adcuelloa/rovelink
- Repository: https://github.com/adcuelloa/rovelink

## Overview

#### Purpose and Topology
RoveLink is a low-latency Internet teleoperation platform for differential-drive robots. A browser controller and ESP32 device each initiate outbound secure WebSocket connections; Cloudflare relay rooms forward control traffic without requiring a public device IP. Video uses a separate relay plane. Workspace packages are coordinated from `package.json` and `pnpm-workspace.yaml`.

#### Shared Protocol Contract
The TypeScript protocol package defines versioned JSON messages, codecs, authentication, control semantics, video tickets, and the shared differential-drive mixer. This contract is the boundary between browser, relays, and firmware behavior.
- `protocol/src/protocol.ts`
- `protocol/src/codec.ts`
- `protocol/src/control.ts`
- `protocol/src/mix.ts`

#### Browser Control Application
The Vite browser app starts at `web/src/main.ts`, presenting login/control views and selecting either an in-browser simulated transport or relay WebSocket transport. Input capture normalizes keyboard, gamepad, and touch into control state; the engine applies ownership, arming, emergency-stop, and profile rules before sender/rhythm logic emits latest-state control frames. Telemetry and PWM visualization feed the dashboard.
- `web/src/main.ts`
- `web/src/control/engine.ts`
- `web/src/transport/websocket.ts`
- `web/src/transport/sender.ts`

#### Control Relay Plane
The control relay is a Cloudflare Worker front door plus Durable Object room implementation. Routes upgrade authenticated browser/device connections and bind them to a robot room; the room forwards messages between peers rather than implementing robot motion. It is the Internet-facing rendezvous and session-routing boundary.
- `relay/src/index.ts`
- `relay/src/route.ts`
- `relay/src/room.ts`
- `relay/wrangler.jsonc`

#### Dedicated Video Relay Plane
Video traffic is intentionally isolated from safety-sensitive control traffic in its own Worker and Durable Object implementation. Publisher/viewer room handling, ticket authorization, and WebSocket routing live independently, while the browser manages ticket sourcing, transport, pairing, renderer selection, and stream statistics.
- `video-relay/src/index.ts`
- `video-relay/src/room.ts`
- `video-relay/src/route.ts`
- `web/src/video/video-transport.ts`
- `web/src/video/renderer.ts`

#### ESP32 Device Runtime and Safety Loop
The Arduino sketch is the device entry point. It joins Wi-Fi, validates WSS TLS roots, reconnects with backoff, authenticates to the control relay, decodes incoming commands, and drives a hardware abstraction. The runtime enforces safe output behavior around arm/disarm, emergency stop, stale-frame watchdogs, and disconnects; hardware can be real or simulated.
- `firmware/rovelink_device/rovelink_device.ino`
- `firmware/rovelink_device/network.cpp`
- `firmware/rovelink_device/transport.cpp`
- `firmware/rovelink_device/hardware_real.cpp`
- `firmware/rovelink_device/hardware_sim.cpp`

#### End-to-End Flow and External Boundaries
1. Browser authenticates as controller and connects to a Cloudflare control room over WSS.
2. ESP32 connects outbound over Wi-Fi/WSS to the same room; relay forwards protocol frames.
3. Browser sends current control state rather than a queued command history; firmware acknowledges/applies valid fresh state and reports telemetry.
4. Firmware converts throttle/steering through the shared differential mix to wheel PWM and actuates hardware only while armed.
5. Optional video publisher/viewers use the separate Cloudflare video Worker/DO and ticket-based authorization.

Material runtimes are browser/Vite TypeScript, Cloudflare Workers with Durable Objects, and Arduino/ESP32 C++. External services are Cloudflare’s Worker/DO platform and Wi-Fi Internet access; TLS CA material is embedded in `firmware/rovelink_device/cloudflare_ca_certs.h`.

Main entry points: [`firmware/rovelink_device/transport.cpp`](https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/transport.cpp) (Device WSS transport), [`protocol/src/protocol.ts`](https://github.com/adcuelloa/rovelink/blob/main/protocol/src/protocol.ts) (Versioned protocol), [`relay/src/index.ts`](https://github.com/adcuelloa/rovelink/blob/main/relay/src/index.ts) (Control Worker), [`protocol/src/mix.ts`](https://github.com/adcuelloa/rovelink/blob/main/protocol/src/mix.ts) (Differential mixer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_shared["Shared contract"]
  node_protocol["Versioned protocol<br/>TypeScript contract<br/>[protocol.ts]"]
  node_mixer["Differential mixer<br/>shared motion logic<br/>[mix.ts]"]
end

subgraph group_browser["Browser controller"]
  node_browser_app["Controller app<br/>Vite entrypoint<br/>[main.ts]"]
  node_control_engine["Control safety engine<br/>input state logic<br/>[engine.ts]"]
  node_control_sender["Latest-state sender<br/>control cadence<br/>[sender.ts]"]
  node_control_ws["Control WSS transport<br/>browser transport<br/>[websocket.ts]"]
  node_video_client["Video transport<br/>browser video client<br/>[video-transport.ts]"]
  node_video_renderer["Video renderer<br/>browser rendering<br/>[renderer.ts]"]
end

subgraph group_control["Control relay plane"]
  node_control_worker{{"Control Worker<br/>Cloudflare Worker<br/>[index.ts]"}}
  node_control_routes["Control routes<br/>connection routing<br/>[route.ts]"]
  node_control_room[("Robot room<br/>Durable Object<br/>[room.ts]")]
end

subgraph group_video["Video relay plane"]
  node_video_worker{{"Video Worker<br/>Cloudflare Worker<br/>[index.ts]"}}
  node_video_room[("Video room<br/>Durable Object<br/>[room.ts]")]
end

subgraph group_device["ESP32 runtime"]
  node_device_sketch["Device runtime<br/>Arduino entrypoint"]
  node_device_network["Wi-Fi and TLS<br/>ESP32 network layer<br/>[network.cpp]"]
  node_device_transport["Device WSS transport<br/>ESP32 transport<br/>[transport.cpp]"]
  node_real_hardware["Motor hardware<br/>real HAL<br/>[hardware_real.cpp]"]
  node_sim_hardware["Simulated hardware<br/>simulation HAL<br/>[hardware_sim.cpp]"]
end

node_browser_app -->|"captures input"| node_control_engine
node_control_engine -->|"approved current state"| node_control_sender
node_control_sender -->|"control frames"| node_control_ws
node_control_ws -->|"authenticated WSS"| node_control_worker
node_control_worker -->|"routes upgrade"| node_control_routes
node_control_routes -->|"binds robot room"| node_control_room
node_device_sketch -->|"initializes"| node_device_network
node_device_network -->|"Wi-Fi / TLS"| node_device_transport
node_device_transport -->|"outbound WSS"| node_control_worker
node_control_room -->|"forwarded frames"| node_device_transport
node_device_transport -->|"commands and telemetry"| node_device_sketch
node_device_sketch -->|"throttle and steering"| node_mixer
node_mixer -->|"wheel PWM"| node_real_hardware
node_mixer -.->|"wheel PWM"| node_sim_hardware
node_protocol -.->|"encodes frames"| node_control_sender
node_protocol -.->|"auth and wire contract"| node_control_worker
node_protocol -.->|"decodes wire contract"| node_device_transport
node_browser_app -->|"manages video"| node_video_client
node_video_client -->|"ticketed WSS"| node_video_worker
node_video_worker -->|"routes sessions"| node_video_room
node_video_client -->|"stream frames"| node_video_renderer
node_protocol -.->|"video tickets"| node_video_worker

click node_protocol "https://github.com/adcuelloa/rovelink/blob/main/protocol/src/protocol.ts"
click node_mixer "https://github.com/adcuelloa/rovelink/blob/main/protocol/src/mix.ts"
click node_browser_app "https://github.com/adcuelloa/rovelink/blob/main/web/src/main.ts"
click node_control_engine "https://github.com/adcuelloa/rovelink/blob/main/web/src/control/engine.ts"
click node_control_sender "https://github.com/adcuelloa/rovelink/blob/main/web/src/transport/sender.ts"
click node_control_ws "https://github.com/adcuelloa/rovelink/blob/main/web/src/transport/websocket.ts"
click node_video_client "https://github.com/adcuelloa/rovelink/blob/main/web/src/video/video-transport.ts"
click node_video_renderer "https://github.com/adcuelloa/rovelink/blob/main/web/src/video/renderer.ts"
click node_control_worker "https://github.com/adcuelloa/rovelink/blob/main/relay/src/index.ts"
click node_control_routes "https://github.com/adcuelloa/rovelink/blob/main/relay/src/route.ts"
click node_control_room "https://github.com/adcuelloa/rovelink/blob/main/relay/src/room.ts"
click node_video_worker "https://github.com/adcuelloa/rovelink/blob/main/video-relay/src/index.ts"
click node_video_room "https://github.com/adcuelloa/rovelink/blob/main/video-relay/src/room.ts"
click node_device_sketch "https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/rovelink_device.ino"
click node_device_network "https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/network.cpp"
click node_device_transport "https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/transport.cpp"
click node_real_hardware "https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/hardware_real.cpp"
click node_sim_hardware "https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/hardware_sim.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_protocol,node_mixer toneBlue
class node_browser_app,node_control_engine,node_control_sender,node_control_ws,node_video_client,node_video_renderer toneAmber
class node_control_worker,node_control_routes,node_control_room toneMint
class node_video_worker,node_video_room toneRose
class node_device_sketch,node_device_network,node_device_transport,node_real_hardware,node_sim_hardware toneIndigo
```

## Components

### Shared contract

- [Versioned protocol](https://github.com/adcuelloa/rovelink/blob/main/protocol/src/protocol.ts): TypeScript contract · Messages, codecs, auth, control semantics, and video tickets. · `protocol/src/protocol.ts`
- [Differential mixer](https://github.com/adcuelloa/rovelink/blob/main/protocol/src/mix.ts): shared motion logic · `protocol/src/mix.ts`

### Browser controller

- [Controller app](https://github.com/adcuelloa/rovelink/blob/main/web/src/main.ts): Vite entrypoint · `web/src/main.ts`
- [Control safety engine](https://github.com/adcuelloa/rovelink/blob/main/web/src/control/engine.ts): input state logic · Applies ownership, arming, e-stop, and profile rules. · `web/src/control/engine.ts`
- [Latest-state sender](https://github.com/adcuelloa/rovelink/blob/main/web/src/transport/sender.ts): control cadence · `web/src/transport/sender.ts`
- [Control WSS transport](https://github.com/adcuelloa/rovelink/blob/main/web/src/transport/websocket.ts): browser transport · `web/src/transport/websocket.ts`
- [Video transport](https://github.com/adcuelloa/rovelink/blob/main/web/src/video/video-transport.ts): browser video client · Sources tickets and pairs publisher/viewer connections. · `web/src/video/video-transport.ts`
- [Video renderer](https://github.com/adcuelloa/rovelink/blob/main/web/src/video/renderer.ts): browser rendering · Selects rendering path and reports stream statistics. · `web/src/video/renderer.ts`

### Control relay plane

- [Control Worker](https://github.com/adcuelloa/rovelink/blob/main/relay/src/index.ts): Cloudflare Worker · Authenticates and upgrades peer connections into robot rooms. · `relay/src/index.ts`
- [Control routes](https://github.com/adcuelloa/rovelink/blob/main/relay/src/route.ts): connection routing · `relay/src/route.ts`
- [Robot room](https://github.com/adcuelloa/rovelink/blob/main/relay/src/room.ts): Durable Object · Forwards frames between controller and device peers. · `relay/src/room.ts`

### Video relay plane

- [Video Worker](https://github.com/adcuelloa/rovelink/blob/main/video-relay/src/index.ts): Cloudflare Worker · Independent video WebSocket front door. · `video-relay/src/index.ts`
- [Video room](https://github.com/adcuelloa/rovelink/blob/main/video-relay/src/room.ts): Durable Object · Authorizes tickets and handles publisher/viewer sessions. · `video-relay/src/room.ts`

### ESP32 runtime

- [Device runtime](https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/rovelink_device.ino): Arduino entrypoint · Coordinates network, transport, safety, and hardware output. · `firmware/rovelink_device/rovelink_device.ino`
- [Wi-Fi and TLS](https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/network.cpp): ESP32 network layer · Joins Wi-Fi and validates WSS TLS roots. · `firmware/rovelink_device/network.cpp`
- [Device WSS transport](https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/transport.cpp): ESP32 transport · Reconnects, authenticates, decodes frames, and reports telemetry. · `firmware/rovelink_device/transport.cpp`
- [Motor hardware](https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/hardware_real.cpp): real HAL · Applies armed wheel PWM to physical hardware. · `firmware/rovelink_device/hardware_real.cpp`
- [Simulated hardware](https://github.com/adcuelloa/rovelink/blob/main/firmware/rovelink_device/hardware_sim.cpp): simulation HAL · `firmware/rovelink_device/hardware_sim.cpp`

## Connections

- Controller app → Control safety engine: captures input
- Control safety engine → Latest-state sender: approved current state
- Latest-state sender → Control WSS transport: control frames
- Control WSS transport → Control Worker: authenticated WSS
- Control Worker → Control routes: routes upgrade
- Control routes → Robot room: binds robot room
- Device runtime → Wi-Fi and TLS: initializes
- Wi-Fi and TLS → Device WSS transport: Wi-Fi / TLS
- Device WSS transport → Control Worker: outbound WSS
- Robot room → Device WSS transport: forwarded frames
- Device WSS transport → Device runtime: commands and telemetry
- Device runtime → Differential mixer: throttle and steering
- Differential mixer → Motor hardware: wheel PWM
- Differential mixer → Simulated hardware: wheel PWM
- Versioned protocol → Latest-state sender: encodes frames
- Versioned protocol → Control Worker: auth and wire contract
- Versioned protocol → Device WSS transport: decodes wire contract
- Controller app → Video transport: manages video
- Video transport → Video Worker: ticketed WSS
- Video Worker → Video room: routes sessions
- Video transport → Video renderer: stream frames
- Versioned protocol → Video Worker: video tickets
