7
7
#define BLDCMotor_h
8
8
9
9
#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"
67
15
68
16
/* *
69
17
BLDC motor class
70
18
*/
71
- class BLDCMotor
19
+ class BLDCMotor : public FOCMotor
72
20
{
73
21
public:
74
22
/* *
@@ -89,22 +37,9 @@ class BLDCMotor
89
37
/* * Motor enable function */
90
38
void enable ();
91
39
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
-
99
40
/* *
100
41
* Function initializing FOC algorithm
101
42
* 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
- *
108
43
*/
109
44
int initFOC ( float zero_electric_offset = NOT_SET , Direction sensor_direction = Direction::CW);
110
45
/* *
@@ -114,6 +49,7 @@ class BLDCMotor
114
49
* - the faster you can run it the better Arduino UNO ~1ms, Bluepill ~ 100us
115
50
*/
116
51
void loopFOC ();
52
+
117
53
/* *
118
54
* Function executing the control loops set by the controller parameter of the BLDCMotor.
119
55
*
@@ -130,53 +66,8 @@ class BLDCMotor
130
66
int pwmC; // !< phase C pwm pin number
131
67
int enable_pin; // !< enable pin number
132
68
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
179
69
70
+ private:
180
71
// FOC methods
181
72
/* *
182
73
* Method using FOC to set Uq to the motor at the optimal angle
@@ -186,77 +77,10 @@ class BLDCMotor
186
77
* @param angle_el current electrical angle of the motor
187
78
*/
188
79
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:
252
80
/* * Sensor alignment to electrical 0 angle of the motor */
253
81
int alignSensor ();
254
82
/* * Motor and sensor alignment to the sensors absolute 0 angle */
255
83
int absoluteZeroAlign ();
256
-
257
- /* * Electrical angle calculation */
258
- float electricAngle (float shaftAngle);
259
-
260
84
/* *
261
85
* Set phase voltages to the harware
262
86
*
@@ -266,33 +90,6 @@ class BLDCMotor
266
90
*/
267
91
void setPwm (float Ua, float Ub, float Uc);
268
92
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
-
296
93
// Open loop motion control
297
94
/* *
298
95
* Function (iterative) generating open loop movement for target velocity
@@ -308,15 +105,8 @@ class BLDCMotor
308
105
* @param target_angle - rad
309
106
*/
310
107
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
-
317
108
// open loop variables
318
109
long open_loop_timestamp;
319
-
320
110
};
321
111
322
112
0 commit comments