# omar-magdy0/forgedrive architecture

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

- Interactive diagram: https://gitdiagram.com/omar-magdy0/forgedrive
- Repository: https://github.com/omar-magdy0/forgedrive

## Overview

#### Purpose and Build Targets
EcoDrive is a C/C++ PMSM inverter-control platform for EV use, supporting Field-Oriented Control (FOC), self-commissioning, telemetry, and host-side simulation. It has two primary executable domains: firmware/simulation in `EcoDrive/main.cpp` and a desktop communication/tooling application in `EcoTool/EcoTool.cpp`. CMake target definitions are rooted per application: `EcoDrive/CMakeLists.txt` and `EcoTool/CMakeLists.txt`.

#### Control Application and Mode Dispatch
`EcoDrive/PmsmControl/PmsmControl.h` exposes the motor-control application layer; `EcoDrive/PmsmControl/PmsmControl.cpp` integrates it with the system. Shared control states, signals, and configuration are concentrated in `EcoDrive/PmsmControl/PmsmControlTypes.h` and `EcoDrive/PmsmControl/PmsmControlConf.h`. The controller selects operational behavior such as safe idle, FOC, and self-commissioning rather than exposing platform drivers directly to application logic.

#### Real-Time Motor-Control Core
`EcoDrive/PmsmControl/PmsmControlCore.h` / `EcoDrive/PmsmControl/PmsmControlCore.cpp` form the common execution core. FOC-specific processing is isolated in `EcoDrive/PmsmControl/PmsmControlCore_Foc.cpp`, corresponding to the fast PWM-rate path described in the README: sampling currents/voltage, frame transforms, current regulation, and PWM duty generation. This is the critical real-time boundary: control code consumes normalized measurements and produces inverter commands through platform abstractions.

#### Commissioning and Position Feedback
Self-commissioning is separated from normal closed-loop control in `EcoDrive/PmsmControl/PmsmControlCore_SComm.cpp` and `EcoDrive/PmsmControl/PmsmControlCore_SComm.h`; this encapsulates HFI-based motor parameter identification. Rotor position acquisition is abstracted by `EcoDrive/PmsmControl/PosDriver.h` and implemented in `EcoDrive/PmsmControl/PosDriver.cpp`, preventing FOC/SComm logic from depending on a particular Hall or hardware timer implementation.

#### Platform Boundary: Host SIL vs STM32F4
`EcoDrive/platform/host/platform.cpp` provides the native-host runtime, including software-in-the-loop support represented by `EcoDrive/platform/host/sil.h`; host motor/Hall models are present in `EcoDrive/platform/host/el_mc3p.cpp` and `EcoDrive/platform/host/el_hall.cpp`. `EcoDrive/platform/stm32f4/platform.c` is the embedded counterpart. Platform-specific CMake selection files (`EcoDrive/platform/host/platform.cmake`, `EcoDrive/platform/stm32f4/platform.cmake`) are the build-time boundary that lets common control code target x86 simulation or STM32F4 hardware.

#### Communications, DAQ, and Desktop Tool
The reusable middleware layer implements binary framing and data acquisition: `ecolib/middleware/ABFStream.h`, `ecolib/middleware/DAQStream.h`, and `ecolib/middleware/DAQCodec.h`. It sits between control/DAQ producers and transport drivers. Host transports include TCP, UART, USB CDC, and USB exchange under `ecolib/driver/host/`; STM32 equivalents are under `ecolib/driver/stm32f4/`. `EcoTool/EcoTool.cpp`, with `EcoTool/Serial.hpp` and `EcoTool/TcpClient.hpp`, is the PC-side consumer/controller for device sessions. The repository also vendors STM32 HAL/CMSIS, CMSIS-DSP, ImGui/ImPlot, and Catch2; external runtime interaction is primarily physical motor/inverter I/O plus USB-CDC, UART, and TCP rather than cloud services.

Main entry points: [`EcoDrive/PmsmControl/PmsmControlCore.cpp`](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControlCore.cpp) (Common control core), [`EcoDrive/CMakeLists.txt`](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/CMakeLists.txt) (EcoDrive build target), [`EcoDrive/PmsmControl/PmsmControl.cpp`](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControl.cpp) (Control application), [`ecolib/middleware/ABFStream.h`](https://github.com/omar-magdy0/forgedrive/blob/main/ecolib/middleware/ABFStream.h) (ABF and DAQ middleware)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_control["Motor Control"]
  node_ecodrive_main["EcoDrive entry<br/>firmware/simulation executable<br/>[main.cpp]"]
  node_ecodrive_cmake["EcoDrive build target<br/>CMake target<br/>[CMakeLists.txt]"]
  node_control_app["Control application<br/>mode dispatch<br/>[PmsmControl.cpp]"]
  node_control_state["Control state &amp; config<br/>shared control model<br/>[PmsmControlTypes.h]"]
  node_control_core["Common control core<br/>real-time control core"]
  node_foc["FOC fast path<br/>PWM-rate control"]
  node_commissioning["Self-commissioning<br/>HFI identification"]
  node_position["Position driver<br/>rotor position abstraction<br/>[PosDriver.cpp]"]
end

subgraph group_platform["Runtime Platforms"]
  node_host_cmake["Host platform selection<br/>CMake platform selector<br/>[platform.cmake]"]
  node_stm_cmake["STM32F4 platform selection<br/>CMake platform selector<br/>[platform.cmake]"]
  node_host_runtime["Host SIL runtime<br/>native simulation platform<br/>[platform.cpp]"]
  node_sil_models["Motor and Hall models<br/>software-in-the-loop models<br/>[el_mc3p.cpp]"]
  node_stm_runtime["STM32F4 runtime<br/>embedded platform<br/>[platform.c]"]
end

subgraph group_comms["Communications"]
  node_middleware["ABF and DAQ middleware<br/>binary framing and DAQ<br/>[ABFStream.h]"]
  node_host_transport["Host transports<br/>TCP, UART, USB CDC"]
  node_stm_transport["STM32 transports<br/>device transport drivers"]
end

subgraph group_tool["Desktop Tool"]
  node_ecotool_cmake["EcoTool build target<br/>CMake target<br/>[CMakeLists.txt]"]
  node_ecotool["EcoTool<br/>desktop session tool<br/>[EcoTool.cpp]"]
end

node_ecodrive_cmake -->|"builds"| node_ecodrive_main
node_ecodrive_main -->|"starts"| node_control_app
node_control_app -->|"uses"| node_control_state
node_control_app -->|"dispatches to"| node_control_core
node_control_core -->|"runs FOC mode"| node_foc
node_control_core -->|"runs commissioning mode"| node_commissioning
node_foc -->|"reads rotor position"| node_position
node_commissioning -->|"uses position feedback"| node_position
node_host_cmake -.->|"selects"| node_host_runtime
node_stm_cmake -.->|"selects"| node_stm_runtime
node_ecodrive_cmake -.->|"host build option"| node_host_cmake
node_ecodrive_cmake -.->|"embedded build option"| node_stm_cmake
node_control_core -->|"normalized I/O boundary"| node_host_runtime
node_control_core -->|"normalized I/O boundary"| node_stm_runtime
node_host_runtime -->|"simulates with"| node_sil_models
node_control_core -->|"publishes DAQ"| node_middleware
node_middleware -->|"frames over"| node_host_transport
node_middleware -->|"frames over"| node_stm_transport
node_stm_transport -->|"device I/O"| node_stm_runtime
node_ecotool_cmake -->|"builds"| node_ecotool
node_ecotool -->|"TCP/serial sessions"| node_host_transport

click node_ecodrive_main "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/main.cpp"
click node_ecodrive_cmake "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/CMakeLists.txt"
click node_control_app "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControl.cpp"
click node_control_state "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControlTypes.h"
click node_control_core "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControlCore.cpp"
click node_foc "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControlCore_Foc.cpp"
click node_commissioning "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControlCore_SComm.cpp"
click node_position "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PosDriver.cpp"
click node_host_cmake "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/platform/host/platform.cmake"
click node_stm_cmake "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/platform/stm32f4/platform.cmake"
click node_host_runtime "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/platform/host/platform.cpp"
click node_sil_models "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/platform/host/el_mc3p.cpp"
click node_stm_runtime "https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/platform/stm32f4/platform.c"
click node_middleware "https://github.com/omar-magdy0/forgedrive/blob/main/ecolib/middleware/ABFStream.h"
click node_ecotool_cmake "https://github.com/omar-magdy0/forgedrive/blob/main/EcoTool/CMakeLists.txt"
click node_ecotool "https://github.com/omar-magdy0/forgedrive/blob/main/EcoTool/EcoTool.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_ecodrive_main,node_ecodrive_cmake,node_control_app,node_control_state,node_control_core,node_foc,node_commissioning,node_position toneBlue
class node_host_cmake,node_stm_cmake,node_host_runtime,node_sil_models,node_stm_runtime toneAmber
class node_middleware,node_host_transport,node_stm_transport toneMint
class node_ecotool_cmake,node_ecotool toneRose
```

## Components

### Motor Control

- [EcoDrive entry](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/main.cpp): firmware/simulation executable · `EcoDrive/main.cpp`
- [EcoDrive build target](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/CMakeLists.txt): CMake target · `EcoDrive/CMakeLists.txt`
- [Control application](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControl.cpp): mode dispatch · Selects safe idle, FOC, and self-commissioning modes. · `EcoDrive/PmsmControl/PmsmControl.cpp`
- [Control state & config](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControlTypes.h): shared control model · `EcoDrive/PmsmControl/PmsmControlTypes.h`
- [Common control core](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControlCore.cpp): real-time control core · Consumes normalized measurements and produces inverter commands. · `EcoDrive/PmsmControl/PmsmControlCore.cpp`
- [FOC fast path](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControlCore_Foc.cpp): PWM-rate control · Sampling, transforms, current regulation, and PWM duty generation. · `EcoDrive/PmsmControl/PmsmControlCore_Foc.cpp`
- [Self-commissioning](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PmsmControlCore_SComm.cpp): HFI identification · `EcoDrive/PmsmControl/PmsmControlCore_SComm.cpp`
- [Position driver](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/PmsmControl/PosDriver.cpp): rotor position abstraction · `EcoDrive/PmsmControl/PosDriver.cpp`

### Runtime Platforms

- [Host platform selection](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/platform/host/platform.cmake): CMake platform selector · `EcoDrive/platform/host/platform.cmake`
- [STM32F4 platform selection](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/platform/stm32f4/platform.cmake): CMake platform selector · `EcoDrive/platform/stm32f4/platform.cmake`
- [Host SIL runtime](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/platform/host/platform.cpp): native simulation platform · `EcoDrive/platform/host/platform.cpp`
- [Motor and Hall models](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/platform/host/el_mc3p.cpp): software-in-the-loop models · `EcoDrive/platform/host/el_mc3p.cpp`
- [STM32F4 runtime](https://github.com/omar-magdy0/forgedrive/blob/main/EcoDrive/platform/stm32f4/platform.c): embedded platform · Connects control commands and measurements to physical inverter I/O. · `EcoDrive/platform/stm32f4/platform.c`

### Communications

- [ABF and DAQ middleware](https://github.com/omar-magdy0/forgedrive/blob/main/ecolib/middleware/ABFStream.h): binary framing and DAQ · `ecolib/middleware/ABFStream.h`
- **Host transports**: TCP, UART, USB CDC
- **STM32 transports**: device transport drivers

### Desktop Tool

- [EcoTool build target](https://github.com/omar-magdy0/forgedrive/blob/main/EcoTool/CMakeLists.txt): CMake target · `EcoTool/CMakeLists.txt`
- [EcoTool](https://github.com/omar-magdy0/forgedrive/blob/main/EcoTool/EcoTool.cpp): desktop session tool · PC-side device consumer and controller. · `EcoTool/EcoTool.cpp`

## Connections

- EcoDrive build target → EcoDrive entry: builds
- EcoDrive entry → Control application: starts
- Control application → Control state & config: uses
- Control application → Common control core: dispatches to
- Common control core → FOC fast path: runs FOC mode
- Common control core → Self-commissioning: runs commissioning mode
- FOC fast path → Position driver: reads rotor position
- Self-commissioning → Position driver: uses position feedback
- Host platform selection → Host SIL runtime: selects
- STM32F4 platform selection → STM32F4 runtime: selects
- EcoDrive build target → Host platform selection: host build option
- EcoDrive build target → STM32F4 platform selection: embedded build option
- Common control core → Host SIL runtime: normalized I/O boundary
- Common control core → STM32F4 runtime: normalized I/O boundary
- Host SIL runtime → Motor and Hall models: simulates with
- Common control core → ABF and DAQ middleware: publishes DAQ
- ABF and DAQ middleware → Host transports: frames over
- ABF and DAQ middleware → STM32 transports: frames over
- STM32 transports → STM32F4 runtime: device I/O
- EcoTool build target → EcoTool: builds
- EcoTool → Host transports: TCP/serial sessions
