Просмотр исходного кода

update

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 3 лет назад
Родитель
Сommit
5c2c80c219

+ 1 - 1
Applications/app/app.c

@@ -104,7 +104,7 @@ static u32 _app_report_task(void *p) {
 		sys_debug("acc vol %d, mos2 %d\n", get_acc_vol(), get_mos_temp2());
 		sys_debug("throttle %f\n", get_throttle_float());
 		sys_debug("ADC Vref %f, %f\n", get_adc_vref(), adc_5vref_compesion());
-		//sys_debug("curr lfp %f\n", PMSM_FOC_Get()->params.n_PhaseFilterCeof);
+		sys_debug("target current %f\n", PMSM_FOC_Get()->in.s_targetCurrent);
 		//sys_debug("fan rpm %d, %d\n", mc_params()->fan[0].rpm, mc_params()->fan[1].rpm);
 		encoder_log();
 		PMSM_FOC_LogDebug();

+ 4 - 4
Applications/app/nv_storage.c

@@ -82,8 +82,8 @@ static void nv_default_foc_params(void) {
 	foc_params.pid_conf[PID_Spd_id].ki = 0.08f;
 	foc_params.pid_conf[PID_Spd_id].kb = 1.0f;
 
-	foc_params.pid_conf[PID_Pow_id].kp = 1.8f;
-	foc_params.pid_conf[PID_Pow_id].ki = 6.0f;
+	foc_params.pid_conf[PID_Pow_id].kp = 5.0f;
+	foc_params.pid_conf[PID_Pow_id].ki = 15.0f;
 	foc_params.pid_conf[PID_Pow_id].kb = 0;
 
 	foc_params.pid_conf[PID_Lock_id].kp = (0.01f);
@@ -311,12 +311,12 @@ trq2dq_table_t *_trq2dq_table(int idx) {
 	trq2dq_table_t *tbl = (trq2dq_table_t *)fmc_get_addr(idx);
 
 	if (tbl->magic != 0x5AA5) {
-		sys_error("trq tlb magic error, 0x%x\n", tbl->magic);
+		sys_error("trq tlb %d magic error, 0x%x\n", idx, tbl->magic);
 		return NULL;
 	}
 	u16 crc = crc16_get((u8 *)tbl, sizeof(trq2dq_table_t) - 4);
 	if (crc != tbl->crc16) {
-		sys_error("trq tlb crc16 error\n");
+		sys_error("trq tlb %d crc16 error\n", idx);
 		return NULL;
 	}
 	return tbl;

+ 3 - 3
Applications/app/nv_storage.h

@@ -88,12 +88,12 @@ typedef struct {
 
 #define motorParam_idx_0 3
 #define motorParam_idx_1 (motorParam_idx_0 + 1)
-#define focParam_idx_0 (motorParam_idx_0 + 2)
-#define focParam_idx_1 (motorParam_idx_0 + 3)
+#define focParam_idx_0 (motorParam_idx_1 + 1)
+#define focParam_idx_1 (focParam_idx_0 + 1)
 
 #define trq_Tbl_size (4)
 #define trq_Tbl_idx0 (focParam_idx_1 + 1)
-#define trq_Tbl_idx1 (trq_Tbl_idx0 + 4)
+#define trq_Tbl_idx1 (trq_Tbl_idx0 + trq_Tbl_size)
 
 #define gear_config_idx_0 (trq_Tbl_idx1 + trq_Tbl_size)
 #define gear_config_idx_1 (gear_config_idx_0 + 1)

+ 3 - 3
Applications/bsp/fmc_flash.c

@@ -28,7 +28,7 @@ void fmc_write_sn(uint8_t *sn, int len){
 }
 
 void fmc_read_sn(uint8_t *sn, int len){
-	_fmc_erase_write_data(_sn_addr(), sn, len);
+	_fmc_read_data(_sn_addr(), sn, len);
 }
 
 void fmc_write_data(int index, uint8_t *data, int len){
@@ -36,7 +36,7 @@ void fmc_write_data(int index, uint8_t *data, int len){
 }
 
 void fmc_read_data(int index, uint8_t *data, int len){
-	_fmc_erase_write_data(_data_addr(index), data, len);
+	_fmc_read_data(_data_addr(index), data, len);
 }
 
 static __inline__ void _fmc_flag_clear(void) {
@@ -98,7 +98,7 @@ static uint32_t _sn_addr(void){
 }
 
 static uint32_t _data_addr(int index){
-	return 0x08000000 + (_flash_capatity() - one_page_size * (data_page_index + index));
+	return 0x08000000 + (_flash_capatity() - one_page_size * index);
 }
 
 static uint32_t _maigc_addr(void){

+ 1 - 1
Applications/foc/commands.c

@@ -63,13 +63,13 @@ static void process_ext_command(foc_cmd_body_t *command) {
 		}
 		s8 ext_gear = decode_8bits(b0, 5, 7);
 		if (ext_gear >= 0 && ext_gear <= 5) {
+			sys_debug("gear %d\n", ext_gear);
 			if (ext_gear == 0) {
 				mc_set_gear(3);
 			}else {
 				mc_set_gear(ext_gear - 1);
 			}
 		}
-		sys_debug("gear %d\n", ext_gear);
 		u8 b1 = decode_u8((u8 *)command->data + 1);
 		u8 cruise = decode_8bits(b1, 0, 1);
 		if (cruise == 2) {

+ 10 - 35
Applications/foc/core/PMSM_FOC_Core.c

@@ -264,7 +264,7 @@ static __INLINE void PMSM_FOC_Update_Hardware(void) {
 	gFoc_Ctrl.in.s_motAngle = 0;
 #endif
 
-	gFoc_Ctrl.in.s_vDC = get_vbus_int();
+	gFoc_Ctrl.in.s_vDC = get_vbus_float();
 	
 	get_phase_vols(gFoc_Ctrl.in.s_vABC);
 	
@@ -493,8 +493,8 @@ static __INLINE void PMSM_FOC_FieldWeak(void) {
 }
 static __INLINE float PMSM_FOC_Limit_iDC(float maxTrq) {
 #if 1
-	PI_Ctrl_Power.max = maxTrq;
-	float errRef = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.DCCurrLimRamp) - ABS(gFoc_Ctrl.out.s_FilteriDC);
+	gFoc_Ctrl.pi_power->max = maxTrq;
+	float errRef = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.DCCurrLimRamp) - (gFoc_Ctrl.out.s_FilteriDC);
 	return PI_Controller_run(gFoc_Ctrl.pi_power, errRef);
 #else
 	return maxTrq;
@@ -676,27 +676,19 @@ float PMSM_FOC_GetDCCurrLimit(void) {
 }
 
 void PMSM_FOC_SpeedLimit(float speedLimit) {
-	PMSM_FOC_SpeedRampLimit(speedLimit, CONFIG_LIMIT_RAMP_TIME, CONFIG_LIMIT_RAMP_TIME);
+	PMSM_FOC_SpeedRampLimit(speedLimit, false);
 }
 
-void PMSM_FOC_SpeedRampLimit(float speedLimit, u32 rampAccTime, u32 rampDecTime) {
+void PMSM_FOC_SpeedDirectLimit(float limit) {
+	PMSM_FOC_SpeedRampLimit(limit, true);
+}
+
+void PMSM_FOC_SpeedRampLimit(float speedLimit, bool direct) {
 	if (speedLimit > gFoc_Ctrl.hwLim.s_motRPMMax) {
 		speedLimit = gFoc_Ctrl.hwLim.s_motRPMMax;
 	}
-	bool need_reset = false;
-	if (speedLimit < gFoc_Ctrl.userLim.s_motRPMLim){
-		need_reset = true;
-	}
 	gFoc_Ctrl.userLim.s_motRPMLim = (speedLimit);
-	eRamp_set_time(&gFoc_Ctrl.rtLim.rpmLimRamp, rampAccTime, rampDecTime);
-	if (need_reset) {
-		if ((gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) && gFoc_Ctrl.out.f_vdqRation >= 1.0f) {
-			PI_Controller_Reset(gFoc_Ctrl.pi_torque, gFoc_Ctrl.out.s_RealCurrent);
-		}else if ((gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) && gFoc_Ctrl.pi_speed->is_sat) {
-			PI_Controller_Reset(gFoc_Ctrl.pi_speed, gFoc_Ctrl.out.s_RealCurrent);
-		}
-	}
-	if (ABS(gFoc_Ctrl.in.s_motRPM) < 10) {
+	if (direct) {
 		eRamp_reset_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit);
 	}else {
 		eRamp_set_step_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS);
@@ -704,23 +696,6 @@ void PMSM_FOC_SpeedRampLimit(float speedLimit, u32 rampAccTime, u32 rampDecTime)
 }
 
 
-/* 扭矩模式下的限速,主要是处理转把对应的最高速 */
-void PMSM_FOC_Torque_SpeedLimit(float speedLimit) {
-	if (speedLimit > gFoc_Ctrl.hwLim.s_motRPMMax) {
-		speedLimit = gFoc_Ctrl.hwLim.s_motRPMMax;
-	}
-	if (speedLimit > gFoc_Ctrl.userLim.s_motRPMLim) {
-		speedLimit = gFoc_Ctrl.userLim.s_motRPMLim;
-	}
-	eRamp_set_time(&gFoc_Ctrl.rtLim.rpmLimRamp, 1000, 1000);
-	if (gFoc_Ctrl.in.s_motRPM == 0) {
-		eRamp_reset_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit/1.3f);//1.3的系数防止限速过冲
-		eRamp_set_step_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS);
-	}else {
-		eRamp_set_step_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS);
-	}
-}
-
 float PMSM_FOC_GetSpeedLimit(void) {
 	return gFoc_Ctrl.userLim.s_motRPMLim;
 }

+ 2 - 2
Applications/foc/core/PMSM_FOC_Core.h

@@ -289,8 +289,8 @@ void PMSM_FOC_RT_PhaseCurrLim(float lim);
 void PMSM_FOC_RT_LimInit(void);
 float PMSM_FOC_Get_Real_Torque(void);
 void PMSM_FOC_Reset_Torque(void);
-void PMSM_FOC_Torque_SpeedLimit(float speedLimit);
-void PMSM_FOC_SpeedRampLimit(float speedLimit, u32 rampAccTime, u32 rampDecTime);
+void PMSM_FOC_SpeedRampLimit(float speedLimit, bool direct);
+void PMSM_FOC_SpeedDirectLimit(float limit);
 
 #endif /* _PMSM_FOC_Core_H__ */
 

+ 1 - 0
Applications/foc/core/torque.c

@@ -22,6 +22,7 @@ static rpm_trq_map_t gear_torques[5][5] = {
 
 static torque_manager_t torque_ctrl;
 void torque_init(void) {
+	trq2dq_lookup_init();
 	torque_reset();
 }
 

+ 2 - 0
Applications/foc/core/torque.h

@@ -15,5 +15,7 @@ void torque_reset(void);
 void trq2dq_lookup(int rpm, float torque, DQ_t *dq_out);
 void throttle_process(u8 run_mode, float f_throttle);
 void request_torque(float thro_ration);
+void trq2dq_lookup_init(void);
+
 #endif /*_TORQUE_LUT_H__ */
 

+ 19 - 13
Applications/foc/core/trq2dq_table.c

@@ -7,6 +7,9 @@
 #define TRQ_I_MAX MAX_TRQ_POINTS
 #define RPM_INTVAL TBL_SPD_INTVAL
 #define IDX2RPM(I) ((I)*RPM_INTVAL + RPM_INTVAL)
+
+#define _DEBUG(fmt, args...) no_debug(fmt, ##args)
+
 static trq2dq_t **table_map = NULL;
 //x -> rpm
 //z -> torque
@@ -14,16 +17,16 @@ static void intp_line2(float frac_x, float z, trq2dq_t **map, DQ_t *dq_out) {
 	float frac_z1 = 0; //对应x1索引的t_maps
 	float frac_z2 = 0; //对应x2索引的t_maps
 
-	sys_debug("low --> %d %d\n", map[1]->torque, map[0]->torque);
+	_DEBUG("low --> %d %d\n", map[1]->torque, map[0]->torque);
 	if ((map[1]->torque != map[0]->torque)) {
 		frac_z1 = (float)(z - map[0]->torque)/(map[1]->torque - map[0]->torque);
 	}
-	sys_debug("high --> %d %d\n", map[3]->torque, map[2]->torque);
+	_DEBUG("high --> %d %d\n", map[3]->torque, map[2]->torque);
 	if ((map[3]->torque != map[2]->torque)) {
 		frac_z2 = (float)(z - map[2]->torque)/(map[3]->torque - map[2]->torque);
 	}
 	
-	sys_debug("%f -- %f -- %f\n", frac_x, frac_z1, frac_z2);
+	_DEBUG("%f -- %f -- %f\n", frac_x, frac_z1, frac_z2);
 
 	float c1 = (1.0f - frac_z1) * map[0]->d + frac_z1 * map[1]->d; //第一行插值
 	float c2 = (1.0f - frac_z2) * map[2]->d + frac_z2 * map[3]->d; //第二行插值
@@ -42,7 +45,7 @@ static void get_torque_range(float z, int index, int max_index, int *left, int *
 		low_right = low_left = max_index - 1;
 	}else {
 		for (int i = 0; i < max_index; i++) {
-			sys_debug("index %d, trq %d\n", i, table_map[index][i].torque);
+			_DEBUG("index %d, trq %d\n", i, table_map[index][i].torque);
 			if (z >= table_map[index][i].torque) {
 				low_left = i;
 				low_right = i + 1;
@@ -57,19 +60,22 @@ static void get_torque_range(float z, int index, int max_index, int *left, int *
 	*right = low_right;
 }
 
-void trq2dq_lookup(int rpm, float torque, DQ_t *dq_out) {
+void trq2dq_lookup_init(void) {
 	if (table_map == NULL) {
 		trq2dq_table_t *table = nv_get_trq2dq_table();
 		if (table != NULL) {
 			table_map = (trq2dq_t **)&table->tdq[0][0];
 		}
-		if (table_map == NULL) {
-			dq_out->d = 0;
-			dq_out->q = torque;
-		}
+	}
+}
+
+void trq2dq_lookup(int rpm, float torque, DQ_t *dq_out) {
+	if (table_map == NULL) {
+		dq_out->d = 0;
+		dq_out->q = torque;
 		return;
 	}
-	
+
 	int low = 0, high = 0;
 
 	low = rpm / RPM_INTVAL - 1;
@@ -82,15 +88,15 @@ void trq2dq_lookup(int rpm, float torque, DQ_t *dq_out) {
 	}else {
 		high = low + 1;
 	}
-	sys_debug("speed %d-%d\n", low, high);
+	_DEBUG("speed %d-%d\n", low, high);
 
 	int low_left = TRQ_I_MAX - 1, low_right = TRQ_I_MAX - 1;
 	get_torque_range(torque, low, TRQ_I_MAX, &low_left, &low_right);
-	sys_debug("low speed torque %d-%d\n", low_left, low_right);
+	_DEBUG("low speed torque %d-%d\n", low_left, low_right);
 	
 	int high_left = TRQ_I_MAX - 1, high_right = TRQ_I_MAX - 1;
 	get_torque_range(torque, high, TRQ_I_MAX, &high_left, &high_right);
-	sys_debug("high speed torque %d-%d\n", high_left, high_right);
+	_DEBUG("high speed torque %d-%d\n", high_left, high_right);
 	trq2dq_t *maps[4];
 	maps[0] = &table_map[low][low_left];
 	maps[1] = &table_map[low][low_right];

+ 4 - 4
Applications/foc/foc_config.h

@@ -39,12 +39,12 @@
 /* 电子刹车,动能回收,加速 */
 #define CONFIG_eCTRL_STEP_TS CONFIG_SPD_CTRL_MS     /* 斜率给定的step的时间值,单位 ms */
 #define CONFIG_eCTRL_Brake_TIME 1500     /* 捏住刹车的时间,超过这个时间启动ebrake,单位 ms */
-#define CONFIG_ACC_TIME 3000
-#define CONFIG_DEC_TIME 2000
+#define CONFIG_ACC_TIME 1
+#define CONFIG_DEC_TIME 1
 #define CONFIG_EBRK_RAMP_TIME 500
 #define CONFIG_AUTOHOLD_DETECT_TIME 3000
 
-#define CONFIG_LIMIT_RAMP_TIME (10 * 1000)
+#define CONFIG_LIMIT_RAMP_TIME (5 * 1000)
 
 #define CONFIG_MTPA_CALI_RAMP_TIME (10 * 1000)
 
@@ -57,7 +57,7 @@
 
 #define CONFIG_TORQUE_MODE_MIN_RPM 600
 
-#define CONFIG_MAX_NEG_TORQUE 5.0F
+#define CONFIG_MAX_NEG_TORQUE 2.0F
 
 #ifdef CONFIG_SMO_OBSERVER
 	#define CONFIG_SMO_MIN_SPEED    1000 //RPM

+ 3 - 4
Applications/foc/limit.c

@@ -5,10 +5,9 @@
 #include "foc/mc_error.h"
 
 static limter_t motor_temp_lim[] = {//电机过温限流,限制相电流
-	{.enter_pointer = 120, .exit_pointer = 20, .limit_value = 0},
-	//{.enter_pointer = 120, .exit_pointer = 110, .limit_value = 90},
-	//{.enter_pointer = 110, .exit_pointer = 100, .limit_value = 120},
-	//{.enter_pointer = 100, .exit_pointer = 90, .limit_value = 130},
+	{.enter_pointer = 120, .exit_pointer = 110, .limit_value = 0},
+	{.enter_pointer = 110, .exit_pointer = 100, .limit_value = 120},
+	{.enter_pointer = 100, .exit_pointer = 90, .limit_value = 130},
 }; 
 static limter_t mos_temp_lim[] = { //mos过温限流,限制相电流
 	{.enter_pointer = 95, .exit_pointer = 85, .limit_value = 0},

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

@@ -149,7 +149,7 @@ static void mc_gear_vmode_changed(void) {
 		gears = &nv_get_gear_configs()->gears_48[0];
 	}
 	sys_debug("limit %d-%d-%d, mode = %s\n", gears[motor.n_gear].u_maxRPM, gears[motor.n_gear].u_maxIdc, gears[motor.n_gear].u_maxTorque, motor.b_is96Mode?"96V":"48V");
-	PMSM_FOC_SpeedRampLimit(gears[motor.n_gear].u_maxRPM, 500, 500);
+	PMSM_FOC_SpeedDirectLimit(gears[motor.n_gear].u_maxRPM);
 	PMSM_FOC_DCCurrLimit(gears[motor.n_gear].u_maxIdc);
 	//PMSM_FOC_TorqueLimit(gears[motor.n_gear].u_maxTorque);
 }
@@ -751,12 +751,12 @@ void Fan_IRQHandler(int idx) {
 	u32 delta_ts = get_delta_ms(pre_ts);
 	fan->det_ts = get_tick_ms();
 	float rpm = 60.0f * 1000 / (float)delta_ts;
-
+	
 	LowPass_Filter(fan->rpm, rpm, 0.1f);
 	if (idx == 0) {
-		shark_timer_post(&_fan_det_timer1, 5000);
+		shark_timer_post(&_fan_det_timer1, 100);
 	}else {
-		shark_timer_post(&_fan_det_timer2, 5000);
+		shark_timer_post(&_fan_det_timer2, 100);
 	}
 }
 

+ 3 - 0
Applications/libs/logger.h

@@ -60,5 +60,8 @@ void plot_1data16(s16 v1);
 #define sys_debug(fmt, args...) log_debug(MOD_SYSTEM, "D"fmt, ##args)
 #define sys_warning(fmt, args...) log_warning(MOD_SYSTEM, fmt, ##args)
 #define sys_error(fmt, args...) log_error(MOD_SYSTEM, fmt, ##args)
+
+#define no_debug(fmt, args...) {}
+
 #endif /* _Shark_Logger_h__ */
 

BIN
Simulink/FOC.slx