|
|
@@ -16,9 +16,10 @@
|
|
|
#include "libs/logger.h"
|
|
|
#include "math/fir.h"
|
|
|
|
|
|
+#define _DEBUG(fmt, args...) no_debug(fmt, ##args)
|
|
|
+
|
|
|
PMSM_FOC_Ctrl gFoc_Ctrl;
|
|
|
static Fir_t phase1, phase2;
|
|
|
-
|
|
|
static bool g_focinit = false;
|
|
|
|
|
|
static u32 PMSM_FOC_Debug_Task(void *p);
|
|
|
@@ -173,14 +174,14 @@ static void PMSM_FOC_UserInit(void) {
|
|
|
gFoc_Ctrl.userLim.s_iDCeBrkLim = nv_get_foc_params()->s_iDCeBrkLim;
|
|
|
gFoc_Ctrl.userLim.s_PhaseCurreBrkLim = nv_get_foc_params()->s_PhaseCurreBrkLim;
|
|
|
gFoc_Ctrl.userLim.s_PhaseVoleBrkLim = gFoc_Ctrl.hwLim.s_PhaseVolMax;
|
|
|
-
|
|
|
- gFoc_Ctrl.protLim.s_iDCLim = gFoc_Ctrl.userLim.s_iDCLim;
|
|
|
- gFoc_Ctrl.protLim.s_PhaseCurrLim = gFoc_Ctrl.userLim.s_PhaseCurrLim;
|
|
|
}
|
|
|
|
|
|
void PMSM_FOC_RT_LimInit(void) {
|
|
|
+ gFoc_Ctrl.protLim.s_iDCLim = gFoc_Ctrl.userLim.s_iDCLim;
|
|
|
+ gFoc_Ctrl.protLim.s_TorqueLim = gFoc_Ctrl.userLim.s_torqueLim;
|
|
|
+
|
|
|
eRamp_init_target(&gFoc_Ctrl.rtLim.rpmLimRamp, gFoc_Ctrl.userLim.s_motRPMLim, CONFIG_LIMIT_RAMP_TIME, CONFIG_LIMIT_RAMP_TIME);
|
|
|
- eRamp_init_target(&gFoc_Ctrl.rtLim.phaseCurrLimRamp, gFoc_Ctrl.userLim.s_PhaseCurrLim, CONFIG_LIMIT_RAMP_TIME, CONFIG_LIMIT_RAMP_TIME);
|
|
|
+ eRamp_init_target(&gFoc_Ctrl.rtLim.torqueLimRamp, gFoc_Ctrl.userLim.s_torqueLim, CONFIG_LIMIT_RAMP_TIME, CONFIG_LIMIT_RAMP_TIME);
|
|
|
eRamp_init_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, gFoc_Ctrl.userLim.s_iDCLim, CONFIG_LIMIT_RAMP_TIME, CONFIG_LIMIT_RAMP_TIME);
|
|
|
}
|
|
|
|
|
|
@@ -205,13 +206,18 @@ void PMSM_FOC_CoreInit(void) {
|
|
|
gFoc_Ctrl.hwLim.s_PhaseCurrMax = CONFIG_MAX_PHASE_CURR;
|
|
|
gFoc_Ctrl.hwLim.s_PhaseVolMax = CONFIG_MAX_PHASE_VOL;
|
|
|
gFoc_Ctrl.hwLim.s_vDCMax = CONFIG_MAX_DC_VOL;
|
|
|
- gFoc_Ctrl.hwLim.s_torqueMax = CONFIG_MAX_TORQUE;
|
|
|
+ gFoc_Ctrl.hwLim.s_torqueMax = CONFIG_MAX_MOTOR_TORQUE;
|
|
|
gFoc_Ctrl.hwLim.s_FWDCurrMax = CONFIG_MAX_FW_D_CURR;
|
|
|
if (!g_focinit) {
|
|
|
PMSM_FOC_UserInit();
|
|
|
PMSM_FOC_RT_LimInit();
|
|
|
shark_task_create(PMSM_FOC_Debug_Task, NULL);
|
|
|
g_focinit = true;
|
|
|
+ _DEBUG("User Limit:\n");
|
|
|
+ _DEBUG("dc %f, rpm %f, torque %f, phase %f, vDCmax %f, vDCmin %f, ebrk %f\n", gFoc_Ctrl.userLim.s_iDCLim, gFoc_Ctrl.userLim.s_motRPMLim, gFoc_Ctrl.userLim.s_torqueLim,
|
|
|
+ gFoc_Ctrl.userLim.s_PhaseCurrLim, gFoc_Ctrl.userLim.s_vDCMaxLim, gFoc_Ctrl.userLim.s_vDCMinLim, gFoc_Ctrl.userLim.s_PhaseCurreBrkLim);
|
|
|
+ _DEBUG("Hw Limit:\n");
|
|
|
+ _DEBUG("dc %f, rpm %f, torque %f, phase %f\n", gFoc_Ctrl.hwLim.s_iDCMax, gFoc_Ctrl.hwLim.s_motRPMMax, gFoc_Ctrl.hwLim.s_torqueMax, gFoc_Ctrl.hwLim.s_PhaseCurrMax);
|
|
|
}
|
|
|
|
|
|
gFoc_Ctrl.params.n_modulation = CONFIG_SVM_MODULATION;//SVM_Modulation;
|
|
|
@@ -546,8 +552,8 @@ static __INLINE void PMSM_FOC_idq_Assign(void) {
|
|
|
/*called in media task */
|
|
|
void PMSM_FOC_idqCalc(void) {
|
|
|
if (gFoc_Ctrl.in.b_AutoHold) {
|
|
|
- gFoc_Ctrl.pi_lock->max = CONFIG_DEFAULT_LOCK_PHASE_CURR_LIM;
|
|
|
- gFoc_Ctrl.pi_lock->min = -CONFIG_DEFAULT_LOCK_PHASE_CURR_LIM;
|
|
|
+ gFoc_Ctrl.pi_lock->max = CONFIG_DEFAULT_LOCK_TORQUE_LIM;
|
|
|
+ gFoc_Ctrl.pi_lock->min = -CONFIG_DEFAULT_LOCK_TORQUE_LIM;
|
|
|
float vel_count = motor_encoder_get_vel_count();
|
|
|
float errRef = 0 - vel_count;
|
|
|
gFoc_Ctrl.in.s_targetTorque = PI_Controller_run(gFoc_Ctrl.pi_lock ,errRef);
|
|
|
@@ -562,7 +568,7 @@ void PMSM_FOC_idqCalc(void) {
|
|
|
}
|
|
|
}
|
|
|
}else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
|
|
|
- float refTorque = min(eCtrl_get_RefTorque(), eRamp_get_intepolation(&gFoc_Ctrl.rtLim.phaseCurrLimRamp));
|
|
|
+ float refTorque = min(eCtrl_get_RefTorque(), eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
|
|
|
float maxTrq = PMSM_FOC_Limit_Speed(refTorque);
|
|
|
maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
|
|
|
crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
|
|
|
@@ -574,10 +580,10 @@ void PMSM_FOC_idqCalc(void) {
|
|
|
refSpeed = eRamp_get_intepolation(&gFoc_Ctrl.in.cruiseRpmRamp);//gFoc_Ctrl.in.s_cruiseRPM;
|
|
|
}
|
|
|
if (maxSpeed >= 0) {
|
|
|
- gFoc_Ctrl.pi_speed->max = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.phaseCurrLimRamp);//gFoc_Ctrl.userLim.s_PhaseCurrLim;
|
|
|
+ gFoc_Ctrl.pi_speed->max = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);//gFoc_Ctrl.userLim.s_PhaseCurrLim;
|
|
|
gFoc_Ctrl.pi_speed->min = -CONFIG_MAX_NEG_TORQUE;
|
|
|
}else if (maxSpeed < 0) {
|
|
|
- gFoc_Ctrl.pi_speed->min = -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.phaseCurrLimRamp);//gFoc_Ctrl.userLim.s_PhaseCurrLim;
|
|
|
+ gFoc_Ctrl.pi_speed->min = -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);//gFoc_Ctrl.userLim.s_PhaseCurrLim;
|
|
|
gFoc_Ctrl.pi_speed->max = CONFIG_MAX_NEG_TORQUE;
|
|
|
}
|
|
|
|
|
|
@@ -596,28 +602,28 @@ void PMSM_FOC_idqCalc(void) {
|
|
|
|
|
|
bool PMSM_FOC_RunTime_Limit(void) {
|
|
|
bool changed = false;
|
|
|
- float dclim = (float)vbus_current_vol_lower_limit();
|
|
|
- float phaselim = (float)phase_current_temp_high_limit();
|
|
|
+ float dc_lim = (float)vbus_current_vol_lower_limit();
|
|
|
+ float torque_lim = (float)torque_temp_high_limit();
|
|
|
|
|
|
- dclim = (dclim!=CURRENT_LIMIT_NONE)?dclim:gFoc_Ctrl.hwLim.s_iDCMax;
|
|
|
- phaselim = (phaselim!=CURRENT_LIMIT_NONE)?phaselim:gFoc_Ctrl.hwLim.s_PhaseCurrMax;
|
|
|
+ dc_lim = (dc_lim != CURRENT_LIMIT_NONE)?dc_lim:gFoc_Ctrl.userLim.s_iDCLim;
|
|
|
+ torque_lim = (torque_lim != CURRENT_LIMIT_NONE)?torque_lim:gFoc_Ctrl.userLim.s_torqueLim;
|
|
|
|
|
|
- if (phaselim < gFoc_Ctrl.rtLim.phaseCurrLimRamp.target) {
|
|
|
- eRamp_set_step_target(&gFoc_Ctrl.rtLim.phaseCurrLimRamp, phaselim, CONFIG_eCTRL_STEP_TS);
|
|
|
+ if (torque_lim < gFoc_Ctrl.rtLim.torqueLimRamp.target) {
|
|
|
+ eRamp_set_step_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torque_lim, CONFIG_eCTRL_STEP_TS);
|
|
|
}
|
|
|
- if (dclim < gFoc_Ctrl.rtLim.DCCurrLimRamp.target) {
|
|
|
- eRamp_set_step_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, dclim, CONFIG_eCTRL_STEP_TS);
|
|
|
+ if (dc_lim < gFoc_Ctrl.rtLim.DCCurrLimRamp.target) {
|
|
|
+ eRamp_set_step_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, dc_lim, CONFIG_eCTRL_STEP_TS);
|
|
|
}
|
|
|
- if (gFoc_Ctrl.protLim.s_iDCLim != dclim || gFoc_Ctrl.protLim.s_PhaseCurrLim != phaselim) {
|
|
|
- gFoc_Ctrl.protLim.s_iDCLim = dclim;
|
|
|
- gFoc_Ctrl.protLim.s_PhaseCurrLim = phaselim;
|
|
|
+ if (gFoc_Ctrl.protLim.s_iDCLim != dc_lim || gFoc_Ctrl.protLim.s_TorqueLim != torque_lim) {
|
|
|
+ gFoc_Ctrl.protLim.s_iDCLim = dc_lim;
|
|
|
+ gFoc_Ctrl.protLim.s_TorqueLim = torque_lim;
|
|
|
changed = true;
|
|
|
}
|
|
|
return changed;
|
|
|
}
|
|
|
|
|
|
void PMSM_FOC_Slow_Task(void) {
|
|
|
- eRamp_running(&gFoc_Ctrl.rtLim.phaseCurrLimRamp);
|
|
|
+ eRamp_running(&gFoc_Ctrl.rtLim.torqueLimRamp);
|
|
|
eRamp_running(&gFoc_Ctrl.rtLim.DCCurrLimRamp);
|
|
|
eRamp_running(&gFoc_Ctrl.rtLim.rpmLimRamp);
|
|
|
eRamp_running(&gFoc_Ctrl.in.cruiseRpmRamp);
|
|
|
@@ -659,12 +665,13 @@ bool PMSM_FOC_Is_Start(void) {
|
|
|
}
|
|
|
|
|
|
void PMSM_FOC_DCCurrLimit(float ibusLimit) {
|
|
|
- float minCurr = min(gFoc_Ctrl.hwLim.s_iDCMax, gFoc_Ctrl.protLim.s_iDCLim);
|
|
|
- if (ibusLimit > minCurr) {
|
|
|
- ibusLimit = minCurr;
|
|
|
+ if (ibusLimit > gFoc_Ctrl.hwLim.s_iDCMax) {
|
|
|
+ ibusLimit = gFoc_Ctrl.hwLim.s_iDCMax;
|
|
|
}
|
|
|
- gFoc_Ctrl.userLim.s_iDCLim = (ibusLimit);
|
|
|
- if (ABS(gFoc_Ctrl.in.s_motRPM) < 10){
|
|
|
+ gFoc_Ctrl.userLim.s_iDCLim = ibusLimit;
|
|
|
+ gFoc_Ctrl.protLim.s_iDCLim = gFoc_Ctrl.userLim.s_iDCLim;
|
|
|
+
|
|
|
+ if (ABS(gFoc_Ctrl.in.s_motRPM) <= CONFIG_ZERO_SPEED_RPM){
|
|
|
eRamp_reset_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit);
|
|
|
}else {
|
|
|
eRamp_set_step_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit, CONFIG_eCTRL_STEP_TS);
|
|
|
@@ -675,26 +682,26 @@ float PMSM_FOC_GetDCCurrLimit(void) {
|
|
|
return gFoc_Ctrl.userLim.s_iDCLim;
|
|
|
}
|
|
|
|
|
|
-void PMSM_FOC_SpeedLimit(float speedLimit) {
|
|
|
- PMSM_FOC_SpeedRampLimit(speedLimit, false);
|
|
|
-}
|
|
|
-
|
|
|
-void PMSM_FOC_SpeedDirectLimit(float limit) {
|
|
|
- PMSM_FOC_SpeedRampLimit(limit, true);
|
|
|
-}
|
|
|
-
|
|
|
-void PMSM_FOC_SpeedRampLimit(float speedLimit, bool direct) {
|
|
|
+void PMSM_FOC_SpeedRampLimit(float speedLimit, float speed) {
|
|
|
if (speedLimit > gFoc_Ctrl.hwLim.s_motRPMMax) {
|
|
|
speedLimit = gFoc_Ctrl.hwLim.s_motRPMMax;
|
|
|
}
|
|
|
gFoc_Ctrl.userLim.s_motRPMLim = (speedLimit);
|
|
|
- if (direct) {
|
|
|
+ if (ABS(speed) <= CONFIG_ZERO_SPEED_RPM) {
|
|
|
eRamp_reset_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit);
|
|
|
}else {
|
|
|
eRamp_set_step_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void PMSM_FOC_SpeedLimit(float speedLimit) {
|
|
|
+ PMSM_FOC_SpeedRampLimit(speedLimit, gFoc_Ctrl.in.s_motRPM);
|
|
|
+}
|
|
|
+
|
|
|
+void PMSM_FOC_SpeedDirectLimit(float limit) {
|
|
|
+ PMSM_FOC_SpeedRampLimit(limit, 0);
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
float PMSM_FOC_GetSpeedLimit(void) {
|
|
|
return gFoc_Ctrl.userLim.s_motRPMLim;
|
|
|
@@ -705,11 +712,12 @@ void PMSM_FOC_TorqueLimit(float torqueLimit) {
|
|
|
torqueLimit = gFoc_Ctrl.hwLim.s_torqueMax;
|
|
|
}
|
|
|
gFoc_Ctrl.userLim.s_torqueLim = torqueLimit;
|
|
|
+ gFoc_Ctrl.protLim.s_TorqueLim = gFoc_Ctrl.userLim.s_torqueLim;
|
|
|
|
|
|
- if (ABS(gFoc_Ctrl.in.s_motRPM) < 10){
|
|
|
- eRamp_reset_target(&gFoc_Ctrl.rtLim.phaseCurrLimRamp, torqueLimit);
|
|
|
+ if (ABS(gFoc_Ctrl.in.s_motRPM) <= CONFIG_ZERO_SPEED_RPM){
|
|
|
+ eRamp_reset_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit);
|
|
|
}else {
|
|
|
- eRamp_set_step_target(&gFoc_Ctrl.rtLim.phaseCurrLimRamp, torqueLimit, CONFIG_eCTRL_STEP_TS);
|
|
|
+ eRamp_set_step_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit, CONFIG_eCTRL_STEP_TS);
|
|
|
}
|
|
|
}
|
|
|
float PMSM_FOC_GetTorqueLimit(void) {
|
|
|
@@ -751,24 +759,17 @@ u8 PMSM_FOC_GetCtrlMode(void) {
|
|
|
}
|
|
|
|
|
|
void PMSM_FOC_PhaseCurrLim(float lim) {
|
|
|
- float minCurr = min(gFoc_Ctrl.hwLim.s_PhaseCurrMax, gFoc_Ctrl.protLim.s_PhaseCurrLim);
|
|
|
- if (lim > minCurr) {
|
|
|
- lim = minCurr;
|
|
|
+ if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
|
|
|
+ lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
|
|
|
}
|
|
|
gFoc_Ctrl.userLim.s_PhaseCurrLim = lim;
|
|
|
- if (ABS(gFoc_Ctrl.in.s_motRPM) < 10){
|
|
|
- eRamp_reset_target(&gFoc_Ctrl.rtLim.phaseCurrLimRamp, lim);
|
|
|
- }else {
|
|
|
- eRamp_set_step_target(&gFoc_Ctrl.rtLim.phaseCurrLimRamp, lim, CONFIG_eCTRL_STEP_TS);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
void PMSM_FOC_RT_PhaseCurrLim(float lim) {
|
|
|
- float minCurr = min(gFoc_Ctrl.hwLim.s_PhaseCurrMax, gFoc_Ctrl.protLim.s_PhaseCurrLim);
|
|
|
- if (lim > minCurr) {
|
|
|
- lim = minCurr;
|
|
|
+ if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
|
|
|
+ lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
|
|
|
}
|
|
|
- eRamp_init_target(&gFoc_Ctrl.rtLim.phaseCurrLimRamp, lim, CONFIG_LIMIT_RAMP_TIME, CONFIG_LIMIT_RAMP_TIME);
|
|
|
+ eRamp_init_target(&gFoc_Ctrl.rtLim.torqueLimRamp, lim, CONFIG_LIMIT_RAMP_TIME, CONFIG_LIMIT_RAMP_TIME);
|
|
|
}
|
|
|
|
|
|
float PMSM_FOC_GetPhaseCurrLim(void) {
|