Skip to content

Commit c32bdd1

Browse files
committed
[ENHANCEMENT] lag compensation using phase inductance - proposed in #246
1 parent 45219c4 commit c32bdd1

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-11
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Therefore this is an attempt to:
3737
> - stm32 - software and hardware 6pwm
3838
> - atmega328
3939
> - atmega2560
40+
> - Lag compensation using motor inductance [#246](https://github.com/simplefoc/Arduino-FOC/issues/246)
41+
> - current control through voltage torque mode enhancement
42+
> - extended `BLDCMotor` and `StepperMotor` constructors to receive the inductance paramerer
43+
> - can also be set using `motor.phase_inductance` or through `Commander`
4044
## Arduino *SimpleFOClibrary* v2.2.3
4145

4246
<p align="">

src/BLDCMotor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ class BLDCMotor: public FOCMotor
1818
/**
1919
BLDCMotor class constructor
2020
@param pp pole pairs number
21-
@param R motor phase resistance
21+
@param R motor phase resistance - [Ohm]
2222
@param KV motor KV rating (1/K_bemf) - rpm/V
23+
@param L motor phase inductance - [H]
2324
*/
24-
BLDCMotor(int pp, float R = NOT_SET, float KV = NOT_SET);
25+
BLDCMotor(int pp, float R = NOT_SET, float KV = NOT_SET, float L = NOT_SET);
2526

2627
/**
2728
* Function linking a motor and a foc driver

src/StepperMotor.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
// - pp - pole pair number
77
// - R - motor phase resistance
88
// - KV - motor kv rating (rmp/v)
9-
StepperMotor::StepperMotor(int pp, float _R, float _KV)
9+
// - L - motor phase inductance [H]
10+
StepperMotor::StepperMotor(int pp, float _R, float _KV, float _inductance)
1011
: FOCMotor()
1112
{
1213
// number od pole pairs
@@ -16,6 +17,8 @@ StepperMotor::StepperMotor(int pp, float _R, float _KV)
1617
// save back emf constant KV = 1/K_bemf
1718
// usually used rms
1819
KV_rating = _KV*_SQRT2;
20+
// save phase inductance
21+
phase_inductance = _inductance;
1922

2023
// torque control type is voltage by default
2124
// current and foc_current not supported yet
@@ -296,7 +299,9 @@ void StepperMotor::move(float new_target) {
296299
if(!_isset(phase_resistance)) voltage.q = target; // if voltage torque control
297300
else voltage.q = target*phase_resistance + voltage_bemf;
298301
voltage.q = _constrain(voltage.q, -voltage_limit, voltage_limit);
299-
voltage.d = 0;
302+
// set d-component (lag compensation if known inductance)
303+
if(!_isset(phase_inductance)) voltage.d = 0;
304+
else voltage.d = _constrain( -target*shaft_velocity*pole_pairs*phase_inductance, -voltage_limit, voltage_limit);
300305
break;
301306
case MotionControlType::angle:
302307
// angle set point
@@ -309,7 +314,9 @@ void StepperMotor::move(float new_target) {
309314
// use voltage if phase-resistance not provided
310315
if(!_isset(phase_resistance)) voltage.q = current_sp;
311316
else voltage.q = _constrain( current_sp*phase_resistance + voltage_bemf , -voltage_limit, voltage_limit);
312-
voltage.d = 0;
317+
// set d-component (lag compensation if known inductance)
318+
if(!_isset(phase_inductance)) voltage.d = 0;
319+
else voltage.d = _constrain( -current_sp*shaft_velocity*pole_pairs*phase_inductance, -voltage_limit, voltage_limit);
313320
break;
314321
case MotionControlType::velocity:
315322
// velocity set point
@@ -320,19 +327,21 @@ void StepperMotor::move(float new_target) {
320327
// use voltage if phase-resistance not provided
321328
if(!_isset(phase_resistance)) voltage.q = current_sp;
322329
else voltage.q = _constrain( current_sp*phase_resistance + voltage_bemf , -voltage_limit, voltage_limit);
323-
voltage.d = 0;
330+
// set d-component (lag compensation if known inductance)
331+
if(!_isset(phase_inductance)) voltage.d = 0;
332+
else voltage.d = _constrain( -current_sp*shaft_velocity*pole_pairs*phase_inductance, -voltage_limit, voltage_limit);
324333
break;
325334
case MotionControlType::velocity_openloop:
326335
// velocity control in open loop
327336
shaft_velocity_sp = target;
328337
voltage.q = velocityOpenloop(shaft_velocity_sp); // returns the voltage that is set to the motor
329-
voltage.d = 0;
338+
voltage.d = 0; // TODO d-component lag-compensation
330339
break;
331340
case MotionControlType::angle_openloop:
332341
// angle control in open loop
333342
shaft_angle_sp = target;
334343
voltage.q = angleOpenloop(shaft_angle_sp); // returns the voltage that is set to the motor
335-
voltage.d = 0;
344+
voltage.d = 0; // TODO d-component lag-compensation
336345
break;
337346
}
338347
}

src/StepperMotor.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ class StepperMotor: public FOCMotor
2323
/**
2424
StepperMotor class constructor
2525
@param pp pole pair number
26-
@param R motor phase resistance
27-
@param KV motor KV rating (1/K_bemf) - rpm/V
26+
@param R motor phase resistance - [Ohm]
27+
@param KV motor KV rating (1/K_bemf) - rpm/V
28+
@param L motor phase inductance - [H]
2829
*/
29-
StepperMotor(int pp, float R = NOT_SET, float KV = NOT_SET);
30+
StepperMotor(int pp, float R = NOT_SET, float KV = NOT_SET, float L = NOT_SET);
3031

3132
/**
3233
* Function linking a motor and a foc driver

src/communication/Commander.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ void Commander::motor(FOCMotor* motor, char* user_command) {
221221
if(_isset(motor->phase_resistance)) println(motor->phase_resistance);
222222
else println(0);
223223
break;
224+
case CMD_INDUCTANCE:
225+
printVerbose(F("L phase: "));
226+
if(!GET){
227+
motor->phase_inductance = value;
228+
}
229+
if(_isset(motor->phase_inductance)) println(motor->phase_inductance);
230+
else println(0);
231+
break;
224232
case CMD_KV_RATING:
225233
printVerbose(F("Motor KV: "));
226234
if(!GET){

src/communication/commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define CMD_SENSOR 'S' //!< sensor offsets
1616
#define CMD_MONITOR 'M' //!< monitoring
1717
#define CMD_RESIST 'R' //!< motor phase resistance
18+
#define CMD_INDUCTANCE 'I' //!< motor phase inductance
1819
#define CMD_KV_RATING 'K' //!< motor kv rating
1920
#define CMD_PWMMOD 'W' //!< pwm modulation
2021

0 commit comments

Comments
 (0)