# foc_controller **Repository Path**: mymcu/foc_controller ## Basic Information - **Project Name**: foc_controller - **Description**: 电机控制器,带c语言的s-function simulink仿真。除了BSP外,仿真代码和mcu上运行的代码完全一致。 - **Primary Language**: Unknown - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 19 - **Created**: 2025-11-16 - **Last Updated**: 2025-11-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # foc_controller FOC Firmware based on STM32F4 ## Project Overview This is a Field-Oriented Control (FOC) firmware project designed for STM32F4-series microcontrollers, featuring complete motor control logic, a hardware abstraction layer, and a communication protocol stack. The project supports USB communication and includes Simulink integration support. ## Directory Structure ``` motor_controller/ ├── Firmware/ - Core firmware code │ ├── app/ - Application layer code │ ├── bsp/ - Board Support Package (hardware drivers) │ ├── controller/ - FOC control algorithm implementation │ ├── drivers/ - STM32F4 HAL drivers and CMSIS core files │ ├── os/ - Real-time operating system support │ ├── protocols/ - Communication protocol implementation │ └── utils/ - Utility functions ├── Projects/ - Project files (e.g., Keil uVision) ├── Simulink/ - Simulink models and simulation support └── simulation/ - Simulation-related code ``` ## Key Features - Field-Oriented Control (FOC) based on SVPWM - Current loop control - Low-pass filter implementation - USB virtual serial port communication support - Real-time operating system support (task scheduling, timers, etc.) - Serial communication protocol with data encoding/decoding - Motor control command handling (start/stop, mode switching, parameter configuration, etc.) ## Dependencies - STM32F4xx HAL Library - CMSIS Core Files - USB Device Library - Real-time Operating System (lightweight custom implementation) ## Build and Deployment 1. Open the project file (`Projects/odrive_mks.uvprojx`) in Keil uVision. 2. Configure target hardware settings. 3. Compile and download to the STM32F4 target board. 4. Communicate and debug via USB connection. ## Communication Protocol A custom binary protocol is implemented over USB, supporting the following commands: - Motor start/stop - Control mode switching - Setting voltage/current target values - Forcing open-loop operation - Data acquisition and waveform visualization ## Simulation Support The project includes Simulink integration for algorithm verification and debugging: - Motor model simulation - PWM signal generation - ADC sampling simulation - Encoder signal simulation ## License This project is licensed under the MIT License — see the LICENSE file for details. ## Contributions Pull requests are welcome. For major changes, please open an issue first to discuss. ## Author Kevin Huhui ## Acknowledgments - STM32F4 HAL drivers and CMSIS files from ST's official library - USB device library from STM32 USB Device Library - Some code structure inspired by the open-source project ODrive