@@ -34,7 +34,8 @@ int trap_150_map[12][3] = {
34
34
// - pp - pole pair number
35
35
// - R - motor phase resistance
36
36
// - KV - motor kv rating (rmp/v)
37
- BLDCMotor::BLDCMotor (int pp, float _R, float _KV)
37
+ // - L - motor phase inductance
38
+ BLDCMotor::BLDCMotor (int pp, float _R, float _KV, float _inductance)
38
39
: FOCMotor()
39
40
{
40
41
// save pole pairs number
@@ -44,6 +45,8 @@ BLDCMotor::BLDCMotor(int pp, float _R, float _KV)
44
45
// save back emf constant KV = 1/KV
45
46
// 1/sqrt(2) - rms value
46
47
KV_rating = _KV*_SQRT2;
48
+ // save phase inductance
49
+ phase_inductance = _inductance;
47
50
48
51
// torque control type is voltage by default
49
52
torque_controller = TorqueControlType::voltage;
@@ -335,7 +338,9 @@ void BLDCMotor::loopFOC() {
335
338
current.q = LPF_current_q (current.q );
336
339
// calculate the phase voltage
337
340
voltage.q = PID_current_q (current_sp - current.q );
338
- voltage.d = 0 ;
341
+ // d voltage - lag compensation
342
+ if (_isset (phase_inductance)) voltage.d = _constrain ( -current_sp*shaft_velocity*pole_pairs*phase_inductance, -voltage_limit, voltage_limit);
343
+ else voltage.d = 0 ;
339
344
break ;
340
345
case TorqueControlType::foc_current:
341
346
if (!current_sense) return ;
@@ -347,6 +352,8 @@ void BLDCMotor::loopFOC() {
347
352
// calculate the phase voltages
348
353
voltage.q = PID_current_q (current_sp - current.q );
349
354
voltage.d = PID_current_d (-current.d );
355
+ // d voltage - lag compensation - TODO verify
356
+ // if(_isset(phase_inductance)) voltage.d = _constrain( voltage.d - current_sp*shaft_velocity*pole_pairs*phase_inductance, -voltage_limit, voltage_limit);
350
357
break ;
351
358
default :
352
359
// no torque control selected
@@ -397,7 +404,9 @@ void BLDCMotor::move(float new_target) {
397
404
if (!_isset (phase_resistance)) voltage.q = target;
398
405
else voltage.q = target*phase_resistance + voltage_bemf;
399
406
voltage.q = _constrain (voltage.q , -voltage_limit, voltage_limit);
400
- voltage.d = 0 ;
407
+ // set d-component (lag compensation if known inductance)
408
+ if (!_isset (phase_inductance)) voltage.d = 0 ;
409
+ else voltage.d = _constrain ( -target*shaft_velocity*pole_pairs*phase_inductance, -voltage_limit, voltage_limit);
401
410
}else {
402
411
current_sp = target; // if current/foc_current torque control
403
412
}
@@ -417,7 +426,9 @@ void BLDCMotor::move(float new_target) {
417
426
// use voltage if phase-resistance not provided
418
427
if (!_isset (phase_resistance)) voltage.q = current_sp;
419
428
else voltage.q = _constrain ( current_sp*phase_resistance + voltage_bemf , -voltage_limit, voltage_limit);
420
- voltage.d = 0 ;
429
+ // set d-component (lag compensation if known inductance)
430
+ if (!_isset (phase_inductance)) voltage.d = 0 ;
431
+ else voltage.d = _constrain ( -current_sp*shaft_velocity*pole_pairs*phase_inductance, -voltage_limit, voltage_limit);
421
432
}
422
433
break ;
423
434
case MotionControlType::velocity:
@@ -430,7 +441,9 @@ void BLDCMotor::move(float new_target) {
430
441
// use voltage if phase-resistance not provided
431
442
if (!_isset (phase_resistance)) voltage.q = current_sp;
432
443
else voltage.q = _constrain ( current_sp*phase_resistance + voltage_bemf , -voltage_limit, voltage_limit);
433
- voltage.d = 0 ;
444
+ // set d-component (lag compensation if known inductance)
445
+ if (!_isset (phase_inductance)) voltage.d = 0 ;
446
+ else voltage.d = _constrain ( -current_sp*shaft_velocity*pole_pairs*phase_inductance, -voltage_limit, voltage_limit);
434
447
}
435
448
break ;
436
449
case MotionControlType::velocity_openloop:
0 commit comments