@@ -33,7 +33,7 @@ void BLDCMotor::linkDriver(BLDCDriver* _driver) {
33
33
void BLDCMotor::init () {
34
34
if (monitor_port) monitor_port->println (" MOT: Initialise variables." );
35
35
// sanity check for the voltage limit configuration
36
- if (voltage_limit > driver->voltage_power_supply ) voltage_limit = driver->voltage_power_supply ;
36
+ if (voltage_limit > driver->voltage_limit ) voltage_limit = driver->voltage_limit ;
37
37
// constrain voltage for sensor alignment
38
38
if (voltage_sensor_align > voltage_limit) voltage_sensor_align = voltage_limit;
39
39
// update the controller limits
@@ -98,13 +98,13 @@ int BLDCMotor::alignSensor() {
98
98
float start_angle = shaftAngle ();
99
99
for (int i = 0 ; i <=5 ; i++ ) {
100
100
float angle = _3PI_2 + _2PI * i / 6.0 ;
101
- setPhaseVoltage (voltage_sensor_align, angle);
101
+ setPhaseVoltage (voltage_sensor_align, 0 , angle);
102
102
_delay (200 );
103
103
}
104
104
float mid_angle = shaftAngle ();
105
105
for (int i = 5 ; i >=0 ; i-- ) {
106
106
float angle = _3PI_2 + _2PI * i / 6.0 ;
107
- setPhaseVoltage (voltage_sensor_align, angle);
107
+ setPhaseVoltage (voltage_sensor_align, 0 , angle);
108
108
_delay (200 );
109
109
}
110
110
if (mid_angle < start_angle) {
@@ -119,7 +119,7 @@ int BLDCMotor::alignSensor() {
119
119
// set sensor to zero
120
120
sensor->initRelativeZero ();
121
121
_delay (500 );
122
- setPhaseVoltage (0 ,0 );
122
+ setPhaseVoltage (0 , 0 , 0 );
123
123
_delay (200 );
124
124
125
125
// find the index if available
@@ -151,7 +151,7 @@ int BLDCMotor::absoluteZeroAlign() {
151
151
}
152
152
voltage_q = 0 ;
153
153
// disable motor
154
- setPhaseVoltage (0 ,0 );
154
+ setPhaseVoltage (0 , 0 , 0 );
155
155
156
156
// align absolute zero if it has been found
157
157
if (!sensor->needsAbsoluteZeroSearch ()){
@@ -170,7 +170,7 @@ void BLDCMotor::loopFOC() {
170
170
// shaft angle
171
171
shaft_angle = shaftAngle ();
172
172
// set the phase voltage - FOC heart function :)
173
- setPhaseVoltage (voltage_q, _electricalAngle (shaft_angle,pole_pairs));
173
+ setPhaseVoltage (voltage_q, voltage_d, _electricalAngle (shaft_angle,pole_pairs));
174
174
}
175
175
176
176
// Iterative function running outer loop of the FOC algorithm
@@ -217,16 +217,17 @@ void BLDCMotor::move(float new_target) {
217
217
}
218
218
219
219
220
- // Method using FOC to set Uq to the motor at the optimal angle
220
+ // Method using FOC to set Uq and Ud to the motor at the optimal angle
221
221
// Function implementing Space Vector PWM and Sine PWM algorithms
222
222
//
223
223
// Function using sine approximation
224
224
// regular sin + cos ~300us (no memory usaage)
225
225
// approx _sin + _cos ~110us (400Byte ~ 20% of memory)
226
- void BLDCMotor::setPhaseVoltage (float Uq, float angle_el) {
226
+ void BLDCMotor::setPhaseVoltage (float Uq, float Ud, float angle_el) {
227
227
228
228
const bool centered = true ;
229
229
int sector;
230
+ float _ca,_sa;
230
231
231
232
switch (foc_modulation)
232
233
{
@@ -243,9 +244,9 @@ void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
243
244
Uc = Uq + trap_120_map[sector][2 ] * Uq;
244
245
245
246
if (centered) {
246
- Ua += (voltage_power_supply )/2 -Uq;
247
- Ub += (voltage_power_supply )/2 -Uq;
248
- Uc += (voltage_power_supply )/2 -Uq;
247
+ Ua += (driver-> voltage_limit )/2 -Uq;
248
+ Ub += (driver-> voltage_limit )/2 -Uq;
249
+ Uc += (driver-> voltage_limit )/2 -Uq;
249
250
}
250
251
break ;
251
252
@@ -263,9 +264,9 @@ void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
263
264
264
265
// center
265
266
if (centered) {
266
- Ua += (voltage_power_supply )/2 -Uq;
267
- Ub += (voltage_power_supply )/2 -Uq;
268
- Uc += (voltage_power_supply )/2 -Uq;
267
+ Ua += (driver-> voltage_limit )/2 -Uq;
268
+ Ub += (driver-> voltage_limit )/2 -Uq;
269
+ Uc += (driver-> voltage_limit )/2 -Uq;
269
270
}
270
271
271
272
break ;
@@ -277,20 +278,22 @@ void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
277
278
// angle normalization in between 0 and 2pi
278
279
// only necessary if using _sin and _cos - approximation functions
279
280
angle_el = _normalizeAngle (angle_el + zero_electric_angle);
281
+ _ca = _cos (angle_el);
282
+ _sa = _sin (angle_el);
280
283
// Inverse park transform
281
- Ualpha = - _sin (angle_el) * Uq; // -sin(angle) * Uq;
282
- Ubeta = _cos (angle_el) * Uq; // cos(angle) * Uq;
284
+ Ualpha = _ca * Ud - _sa * Uq; // -sin(angle) * Uq;
285
+ Ubeta = _sa * Ud + _ca * Uq; // cos(angle) * Uq;
283
286
284
287
// Clarke transform
285
- Ua = Ualpha + driver->voltage_power_supply /2 ;
286
- Ub = -0.5 * Ualpha + _SQRT3_2 * Ubeta + driver->voltage_power_supply /2 ;
287
- Uc = -0.5 * Ualpha - _SQRT3_2 * Ubeta + driver->voltage_power_supply /2 ;
288
+ Ua = Ualpha + driver->voltage_limit /2 ;
289
+ Ub = -0.5 * Ualpha + _SQRT3_2 * Ubeta + driver->voltage_limit /2 ;
290
+ Uc = -0.5 * Ualpha - _SQRT3_2 * Ubeta + driver->voltage_limit /2 ;
288
291
289
292
if (!centered) {
290
293
float Umin = min (Ua, min (Ub, Uc));
291
- Ua -=Umin;
292
- Ub -=Umin;
293
- Uc -=Umin;
294
+ Ua -= Umin;
295
+ Ub -= Umin;
296
+ Uc -= Umin;
294
297
}
295
298
296
299
break ;
@@ -311,12 +314,12 @@ void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
311
314
// find the sector we are in currently
312
315
sector = floor (angle_el / _PI_3) + 1 ;
313
316
// calculate the duty cycles
314
- float T1 = _SQRT3*_sin (sector*_PI_3 - angle_el) * Uq/driver->voltage_power_supply ;
315
- float T2 = _SQRT3*_sin (angle_el - (sector-1.0 )*_PI_3) * Uq/driver->voltage_power_supply ;
317
+ float T1 = _SQRT3*_sin (sector*_PI_3 - angle_el) * Uq/driver->voltage_limit ;
318
+ float T2 = _SQRT3*_sin (angle_el - (sector-1.0 )*_PI_3) * Uq/driver->voltage_limit ;
316
319
// two versions possible
317
320
float T0 = 0 ; // pulled to 0 - better for low power supply voltage
318
321
if (centered) {
319
- T0 = 1 - T1 - T2; // centered around driver->voltage_power_supply /2
322
+ T0 = 1 - T1 - T2; // centered around driver->voltage_limit /2
320
323
}
321
324
322
325
// calculate the duty cycles(times)
@@ -360,9 +363,9 @@ void BLDCMotor::setPhaseVoltage(float Uq, float angle_el) {
360
363
}
361
364
362
365
// calculate the phase voltages and center
363
- Ua = Ta*driver->voltage_power_supply ;
364
- Ub = Tb*driver->voltage_power_supply ;
365
- Uc = Tc*driver->voltage_power_supply ;
366
+ Ua = Ta*driver->voltage_limit ;
367
+ Ub = Tb*driver->voltage_limit ;
368
+ Uc = Tc*driver->voltage_limit ;
366
369
break ;
367
370
368
371
}
@@ -386,7 +389,7 @@ void BLDCMotor::velocityOpenloop(float target_velocity){
386
389
shaft_angle += target_velocity*Ts;
387
390
388
391
// set the maximal allowed voltage (voltage_limit) with the necessary angle
389
- setPhaseVoltage (voltage_limit, _electricalAngle (shaft_angle, pole_pairs));
392
+ setPhaseVoltage (voltage_limit, 0 , _electricalAngle (shaft_angle, pole_pairs));
390
393
391
394
// save timestamp for next call
392
395
open_loop_timestamp = now_us;
@@ -409,7 +412,7 @@ void BLDCMotor::angleOpenloop(float target_angle){
409
412
shaft_angle = target_angle;
410
413
411
414
// set the maximal allowed voltage (voltage_limit) with the necessary angle
412
- setPhaseVoltage (voltage_limit, _electricalAngle (shaft_angle, pole_pairs));
415
+ setPhaseVoltage (voltage_limit, 0 , _electricalAngle (shaft_angle, pole_pairs));
413
416
414
417
// save timestamp for next call
415
418
open_loop_timestamp = now_us;
0 commit comments