Skip to content

Commit 2de0ac0

Browse files
committed
update of v2.0.2 and sfoc shield v2.0
1 parent b7bb9ab commit 2de0ac0

File tree

20 files changed

+758
-333
lines changed

20 files changed

+758
-333
lines changed

_includes/js/custom.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ var classNames = [
5656
"Serial",
5757
"MySensor",
5858
"Wire",
59-
"SPIClass"
59+
"SPIClass",
60+
"LowPassFilter",
61+
"PIDController"
6062
];
6163

6264
var classProps = [

docs/simplefoc_library/digging_deeper/libray_source/index.md

Lines changed: 117 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,35 @@ has_children: True
1010
has_toc: false
1111
---
1212

13-
# Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span> source code [v1.5.0](https://github.com/simplefoc/Arduino-FOC/releases)
13+
# Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span> source code [v2.0.2](https://github.com/simplefoc/Arduino-FOC/releases)
1414
The arduino library code is organized into the standard [Arduino library structure](https://github.com/arduino/Arduino/wiki/Library-Manager-FAQ).
15-
The library contains main BLDC motor class `BLDCMotor` and three sensor classes `Encoder`, `MagneticSensorSPI` and `MagneticSensorI2C` implementing the `Sensor` interface. Furthermore it has `FOCutils` file with all the necessary utility functions and `defaults.h` with all the default configuration variables.
16-
15+
The library contains FOC implementation for two types of BLDC motors, standard three phase BLDC motor in the class `BLDCMotor` and 2 phase stepper motors `StepperMotor`. The library implements nu;erous position sensors and they are all placed in the `senors` directory as well as drivers which are in the `drivers` directory. Finally all the utility functions and classes are placed in the `common` folder.
1716
## Arduino library source structure
1817
```sh
1918
| src
20-
| ├─ SimpleFOC.h # SimpleFOC library include file
21-
| |
22-
| ├─ BLDCMotor.cpp/h # BLDCMotor class implementing all the FOC operations
23-
│ │
24-
│ ├─ Sensor.h # Abstract Sensor class that all the sensors implement
25-
│ ├─ Encoder.cpp/h # Encoder class implementing the Quadrature encoder operations
26-
│ ├─ MagneticSensorSPI.cpp/h # class implementing SPI communication for Magnetic sensors
27-
│ ├─ MagneticSensorI2C.cpp/h # class implementing I2C communication for Magnetic sensors
28-
│ ├─ MagneticSensorAnalog.cpp/h # class implementing Analog output for Magnetic sensors
29-
│ ├─ HallSensor.cpp/h # class implementing Hall sensor
30-
| |
31-
| ├─ FOCutils.cpp/h # Utility functions
19+
| ├─ SimpleFOC.h # Main include file
20+
| |
21+
| ├─ BLDCMotor.cpp/h # BLDC motor handling class
22+
| ├─ StepperMotor.cpp/h # Stepper motor handling class
3223
| |
33-
└─ defaults.h # Default configuration values
24+
│ ├─── common # Contains all the common utility classes and functions
25+
| ├─── drivers # PWM setting and driver handling specific code
26+
| ├─── sensors # Position sensor specific code
3427
```
3528

3629
<blockquote class="info">For more info visit <a href="http://source.simplefoc.com/" target="_blank"> full source code documentation <i class="fa fa-external-link fa-sm"></i></a></blockquote>
3730

31+
## Motors
3832
### `BLDCMotor.cpp/h`
3933
BLDCMotor class implementation
40-
- BLDC driver hardware configuration: `init()`, `pwmA,pwmB,pwmC,enable`
41-
- FOC algorithm functions and configuration: `loopFOC()`, `initFOC`,`foc_modulation`
42-
- PID controllers for the velocity and angle control: `PID_s`, `controllerPID()`,`velocityPID()`,`positionP`
43-
- Monitoring functions: `useMonitoring()`,`monitor()`,`monitor_port`
44-
- Communication interface with user in form of motor commands: `command()`
34+
- FOC algorithm implementation
35+
- Motion control implementation
36+
37+
### `StepperMotor.cpp/h`
38+
BLDCMotor class implementation
39+
- FOC algorithm implementation
40+
- Motion control implementation
41+
4542

4643
<blockquote class="info"><a href="foc_implementation"><i class="fa fa-copy"></i> FOC implementation details</a> - Documentation of the procedures and detailed explanations of the code implementing FOC algorithm
4744
</blockquote>
@@ -51,9 +48,89 @@ BLDCMotor class implementation
5148
<a href="commands_source"><i class="fa fa-copy"></i> Motor commands implementation </a> - Documentation of the motor commands functionality
5249
</blockquote>
5350

51+
## Drivers
52+
All the drivers that are supported in this library are placed in the drivers directory.
53+
```sh
54+
| ├─── drivers
55+
| | ├─ BLDCDriver3PWM.cpp/h # Implementation of generic 3PWM bldc driver
56+
| | ├─ BLDCDriver6PWM.cpp/h # Implementation of generic 6PWM bldc driver
57+
| | ├─ StepperDriver2PWM.cpp/h # Implementation of generic 2PWM stepper driver
58+
| | ├─ StepperDriver4PWM.cpp/h # Implementation of generic 4PWM stepper driver
59+
| | |
60+
| | ├─ hardware_api.h # common mcu specific api handling pwm setting and configuration
61+
| | |
62+
| | ├─── hardware_specific # mcu specific hadrware_api.h implementations
63+
| | | ├─ atmega2560_mcu.cpp # ATMega 2560 implementation
64+
| | | ├─ atmega328_mcu.cpp # ATMega 328 (Arduino UNO) implementation
65+
| | | ├─ esp32_mcu.cpp # esp32 implementation
66+
| | | ├─ stm32_mcu.cpp # stm32 implementation
67+
| | | ├─ teensy_mcu.cpp # teensy implementation
68+
| | | └─ generic_mcu./h # generic implementation - if not nay of above (not complete)
69+
```
70+
ALl BLDC drivers implement the abstract class `BLDCDriver`.
71+
```cpp
72+
class BLDCDriver{
73+
public:
74+
75+
/** Initialise hardware */
76+
virtual int init();
77+
/** Enable hardware */
78+
virtual void enable();
79+
/** Disable hardware */
80+
virtual void disable();
81+
82+
long pwm_frequency; //!< pwm frequency value in hertz
83+
float voltage_power_supply; //!< power supply voltage
84+
float voltage_limit; //!< limiting voltage set to the motor
85+
86+
/**
87+
* Set phase voltages to the harware
88+
*
89+
* @param Ua - phase A voltage
90+
* @param Ub - phase B voltage
91+
* @param Uc - phase C voltage
92+
*/
93+
virtual void setPwm(float Ua, float Ub, float Uc);
94+
};
95+
```
96+
And all the stepper drivers implement the `StepperDriver` abstract class.
97+
```cpp
98+
class StepperDriver{
99+
public:
100+
101+
/** Initialise hardware */
102+
virtual int init();
103+
/** Enable hardware */
104+
virtual void enable();
105+
/** Disable hardware */
106+
virtual void disable();
107+
108+
long pwm_frequency; //!< pwm frequency value in hertz
109+
float voltage_power_supply; //!< power supply voltage
110+
float voltage_limit; //!< limiting voltage set to the motor
111+
112+
/**
113+
* Set phase voltages to the harware
114+
*
115+
* @param Ua phase A voltage
116+
* @param Ub phase B voltage
117+
*/
118+
virtual void setPwm(float Ua, float Ub);
119+
};
120+
```
121+
122+
Furthermore all the supported MCU architectures with the simplefoc library have to implement the header file `hardware_api.h`. The off-the-shelf supported architectures will have implementation of the `hardware_api.h` placed in the `hardware_specific` folder. If you wish to implement a new MCU please do create a new instance of the `my_new_mcu.cpp` and implement all the functions from the `hardware_api.h`, or at least the ones that you need.
123+
## Sensors
54124

55-
### `Sensor.h`
56-
Abstract sensor class that every sensor needs to implement in order to be connectable to the motor (`BLDCMotor` class).
125+
```sh
126+
| ├─── sensors
127+
| │ ├─ Encoder.cpp/h # Encoder class implementing the Quadrature encoder operations
128+
| │ ├─ MagneticSensorSPI.cpp/h # class implementing SPI communication for Magnetic sensors
129+
| │ ├─ MagneticSensorI2C.cpp/h # class implementing I2C communication for Magnetic sensors
130+
| │ ├─ MagneticSensorAnalog.cpp/h # class implementing Analog output for Magnetic sensors
131+
└─ HallSensor.cpp/h # class implementing Hall sensor
132+
```
133+
All position sensor classes implemented in this library are placed in this directory, and all of them will be implementing abstract sensor class `Sensor`. Every sensor needs to implement the `Sensor` class in order to be linkable to the motor (`BLDCMotor` and `StepperMotor` class).
57134
If you want to implement your own version of the sensor, jut extend this class and implement the virtual functions and you will be able to run the FOC algorithm with it.
58135
You will be abele to link motor and the sensor by doing `motor.linkSensor(your sensor)`
59136
```cpp
@@ -81,75 +158,24 @@ public:
81158
}
82159
```
83160
84-
### `Encoder.cpp/h`
85-
Quadrature Encoder class implementation
86-
- Extends `Sensor` class
87-
- Encoder interrupt functions and configuration: `enableInterrupt()`, `handleA()`, `handleB()`, `handleIndex()`...
88-
- Calculates motor angle and velocity ( using the [Mixed Time Frequency Method](https://github.com/askuric/Arduino-Mixed-Time-Frequency-Method)).
89-
- Support any type of optical and magnetic encoder.
90-
91-
### `MagneticSensorSPI.cpp/h`
92-
Absolute Magnetic sensor(encoder) class implementation
93-
- Extends `Sensor` class
94-
- SPI communication
95-
- Calculates motor angle and velocity
96-
- Supports magnetic position sensors such as AS5048A, AS5047 and similar.
97-
98-
### `MagneticSensorI2C.cpp/h`
99-
Absolute Magnetic sensor(encoder) class implementation
100-
- Extends `Sensor` class
101-
- I2C communication
102-
- Calculates motor angle and velocity
103-
- Supports magnetic position sensors such as AS5048B, AS5600 and similar
104-
105-
### `MagneticSensorAnalog.cpp/h`
106-
Absolute Magnetic sensor(encoder) class implementation
107-
- Extends `Sensor` class
108-
- Analog value reading
109-
- Calculates motor angle and velocity
110-
- Supports magnetic position sensors such as AS5047, AS5600 and similar
111-
112-
### `HallSensor.cpp/h`
113-
Hall sensor class implementation
114-
- Extends `Sensor` class
115-
- Hall sensor interrupt functions and configuration: `enableInterrupt()`, `handleA()`, `handleB()`, `handleC()`...
116-
- Calculates motor angle and velocity.
117-
- Support any type of hall sensor - even magnetic sensor AS5047 (UVW interface).
118-
119-
120-
### `FOCutils.cpp/h`
121-
This is implementation of all the necessary hardware specific and FOC utility functions.
122-
```cpp
123-
// High PWM frequency setting function
124-
// - hardware specific
125-
// pinA,pinB,pinC hardware pin numbers
126-
void _setPwmFrequency(int pinA, int pinB, int pinC);
127-
128-
// Function implementing delay() function in milliseconds
129-
// - blocking function
130-
// - hardware specific
131-
void _delay(unsigned long ms);
132-
133-
//Function implementing timestamp getting function in microseconds
134-
// hardware specific
135-
unsigned long _micros();
136-
137-
//Function setting the duty cycle to the pwm pin (ex. analogWrite())
138-
// hardware specific
139-
// dc_a, dc_b, dc_c - duty cycle [0, 1]
140-
// pinA,pinB,pinC - hardware pin number
141-
void _writeDutyCycle(float dc_a, float dc_b, float dc_c, int pinA, int pinB, int pinC );
142-
143-
// Function approximating the sine calculation by using fixed size array
144-
// - execution time ~40us (Arduino UNO)
145-
float _sin(float a);
146-
// Function approximating cosine calculation by using fixed size array
147-
// - execution time ~50us (Arduino UNO)
148-
float _cos(float a);
161+
## Common
162+
```sh
163+
│ ├─── common # Contains all the common utility classes and functions
164+
| | |
165+
| | ├─ defaults.h # default motion control parameters
166+
| | ├─ foc_utils.cpp./h # utility functions of the FOC algorithm
167+
| | ├─ time_utils.cpp/h # utility functions for dealing with time measurements and delays
168+
| | ├─ pid.cpp./h # class implementing PID controller
169+
| | ├─ lowpass_filter.cpp./h # class implementing Low pass filter
170+
| | |
171+
| | ├─── base_classes
172+
| | | ├─ FOCMotor.cpp./h # common class for all implemented motors
173+
| | | ├─ BLDCDriver.h # common class for all BLDC drivers
174+
| | | ├─ StepperDriver.h # common class for all Stepper drivers
175+
| | | └─ Sensor./h # common class for all implemented sensors
176+
| |
149177
```
150-
151-
152-
### `defaults.h`
178+
The common directory contains all the definitions and common utility functions for the <span class="simple">Simple<span class="foc">FOC</span>library</span>. It contains the definitions of the abstract classes for motors, sensors and drivers in the `base_classes` directory. It has two libraries of utility functions for time management `time_utils.cpp/h` and FOC helpers `foc_utils.cpp/h`. Finally it has definition and implementation of the two signal processing classes: pid controller `pid.cpp/h` and low pass filter `lowpass_filter.cpp/h`. It also contains the default configuration parameters of the library in the `defaults.h` header file.
153179
Header file containing all the default configuration variables
154180
```cpp
155181
// default configuration values
@@ -172,18 +198,6 @@ Header file containing all the default configuration variables
172198
#define DEF_VEL_FILTER_Tf 0.005 //!< default velocity filter time constant
173199
```
174200
175-
### `SimpleFOC.h`
176-
The main library include file, its contents is:
177-
```cpp
178-
#include "FOCutils.h"
179-
#include "Sensor.h"
180-
#include "Encoder.h"
181-
#include "MagneticSensorSPI.h"
182-
#include "MagneticSensorI2C.h"
183-
#include "MagneticSensorAnalog.h"
184-
#include "HallSensor.h"
185-
#include "BLDCMotor.h"
186-
```
187201
188202
189203
## Digging deeper

docs/simplefoc_library/digging_deeper/theory/low_pass_filtering.md

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,40 @@ Which means that your actual velocity measurement <i>v</i> will influence the fi
3838

3939
## Implementation details
4040

41-
Low pass filtering function implemented in the <span class="simple">Simple<span class="foc">FOC</span>library</span>:
41+
Low pass filtering function implemented in the <span class="simple">Simple<span class="foc">FOC</span>library</span> as a class called `LowPassFilter`.
42+
This class receives the time constant in the constructor:
4243
```cpp
43-
// low pass filter function
44-
// - input - signal to be filtered
45-
// - lpf - LPF_s structure with filter parameters
46-
float BLDCMotor::lowPassFilter(float input, LPF_s& lpf){
47-
unsigned long now_us = _micros();
48-
float Ts = (now_us - lpf.timestamp) * 1e-6;
44+
LowPassFilter filter = LowPassFilter(0.001); // Tf = 1ms
45+
```
46+
The filtering function is implemented as follows:
47+
```cpp
48+
// low pass filtering function
49+
float LowPassFilter::operator(float input){
50+
unsigned long timestamp = _micros();
51+
float dt = (timestamp - timestamp_prev)*1e-6f;
4952
// quick fix for strange cases (micros overflow)
50-
if(Ts <= 0 || Ts > 0.5) Ts = 1e-3;
53+
if (dt < 0.0f || dt > 0.5f) dt = 1e-3f;
5154

5255
// calculate the filtering
53-
float alpha = lpf.Tf/(lpf.Tf + Ts);
54-
float out = alpha*lpf.prev + (1-alpha)*input;
56+
float alpha = Tf/(Tf + dt);
57+
float y = alpha*y_prev + (1.0f - alpha)*x;
5558

5659
// save the variables
57-
lpf.prev = out;
58-
lpf.timestamp = now_us;
59-
return out;
60+
y_prev = y;
61+
timestamp_prev = timestamp;
62+
return y;
6063
}
6164
```
62-
The low pass filter is configured with `motor.LPF_velocity`structure:
65+
You can use it in code just by calling:
66+
```cpp
67+
float signal_filtered = filter(signal);
68+
```
69+
And you can change the filtering constant at any time with line:
6370
```cpp
64-
// Low pass filter structure
65-
struct LPF_s{
66-
float Tf; // Low pass filter time constant
67-
long timestamp; // Last execution timestamp
68-
float prev; // filtered value in previous execution step
69-
};
71+
filter.Tf = 0.01; // changed to 10ms
7072
```
73+
This low pass filter is implemented in the motor class and its time constant can be changed by calling:
74+
```cpp
75+
motor.LPF_velocity.Tf = 0.01;// to set it to 10ms
76+
```
77+

0 commit comments

Comments
 (0)