# nickrehm/drehmflight architecture

> Architecture diagram of the GitHub repository nickrehm/drehmflight, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nickrehm/drehmflight
- Repository: https://github.com/nickrehm/drehmflight

## Overview

##### Project type and purpose
- This is an embedded, Arduino-style flight controller for VTOL (vertical take-off and landing) aircraft, running on a Teensy 4.0/4.1 microcontroller.
- It stabilizes multirotor and VTOL airframes by reading IMU data, processing control loops, and driving motors/servos.

##### File structure
- Top-level:
- LICENSE, README.md, documentation PDF
- Versions/…: Separate Beta releases, each contains two .ino sketches and a src/ directory
- .ino files:
- dRehmFlight_Teensy_BETA_x.x.ino – main application (setup, loop, control algorithm)
- radioComm.ino – telemetry & radio-link routines
- src/ subdirectories: Modular hardware drivers and utility code for each version:
- MPU6050, MPU9250 – IMU drivers
- SBUS, DSMRX – RC receiver protocols
- TFMPlus – motor-mixing library
- elapsedMillis.h – timing utility

##### Key insights from README
- Hardware: Teensy 4.x MCU, MPU6050 IMU, optional Spektrum DSM
- Software: Arduino IDE + Teensyduino
- Purpose: rapid-prototype, educational flight controller with on-board calibration & arming

##### Main components
a. Hardware layer
- Teensy 4.x MCU (CPU, Memory, GPIO)
- Power supply / voltage regulators
- IMU sensors (MPU6050 / MPU9250) connected via I2C
- RC input (SBUS or DSM satellite) on UART or dedicated pin
- ESC/motor outputs (PWM or digital)
- Telemetry radio (optional serial link)
b. Software layer
- Boot & Initialization (pin mapping, sensor init, calibration routines)
- Sensor Drivers (I2Cdev, MPU6050, MPU9250 modules)
- RC Receiver Drivers (SBUS.cpp/h, DSMRX.cpp/h)
- Control Core (flight stabilization loop, PID controllers, mixer via TFMPlus)
- RadioComm module (telemetry packet formatting, serial comms)
- Utility modules (elapsedMillis timer, math helpers)

##### Relationships and interactions
- On each loop iteration:
1. Read raw IMU data via sensor drivers → filter/scale
2. Read pilot stick commands from RC driver → map to desired attitudes
3. Pass sensor + pilot inputs to Control Core → compute motor/servo outputs
4. Use TFMPlus mixer to convert attitude commands into ESC/servo PWM signals
5. Send telemetry out via radioComm module
- Calibration & arming routines run in setup or on special stick commands

##### Architectural patterns & design principles
- Modular driver layer: each hardware interface isolated in its own .cpp/.h
- Single-threaded, periodic main loop (typical embedded control loop)
- Separation of concerns: drivers vs. control logic vs. comms
- Versioning via separate directories to maintain backward compatibility

- Clear labels for each hardware and software component
- A boundary around the MCU block delineating “on-board software modules” vs. external peripherals

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Peripherals
    IMU["IMU Sensor\n(MPU6050/MPU9250)"]:::input
    RCInput["RC Receiver\n(SBUS/DSMRX)"]:::input
    ESCMotors["ESC/Motors\n(PWM Outputs)"]:::output
    TelemetryRadio["Telemetry Radio\n(Serial Link)"]:::comms

    %% MCU and On-Board Software
    subgraph "Teensy 4.x MCU"
        direction TB
        BootInit["Boot & Initialization"]:::core
        subgraph "Sensor Drivers"
            direction TB
            MPU6050["MPU6050 Driver"]:::input
            MPU9250["MPU9250 Driver"]:::input
        end
        subgraph "RC Drivers"
            direction TB
            SBUS["SBUS Driver"]:::input
            DSMRX["DSMRX Driver"]:::input
        end
        ControlCore["Control Core\n(Flight Loop & PID)"]:::processing
        MotorMixer["Motor Mixer\n(TFMPlus)"]:::processing
        TelemetryModule["RadioComm Module"]:::comms
    end

    %% Data Flows
    IMU -->|"I2C"| MPU6050
    IMU -->|"I2C"| MPU9250
    MPU6050 --> ControlCore
    MPU9250 --> ControlCore

    RCInput -->|"UART"| SBUS
    RCInput -->|"UART"| DSMRX
    SBUS --> ControlCore
    DSMRX --> ControlCore

    ControlCore --> MotorMixer
    MotorMixer -->|"PWM"| ESCMotors

    ControlCore --> TelemetryModule
    TelemetryModule -->|"Serial"| TelemetryRadio

    BootInit --> ControlCore

    %% Click Events
    click BootInit "https://github.com/nickrehm/drehmflight/blob/master/Versions/dRehmFlight_Teensy_BETA_1.3/dRehmFlight_Teensy_BETA_1.3.ino"
    click TelemetryModule "https://github.com/nickrehm/drehmflight/blob/master/Versions/dRehmFlight_Teensy_BETA_1.3/radioComm.ino"
    click MPU6050 "https://github.com/nickrehm/drehmflight/tree/master/Versions/dRehmFlight_Teensy_BETA_1.3/src/MPU6050"
    click MPU9250 "https://github.com/nickrehm/drehmflight/blob/master/Versions/dRehmFlight_Teensy_BETA_1.3/src/MPU9250/MPU9250.cpp"
    click SBUS "https://github.com/nickrehm/drehmflight/tree/master/Versions/dRehmFlight_Teensy_BETA_1.3/src/SBUS"
    click DSMRX "https://github.com/nickrehm/drehmflight/tree/master/Versions/dRehmFlight_Teensy_BETA_1.3/src/DSMRX"
    click MotorMixer "https://github.com/nickrehm/drehmflight/blob/master/Versions/dRehmFlight_Teensy_BETA_1.3/src/TFMPlus/TFMPlus.cpp"

    %% Styles
    classDef input fill:#9f9,stroke:#333,stroke-width:1px
    classDef processing fill:#99f,stroke:#333,stroke-width:1px
    classDef output fill:#f99,stroke:#333,stroke-width:1px
    classDef comms fill:#fa0,stroke:#333,stroke-width:1px
    classDef core fill:#ccf,stroke:#333,stroke-width:1px
```
