Skip to content

Commit 26fbea0

Browse files
committed
Autotuning
1 parent 2de948c commit 26fbea0

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/BLDCMotor.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,16 @@ int BLDCMotor::init() {
8282
// current control loop controls voltage
8383
PID_current_q.limit = voltage_limit;
8484
PID_current_d.limit = voltage_limit;
85+
86+
if (_isset(phase_resistance) && _isset(phase_inductance)){
87+
PID_current_d.P = phase_inductance * DEF_CURR_BANDWIDTH * _2PI;
88+
PID_current_d.I = PID_current_d.P * phase_resistance / phase_inductance;
89+
90+
PID_current_q.P = phase_inductance * DEF_CURR_BANDWIDTH * _2PI;
91+
PID_current_q.I = PID_current_q.P * phase_resistance /phase_inductance;
92+
}
8593
}
94+
8695
if(_isset(phase_resistance) || torque_controller != TorqueControlType::voltage){
8796
// velocity control loop controls current
8897
PID_velocity.limit = current_limit;

src/StepperMotor.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ int StepperMotor::init() {
4747
// constrain voltage for sensor alignment
4848
if(voltage_sensor_align > voltage_limit) voltage_sensor_align = voltage_limit;
4949

50+
// update the controller limits
51+
if(current_sense){
52+
// current control loop controls voltage
53+
PID_current_q.limit = voltage_limit;
54+
PID_current_d.limit = voltage_limit;
55+
56+
if (_isset(phase_resistance) && _isset(phase_inductance)){
57+
PID_current_d.P = phase_inductance * DEF_CURR_BANDWIDTH * _2PI;
58+
PID_current_d.I = PID_current_d.P * phase_resistance / phase_inductance;
59+
60+
PID_current_q.P = phase_inductance * DEF_CURR_BANDWIDTH * _2PI;
61+
PID_current_q.I = PID_current_q.P * phase_resistance /phase_inductance;
62+
}
63+
}
64+
5065
// update the controller limits
5166
if(_isset(phase_resistance)){
5267
// velocity control loop controls current

src/common/defaults.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define DEF_PID_CURR_RAMP 1000.0f //!< default PID controller voltage ramp value
1919
#define DEF_PID_CURR_LIMIT (DEF_POWER_SUPPLY) //!< default PID controller voltage limit
2020
#define DEF_CURR_FILTER_Tf 0.01f //!< default velocity filter time constant
21+
#define DEF_CURR_BANDWIDTH 300.0f //!< current bandwidth
2122
#else
2223
// for stm32, due, teensy, esp32 and similar
2324
#define DEF_PID_CURR_P 3 //!< default PID controller P value
@@ -26,6 +27,7 @@
2627
#define DEF_PID_CURR_RAMP 0 //!< default PID controller voltage ramp value
2728
#define DEF_PID_CURR_LIMIT (DEF_POWER_SUPPLY) //!< default PID controller voltage limit
2829
#define DEF_CURR_FILTER_Tf 0.005f //!< default currnet filter time constant
30+
#define DEF_CURR_BANDWIDTH 300.0f //!< current bandwidth
2931
#endif
3032
// default current limit values
3133
#define DEF_CURRENT_LIM 2.0f //!< 2Amps current limit by default

0 commit comments

Comments
 (0)