Skip to content

Commit 1aa37bb

Browse files
committed
FEAT library version 1.6.0
1 parent 156eb70 commit 1aa37bb

File tree

166 files changed

+10744
-6004
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

166 files changed

+10744
-6004
lines changed

README.md

Lines changed: 110 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,63 @@
1-
# Arduino Simple FOC library minimal example
1+
# Arduino *SimpleFOClibrary* v1.6.0 - minimal project builder
22

33
![MinimalBuild](https://github.com/askuric/Arduino-FOC/workflows/MinimalBuild/badge.svg?branch=minimal)
44
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
55
[![arduino-library-badge](https://www.ardu-badge.com/badge/Simple%20FOC.svg?)](https://www.ardu-badge.com/badge/Simple%20FOC.svg)
66

7-
This is the repository of the [*SimpleFOClibrary*](https://github.com/askuric/Arduino-FOC) intended to be used to crete the projects with minimal code possible which is specific for certain **motor+sensor+driver** combination.
7+
This is the branch of the [*SimpleFOClibrary*](https://github.com/askuric/Arduino-FOC) repository intended to be used to simplify the creation of the projects with minimal code possible which is specific for certain **motor+sensor+driver** combination.
88

99
### Repository structure
10+
Library source code structure
1011
```shell
1112
├─── library_source
1213
| |
13-
| ├─ BLDCMotor.cpp/h # BLDCMotor class implementing all the FOC operation
14-
| ├─ FOCutils.cpp/h # Utility functions
15-
| ├─ defaults.h # Default configuration values
16-
│ ├─ Sensor.h # Abstract Sensor class that all the sensors implement
14+
| ├─── common # Contains all the common utility classes and functions
15+
| | |
16+
| | ├─ defaults.h # default motion control parameters
17+
| | |
18+
| | ├─ foc_utils.cpp./h # utility functions of the FOC algorithm
19+
| | ├─ hardware_utils.cpp./h # all the hardware specific implementations are in these files
20+
| | ├─ pid.cpp./h # class implementing PID controller
21+
| | ├─ lowpass_filter.cpp./h # class implementing Low pass filter
22+
| | |
23+
| | ├─ FOCMotor.cpp./h # common class for all implemented motors
24+
| | └─ Sensor./h # common class for all implemented sensors
25+
| |
26+
| ├─ BLDCMotor.cpp/h # BLDC motor handling class
27+
| ├─ StepperMotor.cpp/h # Stepper motor handling class
1728
│ │
1829
│ ├─ Encoder.cpp/h # Encoder class implementing the Quadrature encoder operations
1930
│ ├─ MagneticSensorSPI.cpp/h # class implementing SPI communication for Magnetic sensors
2031
│ ├─ MagneticSensorI2C.cpp/h # class implementing I2C communication for Magnetic sensors
2132
│ ├─ MagneticSensorAnalog.cpp/h # class implementing Analog output for Magnetic sensors
22-
│ └─ HallSensor.cpp/h # class implementing Hall sensor
23-
|
24-
└─── minimal_project_examples
25-
├─ arduino_foc_minimal_openloop # Arduino minimal code for running a motor in the open loop
26-
├─ arduino_foc_minimal_encoder # Arduino minimal code for running a motor with Encoder
27-
├─ arduino_foc_minimal_hall # Arduino minimal code for running a motor with Hall sensors
28-
├─ arduino_foc_minimal_magnetic_i2c # Arduino minimal code for running a motor with I2C magnetic sensor
29-
└─ arduino_foc_minimal_magnetic_spi # Arduino minimal code for running a motor with SPI magnetic sensor
33+
└─ HallSensor.cpp/h # class implementing Hall sensor
34+
```
35+
36+
Minimal project examples provided for quick start:
37+
```shell
38+
├─── minimal_project_examples # Project examples
39+
│ ├─ arduino_foc_bldc_openloop # BLDC motor + Open loop control
40+
│ ├─ arduino_foc_bldc_encoder # BLDC motor + Encoder
41+
│ ├─ arduino_foc_bldc_hall # BLDC motor + Hall sensors
42+
│ ├─ arduino_foc_bldc_magnetic_i2c # BLDC motor + I2C magnetic sensor
43+
│ ├─ arduino_foc_bldc_magnetic_spi # BLDC motor + SPI magnetic sensor
44+
|
45+
│ ├─ arduino_foc_stepper_openloop # Stepper motor + Open loop control
46+
│ ├─ arduino_foc_stepper_encoder # Stepper motor + Encoder
47+
└─ arduino_foc_stepper_magnetic_i2c # Stepper motor + I2C magnetic sensor
3048
```
3149

50+
51+
3252
# Creating your own minimal project
33-
First you need to download this repository to your computer.
3453

54+
Creating your own minimal project version is very simple and is done in four steps:
55+
- Step 0: Download minimal branch contents to your PC
56+
- Step 1: Create your the arduino project
57+
- Step 2: Add motor specific code
58+
- Step 3: Add sensor specific code
59+
60+
## Step 0. Download the code
3561
#### Github website download
3662
- Make sure you are in [minimal branch](https://github.com/askuric/Arduino-FOC/tree/minimal)
3763
- Download the code by clicking on the `Clone or Download > Download ZIP`.
@@ -48,100 +74,119 @@ After this step you will be able to open the example projects directly with Ardu
4874

4975
> **BEWARE** In some cases this minimal version of the code will produce conflicts with the *Simple FOC* library if it is installed through Arduino library manager. So you might need to uninstall the library to run minimal projects.
5076
51-
## BLDC motor support code
77+
## Step 1. Creating the Arduino project
5278

53-
In the `library_source` folder you will find all *SimpleFOClibrary* source files. From those files you will choose just the ones you need for your own project.
54-
55-
In each Arduino project using the *SimpleFOClibrary* you will need to copy these six files into your project directory.
79+
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:
5680

5781
```shell
5882
├─── my_arduino_project
5983
| ├─ my_arduino_project.ino
60-
| |
61-
| ├─ BLDCMotor.cpp/h # BLDCMotor class implementing all the FOC operation
62-
| ├─ FOCutils.cpp/h # Utility functions
63-
| ├─ defaults.h # Default configuration values
64-
└─ Sensor.h # Abstract Sensor class that all the sensors implement
84+
| └─── src
85+
| | ├─── common # Contains all the common utility classes and functions
86+
| | ├─ defaults.h # default motion control parameters
87+
| | ├─ foc_utils.cpp./h # utility functions of the FOC algorithm
88+
| | ├─ hardware_utils.cpp./h # all the hardware specific implementations are in these files
89+
| | ├─ pid.cpp./h # class implementing PID controller
90+
| | ├─ lowpass_filter.cpp./h # class implementing Low pass filter
91+
| | ├─ FOCMotor.cpp./h # common class for all implemented motors
92+
| | └─ Sensor./h # common class for all implemented sensors
6593
```
6694

67-
And in your arduino code you need to add the include:
95+
## Step 2. Add motor specific code
96+
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
97+
```shell
98+
├─── my_arduino_project
99+
| ├─ my_arduino_project.ino
100+
| └─── src
101+
| | ├─── common # Common utility classes and functions
102+
| | |
103+
| | └─ BLDCMotor.cpp/h # BLDC motor handling class
104+
```
105+
And in your Arduino code in the `my_arduino_project.ino` file make sure to add the the include:
68106
```cpp
69-
#include "BLDCMotor.h"
107+
#include "src/BLDCMotor.h"
70108
```
109+
For stepper motors the procedure is equivalent:
71110

72-
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_minimal_openloop.ino` project example.
111+
```shell
112+
├─── my_arduino_project
113+
| ├─ my_arduino_project.ino
114+
| └─── src
115+
| | ├─── common # Common utility classes and functions
116+
| | |
117+
| | └─ StepperMotor.cpp/h # Stepper motor handling class
118+
```
119+
And the include:
120+
```cpp
121+
#include "src/StepperMotor.h"
122+
```
123+
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.
73124

74-
## Sensor support
75-
In order to support the different position sensors you will have to add their `*.cpp` and `*.h` files into the directory. All you need to do is copy the header files from the `library_source` directory.
125+
## Step 3. Add sensor specific code
126+
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.
76127

77128

78129
### Example: Encoder sensor
79-
For example if you wish to use encoder, your arduino project will have structure:
80-
130+
For example if you wish to use BLDC motor and encoder as a sensor, your arduino project will have structure:
81131
```shell
82132
├─── my_arduino_project
83133
| ├─ my_arduino_project.ino
84-
| |
85-
| ├─ BLDCMotor.cpp/h # BLDCMotor class implementing all the FOC operation
86-
| ├─ FOCutils.cpp/h # Utility functions
87-
│ ├─ defaults.h # Default configuration values
88-
│ ├─ Sensor.h # Abstract Sensor class that all the sensors implement
89-
|
90-
└─ Encoder.cpp/h # Encoder class implementing the Quadrature encoder operations
134+
| └─── src
135+
| | ├─── common # Common utility classes and functions
136+
| | |
137+
| | ├─ BLDCMotor.cpp/h # BLDC motor handling class
138+
| | └─ Encoder.cpp/h # Encoder class implementing the Quadrature encoder operations
91139
```
92140
And your includes will be:
93141
```cpp
94-
#include "BLDCMotor.h"
95-
#include "Encoder.h"
142+
#include "src/BLDCMotor.h"
143+
#include "src/Encoder.h"
96144
```
97-
See `arduino_foc_minimal_encoder.ino`.
145+
See `arduino_foc_bldc_encoder` project example or `arduino_foc_stepper_encoder` for stepper equivalent.
98146

99147
### Example: SPI Magnetic sensor
100-
If you wish to use SPI magnetic sensor with your project, your folder structure will be:
148+
If you wish to use Stepper motor and SPI magnetic sensor in your project, your folder structure will be:
101149

102150
```shell
103151
├─── my_arduino_project
104152
| ├─ my_arduino_project.ino
105-
| |
106-
| ├─ BLDCMotor.cpp/h # BLDCMotor class implementing all the FOC operation
107-
| ├─ FOCutils.cpp/h # Utility functions
108-
│ ├─ defaults.h # Default configuration values
109-
│ ├─ Sensor.h # Abstract Sensor class that all the sensors implement
110-
|
111-
└─ MagneticSensorSPI.cpp/h # class implementing SPI communication for Magnetic sensors
153+
| └─── src
154+
| | ├─── common # Common utility classes and functions
155+
| | |
156+
| | ├─ StepperMotor.cpp/h # Stepper motor handling class
157+
| | └─ MagneticSensorSPI.cpp/h # class implementing SPI communication for Magnetic sensors
112158
```
113159
And your includes will be:
114160
```cpp
115-
#include "BLDCMotor.h"
116-
#include "MagneticSensorSPI.h"
161+
#include "src/StepperMotor.h"
162+
#include "src/MagneticSensorSPI.h"
117163
```
118-
See `arduino_foc_minimal_magnetic_spi.ino`.
164+
See `arduino_foc_stepper_magnetic_spi` project example or `arduino_foc_bldc_magnetic_spi` for BLDC motor equivalent.
119165

120166

121-
### Example: Analog magnetic sensor and encoder
167+
### Example: Multiple sensors: analog magnetic sensor and encoder
122168
For example if you wish to use magnetic sensor with SPI communication, your arduino project will have structure:
123169

124170
```shell
125171
├─── my_arduino_project
126172
| ├─ my_arduino_project.ino
127-
| |
128-
| ├─ BLDCMotor.cpp/h # BLDCMotor class implementing all the FOC operation
129-
| ├─ FOCutils.cpp/h # Utility functions
130-
│ ├─ defaults.h # Default configuration values
131-
│ ├─ Sensor.h # Abstract Sensor class that all the sensors implement
132-
|
133-
│ ├─ Encoder.cpp/h # Encoder class implementing the Quadrature encoder operations
134-
└─ MagneticSensorAnalog.cpp/h # class implementing Analog output for Magnetic sensors
173+
| └─── src
174+
| | ├─── common # Common utility classes and functions
175+
| | |
176+
| | ├─ BLDCMotor.cpp/h # BLDC motor handling class
177+
| | ├─ Encoder.cpp/h # Encoder class implementing the Quadrature encoder operations
178+
| | └─ MagneticSensorAnalog.cpp/h # class implementing Analog output for Magnetic sensors
135179
```
136180
And your includes will be:
137181
```cpp
138-
#include "BLDCMotor.h"
139-
#include "MagneticSensorAnalog.h"
140-
#include "Encoder.h"
182+
#include "src/BLDCMotor.h"
183+
#include "src/MagneticSensorAnalog.h"
184+
#include "src/Encoder.h"
141185
```
142186

187+
143188
## Documentation
144-
Find out more information about the Arduino *SimpleFOCproject* in [docs website](https://docs.simplefoc.com/)
189+
Find out more information about the Arduino *Simple**FOC**library* and *Simple**FOC**project* in [docs website](https://docs.simplefoc.com/)
145190

146191

147192
## Arduino FOC repo structure

library_source/BLDCMotor.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ BLDCMotor::BLDCMotor(int phA, int phB, int phC, int pp, int en)
2121

2222

2323
// init hardware pins
24-
void BLDCMotor::init() {
24+
void BLDCMotor::init(long pwm_frequency) {
2525
if(monitor_port) monitor_port->println("MOT: Init pins.");
2626
// PWM pins
2727
pinMode(pwmA, OUTPUT);
@@ -32,19 +32,21 @@ void BLDCMotor::init() {
3232
if(monitor_port) monitor_port->println("MOT: PWM config.");
3333
// Increase PWM frequency to 32 kHz
3434
// make silent
35-
_setPwmFrequency(pwmA, pwmB, pwmC);
35+
_setPwmFrequency(pwm_frequency, pwmA, pwmB, pwmC);
3636

3737
// sanity check for the voltage limit configuration
3838
if(voltage_limit > voltage_power_supply) voltage_limit = voltage_power_supply;
3939
// constrain voltage for sensor alignment
4040
if(voltage_sensor_align > voltage_limit) voltage_sensor_align = voltage_limit;
41+
// update the controller limits
42+
PID_velocity.limit = voltage_limit;
43+
P_angle.limit = velocity_limit;
4144

4245
_delay(500);
4346
// enable motor
4447
if(monitor_port) monitor_port->println("MOT: Enable.");
4548
enable();
4649
_delay(500);
47-
4850
}
4951

5052

library_source/BLDCMotor.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@ class BLDCMotor: public FOCMotor
3131
BLDCMotor(int phA,int phB,int phC,int pp, int en = NOT_SET);
3232

3333
/** Motor hardware init function */
34-
void init();
34+
void init(long pwm_frequency = NOT_SET) override;
3535
/** Motor disable function */
36-
void disable();
36+
void disable() override;
3737
/** Motor enable function */
38-
void enable();
38+
void enable() override;
3939

4040
/**
4141
* Function initializing FOC algorithm
4242
* and aligning sensor's and motors' zero position
4343
*/
44-
int initFOC( float zero_electric_offset = NOT_SET , Direction sensor_direction = Direction::CW);
44+
int initFOC( float zero_electric_offset = NOT_SET , Direction sensor_direction = Direction::CW) override;
4545
/**
4646
* Function running FOC algorithm in real-time
4747
* it calculates the gets motor angle and sets the appropriate voltages
4848
* to the phase pwm signals
4949
* - the faster you can run it the better Arduino UNO ~1ms, Bluepill ~ 100us
5050
*/
51-
void loopFOC();
51+
void loopFOC() override;
5252

5353
/**
5454
* Function executing the control loops set by the controller parameter of the BLDCMotor.
@@ -58,7 +58,7 @@ class BLDCMotor: public FOCMotor
5858
*
5959
* This function doesn't need to be run upon each loop execution - depends of the use case
6060
*/
61-
void move(float target = NOT_SET);
61+
void move(float target = NOT_SET) override;
6262

6363
// hardware variables
6464
int pwmA; //!< phase A pwm pin number

library_source/Encoder.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
Encoder::Encoder(int _encA, int _encB , float _ppr, int _index){
12-
12+
1313
// Encoder measurement structure init
1414
// hardware pins
1515
pinA = _encA;
@@ -87,11 +87,11 @@ void Encoder::handleIndex() {
8787
if(hasIndex()){
8888
int I = digitalRead(index_pin);
8989
if(I && !I_active){
90-
// align encoder on each index
90+
// align encoder on each index
9191
if(index_pulse_counter){
9292
long tmp = pulse_counter;
9393
// corrent the counter value
94-
pulse_counter = round((float)pulse_counter/(float)cpr)*cpr;
94+
pulse_counter = round((double)pulse_counter/(double)cpr)*cpr;
9595
// preserve relative speed
9696
prev_pulse_counter += pulse_counter - tmp;
9797
} else {
@@ -119,8 +119,8 @@ float Encoder::getVelocity(){
119119
// sampling time calculation
120120
float Ts = (timestamp_us - prev_timestamp_us) * 1e-6;
121121
// quick fix for strange cases (micros overflow)
122-
if(Ts <= 0 || Ts > 0.5) Ts = 1e-3;
123-
122+
if(Ts <= 0 || Ts > 0.5) Ts = 1e-3;
123+
124124
// time from last impulse
125125
float Th = (timestamp_us - pulse_timestamp) * 1e-6;
126126
long dN = pulse_counter - prev_pulse_counter;
@@ -133,7 +133,7 @@ float Encoder::getVelocity(){
133133
// only increment if some impulses received
134134
float dt = Ts + prev_Th - Th;
135135
pulse_per_second = (dN != 0 && dt > Ts/2) ? dN / dt : pulse_per_second;
136-
136+
137137
// if more than 0.05 passed in between impulses
138138
if ( Th > 0.1) pulse_per_second = 0;
139139

@@ -176,10 +176,10 @@ int Encoder::hasIndex(){
176176
}
177177

178178

179-
// encoder initialisation of the hardware pins
179+
// encoder initialisation of the hardware pins
180180
// and calculation variables
181181
void Encoder::init(){
182-
182+
183183
// Encoder - check if pullup needed for your encoder
184184
if(pullup == Pullup::INTERN){
185185
pinMode(pinA, INPUT_PULLUP);
@@ -190,7 +190,7 @@ void Encoder::init(){
190190
pinMode(pinB, INPUT);
191191
if(hasIndex()) pinMode(index_pin,INPUT);
192192
}
193-
193+
194194
// counter setup
195195
pulse_counter = 0;
196196
pulse_timestamp = _micros();
@@ -222,8 +222,7 @@ void Encoder::enableInterrupts(void (*doA)(), void(*doB)(), void(*doIndex)()){
222222
if(doB != nullptr) attachInterrupt(digitalPinToInterrupt(pinB), doB, RISING);
223223
break;
224224
}
225-
225+
226226
// if index used initialize the index interrupt
227227
if(hasIndex() && doIndex != nullptr) attachInterrupt(digitalPinToInterrupt(index_pin), doIndex, CHANGE);
228228
}
229-

0 commit comments

Comments
 (0)