Skip to content

Commit c1c8b38

Browse files
committed
library source updated with stepper motors
1 parent 0166469 commit c1c8b38

21 files changed

+1267
-705
lines changed

library_source/BLDCMotor.cpp

Lines changed: 37 additions & 377 deletions
Large diffs are not rendered by default.

library_source/BLDCMotor.h

Lines changed: 8 additions & 218 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,16 @@
77
#define BLDCMotor_h
88

99
#include "Arduino.h"
10-
#include "FOCutils.h"
11-
#include "Sensor.h"
12-
#include "defaults.h"
13-
14-
15-
#define NOT_SET -12345.0
16-
17-
/**
18-
* Motiron control type
19-
*/
20-
enum ControlType{
21-
voltage,//!< Torque control using voltage
22-
velocity,//!< Velocity motion control
23-
angle,//!< Position/angle motion control
24-
velocity_openloop,
25-
angle_openloop
26-
};
27-
28-
/**
29-
* FOC modulation type
30-
*/
31-
enum FOCModulationType{
32-
SinePWM, //!< Sinusoidal PWM modulation
33-
SpaceVectorPWM //!< Space vector modulation method
34-
};
35-
36-
/**
37-
* PID controller structure
38-
*/
39-
struct PID_s{
40-
float P; //!< Proportional gain
41-
float I; //!< Integral gain
42-
float D; //!< Derivative gain
43-
long timestamp; //!< Last execution timestamp
44-
float integral_prev; //!< last integral component value
45-
float output_prev; //!< last pid output value
46-
float output_ramp; //!< Maximum speed of change of the output value
47-
float tracking_error_prev; //!< last tracking error value
48-
};
49-
50-
// P controller structure
51-
struct P_s{
52-
float P; //!< Proportional gain
53-
long timestamp; //!< Last execution timestamp
54-
};
55-
56-
/**
57-
* Low pass filter structure
58-
*/
59-
struct LPF_s{
60-
float Tf; //!< Low pass filter time constant
61-
long timestamp; //!< Last execution timestamp
62-
float prev; //!< filtered value in previous execution step
63-
};
64-
65-
66-
10+
#include "common/FOCMotor.h"
11+
#include "common/foc_utils.h"
12+
#include "common/hardware_utils.h"
13+
#include "common/Sensor.h"
14+
#include "common/defaults.h"
6715

6816
/**
6917
BLDC motor class
7018
*/
71-
class BLDCMotor
19+
class BLDCMotor: public FOCMotor
7220
{
7321
public:
7422
/**
@@ -89,22 +37,9 @@ class BLDCMotor
8937
/** Motor enable function */
9038
void enable();
9139

92-
/**
93-
* Function linking a motor and a sensor
94-
*
95-
* @param sensor Sensor class wrapper for the FOC algorihtm to read the motor angle and velocity
96-
*/
97-
void linkSensor(Sensor* sensor);
98-
9940
/**
10041
* Function initializing FOC algorithm
10142
* and aligning sensor's and motors' zero position
102-
*
103-
* - If zero_electric_offset parameter is set the alignment procedure is skipped
104-
*
105-
* @param zero_electric_offset value of the sensors absolute position electrical offset in respect to motor's electrical 0 position.
106-
* @param sensor_direction sensor natural direction - default is CW
107-
*
10843
*/
10944
int initFOC( float zero_electric_offset = NOT_SET , Direction sensor_direction = Direction::CW);
11045
/**
@@ -114,6 +49,7 @@ class BLDCMotor
11449
* - the faster you can run it the better Arduino UNO ~1ms, Bluepill ~ 100us
11550
*/
11651
void loopFOC();
52+
11753
/**
11854
* Function executing the control loops set by the controller parameter of the BLDCMotor.
11955
*
@@ -130,53 +66,8 @@ class BLDCMotor
13066
int pwmC; //!< phase C pwm pin number
13167
int enable_pin; //!< enable pin number
13268

133-
134-
135-
136-
// State calculation methods
137-
/** Shaft angle calculation in radians [rad] */
138-
float shaftAngle();
139-
/**
140-
* Shaft angle calculation function in radian per second [rad/s]
141-
* It implements low pass filtering
142-
*/
143-
float shaftVelocity();
144-
145-
// state variables
146-
float target; //!< current target value - depends of the controller
147-
float shaft_angle;//!< current motor angle
148-
float shaft_velocity;//!< current motor velocity
149-
float shaft_velocity_sp;//!< current target velocity
150-
float shaft_angle_sp;//!< current target angle
151-
float voltage_q;//!< current voltage u_q set
152-
float Ua,Ub,Uc;//!< Current phase voltages Ua,Ub and Uc set to motor
153-
154-
// motor configuration parameters
155-
float voltage_power_supply;//!< Power supply voltage
156-
float voltage_sensor_align;//!< sensor and motor align voltage parameter
157-
float velocity_index_search;//!< target velocity for index search
158-
int pole_pairs;//!< Motor pole pairs number
159-
160-
// limiting variables
161-
float voltage_limit; //!< Voltage limitting variable - global limit
162-
float velocity_limit; //!< Velocity limitting variable - global limit
163-
164-
// configuration structures
165-
ControlType controller; //!< parameter determining the control loop to be used
166-
FOCModulationType foc_modulation;//!< parameter derterniming modulation algorithm
167-
PID_s PID_velocity;//!< parameter determining the velocity PI configuration
168-
P_s P_angle; //!< parameter determining the position P configuration
169-
LPF_s LPF_velocity;//!< parameter determining the velocity Lpw pass filter configuration
170-
171-
/**
172-
* Sensor link:
173-
* - Encoder
174-
* - MagneticSensor
175-
*/
176-
Sensor* sensor;
177-
178-
float zero_electric_angle;//!<absolute zero electric angle - if available
17969

70+
private:
18071
// FOC methods
18172
/**
18273
* Method using FOC to set Uq to the motor at the optimal angle
@@ -186,77 +77,10 @@ class BLDCMotor
18677
* @param angle_el current electrical angle of the motor
18778
*/
18879
void setPhaseVoltage(float Uq, float angle_el);
189-
190-
// monitoring functions
191-
Print* monitor_port; //!< Serial terminal variable if provided
192-
/**
193-
* Function providing BLDCMotor class with the
194-
* Serial interface and enabling monitoring mode
195-
*
196-
* @param serial Monitoring Serial class reference
197-
*/
198-
void useMonitoring(Print &serial);
199-
/**
200-
* Utility function intended to be used with serial plotter to monitor motor variables
201-
* significantly slowing the execution down!!!!
202-
*/
203-
void monitor();
204-
205-
/**
206-
* Function setting the configuration parameters of the motor, target value of the control loop
207-
* and outputing them to the monitoring port( if available ) :
208-
* - configure PID controller constants
209-
* - change motion control loops
210-
* - monitor motor variabels
211-
* - set target values
212-
* - check all the configuration values
213-
*
214-
* To check the config value just enter the command letter.
215-
* For example:
216-
* - to read velocity PI controller P gain run: P
217-
* - to set velocity PI controller P gain to 1.2 run: P1.2
218-
*
219-
* To change the target value just enter a number in the terminal:
220-
* For example:
221-
* - to change the target value to -0.1453 enter: -0.1453
222-
* - to get the current target value enter: V3
223-
*
224-
* List of commands:
225-
* - P: velocity PI controller P gain
226-
* - I: velocity PI controller I gain
227-
* - L: velocity PI controller voltage limit
228-
* - R: velocity PI controller voltage ramp
229-
* - F: velocity Low pass filter time constant
230-
* - K: angle P controller P gain
231-
* - N: angle P controller velocity limit
232-
* - C: control loop
233-
* - 0: voltage
234-
* - 1: velocity
235-
* - 2: angle
236-
* - V: get motor variables
237-
* - 0: currently set voltage
238-
* - 1: current velocity
239-
* - 2: current angle
240-
* - 3: current target value
241-
*
242-
* - Look into the documentation (docs.simplefoc.com) for more information.
243-
*
244-
* @param command String containing the user command
245-
*
246-
* returns 0 for error or 1 for executed command
247-
*/
248-
int command(String command);
249-
250-
251-
private:
25280
/** Sensor alignment to electrical 0 angle of the motor */
25381
int alignSensor();
25482
/** Motor and sensor alignment to the sensors absolute 0 angle */
25583
int absoluteZeroAlign();
256-
257-
/** Electrical angle calculation */
258-
float electricAngle(float shaftAngle);
259-
26084
/**
26185
* Set phase voltages to the harware
26286
*
@@ -266,33 +90,6 @@ class BLDCMotor
26690
*/
26791
void setPwm(float Ua, float Ub, float Uc);
26892

269-
// Utility functions
270-
/** normalizing radian angle to [0,2PI] */
271-
float normalizeAngle(float angle);
272-
/** determining if the enable pin has been provided */
273-
int hasEnable();
274-
275-
/**
276-
* Low pass filter function - iterative
277-
* @param input - singal to be filtered
278-
* @param lpf - LPF_s structure with filter parameters
279-
*/
280-
float lowPassFilter(float input, LPF_s& lpf);
281-
282-
// Motion control functions
283-
/**
284-
* Generic PI controller function executing one step of a controller
285-
* receives tracking error and PID_s structure and outputs the control signal
286-
*
287-
* @param tracking_error Current error in between target value and mesasured value
288-
* @param controller PID_s structure containing all the necessary PI controller config and variables
289-
*/
290-
float controllerPID(float tracking_error, PID_s &controller);
291-
/** Velocity PI controller implementation */
292-
float velocityPID(float tracking_error);
293-
/** Position P controller implementation */
294-
float positionP(float ek);
295-
29693
// Open loop motion control
29794
/**
29895
* Function (iterative) generating open loop movement for target velocity
@@ -308,15 +105,8 @@ class BLDCMotor
308105
* @param target_angle - rad
309106
*/
310107
void angleOpenloop(float target_angle);
311-
312-
313-
// phase voltages
314-
float Ualpha,Ubeta; //!< Phase voltages U alpha and U beta used for inverse Park and Clarke transform
315-
316-
317108
// open loop variables
318109
long open_loop_timestamp;
319-
320110
};
321111

322112

library_source/Encoder.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#define ENCODER_LIB_H
33

44
#include "Arduino.h"
5-
#include "FOCutils.h"
6-
#include "Sensor.h"
5+
#include "common/foc_utils.h"
6+
#include "common/hardware_utils.h"
7+
#include "common/Sensor.h"
78

89

910
/**

library_source/HallSensor.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#define HALL_SENSOR_LIB_H
33

44
#include "Arduino.h"
5-
#include "FOCutils.h"
6-
#include "Sensor.h"
5+
#include "common/foc_utils.h"
6+
#include "common/hardware_utils.h"
7+
#include "common/Sensor.h"
78

89

910
class HallSensor: public Sensor{
@@ -12,7 +13,7 @@ class HallSensor: public Sensor{
1213
HallSensor class constructor
1314
@param encA HallSensor B pin
1415
@param encB HallSensor B pin
15-
@param encC HallSensor B pin
16+
@param encC HallSensor C pin
1617
@param pp pole pairs (e.g hoverboard motor has 15pp and small gimbals often have 7pp)
1718
@param index index pin number (optional input)
1819
*/

library_source/MagneticSensorAnalog.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#define MAGNETICSENSORANALOG_LIB_H
33

44
#include "Arduino.h"
5-
#include <Wire.h>
6-
#include "FOCutils.h"
7-
#include "Sensor.h"
5+
#include "common/foc_utils.h"
6+
#include "common/hardware_utils.h"
7+
#include "common/Sensor.h"
88

99
/**
1010
* This sensor has been tested with AS5600 running in 'analog mode'. This is where output pin of AS6000 is connected to an analog pin on your microcontroller.
@@ -15,7 +15,8 @@ class MagneticSensorAnalog: public Sensor{
1515
/**
1616
* MagneticSensorAnalog class constructor
1717
* @param _pinAnalog the pin to read the PWM signal
18-
* @param _pinAnalog the pin to read the PWM signal
18+
* @param _min minimal value of angle reading
19+
* @param _max maximal value of angle reading
1920
*/
2021
MagneticSensorAnalog(uint8_t _pinAnalog, int _min = 0, int _max = 0);
2122

library_source/MagneticSensorI2C.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
#include "Arduino.h"
55
#include <Wire.h>
6-
#include "FOCutils.h"
7-
#include "Sensor.h"
6+
#include "common/foc_utils.h"
7+
#include "common/hardware_utils.h"
8+
#include "common/Sensor.h"
89

910

1011
class MagneticSensorI2C: public Sensor{

library_source/MagneticSensorSPI.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
#include "Arduino.h"
55
#include <SPI.h>
6-
#include "FOCutils.h"
7-
#include "Sensor.h"
6+
#include "common/foc_utils.h"
7+
#include "common/hardware_utils.h"
8+
#include "common/Sensor.h"
89

910
#define DEF_ANGLE_REGISTAR 0x3FFF
1011

0 commit comments

Comments
 (0)