diff --git a/src/common/base_classes/FOCMotor.cpp b/src/common/base_classes/FOCMotor.cpp index 214b74ce2..3704787ec 100644 --- a/src/common/base_classes/FOCMotor.cpp +++ b/src/common/base_classes/FOCMotor.cpp @@ -674,6 +674,17 @@ void FOCMotor::loopFOC() { SIMPLEFOC_MOTOR_ERROR("no torque control selected!"); break; } + // optionally limit the magnitude of the dq voltage vector + // d and q are scaled together, so the direction of the voltage vector is preserved + if(limit_voltage_vector){ + float magnitude_sq = voltage.q*voltage.q + voltage.d*voltage.d; + float limit_sq = voltage_limit*voltage_limit; + if(magnitude_sq > limit_sq){ + float scale = voltage_limit / sqrtf(magnitude_sq); + voltage.q *= scale; + voltage.d *= scale; + } + } // set the phase voltage - FOC heart function :) setPhaseVoltage(voltage.q, voltage.d, electrical_angle); } diff --git a/src/common/base_classes/FOCMotor.h b/src/common/base_classes/FOCMotor.h index fbac503ff..49b13ff78 100644 --- a/src/common/base_classes/FOCMotor.h +++ b/src/common/base_classes/FOCMotor.h @@ -237,6 +237,7 @@ class FOCMotor // limiting variables float voltage_limit; //!< Voltage limiting variable - global limit + bool limit_voltage_vector = false; //!< if true, the d and q voltages are scaled together so that the voltage vector magnitude does not exceed voltage_limit (default: false, d and q are limited independently) float current_limit; //!< Current limiting variable - global limit float velocity_limit; //!< Velocity limiting variable - global limit