$ show Sguan-ZhouQing/SguanFOC_Library
This repository provides a comprehensive, production-grade Field-Oriented Control (FOC) library for Permanent Magnet Synchronous Motors (PMSM) targeting embedded microcontrollers (STM32G4, CH32V3). It is written in C and organized into modular algorithm files covering coordinate transforms (Clarke, Park), PWM modulation (SVPWM, SPWM), multiple control strategies (PID, LADRC, SMC, STA), sensor feedback (encoder, Hall, sensorless observers like HFI, SMO, NLFO), and advanced features (MTPA, field weakening, harmonic suppression, cogging calibration, dead-time compensation, feedforward, disturbance observer, parameter identification). The architecture is driven by interrupt-based control loops (high-frequency current loop, low-frequency state machine, and serial communication loop) with compile-time configuration via macro switches and user-supplied hardware abstraction layers. The repository also includes open-source hardware designs (schematic and PCB for a robot-joint motor driver), a QT-based host computer application, Simulink simulation models, and a complete STM32G4 example project. Although the library contains multiple coexisting versions and v4.0.0 is partially incomplete, the core modules are well-crafted, independently reusable, and demonstrate advanced control theory implementation suitable for robotics, drones, industrial servos, and electric tools.
The LLM identified 4 files worth highlighting for reuse:
Implements fast sine/cosine via Taylor series expansion with Pade approximation, Newton-Raphson iterative square root, and utility functions (limit, interpolation, sign). The code is self-contained, uses no external dependencies, and is optimized for microcontroller execution without FPU. Can be extracted and reused in any embedded project requiring efficient trigonometric or numerical operations.
Provides a complete set of Q31 and Q15 fixed-point arithmetic functions including multiplication, division, accumulation, trigonometric approximations (sin, cos, atan2), and square root. Uses saturating arithmetic to avoid overflow in control loops. The module is independent of any hardware or OS, making it reusable in any fixed-point embedded control application.
Implements second-order IIR filters (Butterworth, Chebyshev Type I, Bessel) with coefficient precalculation, and a three-parameter notch filter. Supports low-pass, band-pass, and high-pass configurations. The filter structures use direct form II transposed, and the coefficient calculation is done offline with explicit formulas. The code is modular, well-documented (in Chinese), and can be ported to any project needing digital filtering.
Implements a lightweight serial debug interface using a custom JustFloat protocol. It includes a DJB2 hash-based command dictionary for register read/write and parameter tuning. The module handles UART transmission with buffering and CRC validation. The hash-table dispatch pattern is isolated and can be extracted for any embedded project needing a simple command-line interface over serial.