Skip to content

Arduino FOC for BLDC and Stepper motors - Arduino Based Field Oriented Control Algorithm Library

License

Notifications You must be signed in to change notification settings

jimeixuehua/Arduino-FOC

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arduino Simple Field Oriented Control (FOC) library

Library Compile License: MIT arduino-library-badge

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

  • Demystify FOC algorithm and make a robust but simple Arduino library: Arduino SimpleFOClibrary
  • Develop a modular BLDC driver board: Arduino SimpleFOCShield.
  • New 📢: Develop a modular Stepper motor board for FOC control: Arduino StepperFOCShield

NEW RELEASE 📢: SimpleFOClibrary v1.6.0

  • Stepper motor FOC support 🎨🎉 🎊 See in docs!
    • No losing steps
    • Backdrivable
    • Better dynamics than open-loop, Smoother than open-loop
    • short demo youtube video
  • Teensy support by Christopher Parrott
  • Pull requests by @cousinitt
    • refactoring and c++11 improvements
    • pid + low pass filter refactoring
  • Extended configurability of the sensor classes by @owennewo See in docs!
  • configurable pwm frequency See in docs!
    • stm32,teensy,eps32 - not for Arduino
    • stm32 added 12bit pwm resolution by Jürgen Frisch
  • Huge refactoring done in the library 😄

Arduino SimpleFOCShield

Features

  • Plug & play: In combination with Arduino SimpleFOClibrary
  • Low-cost: Price of €15 - Check the pricing
  • Max power 100W - max current 5A, power-supply 12-24V
    • Designed for Gimbal motors with the internal resistance >10 Ω.
  • Stackable: running 2 motors in the same time
  • Encoder/Hall sensor 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,
If you are interested in this board, order your version on this link: Simple FOC Shop

Arduino SimpleFOClibrary

This video demonstrates the Simple FOC library 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:
  • Modular:
  • Plug & play: Arduino SimpleFOCShield

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.

  • Full library installation Docs
  • Minimal code installation Docs

Arduino SimpleFOC library 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/askuric/Arduino-FOC.git
  • Reopen Arduino IDE and you should have the library examples in File > Examples > Simple FOC.

SimpleFOC library minimal sketch example

For those willing to experiment and to modify the code I suggest using the minimal version of the code.

This code is completely independent and you can run it as any other Arduino Sketch without the need for any libraries.

Github website download

  • Go to minimal branch
  • Download the code by clicking on the Clone or Download > Download ZIP.
  • Unzip it and open the sketch in Arduino IDE.

Using terminal

  • Open the terminal:
    cd *to you desired directory*
    git clone -b minimal https://github.com/askuric/Arduino-FOC.git
  • Then you just open it with the Arduino IDE and run it.

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( pin_pwmA, pin_pwmB, pin_pwmC, pole_pairs, enable (optional))
BLDCMotor motor = BLDCMotor(9, 10, 11, 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);
  
  // use monitoring with the BLDCMotor
  Serial.begin(115200);
  // monitoring port
  motor.useMonitoring(Serial);

  // set control loop type to be used
  motor.controller = ControlType::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);

  // monitoring function outputting motor variables to the serial terminal 
  motor.monitor();
}

You can find more details in the SimpleFOC documentation.

Example projects

Here are some of the SimpleFOC application examples.

Arduino Field Oriented Controlled Reaction Wheel Inverted Pendulum

This is a very cool open-source project of one of the simplest setups of the Reaction wheel inverted pendulum. Check out all the components and projects notes in the github repository.

The main benefits of using the BLDC motor in this project are:

  • High torque to weight ratio
    • The lighter the better
  • Lots of torque for low angular velocities
    • No need to spin the motor to very high PRM to achieve high torques
  • No gearboxes and backlash
    • Very smooth operation = very stable pendulum

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 Library Compile
dev Development library version Library Dev Compile
minimal Minimal Arduino example with integrated library MinimalBuild

About

Arduino FOC for BLDC and Stepper motors - Arduino Based Field Oriented Control Algorithm Library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 93.8%
  • C 6.2%