SimpleFOClibrary 2.4.0
Loading...
Searching...
No Matches
Simple Field Oriented Control BLDC motor control library

Introduction

Proper low-cost and low-power FOC supporting boards are very hard to find these days and even may not exist.
Even harder to find is a stable and simple FOC algorithm code capable of running on Arduino devices. Therefore this is an attempt to:

  • Demystify FOC algorithm and make a robust but simple Arduino library: Arduino SimpleFOC library
  • Develop a modular BLDC driver board: Arduino SimpleFOC shield.

Features

  • Arduino compatible: Arduino library code
  • Easy to setup and configure:
    • Easy hardware configuration
    • Easy tuning the control loops
  • Modular:
    • Supports as many sensors , BLDC motors and driver boards as possible
    • Supports as many application requirements as possible
  • Plug & play: Arduino SimpleFOC shield

Supported Hardware

  • Motors
    • BLDC motors
    • Stepper motors
    • Hybrid Stepper motors

Drivers

  • BLDC drivers
  • Gimbal drivers
  • Stepper drivers

Position sensors

  • Encoders
  • Magnetic sensors
  • Hall sensors

Microcontrollers

  • Arduino
  • STM32
  • ESP32
  • Teensy
  • many more ...

How to Use Examples

  1. Arduino IDE: Go to File > Examples > Simple FOC > [category] > [example]
  2. PlatformIO: Copy example code from the examples/ directory
  3. GitHub: Browse examples at https://github.com/simplefoc/Arduino-FOC/tree/master/examples

Each example includes:

  • Detailed comments explaining the code
  • Hardware connection diagrams (where applicable)
  • Expected behavior description
  • Common troubleshooting tips

Example code

#include <SimpleFOC.h>
// BLDCMotor( pole_pairs )
BLDCMotor motor = BLDCMotor(11);
// BLDCDriver( pin_pwmA, pin_pwmB, pin_pwmC, enable (optional) )
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 10, 11, 8);
// Encoder(pin_A, pin_B, CPR)
Encoder encoder = Encoder(2, 3, 2048);
// channel A and B callbacks
void doA(){encoder.handleA();}
void doB(){encoder.handleB();}
void setup() {
// initialize encoder hardware
encoder.init();
// hardware interrupt enable
encoder.enableInterrupts(doA, doB);
// link the motor to the sensor
motor.linkSensor(&encoder);
// power supply voltage [V]
driver.voltage_power_supply = 12;
// initialise driver hardware
driver.init();
// link driver
motor.linkDriver(&driver);
// set control loop type to be used
// initialize motor
motor.init();
// align encoder and start FOC
motor.initFOC();
}
void loop() {
// FOC algorithm function
motor.loopFOC();
// velocity control loop function
// setting the target velocity or 2rad/s
motor.move(2);
}
@ velocity
Velocity motion control.
Definition FOCMotor.h:51
int init() override
int init() override
Definition BLDCMotor.cpp:66
virtual void linkDriver(BLDCDriver *driver)
Definition BLDCMotor.cpp:61
void handleA()
Definition Encoder.cpp:42
void init() override
Definition Encoder.cpp:181
void handleB()
Definition Encoder.cpp:63
void enableInterrupts(void(*doA)()=nullptr, void(*doB)()=nullptr, void(*doIndex)()=nullptr)
Definition Encoder.cpp:212
float voltage_power_supply
power supply voltage
Definition FOCDriver.h:36
void linkSensor(Sensor *sensor)
Definition FOCMotor.cpp:53
MotionControlType controller
parameter determining the control loop to be used
Definition FOCMotor.h:253
virtual int initFOC()
Definition FOCMotor.cpp:774
virtual void move(float target=NOT_SET)
Definition FOCMotor.cpp:673
virtual void loopFOC()
Definition FOCMotor.cpp:580

License

MIT license, all text here must be included in any redistribution.