SimpleFOClibrary - project builder

Hi guys.

I was recently restructuring the minimal branch of the git repository and I decided to change the paradigm a bit. I restructured the minimal branch to a branch that allows you to create minimal versions of the SimpleFOClibrary specific for everyone’s needs.

This feature will enable you to:

  • Have smaller code size
  • Compile only the parts of the library you will be using in your code
  • Have more than one version of the library on your system
  • Dig into the library source code much easier
    • Customization
    • Implementation of new features/microcontrollers

How to create your own minimal version of the SimpleFOClibrary project

Creating your own minimal project version is very simple and is done in four steps:

  • Step 0: Download minimal branch contents to your PC
  • Step 1: Create your the arduino project
  • Step 2: Add motor specific code
  • Step 3: Add sensor specific code

Step 0. Download the code

Github website download

  • Make sure you are in minimal branch
  • Download the code by clicking on the Clone or Download > Download ZIP.
  • Unzip it

Using terminal

  • Open the terminal:
    cd *to you desired directory*
    git clone -b minimal https://github.com/simplefoc/Arduino-FOC.git
    

Step 1. Creating the Arduino project

Open a directory you want to use as your arduino project directory my_arduino_project and create my_arduino_project.ino file. After this you create src folder in this directory and copy the folder named common from the library_source folder. Your project directory should now have structure:

├─── my_arduino_project
| ├─ my_arduino_project.ino
| └─── src
| | ├─── common               # Contains all the common utility classes and functions
| | ├─ defaults.h             # default motion control parameters
| | ├─ foc_utils.cpp./h       # utility functions of the FOC algorithm
| | ├─ hardware_utils.cpp./h  # all the hardware specific implementations are in these files
| | ├─ pid.cpp./h             # class implementing PID controller
| | ├─ lowpass_filter.cpp./h  # class implementing Low pass filter
| | ├─ FOCMotor.cpp./h        # common class for all implemented motors  
| | └─ Sensor./h              # common class for all implemented sensors  

Step 2. Add motor specific code

If you wish to use the BLDC motor with your setup you will have to copy the BLDCMotor.cpp/h from the library_source folder, and if you wish to use the stepper motor make sure to copy the StepperMotor.cpp/h files and place them to the src folder

├─── my_arduino_project
| ├─ my_arduino_project.ino
| └─── src
| | ├─── common             # Common utility classes and functions
| | |
| | └─ BLDCMotor.cpp/h      # BLDC motor handling class  

And in your Arduino code in the my_arduino_project.ino file make sure to add the the include:

#include "src/BLDCMotor.h"

For stepper motors the procedure is equivalent:

├─── my_arduino_project
| ├─ my_arduino_project.ino
| └─── src
| | ├─── common             # Common utility classes and functions
| | |
| | └─ StepperMotor.cpp/h   # Stepper motor handling class 

And the include:

#include "src/StepperMotor.h"

If you wish to run your motor in the open loop mode these are all the files that you will need. See the arduino_foc_bldc_openloop and arduino_foc_stepper_openloop project example.

Step 3. Add sensor specific code

In order to support the different position sensors you will have to copy their *.cpp and *.h into your src directory. All you need to do is copy the header files from the library_source directory.

Example: Encoder sensor

For example if you wish to use BLDC motor and encoder as a sensor, your arduino project will have structure:

├─── my_arduino_project
| ├─ my_arduino_project.ino
| └─── src
| | ├─── common             # Common utility classes and functions
| | |
| | ├─ BLDCMotor.cpp/h      # BLDC motor handling class 
| | └─ Encoder.cpp/h        # Encoder class implementing the Quadrature encoder operations

And your includes will be:

#include "src/BLDCMotor.h"
#include "src/Encoder.h"

See arduino_foc_bldc_encoder project example or arduino_foc_stepper_encoder for stepper equivalent.

Example: SPI Magnetic sensor

If you wish to use Stepper motor and SPI magnetic sensor in your project, your folder structure will be:

├─── my_arduino_project
| ├─ my_arduino_project.ino
| └─── src
| | ├─── common                # Common utility classes and functions
| | |
| | ├─ StepperMotor.cpp/h      # Stepper motor handling class  
| | └─ MagneticSensorSPI.cpp/h  # class implementing SPI communication for Magnetic sensors

And your includes will be:

#include "src/StepperMotor.h"
#include "src/MagneticSensorSPI.h"

See arduino_foc_stepper_magnetic_spi project example or arduino_foc_bldc_magnetic_spi for BLDC motor equivalent.

Example: Multiple sensors: analog magnetic sensor and encoder

For example if you wish to use magnetic sensor with SPI communication, your arduino project will have structure:

├─── my_arduino_project
| ├─ my_arduino_project.ino
| └─── src
| | ├─── common                # Common utility classes and functions
| | |
| | ├─ BLDCMotor.cpp/h              # BLDC motor handling class  
| | ├─ Encoder.cpp/h                # Encoder class implementing the Quadrature encoder operations
| | └─ MagneticSensorAnalog.cpp/h   # class implementing Analog output for Magnetic sensors

And your includes will be:

#include "src/BLDCMotor.h"
#include "src/MagneticSensorAnalog.h"
#include "src/Encoder.h"

Let me know what do you think.