# tsfk9981/simple-force-control architecture

> Architecture diagram of the GitHub repository tsfk9981/simple-force-control, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tsfk9981/simple-force-control
- Repository: https://github.com/tsfk9981/simple-force-control

## Overview

Based on our review of the complete file tree and README, this project is clearly an Arduino library dedicated to implementing Field Oriented Control (FOC) for BLDC (and stepper) motors. It is designed to work with many kinds of sensors, drivers, and MCUs and even provides a rich set of example sketches for various hardware configurations. The overall purpose is to “demystify” FOC by providing a modular, portable, and easy-to-use library for motor control on platforms like Arduino, STM32, ESP32, and others.

##### Main Components
- Library Core Modules:
– Motor Control: This includes BLDCMotor, StepperMotor classes, current control algorithms, FOC routines, and the motion control loop.
– Sensor Abstraction: Modules for handling Encoder, HallSensor, and MagneticSensor input (located under src/sensors and common base classes).
– Driver Abstraction: Software drivers interfacing with motor drivers (for example, BLDCDriver3PWM, BLDCDriver6PWM, StepperDriver variants) found under src/drivers.
– Current Sensing: A dedicated set of modules for both generic and hardware-specific current sensing (under src/current_sense and its hardware_specific subfolders).
– Communication Modules: Components for handling configuration commands, debugging, and step/dir interfaces (all located in src/communication).

- Hardware Abstraction:
– Hardware-specific implementations for different microcontrollers (Arduino boards, STM32, ESP32, SAMD, etc.) are extensively provided, especially under folders such as src/current_sense/hardware_specific and src/drivers/hardware_specific.

- Examples and Tools:
– A rich set of example sketches are included (in the examples folder) demonstrating various configurations – motor control (velocity, position, torque, open loop), calibration routines, sensor tests, and even OSC control interfaces.
– Integration with SimpleFOCShield, SimpleFOCMini, and other board-specific implementations.

##### Relationships and Interactions
- At the top level, an Arduino sketch (from the examples) creates and configoses objects in the following order:
– Instantiate a motor control object (e.g., BLDCMotor) and link it with:
▪ A sensor object (Encoder, HallSensor, etc.) to monitor rotor position.
▪ A driver object (e.g., BLDCDriver3PWM) to translate control signals to the motor hardware.
▪ Optionally, link current sensing modules to provide feedback for torque and current control.
- The Motor Control layer calls the FOC algorithm (loopFOC) periodically in the control loop and uses the sensor feedback to update control commands.
- The Communication module (e.g., Commander, Debug) interfaces with external tools or user interfaces to configure or monitor the motor control behavior.
- Hardware abstraction layers isolate platform/mcu-specific code (like using ESP32 adc modules or STM32 HAL libraries) so that the same high-level API works across multiple microcontrollers.

##### Architectural Patterns and Design Principles
- Abstraction and Modularity: Each hardware component (motor, sensor, driver, current sensing) is represented as a distinct C++ object, enabling clear separation of concerns.
- Hardware Independence: Use of hardware-specific subfolders and API chaining (linkSensor, linkDriver) to hide low-level details.
- Layered Architecture: There is a clear separation between the high-level FOC control algorithm and the low-level hardware drivers and sensing implementations.
- Extensibility for multiple combinations of sensor+motor+driver, which makes the library highly modular.

##### Relevant Technologies and Frameworks
- Arduino IDE and Library Manager integration.
- Support for PlatformIO.
- Specific MCU support for Arduino, STM32, ESP32, SAMD, Teensy, and others.
- Use of various communication protocols (PWM signals, I2C, SPI) and standard Arduino libraries.

##### Elements
– Application/Example Sketch
– Motor Control Module (FOC algorithm)
– Sensor Abstraction Layer (Encoders, Hall and Magnetic Sensors)
– Driver Abstraction Layer (BLDCDriver, StepperDriver)
– Current Sensing Module (Generic + Hardware-Specific implementations)
– Communication & Debugging Module
– Hardware Abstraction Layer for MCU specifics
– Data flows from sensors to the Motor Control Module.
– Control commands flow from the Motor Control Module to the Driver.
– Feedback loops from current sensing back to control routines.
– Communication links from the control unit to external monitoring (e.g., debugging interfaces).
– High-level logic components (control algorithms) versus lower-level hardware abstraction.
– Components that interface with physical hardware versus software-only modules.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Application Layer"
        A["Example Sketches"]:::app
    end

    subgraph "Core Library"
        M["Motor Control Module"]:::core
        S["Sensor Abstraction Layer"]:::core
        D["Driver Abstraction Layer"]:::core
        C["Current Sensing Module"]:::core
        CD["Communication & Debugging Module"]:::core
    end

    subgraph "Hardware Abstraction Layer"
        HAL["MCU Specific Implementations"]:::hal
    end

    A -->|"instantiates"| M
    M -->|"linkSensor"| S
    M -->|"linkDriver"| D
    M -->|"feedback"| C
    M -->|"sendsCommand"| CD
    M -->|"utilizes"| HAL
    HAL -->|"supports"| S
    HAL -->|"supports"| D
    HAL -->|"supports"| C

    click A "https://github.com/tsfk9981/simple-force-control/tree/master/examples"
    click M "https://github.com/tsfk9981/simple-force-control/blob/master/src/BLDCMotor.cpp"
    click S "https://github.com/tsfk9981/simple-force-control/blob/master/src/common/base_classes/Sensor.cpp"
    click D "https://github.com/tsfk9981/simple-force-control/blob/master/src/common/base_classes/BLDCDriver.h"
    click C "https://github.com/tsfk9981/simple-force-control/blob/master/src/common/base_classes/CurrentSense.cpp"
    click CD "https://github.com/tsfk9981/simple-force-control/blob/master/src/communication/Commander.cpp"
    click HAL "https://github.com/tsfk9981/simple-force-control/blob/master/src/drivers/hardware_specific/atmega2560_mcu.cpp"

    classDef app fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef core fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef hal fill:#ccf5ff,stroke:#333,stroke-width:2px;
```
