Arduino Simple Field Oriented Control (FOC) library
We live in very exciting times
Therefore this is an attempt to:
🎯 Demystify FOC algorithm and make a robust but simple Arduino library: Arduino SimpleFOClibrary- Support as many motor + sensor + driver + mcu combinations out there
🎯 Develop a modular FOC supporting BLDC driver boards:- Low-power gimbal driver (<5Amps) : Arduino SimpleFOCShield.
- NEW
📢 : Medium-power BLDC driver (<30Amps): Arduino SimpleFOCPowerShield . - See also @byDagor's fully-integrated ESP32 based board: Dagor Brushless Controller
NEW RELEASE
📢 : SimpleFOClibrary v2.2.1 see release
- Sensor class init bugfix #121
- Voltage/current limit handling bugs #118
- Added the new motion control interface to the commander see docs
- New target setting - possible to set the position, velocity and torque target at once
- Separated the motion control interface from full motor callback - only motion control and torque control type, enable disable and target setting
- New MCU support see docs
- Generic sensor class - to implement a new sensor only implement one function see docs
Arduino SimpleFOClibrary v2.2
This video demonstrates the SimpleFOClibrary basic usage, electronic connections and shows its capabilities.
Features
- Arduino compatible:
- Arduino library code
- Arduino Library Manager integration
- Open-Source: Full code and documentation available on github
- 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 multiple MCU architectures:
- Plug & play: Arduino SimpleFOCShield
Arduino SimpleFOCShield v2.0.4
Features
- Plug & play: In combination with Arduino SimpleFOClibrary - github
- Low-cost: Price of €15 - Check the pricing
- In-line current sensing: Up to 3Amps/5Amps bidirectional
- configurable: 3.3Amps - 3.3V adc, 5Amps - 5V adc
- Integrated 8V regulator:
- Enable/disable by soldering pads
- Max power 120W - max current 5A, power-supply 12-24V
- Designed for Gimbal motors with the internal resistance >10 Ωs.
- Stackable: running 2 motors in the same time
- Encoder/Hall sensors interface: Integrated 3.3kΩ pullups (configurable)
- I2C interface: Integrated 4.7kΩ pullups (configurable)
- Configurable pinout: Hardware configuration - soldering connections
- Arduino headers: Arduino UNO, Arduino MEGA, STM32 Nucleo boards...
- Open Source: Fully available fabrication files - how to make it yourself
Getting Started
Depending on if you want to use this library as the plug and play Arduino library or you want to get insight in the algorithm and make changes there are two ways to install this code.
Arduino SimpleFOClibrary installation to Arduino IDE
Arduino Library Manager
The simplest way to get hold of the library is directly by using Arduino IDE and its integrated Library Manager.
- Open Arduino IDE and start Arduino Library Manager by clicking:
Tools > Manage Libraries...
. - Search for
Simple FOC
library and install the latest version. - Reopen Arduino IDE and you should have the library examples in
File > Examples > Simple FOC
.
Using Github website
- Go to the github repository
- Click first on
Clone or Download > Download ZIP
. - Unzip it and place it in
Arduino Libraries
folder. Windows:Documents > Arduino > libraries
. - Reopen Arduino IDE and you should have the library examples in
File > Examples > Simple FOC
.
Using terminal
- Open terminal and run
cd #Arduino libraries folder
git clone https://github.com/simplefoc/Arduino-FOC.git
- Reopen Arduino IDE and you should have the library examples in
File > Examples > Simple FOC
.
SimpleFOClibrary minimal project builder
For those willing to experiment and to modify the code I suggest using the minimal project builder minimal branch.
This code is completely independent and you can run it as any other Arduino Sketch without the need for any libraries.
All you need to do is:
- Go to minimal branch
- Follow the tutorial in the README file and choose only the library files that are necessary for your application.
Arduino code example
This is a simple Arduino code example implementing the velocity control program of a BLDC motor with encoder.
NOTE: This program uses all the default control parameters.
#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
motor.controller = MotionControlType::velocity;
// 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);
}
You can find more details in the SimpleFOC documentation.
Example projects
Here are some of the SimpleFOClibrary and SimpleFOCShield application examples.
Documentation
Find out more information about the Arduino SimpleFOC project in docs website
Arduino FOC repo structure
Branch | Description | Status |
---|---|---|
master | Stable and tested library version | |
dev | Development library version | |
minimal | Minimal Arduino example with integrated library |