Kaynağa Gözat

限速,限相电流,慢速给定,默认10s

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 3 yıl önce
ebeveyn
işleme
5a3500c184

+ 24 - 5
Applications/app/nv_storage.c

@@ -1,6 +1,7 @@
 #include "app/nv_storage.h"
 #include "bsp/fmc_flash.h"
 #include "libs/crc16.h"
+#include "libs/logger.h"
 #include "foc/motor/motor_param.h"
 #include "foc/foc_config.h"
 
@@ -39,7 +40,8 @@ static void nv_default_motor_params(void) {
 }
 
 static void nv_default_foc_params(void) {
-	foc_params.s_maxvDC = 48;
+	foc_params.s_maxDCVol = CONFIG_RATED_DC_VOL;
+	foc_params.s_minDCVol = CONFIG_MIN_DC_VOL;
 	foc_params.s_maxiDC = 30;
 	//foc_params.s_maxIdq = 200;
 	//foc_params.s_minIdq = -200;
@@ -91,6 +93,7 @@ void nv_read_motor_params(void) {
 	fmc_read_data(motorParam_idx_0, (u8 *)&m_params, sizeof(m_params));
 	u16 crc0 = crc16_get((u8 *)&m_params, sizeof(m_params) - 2);
 	if (crc0 != m_params.crc16) {
+		sys_debug("mp 0 error\n");
 		fmc_read_data(motorParam_idx_1, (u8 *)&m_params, sizeof(m_params));
 		crc0 = crc16_get((u8 *)&m_params, sizeof(m_params) - 2);
 		if (crc0 != m_params.crc16) {
@@ -100,7 +103,13 @@ void nv_read_motor_params(void) {
 		}
 		fmc_write_data(motorParam_idx_0, (u8 *)&m_params, sizeof(m_params));
 	}else {
-		fmc_write_data(motorParam_idx_1, (u8 *)&m_params, sizeof(m_params));
+		fmc_read_data(motorParam_idx_1, (u8 *)&m_params, sizeof(m_params));
+		crc0 = crc16_get((u8 *)&m_params, sizeof(m_params) - 2);
+		if (crc0 != m_params.crc16) {
+			sys_debug("mp 1 error\n");
+			fmc_read_data(motorParam_idx_0, (u8 *)&m_params, sizeof(m_params));
+			fmc_write_data(motorParam_idx_1, (u8 *)&m_params, sizeof(m_params));
+		}
 	}
 }
 void nv_save_foc_params(void) {
@@ -114,6 +123,7 @@ void nv_read_foc_params(void) {
 	fmc_read_data(focParam_idx_0, (u8 *)&foc_params, sizeof(foc_params));
 	u16 crc0 = crc16_get((u8 *)&foc_params, sizeof(foc_params) - 2);
 	if (crc0 != foc_params.crc16) {
+		sys_debug("fp 0 error\n");
 		fmc_read_data(focParam_idx_1, (u8 *)&foc_params, sizeof(foc_params));
 		crc0 = crc16_get((u8 *)&foc_params, sizeof(foc_params) - 2);
 		if (crc0 != foc_params.crc16) {
@@ -123,7 +133,13 @@ void nv_read_foc_params(void) {
 		}
 		fmc_write_data(focParam_idx_0, (u8 *)&foc_params, sizeof(foc_params));
 	}else {
-		fmc_write_data(focParam_idx_1, (u8 *)&foc_params, sizeof(foc_params));
+		fmc_read_data(focParam_idx_1, (u8 *)&foc_params, sizeof(foc_params));
+		crc0 = crc16_get((u8 *)&foc_params, sizeof(foc_params) - 2);
+		if (crc0 != foc_params.crc16) {
+			sys_debug("fp 1 error\n");
+			fmc_read_data(focParam_idx_0, (u8 *)&foc_params, sizeof(foc_params));
+			fmc_write_data(focParam_idx_1, (u8 *)&foc_params, sizeof(foc_params));
+		}
 	}
 }
 
@@ -153,7 +169,10 @@ void nv_set_ebrake_current(float phase_curr, float dc_curr) {
 void nv_storage_init(void) {
 	nv_read_motor_params();
 	nv_read_foc_params();
-	nv_default_motor_params();
-	nv_default_foc_params();
+	sys_debug("encoder_off = %f\n", m_params.offset);
+	//nv_default_motor_params();
+	//nv_default_foc_params();
+	//nv_save_foc_params();
+	//nv_save_motor_params();
 }
 

+ 2 - 1
Applications/app/nv_storage.h

@@ -10,7 +10,8 @@ typedef struct {
 	//float s_maxIdq;
 	//float s_minIdq;
 	float s_maxiDC;
-	float s_maxvDC;
+	float s_maxDCVol;
+	float s_minDCVol;
 	float s_maxRPM;
 	float s_maxEpmRPM;
 	float s_maxTorque;

+ 3 - 0
Applications/bsp/board_gd32demo.h

@@ -6,6 +6,9 @@
 #include "gd32e10x.h"
 #endif
 
+#define CONFIG_RATED_DC_VOL (16)   /* 母线最大电压 V*/
+
+
 #define SCHED_TIMER TIMER5
 #define SCHED_TIMER_RCU RCU_TIMER5
 #define SCHED_TIMER_IRQ TIMER5_IRQn

+ 15 - 12
Applications/bsp/board_mc_v1.h

@@ -6,6 +6,21 @@
 #include "gd32e10x.h"
 #endif
 
+#define CONFIG_MOS_MAX_VOL 145.0F
+#define CONFIG_MAX_DC_VOL 120.0F
+#define CONFIG_RATED_DC_VOL (96.0f)   /* 母线最大电压 V*/
+#define CONFIG_MIN_DC_VOL   (36.0f)
+
+#define CONFIG_MAX_VBUS_CURRENT 45.0f
+#define CONFIG_MAX_MOT_RPM      8000.0f
+#define CONFIG_MAX_PHASE_CURR   180.0F
+#define CONFIG_MAX_PHASE_VOL    (CONFIG_MOS_MAX_VOL - 20.0F)
+#define CONFIG_MAX_TORQUE       50.0F
+#define CONFIG_CURRENT_BANDWITH 1000.0f /* 电流环带宽 */
+#define CONFIG_BEEP 
+#define CONFIG_STALL_MAX_CURRENT 100.0f //最大堵转相电流电流
+#define CONFIG_STALL_MAX_TIME    3000   //ms, 超过最大堵转电流持续时间,判断堵转
+
 #define SCHED_TIMER TIMER5
 #define SCHED_TIMER_RCU RCU_TIMER5
 #define SCHED_TIMER_IRQ TIMER5_IRQn
@@ -26,14 +41,6 @@
 #define ADC_REFERENCE_VOLTAGE  (3.3F)
 #define ADC_FULL_MAX          (4096.0F)
 
-#define CONFIG_MAX_VBUS_CURRENT 45.0f
-#define CONFIG_MAX_MOT_RPM      8000.0f
-#define CONFIG_MAX_PHASE_CURR   180.0F
-#define CONFIG_MAX_PHASE_VOL    140.0F
-#define CONFIG_MAX_VBUS_VOLTAGE 100.0F
-#define CONFIG_MAX_TORQUE       50.0F
-#define CONFIG_CURRENT_BANDWITH 1000.0f /* 电流环带宽 */
-
 /* MOS驱动 */
 #define pwm_timer TIMER0
 #define PWM_MODE TIMER_OC_MODE_PWM0
@@ -210,9 +217,5 @@
 
 #define DEBUG_PORT_UART2
 
-#define CONFIG_BEEP 
-
-#define CONFIG_STALL_MAX_CURRENT 100.0f //最大堵转相电流电流
-#define CONFIG_STALL_MAX_TIME    3000   //ms, 超过最大堵转电流持续时间,判断堵转
 #endif /*_BOARD_MC_V1_H__ */
 

+ 31 - 22
Applications/foc/core/PMSM_FOC_Core.c

@@ -112,11 +112,11 @@ static __INLINE void FOC_DqRamp_init(dq_Rctrl *c, int count) {
 }
 
 static __INLINE void FOC_Set_iDqRamp(dq_Rctrl *c, float target) {
-	FOC_Set_DqRamp(c, target, (/*IDQ_CTRL_TS/SPD_CTRL_TS - 1*/CURRENT_LOOP_RAMP_COUNT));
+	FOC_Set_DqRamp(c, target, (/*CONFIG_IDQ_CTRL_TS/CONFIG_SPD_CTRL_TS - 1*/CURRENT_LOOP_RAMP_COUNT));
 }
 
 static __INLINE void FOC_Set_vDqRamp(dq_Rctrl *c, float target) {
-	FOC_Set_DqRamp(c, target, (VDQ_RAMP_FINAL_TIME/1000*((IDQ_CTRL_TS/VDQ_RAMP_TS))));
+	FOC_Set_DqRamp(c, target, (CONFIG_FOC_VDQ_RAMP_FINAL_TIME/1000*((CONFIG_IDQ_CTRL_TS/CONFIG_FOC_VDQ_RAMP_TS))));
 }
 
 
@@ -134,32 +134,32 @@ static void PMSM_FOC_Conf_PID(void) {
 	gFoc_Ctrl.pi_id->kp = nv_get_foc_params()->pid_conf[PID_D_id].kp;
 	gFoc_Ctrl.pi_id->ki = nv_get_foc_params()->pid_conf[PID_D_id].ki;
 	gFoc_Ctrl.pi_id->kb = nv_get_foc_params()->pid_conf[PID_D_id].kb;
-	gFoc_Ctrl.pi_id->DT = (1.0f/(float)IDQ_CTRL_TS);
+	gFoc_Ctrl.pi_id->DT = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
 
 	gFoc_Ctrl.pi_iq->kp = nv_get_foc_params()->pid_conf[PID_Q_id].kp;
 	gFoc_Ctrl.pi_iq->ki = nv_get_foc_params()->pid_conf[PID_Q_id].ki;
 	gFoc_Ctrl.pi_iq->kb = nv_get_foc_params()->pid_conf[PID_Q_id].kb;
-	gFoc_Ctrl.pi_iq->DT = (1.0f/(float)IDQ_CTRL_TS);
+	gFoc_Ctrl.pi_iq->DT = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
 
 	gFoc_Ctrl.pi_torque->kp = nv_get_foc_params()->pid_conf[PID_TRQ_id].kp;
 	gFoc_Ctrl.pi_torque->ki = nv_get_foc_params()->pid_conf[PID_TRQ_id].ki;
 	gFoc_Ctrl.pi_torque->kb = nv_get_foc_params()->pid_conf[PID_TRQ_id].kb;
-	gFoc_Ctrl.pi_torque->DT = (1.0f/(float)SPD_CTRL_TS);
+	gFoc_Ctrl.pi_torque->DT = (1.0f/(float)CONFIG_SPD_CTRL_TS);
 
 	gFoc_Ctrl.pi_speed->kp = nv_get_foc_params()->pid_conf[PID_Spd_id].kp;
 	gFoc_Ctrl.pi_speed->ki = nv_get_foc_params()->pid_conf[PID_Spd_id].ki;
 	gFoc_Ctrl.pi_speed->kb = nv_get_foc_params()->pid_conf[PID_Spd_id].kb;
-	gFoc_Ctrl.pi_speed->DT = (1.0f/(float)SPD_CTRL_TS);
+	gFoc_Ctrl.pi_speed->DT = (1.0f/(float)CONFIG_SPD_CTRL_TS);
 
 	gFoc_Ctrl.pi_power->kp = nv_get_foc_params()->pid_conf[PID_Pow_id].kp;
 	gFoc_Ctrl.pi_power->ki = nv_get_foc_params()->pid_conf[PID_Pow_id].ki;
 	gFoc_Ctrl.pi_power->kb = nv_get_foc_params()->pid_conf[PID_Pow_id].kb;
-	gFoc_Ctrl.pi_power->DT = (1.0f/(float)SPD_CTRL_TS);
+	gFoc_Ctrl.pi_power->DT = (1.0f/(float)CONFIG_SPD_CTRL_TS);
 
 	gFoc_Ctrl.pi_lock->kp = nv_get_foc_params()->pid_conf[PID_Lock_id].kp;
 	gFoc_Ctrl.pi_lock->ki = nv_get_foc_params()->pid_conf[PID_Lock_id].ki;
 	gFoc_Ctrl.pi_lock->kb = nv_get_foc_params()->pid_conf[PID_Lock_id].kb;
-	gFoc_Ctrl.pi_lock->DT = (1.0f/(float)SPD_CTRL_TS);
+	gFoc_Ctrl.pi_lock->DT = (1.0f/(float)CONFIG_SPD_CTRL_TS);
 
 }
 
@@ -169,11 +169,13 @@ static void PMSM_FOC_UserInit(void) {
 	gFoc_Ctrl.userLim.s_motRPMLim = nv_get_foc_params()->s_maxRPM;//(MAX_SPEED);
 	gFoc_Ctrl.userLim.s_torqueLim = nv_get_foc_params()->s_maxTorque;//MAX_TORQUE;
 	gFoc_Ctrl.userLim.s_PhaseCurrLim = nv_get_foc_params()->s_PhaseCurrLim;
-	gFoc_Ctrl.userLim.s_vDCMaxLim = nv_get_foc_params()->s_maxvDC;
-	gFoc_Ctrl.userLim.s_vDCMinLim = gFoc_Ctrl.userLim.s_vDCMaxLim / 3;
+	gFoc_Ctrl.userLim.s_vDCMaxLim = nv_get_foc_params()->s_maxDCVol;
+	gFoc_Ctrl.userLim.s_vDCMinLim = nv_get_foc_params()->s_minDCVol;
 	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 - 20;
+	gFoc_Ctrl.userLim.s_PhaseVoleBrkLim = gFoc_Ctrl.hwLim.s_PhaseVolMax;
+	eRamp_init_target(&gFoc_Ctrl.userLim.rpmLimRamp, gFoc_Ctrl.userLim.s_motRPMLim);
+	eRamp_init_target(&gFoc_Ctrl.userLim.phaseCurrLimRamp, gFoc_Ctrl.userLim.s_PhaseCurrLim);
 }
 
 void PMSM_FOC_CoreInit(void) {
@@ -196,7 +198,7 @@ void PMSM_FOC_CoreInit(void) {
 	gFoc_Ctrl.hwLim.s_motRPMMax = CONFIG_MAX_MOT_RPM;
 	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_VBUS_VOLTAGE;
+	gFoc_Ctrl.hwLim.s_vDCMax      = CONFIG_MAX_DC_VOL;
 	gFoc_Ctrl.hwLim.s_torqueMax  = CONFIG_MAX_TORQUE;
 
 	if (!g_focinit) {
@@ -206,10 +208,9 @@ void PMSM_FOC_CoreInit(void) {
 	}
 	gFoc_Ctrl.params.n_modulation = nv_get_foc_params()->n_modulation;//SVM_Modulation;
 	gFoc_Ctrl.params.n_PhaseFilterCeof = nv_get_foc_params()->n_PhaseFilterCeof;//(0.2f);
-	//gFoc_Ctrl.params.n_TrqVelLimGain = nv_get_foc_params()->n_TrqVelLimGain;
 	gFoc_Ctrl.params.n_poles = nv_get_motor_params()->poles;//MOTOR_POLES;
 	gFoc_Ctrl.in.s_manualAngle = INVALID_ANGLE;
-	gFoc_Ctrl.in.s_vDC = nv_get_foc_params()->s_maxvDC;//(MAX_vDC);
+	gFoc_Ctrl.in.s_vDC = nv_get_foc_params()->s_maxDCVol;//(CONFIG_RATED_DC_VOL);
 
 	gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
 	gFoc_Ctrl.out.f_vdqRation = 0;
@@ -217,8 +218,8 @@ void PMSM_FOC_CoreInit(void) {
 	FOC_DqRamp_init(&gFoc_Ctrl.idq_ctl[0], 1);
 	FOC_DqRamp_init(&gFoc_Ctrl.idq_ctl[1], 1);
 
-	FOC_DqRamp_init(&gFoc_Ctrl.vdq_ctl[0], (IDQ_CTRL_TS/VDQ_RAMP_TS));
-	FOC_DqRamp_init(&gFoc_Ctrl.vdq_ctl[1], (IDQ_CTRL_TS/VDQ_RAMP_TS));	
+	FOC_DqRamp_init(&gFoc_Ctrl.vdq_ctl[0], (CONFIG_IDQ_CTRL_TS/CONFIG_FOC_VDQ_RAMP_TS));
+	FOC_DqRamp_init(&gFoc_Ctrl.vdq_ctl[1], (CONFIG_IDQ_CTRL_TS/CONFIG_FOC_VDQ_RAMP_TS));	
 	PMSM_FOC_Reset_PID();
 }
 
@@ -273,10 +274,10 @@ static __INLINE void PMSM_FOC_Update_Hardware(void) {
 
 static __INLINE void PMSM_FOC_Update_PI_Idq(void) {
 	/* update id pi ctrl */
-	gFoc_Ctrl.params.maxvDQ.d = gFoc_Ctrl.in.s_vDC;//MAX_vDC;
-	gFoc_Ctrl.params.minvDQ.d = -gFoc_Ctrl.in.s_vDC;//MAX_vDC;
-	gFoc_Ctrl.params.maxvDQ.q = gFoc_Ctrl.in.s_vDC;//MAX_vDC;
-	gFoc_Ctrl.params.minvDQ.q = -gFoc_Ctrl.in.s_vDC;//MAX_vDC;
+	gFoc_Ctrl.params.maxvDQ.d = gFoc_Ctrl.in.s_vDC;//CONFIG_RATED_DC_VOL;
+	gFoc_Ctrl.params.minvDQ.d = -gFoc_Ctrl.in.s_vDC;//CONFIG_RATED_DC_VOL;
+	gFoc_Ctrl.params.maxvDQ.q = gFoc_Ctrl.in.s_vDC;//CONFIG_RATED_DC_VOL;
+	gFoc_Ctrl.params.minvDQ.q = -gFoc_Ctrl.in.s_vDC;//CONFIG_RATED_DC_VOL;
 
 	if (gFoc_Ctrl.params.maxvDQ.d != gFoc_Ctrl.pi_id->max) {
 		gFoc_Ctrl.pi_id->max = gFoc_Ctrl.params.maxvDQ.d;
@@ -435,11 +436,11 @@ void PMSM_FOC_idqCalc(void) {
 			gFoc_Ctrl.pi_torque->max = 0;
 			gFoc_Ctrl.pi_torque->min = 0; //防止倒转
 		}
-		float errRef = gFoc_Ctrl.userLim.s_motRPMLim - gFoc_Ctrl.in.s_motRPM;
+		float errRef = eRamp_get_intepolation(&gFoc_Ctrl.userLim.rpmLimRamp) - gFoc_Ctrl.in.s_motRPM;
 		float maxTrq = PI_Controller_RunSat(gFoc_Ctrl.pi_torque, errRef);
 		gFoc_Ctrl.in.s_targetTorque = PMSM_FOC_Limit_Power(maxTrq);
 	}else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD){
-		gFoc_Ctrl.pi_speed->max = gFoc_Ctrl.userLim.s_PhaseCurrLim;
+		gFoc_Ctrl.pi_speed->max = eRamp_get_intepolation(&gFoc_Ctrl.userLim.phaseCurrLimRamp);//gFoc_Ctrl.userLim.s_PhaseCurrLim;
 		gFoc_Ctrl.pi_speed->min = 0;//-gFoc_Ctrl.userLim.s_PhaseCurrLim;
 		float refSpeed = eCtrl_get_RefSpeed();
 		if (gFoc_Ctrl.in.b_cruiseEna) {
@@ -459,6 +460,12 @@ void PMSM_FOC_idqCalc(void) {
 	PMSM_FOC_idq_Assign();
 }
 
+void PMSM_FOC_Slow_Task(void) {
+	eRamp_running(&gFoc_Ctrl.userLim.phaseCurrLimRamp);
+	eRamp_running(&gFoc_Ctrl.userLim.rpmLimRamp);
+	PMSM_FOC_idqCalc();
+}
+
 PMSM_FOC_Ctrl *PMSM_FOC_Get(void) {
 	return &gFoc_Ctrl;
 }
@@ -498,6 +505,7 @@ void PMSM_FOC_SpeedLimit(float speedLimit) {
 		speedLimit = gFoc_Ctrl.hwLim.s_motRPMMax;
 	}
 	gFoc_Ctrl.userLim.s_motRPMLim = (speedLimit);
+	eRamp_set_step_target(&gFoc_Ctrl.userLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS, CONFIG_LIMIT_RAMP_TIME, CONFIG_LIMIT_RAMP_TIME);
 }
 
 float PMSM_FOC_GetSpeedLimit(void) {
@@ -561,6 +569,7 @@ void PMSM_FOC_PhaseCurrLim(float lim) {
 		lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
 	}
 	gFoc_Ctrl.userLim.s_PhaseCurrLim = lim;
+	eRamp_set_step_target(&gFoc_Ctrl.userLim.phaseCurrLimRamp, lim, CONFIG_eCTRL_STEP_TS, CONFIG_LIMIT_RAMP_TIME, CONFIG_LIMIT_RAMP_TIME);
 }
 
 float PMSM_FOC_GetPhaseCurrLim(void) {

+ 5 - 0
Applications/foc/core/PMSM_FOC_Core.h

@@ -2,6 +2,8 @@
 #define _PMSM_FOC_Core_H__
 #include "math/fix_math.h"
 #include "foc/core/PI_Controller.h"
+#include "foc/core/e_ctrl.h"
+
 typedef struct {
 	float a;
 	float b;
@@ -63,6 +65,8 @@ typedef struct {
 	float s_PhaseVoleBrkLim;
 	float s_vDCMinLim;
 	float s_vDCMaxLim;
+	e_Ramp rpmLimRamp;
+	e_Ramp phaseCurrLimRamp;
 }FOC_UserLimit;
 
 typedef struct {
@@ -241,6 +245,7 @@ bool PMSM_FOC_Is_CruiseEnabled(void);
 void PMSM_FOC_SetPid(u8 id, float kp, float ki, float kb);
 void PMSM_FOC_GetPid(u8 id, float *kp, float *ki, float *kb);
 bool PMSM_FOC_MotorLocking(void);
+void PMSM_FOC_Slow_Task(void);
 
 #endif /* _PMSM_FOC_Core_H__ */
 

+ 11 - 11
Applications/foc/core/PMSM_FOC_Params.h

@@ -7,9 +7,9 @@ static PI_Controller PI_Ctrl_ID = {
 	//.ki = (CURRENT_BANDWITH * MOTOR_R),
 	//.kp = (CURRENT_BANDWITH * MOTOR_Ld),
 	//.ki = (MOTOR_R/MOTOR_Ld),
-	.max = (MAX_vDC),
-	.min = (-MAX_vDC),
-	.DT  = (1.0f/(float)IDQ_CTRL_TS),
+	.max = (CONFIG_RATED_DC_VOL),
+	.min = (-CONFIG_RATED_DC_VOL),
+	.DT  = (1.0f/(float)CONFIG_IDQ_CTRL_TS),
 	.Ui = 0,
 };
 
@@ -18,9 +18,9 @@ static PI_Controller PI_Ctrl_IQ = {
 	//.ki = (CURRENT_BANDWITH * MOTOR_R),
 	//.kp = (CURRENT_BANDWITH * MOTOR_Lq),
 	//.ki = (MOTOR_R/MOTOR_Lq),
-	.max = (MAX_vDC),
-	.min = (-MAX_vDC),
-	.DT  = (1.0f/(float)IDQ_CTRL_TS),
+	.max = (CONFIG_RATED_DC_VOL),
+	.min = (-CONFIG_RATED_DC_VOL),
+	.DT  = (1.0f/(float)CONFIG_IDQ_CTRL_TS),
 	.Ui = 0,
 };
 
@@ -30,7 +30,7 @@ static PI_Controller PI_Ctrl_trq = {
 	//.kb = 1.2f,
 	.max = (0),
 	.min = (0),
-	.DT  = (1.0f/(float)SPD_CTRL_TS),
+	.DT  = (1.0f/(float)CONFIG_SPD_CTRL_TS),
 	.Ui = 0,
 };
 
@@ -40,7 +40,7 @@ static PI_Controller PI_Ctrl_Spd = {
 	//.kb = 1.2f,
 	.max = (0),
 	.min = (-0),
-	.DT  = (1.0f/(float)SPD_CTRL_TS),
+	.DT  = (1.0f/(float)CONFIG_SPD_CTRL_TS),
 	.Ui = 0,
 };
 
@@ -50,7 +50,7 @@ static PI_Controller PI_Ctrl_Power = {
 	//.ki = (100.0f),
 	.max = (0),
 	.min = 0,
-	.DT  = (1.0f/(float)SPD_CTRL_TS),
+	.DT  = (1.0f/(float)CONFIG_SPD_CTRL_TS),
 	.Ui = 0,
 };
 
@@ -59,7 +59,7 @@ static PI_Controller PI_Ctrl_lock = {
 	//.ki = (0.01f),
 	.max = (0),
 	.min = (-0),
-	.DT  = (1.0f/(float)SPD_CTRL_TS),
+	.DT  = (1.0f/(float)CONFIG_SPD_CTRL_TS),
 	.Ui = 0,
 };
 
@@ -68,7 +68,7 @@ static PI_Controller PI_Ctrl_fw = {
 	//.ki = (0.003f),
 	.max = (20),
 	.min = (0),
-	.DT  = (1.0f/(float)SPD_CTRL_TS),
+	.DT  = (1.0f/(float)CONFIG_SPD_CTRL_TS),
 	.Ui = 0,
 };
 

+ 3 - 24
Applications/foc/core/e_ctrl.c

@@ -15,10 +15,10 @@ void eCtrl_init(u16 accl_time, u16 dec_time){
 	g_eCtrl.dec_time = dec_time;
 	g_eCtrl.accl_time = accl_time;
 	if (g_eCtrl.accl_time == 0) {
-		g_eCtrl.accl_time = DEFAULT_D_TIME;
+		g_eCtrl.accl_time = CONFIG_DEFAULT_D_TIME;
 	}
 	if (g_eCtrl.dec_time == 0) {
-		g_eCtrl.dec_time = DEFAULT_D_TIME;
+		g_eCtrl.dec_time = CONFIG_DEFAULT_D_TIME;
 	}
 	g_eCtrl.ebrk_time = 2;
 	
@@ -113,28 +113,7 @@ static bool _eCtrl_isHwBrk_shutPower(void) {
 }
 
 static void _eCtrl_set_target(e_Ramp *ramp, float c) {
-	float c_now = eRamp_get_intepolation(ramp);
-	float step_val = 0;
-	int   sign = 1;
-
-	if (c < c_now) {
-		sign = -1;
-	}
-	u32 step_ms = eCTRL_STEP_TS;	
-	if (sign > 0) { //增加扭矩
-		step_val = (c - c_now)/(g_eCtrl.accl_time/step_ms);
-		if (step_val < MIN_S16Q5) {
-			step_val = MIN_S16Q5;
-		}
-	}else if (sign < 0) {
-		step_val = (c_now - c)/(g_eCtrl.dec_time/step_ms);
-		if (step_val < MIN_S16Q5) {
-			step_val = MIN_S16Q5;
-		}
-		step_val = -step_val;
-	}
-	eRamp_set_target(ramp, c);
-	eRamp_set_step(ramp, step_val);
+	eRamp_set_step_target(ramp, c, CONFIG_eCTRL_STEP_TS, (u32)g_eCtrl.accl_time, (u32)g_eCtrl.dec_time);
 }
 
 static void _eCtrl_set_TgtCurrent(float c) {

+ 37 - 2
Applications/foc/core/e_ctrl.h

@@ -2,9 +2,10 @@
 #define EBRAKE_CTRL_H__
 #include "os/os_types.h"
 #include "foc/core/ramp_ctrl.h"
+#include "math/fix_math.h"
 
 typedef struct {
-	float start;
+	//float start;
 	float target;
 	float interpolation;
 	float step_val;	
@@ -30,11 +31,19 @@ typedef struct {
 	bool is_ebrake_shadow;
 }e_Ctrl;
 static void eRamp_init(e_Ramp *r) {
-	r->start = 0;
+	//r->start = 0;
 	r->target = 0;
 	r->interpolation = 0;
 	r->step_val = 0;
 }
+
+static void eRamp_init_target(e_Ramp *r, float target) {
+	//r->start = 0;
+	r->target = target;
+	r->interpolation = target;
+	r->step_val = 0;
+}
+
 static void eRamp_set_target(e_Ramp *r, float target) {
 	r->target = target;
 }
@@ -64,6 +73,32 @@ static float eRamp_get_target(e_Ramp *r) {
 	return r->target;
 }
 
+static void eRamp_set_step_target(e_Ramp *ramp, float c, u32 intval, u32 acct, u32 dect) {
+	float c_now = eRamp_get_intepolation(ramp);
+	float step_val = 0;
+	int   sign = 1;
+
+	if (c < c_now) {
+		sign = -1;
+	}
+	u32 step_ms = intval;	
+	if (sign > 0) { //增加扭矩
+		step_val = (c - c_now)/(acct/step_ms);
+		if (step_val < MIN_FLOAT) {
+			step_val = MIN_FLOAT;
+		}
+	}else if (sign < 0) {
+		step_val = (c_now - c)/(dect/step_ms);
+		if (step_val < MIN_FLOAT) {
+			step_val = MIN_FLOAT;
+		}
+		step_val = -step_val;
+	}
+	eRamp_set_target(ramp, c);
+	eRamp_set_step(ramp, step_val);
+}
+
+
 void eCtrl_init(u16 accl_time, u16 dec_time);
 void eCtrl_set_ebrk_time(u16 ebrk_time);
 void eCtrl_brake_signal(bool hw_brake);

+ 10 - 16
Applications/foc/foc_config.h

@@ -8,29 +8,23 @@
 #define CONFIG_THROTTLE_MIN_RPM   10   /* 转把对应最小的速度 */
 #define CONFIG_THROTTLE_MIN_IDQ   20   /* 转把对应最小的扭矩电流 Q轴 */
 #define CONFIG_MIN_CRUISE_RPM 	  1000   /* 能启动定速巡航的最小速度 */
-  
-#ifdef GD32_FOC_DEMO
-#define MAX_vDC (16)   /* 母线最大电压 V*/
-#else
-#define MAX_vDC (50)   /* 母线最大电压 V*/
-#endif
 
 #define CONFIG_MIN_RPM_FOR_EBRAKE 1000 //进入电流回收的最小转速
 #define CONFIG_MIN_RPM_EXIT_EBRAKE 100 //推出电流回收的最小转速
 
-#define IDQ_CTRL_TS FOC_PWM_FS
-#define SPD_CTRL_TS 1000
-#define SPD_CTRL_MS (1000/SPD_CTRL_TS * 1000)
-#define VDQ_RAMP_TS 100
-#define VDQ_RAMP_FINAL_TIME 3000
+#define CONFIG_IDQ_CTRL_TS FOC_PWM_FS
+#define CONFIG_SPD_CTRL_TS 1000
+#define CONFIG_SPD_CTRL_MS (1000/CONFIG_SPD_CTRL_TS)
+#define CONFIG_SPD_CTRL_US (CONFIG_SPD_CTRL_MS * 1000)
+#define CONFIG_FOC_VDQ_RAMP_TS 100
+#define CONFIG_FOC_VDQ_RAMP_FINAL_TIME 3000
 
 /* 电子刹车,动能回收,加速 */
-#define TORQUE_STEP 1.0f          /* 扭矩斜率给定的step值,单位 A */
-#define SPEED_STEP  20.0f         /* 速度斜率给定的step值,单位 RPM */
-#define DEFAULT_D_TIME 3000       /* 默认的斜率给定时间,越大,越慢到给定值*/
-#define eCTRL_STEP_TS (SPD_CTRL_MS/1000 + 1)     /* 斜率给定的step的时间值,单位 ms */
-#define eCTRL_Brake_TIME 1500     /* 捏住刹车的时间,超过这个时间启动ebrake,单位 ms */
+#define CONFIG_DEFAULT_D_TIME 3000       /* 默认的斜率给定时间,越大,越慢到给定值*/
+#define CONFIG_eCTRL_STEP_TS CONFIG_SPD_CTRL_MS     /* 斜率给定的step的时间值,单位 ms */
+#define CONFIG_eCTRL_Brake_TIME 1500     /* 捏住刹车的时间,超过这个时间启动ebrake,单位 ms */
 
+#define CONFIG_LIMIT_RAMP_TIME (10 * 1000)
 
 #define CURRENT_LOOP_RAMP_COUNT 300
 

+ 2 - 2
Applications/foc/motor/motor.c

@@ -78,7 +78,7 @@ void mc_init(void) {
 	PMSM_FOC_CoreInit();
 	mc_gpio_init();
 	MC_Mos_Check_error();
-	sched_timer_enable(SPD_CTRL_MS);
+	sched_timer_enable(CONFIG_SPD_CTRL_US);
 	shark_task_create(_self_check_task, NULL);
 }
 
@@ -455,7 +455,7 @@ void Sched_MC_mTask(void) {
 				motor.throttle = f_throttle;
 				torque_speed_target(runMode, f_throttle);
 			}
-			PMSM_FOC_idqCalc();
+			PMSM_FOC_Slow_Task();
 		}
 	}
 }

+ 2 - 2
Applications/foc/samples.c

@@ -26,8 +26,8 @@ static samples_t _throttle;
 static samples_t _uvw_phase[3];
 #endif
 void samples_init(void){
-	_vbus.filted_value = (MAX_vDC);
-	_vbus.value = (MAX_vDC);
+	_vbus.filted_value = (CONFIG_RATED_DC_VOL);
+	_vbus.value = (CONFIG_RATED_DC_VOL);
 	_vbus.lowpass = (0.001f); 
 	sample_vbus();
 #ifdef THROTTLE_CHAN

+ 1 - 1
Applications/math/fix_math.h

@@ -47,7 +47,7 @@ typedef int32_t   s32q19_t;
 #define LowPass_Filter_Fix(value, sample, filter_constant, shift)	(value = ((s32)value * (S16Q14(1) - filter_constant) + (s32)sample * filter_constant) >> (shift))
 
 #define MIN_S16Q5 (1.0F/32.0F)
-
+#define MIN_FLOAT (1.0F/1024.0F)
 /*
 static __INLINE float S16Q4toF(s16q4_t v) {
 	s16 num = (v >> 4) & 0xFFFF;