Link
★ Loading...
Helpful?
On this page

SimpleFOCDrivers library

Library Compile License: MIT arduino-library-badge

The SimpleFOClibrary is kept lean on purpose: motor control algorithms, PWM support for the different MCUs, and generic sensors and communication. Everything that is specific to a particular chip, or that not everyone needs, lives in the SimpleFOCDrivers library:

  • Driver ICs with SPI or I2C configuration, like the DRV8316 or TMC6200
  • Sensor drivers for over 20 position sensor ICs, plus special sensors like the CalibratedSensor
  • Sensorless control with a flux observer, no position sensor needed
  • Communication: CAN, binary and ASCII packet protocols, telemetry, I2C
  • Settings storage: save the motor calibration and tuning to flash or EEPROM
  • Utilities: STM32 CORDIC acceleration, PWM input, motion planning

Source on GitHub

Guides

These modules have a dedicated page in the docs:

Guide What it’s for
DRV8316 driver Configuring and diagnosing the DRV8316 integrated driver, current sensing, fault handling
Calibrated sensor Compensating magnet eccentricity errors with a lookup table
Sensorless flux observer Running FOC without a position sensor, estimating the angle from the phase currents
Registers, PacketCommander and telemetry Reading and writing any motor value, binary and ASCII protocols, monitoring
CAN communication Controlling one or more motors over a CAN bus
Settings storage Saving calibration and tuning so you can skip them on startup

For all the other modules, see the README in each module’s folder, linked in the catalog below.

Installation

The library is available in the Arduino Library Manager and PlatformIO as Simple FOC Drivers.

  • Arduino IDE: Tools > Manage Libraries..., search for Simple FOC drivers and install it.
  • PlatformIO: add it to your platformio.ini:
lib_deps =
    Simple FOC
    SimpleFOCDrivers

To get the newest features before they are released, use the dev branch from GitHub instead: https://github.com/simplefoc/Arduino-FOC-drivers.git#dev.

Usage

Include the library header, and then the header of the module you want to use:

#include <SimpleFOC.h>
#include <SimpleFOCDrivers.h>
#include "encoders/as5048a/MagneticSensorAS5048A.h"

The modules are designed to slot into normal SimpleFOClibrary code: a sensor driver is a Sensor, a driver IC class is a BLDCDriver, so you link them to the motor exactly like the built-in classes.

Catalog

Motor and gate driver ICs

Drivers for ICs with an SPI or I2C configuration and status interface. They extend the BLDCDriver3PWM / BLDCDriver6PWM classes, so they work as drop-in replacements.

Module Description
DRV8316 TI DRV8316 integrated 3-phase driver with current sensing, SPI
DRV8376 TI DRV8376 integrated 3-phase driver, SPI
DRV8320 / DRV8323 TI DRV832x gate drivers, SPI
TMC6200 Trinamic TMC6200 gate driver, SPI
STSPIN32G4 ST STSPIN32G4 gate driver with integrated STM32G4 MCU, I2C
SimpleFOCNano Driver preconfigured for the SimpleFOCNano board

Position sensors

Hardware-specific sensor drivers. Compared to the generic MagneticSensorSPI / MagneticSensorI2C, they support chips the generic classes can’t (SSI, half-duplex SPI, special protocols) and give access to status and configuration registers.

Module Description
AS5047 AMS AS5047P / AS5047D, SPI
AS5047U AMS AS5047U, SPI
AS5048A AMS AS5048A, SPI
AS5145 AMS AS5145 / AS5045, SSI
AS5600 AMS AS5600 / AS5600L, I2C
AEAT-8800-Q24 Broadcom AEAT-8800-Q24, mixed SPI/SSI
A1334 Allegro A1334, SPI
KTH78xx Conntek KTH78xx series, SPI
MA330 MPS MagAlpha MA330, SPI
MA730 MPS MagAlpha MA730, SPI or SSI
MA735 MPS MagAlpha MA735, SPI or SSI
MT6701 MagnTek MT6701, SSI
MT6816 MagnTek MT6816, SPI
MT6826S MagnTek MT6826S, SPI
MT6835 MagnTek MT6835 21-bit, SPI
SC60228 SemiMent SC60228, SPI
TLE5012B Infineon TLE5012B, half-duplex SPI (STM32)
STM32 hardware encoder ABI quadrature encoders using STM32 timers, no interrupts needed
STM32 low power encoder ABI quadrature encoders using STM32 LPTIM timers
ESP32 hardware encoder ABI quadrature encoders using the ESP32 pulse counter
STM32 PWM sensor Sensors with PWM output, measured with STM32 timers

Special sensors

Sensors that wrap or replace another sensor to improve or extend it.

Module Description
CalibratedSensor Compensates magnet eccentricity with a calibration lookup table
SmoothingSensor Extrapolates the angle between sensor updates, for slow or low-resolution sensors
HysteresisSensor Adds configurable hysteresis to any sensor
LinearHall Analog (linear) hall sensors with interpolation
Flux observer Sensorless control: estimates the rotor angle from the phase currents, requires current sensing

Communication

Most communication modules are built on the registers abstraction: every motor value (target, PID gains, limits, measured currents…) has a register number, and can be read or written the same way over any channel. See Registers, PacketCommander and telemetry.

Module Description
PacketCommander Read and write registers over serial, in ASCII or binary
Telemetry Stream any registers at a fixed rate, for logging and plotting
CAN Register-based CAN protocol, multiple nodes and motors per bus
I2CCommander Register-based control over I2C
STM32 speed/dir input Set the target velocity with PWM speed and direction inputs

All register-based protocols are supported by the Python library PySimpleFOC.

Settings storage

Save motor calibration and settings, so you can skip the calibration on startup. See Settings storage.

Module Description
STM32 flash STM32G4 on-board flash
SAMD NVM SAMD on-board flash or RWWEE memory
CAT24 I2C EEPROM External I2C EEPROMs (unfinished)

Utilities

Module Description
STM32 CORDIC Hardware-accelerated sine and cosine on STM32 MCUs with a CORDIC unit (e.g. STM32G4)
STM32 PWM input Measure PWM inputs with zero CPU overhead
Trapezoidal planner Simple trapezoidal motion profiles
Voltage sense Supply voltage measurement

How do we decide what goes there?

While working on the SimpleFOClibrary we sometimes develop code that doesn’t directly belong in the core library, or receive contributions that don’t quite fit. The drivers library is their home. It holds code that is:

  • hardware-specific, like a driver for a specific sensor or driver chip
  • related to motor control, but not needed by everyone
  • an implementation of a specific communication protocol

Keep in mind that code in the drivers library is used by fewer people than the core library, so it is less tested and less completely documented, and generally better suited to more experienced users. If you get stuck, ask on the community forum.