Skip to content

Commit 45219c4

Browse files
committed
src/BLDCMotor.h
1 parent 3187949 commit 45219c4

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/BLDCMotor.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ int trap_150_map[12][3] = {
3434
// - pp - pole pair number
3535
// - R - motor phase resistance
3636
// - 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)
3839
: FOCMotor()
3940
{
4041
// save pole pairs number
@@ -44,6 +45,8 @@ BLDCMotor::BLDCMotor(int pp, float _R, float _KV)
4445
// save back emf constant KV = 1/KV
4546
// 1/sqrt(2) - rms value
4647
KV_rating = _KV*_SQRT2;
48+
// save phase inductance
49+
phase_inductance = _inductance;
4750

4851
// torque control type is voltage by default
4952
torque_controller = TorqueControlType::voltage;
@@ -335,7 +338,9 @@ void BLDCMotor::loopFOC() {
335338
current.q = LPF_current_q(current.q);
336339
// calculate the phase voltage
337340
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;
339344
break;
340345
case TorqueControlType::foc_current:
341346
if(!current_sense) return;
@@ -347,6 +352,8 @@ void BLDCMotor::loopFOC() {
347352
// calculate the phase voltages
348353
voltage.q = PID_current_q(current_sp - current.q);
349354
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);
350357
break;
351358
default:
352359
// no torque control selected
@@ -397,7 +404,9 @@ void BLDCMotor::move(float new_target) {
397404
if(!_isset(phase_resistance)) voltage.q = target;
398405
else voltage.q = target*phase_resistance + voltage_bemf;
399406
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);
401410
}else{
402411
current_sp = target; // if current/foc_current torque control
403412
}
@@ -417,7 +426,9 @@ void BLDCMotor::move(float new_target) {
417426
// use voltage if phase-resistance not provided
418427
if(!_isset(phase_resistance)) voltage.q = current_sp;
419428
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);
421432
}
422433
break;
423434
case MotionControlType::velocity:
@@ -430,7 +441,9 @@ void BLDCMotor::move(float new_target) {
430441
// use voltage if phase-resistance not provided
431442
if(!_isset(phase_resistance)) voltage.q = current_sp;
432443
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);
434447
}
435448
break;
436449
case MotionControlType::velocity_openloop:

src/common/base_classes/FOCMotor.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,19 @@ class FOCMotor
173173
float phase_resistance; //!< motor phase resistance
174174
int pole_pairs;//!< motor pole pairs number
175175
float KV_rating; //!< motor KV rating
176+
float phase_inductance; //!< motor phase inductance
176177

177178
// limiting variables
178-
float voltage_limit; //!< Voltage limitting variable - global limit
179-
float current_limit; //!< Current limitting variable - global limit
180-
float velocity_limit; //!< Velocity limitting variable - global limit
179+
float voltage_limit; //!< Voltage limiting variable - global limit
180+
float current_limit; //!< Current limiting variable - global limit
181+
float velocity_limit; //!< Velocity limiting variable - global limit
181182

182183
// motor status vairables
183184
int8_t enabled = 0;//!< enabled or disabled motor flag
184185
FOCMotorStatus motor_status = FOCMotorStatus::motor_uninitialized; //!< motor status
185186

186187
// pwm modulation related variables
187-
FOCModulationType foc_modulation;//!< parameter derterniming modulation algorithm
188+
FOCModulationType foc_modulation;//!< parameter determining modulation algorithm
188189
int8_t modulation_centered = 1;//!< flag (1) centered modulation around driver limit /2 or (0) pulled to 0
189190

190191

0 commit comments

Comments
 (0)