# tangwp1588/self-balancing-robot architecture

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

- Interactive diagram: https://gitdiagram.com/tangwp1588/self-balancing-robot
- Repository: https://github.com/tangwp1588/self-balancing-robot

## Overview

This repository implements firmware for an STM32F407 self-balancing robot. Controller commands are received over UART, translated into wheel-motion requests, and combined with IMU pitch and encoder speed feedback for cascaded balancing and motor-speed control; motor output is subject to a pitch safety lock. The firmware also provides an LCD telemetry and live PID-tuning menu. The README documents these capabilities, but most runtime modules were not sampled, so their implementation-level connections are shown as unverified where needed.

Main entry points: [`Core/Src/pid.c`](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/pid.c) (Cascaded PID), [`Core/Src/controller_input.c`](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/controller_input.c) (Command processing), [`Core/Src/uart_handler.c`](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/uart_handler.c) (UART input), [`Core/Src/menu.c`](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/menu.c) (PID tuning menu)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Firmware runtime"]
  node_entry["Application startup<br/>[main.c]"]
  node_tasks["RTOS tasks<br/>[freertos.c]"]
end

subgraph group_inputs["Commands and sensing"]
  node_uart["UART input<br/>[uart_handler.c]"]
  node_gamepad["Command processing<br/>[controller_input.c]"]
  node_imu["BNO08x sensing<br/>[bno08x.c]"]
  node_encoder["Encoder feedback<br/>[encoder.c]"]
end

subgraph group_control["Motion control"]
  node_kinematics["Wheel kinematics<br/>[kinematics.c]"]
  node_pid["Cascaded PID<br/>[pid.c]"]
  node_motor["Motor drive<br/>[motor.c]"]
end

subgraph group_interface["Display and tuning"]
  node_menu["PID tuning menu<br/>[menu.c]"]
  node_display["ST7789 display<br/>[st7789.c]"]
end

node_operator(("Operator"))
node_controller["ESP32 controller"]
node_robot["Robot hardware"]
node_motor_driver["TB6612 driver"]
node_lcd["LCD panel"]

node_operator -->|"sets commands"| node_controller
node_controller -->|"sends commands"| node_uart
node_entry -->|"starts scheduler"| node_tasks
node_uart -->|"provides data"| node_gamepad
node_gamepad -->|"reads data"| node_uart
node_gamepad -->|"computes wheel requests"| node_kinematics
node_robot -->|"provides pitch feedback"| node_imu
node_robot -->|"provides speed feedback"| node_encoder
node_kinematics -.->|"sets motion targets"| node_pid
node_imu -->|"provides pitch feedback"| node_pid
node_encoder -->|"provides speed feedback"| node_pid
node_pid -->|"commands PWM"| node_motor
node_motor -->|"drives motors"| node_motor_driver
node_menu -->|"tunes parameters"| node_pid
node_menu -.->|"renders telemetry"| node_display
node_display -.->|"updates display"| node_lcd

click node_entry "https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/main.c"
click node_tasks "https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/freertos.c"
click node_uart "https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/uart_handler.c"
click node_gamepad "https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/controller_input.c"
click node_kinematics "https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/kinematics.c"
click node_imu "https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/bno08x.c"
click node_encoder "https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/encoder.c"
click node_pid "https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/pid.c"
click node_motor "https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/motor.c"
click node_menu "https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/menu.c"
click node_display "https://github.com/tangwp1588/self-balancing-robot/blob/main/Drivers/ST7789/st7789.c"

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_entry,node_tasks toneBlue
class node_uart,node_gamepad,node_imu,node_encoder toneAmber
class node_kinematics,node_pid,node_motor toneMint
class node_menu,node_display toneRose
class node_operator,node_controller,node_robot,node_motor_driver,node_lcd toneIndigo
```

## Components

### Firmware runtime

- [Application startup](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/main.c): `Core/Src/main.c`
- [RTOS tasks](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/freertos.c): `Core/Src/freertos.c`

### Commands and sensing

- [UART input](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/uart_handler.c): `Core/Src/uart_handler.c`
- [Command processing](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/controller_input.c): `Core/Src/controller_input.c`
- [BNO08x sensing](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/bno08x.c): `Core/Src/bno08x.c`
- [Encoder feedback](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/encoder.c): `Core/Src/encoder.c`

### Motion control

- [Wheel kinematics](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/kinematics.c): `Core/Src/kinematics.c`
- [Cascaded PID](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/pid.c): `Core/Src/pid.c`
- [Motor drive](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/motor.c): `Core/Src/motor.c`

### Display and tuning

- [PID tuning menu](https://github.com/tangwp1588/self-balancing-robot/blob/main/Core/Src/menu.c): `Core/Src/menu.c`
- [ST7789 display](https://github.com/tangwp1588/self-balancing-robot/blob/main/Drivers/ST7789/st7789.c): `Drivers/ST7789/st7789.c`

### Other components

- **Operator**
- **ESP32 controller**
- **Robot hardware**
- **TB6612 driver**
- **LCD panel**

## Connections

- Operator → ESP32 controller: sets commands (evidence: `README.md`)
- ESP32 controller → UART input: sends commands (evidence: `README.md`)
- Application startup → RTOS tasks: starts scheduler (evidence: `Core/Src/main.c`)
- UART input → Command processing: provides data (evidence: `Core/Src/controller_input.c`)
- Command processing → UART input: reads data (evidence: `Core/Src/controller_input.c`)
- Command processing → Wheel kinematics: computes wheel requests (evidence: `Core/Src/controller_input.c`)
- Robot hardware → BNO08x sensing: provides pitch feedback (evidence: `README.md`)
- Robot hardware → Encoder feedback: provides speed feedback (evidence: `README.md`)
- Wheel kinematics → Cascaded PID: sets motion targets
- BNO08x sensing → Cascaded PID: provides pitch feedback (evidence: `README.md`)
- Encoder feedback → Cascaded PID: provides speed feedback (evidence: `README.md`)
- Cascaded PID → Motor drive: commands PWM (evidence: `README.md`)
- Motor drive → TB6612 driver: drives motors (evidence: `README.md`)
- PID tuning menu → Cascaded PID: tunes parameters (evidence: `README.md`)
- PID tuning menu → ST7789 display: renders telemetry
- ST7789 display → LCD panel: updates display
