فهرست منبع

支持新的配置文件

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 2 سال پیش
والد
کامیت
d4b5e106bc
5فایلهای تغییر یافته به همراه39 افزوده شده و 148 حذف شده
  1. 6 6
      Applications/app/app.c
  2. 1 121
      Applications/autogen_config.h
  3. 8 5
      Applications/foc/commands.c
  4. 0 6
      Applications/foc/commands.h
  5. 24 10
      Applications/foc/mc_config.c

+ 6 - 6
Applications/app/app.c

@@ -103,7 +103,7 @@ static u32 _app_trq_test_task(void *args) {
 }
 #endif
 void app_start(void){
-	can_debug(false);
+	can_debug(true);
 	can_message_init();
 	nv_storage_init();
 	mc_conf_init();
@@ -150,14 +150,14 @@ static u32 _app_report_task(void *p) {
 
 	if (++loop % 5 == 0) {
 		//sys_debug("rst 0x%x\n", get_mcu_reset_source());
-		sys_debug("Slow: %d - %d\n", g_meas_MCTask.intval_time, g_meas_MCTask.exec_time);
-		sys_debug("Fast: %d - %d, err: %d-%d-%d\n", g_meas_foc.intval_time, g_meas_foc.exec_time, g_meas_foc.intval_hi_err, g_meas_foc.intval_low_err, g_meas_foc.exec_max_error_time);
-		sys_debug("FOC time err %d %d\n", g_meas_foc.intval_time_h_error, g_meas_foc.intval_time_l_error);
+		//sys_debug("Slow: %d - %d\n", g_meas_MCTask.intval_time, g_meas_MCTask.exec_time);
+		//sys_debug("Fast: %d - %d, err: %d-%d-%d\n", g_meas_foc.intval_time, g_meas_foc.exec_time, g_meas_foc.intval_hi_err, g_meas_foc.intval_low_err, g_meas_foc.exec_max_error_time);
+		//sys_debug("FOC time err %d %d\n", g_meas_foc.intval_time_h_error, g_meas_foc.intval_time_l_error);
 		//sys_debug("acc vol %d, bid %d\n", get_acc_vol(), gpio_board_id());
 		//sys_debug("throttle %f\n", get_throttle_float());
 		//sys_debug("ADC Vref %f, %f\n", get_adc_vref(), adc_5vref_compesion());
 		//sys_debug("dead time %d\n", get_deadtime());
-		thro_torque_log();
+		//thro_torque_log();
 		//sys_debug("_>%f, %f, %f\n", ladrc_observer_get()->ld, ladrc_observer_get()->lq, ladrc_observer_get()->poles);
 		//encoder_log();
 		//motor_debug();
@@ -167,7 +167,7 @@ static u32 _app_report_task(void *p) {
 		//eCtrl_debug_log();
 		//sys_debug("enc err %d, %d\n", foc_observer_enc_errcount(), foc_observer_sensorless_stable());
 		//mc_err_code_log();
-		sys_debug("=====\n");
+		//sys_debug("=====\n");
 	}
 	return 200;
 }

+ 1 - 121
Applications/autogen_config.h

@@ -1,4 +1,4 @@
-/* auto gen 2023/8/29 13:54:06*/
+/* auto gen 2023/8/30 10:53:32*/
 #ifndef _AUTOGEN_CONFIG_H__
 #define _AUTOGEN_CONFIG_H__
 
@@ -107,126 +107,6 @@
 #define CONFIG_Gear3_Torque7 100
 #define CONFIG_Gear3_Torque8 0
 #define CONFIG_Gear3_Torque9 0
-#define CONFIG_Gear0_MaxSpeed 1000
-#define CONFIG_Gear0_MaxTorque 100
-#define CONFIG_Gear0_MaxIdc 30
-#define CONFIG_Gear0_ZeroAccl 500
-#define CONFIG_Gear0_NormalAccl 100
-#define CONFIG_Gear0_Torque0 100
-#define CONFIG_Gear0_Torque1 100
-#define CONFIG_Gear0_Torque2 100
-#define CONFIG_Gear0_Torque3 100
-#define CONFIG_Gear0_Torque4 100
-#define CONFIG_Gear0_Torque5 100
-#define CONFIG_Gear0_Torque6 100
-#define CONFIG_Gear0_Torque7 100
-#define CONFIG_Gear0_Torque8 0
-#define CONFIG_Gear0_Torque9 0
-#define CONFIG_Gear1_MaxSpeed 1000
-#define CONFIG_Gear1_MaxTorque 100
-#define CONFIG_Gear1_MaxIdc 30
-#define CONFIG_Gear1_ZeroAccl 500
-#define CONFIG_Gear1_NormalAccl 100
-#define CONFIG_Gear1_Torque0 100
-#define CONFIG_Gear1_Torque1 100
-#define CONFIG_Gear1_Torque2 100
-#define CONFIG_Gear1_Torque3 100
-#define CONFIG_Gear1_Torque4 100
-#define CONFIG_Gear1_Torque5 100
-#define CONFIG_Gear1_Torque6 100
-#define CONFIG_Gear1_Torque7 100
-#define CONFIG_Gear1_Torque8 0
-#define CONFIG_Gear1_Torque9 0
-#define CONFIG_Gear2_MaxSpeed 1000
-#define CONFIG_Gear2_MaxTorque 100
-#define CONFIG_Gear2_MaxIdc 30
-#define CONFIG_Gear2_ZeroAccl 500
-#define CONFIG_Gear2_NormalAccl 100
-#define CONFIG_Gear2_Torque0 100
-#define CONFIG_Gear2_Torque1 100
-#define CONFIG_Gear2_Torque2 100
-#define CONFIG_Gear2_Torque3 100
-#define CONFIG_Gear2_Torque4 100
-#define CONFIG_Gear2_Torque5 100
-#define CONFIG_Gear2_Torque6 100
-#define CONFIG_Gear2_Torque7 100
-#define CONFIG_Gear2_Torque8 0
-#define CONFIG_Gear2_Torque9 0
-#define CONFIG_Gear3_MaxSpeed 1000
-#define CONFIG_Gear3_MaxTorque 100
-#define CONFIG_Gear3_MaxIdc 30
-#define CONFIG_Gear3_ZeroAccl 500
-#define CONFIG_Gear3_NormalAccl 100
-#define CONFIG_Gear3_Torque0 100
-#define CONFIG_Gear3_Torque1 100
-#define CONFIG_Gear3_Torque2 100
-#define CONFIG_Gear3_Torque3 100
-#define CONFIG_Gear3_Torque4 100
-#define CONFIG_Gear3_Torque5 100
-#define CONFIG_Gear3_Torque6 100
-#define CONFIG_Gear3_Torque7 100
-#define CONFIG_Gear3_Torque8 0
-#define CONFIG_Gear3_Torque9 0
-#define CONFIG_GearLow0_MaxSpeed 1000
-#define CONFIG_GearLow0_MaxTorque 100
-#define CONFIG_GearLow0_MaxIdc 30
-#define CONFIG_GearLow0_ZeroAccl 500
-#define CONFIG_GearLow0_NormalAccl 100
-#define CONFIG_GearLow0_Torque0 100
-#define CONFIG_GearLow0_Torque1 100
-#define CONFIG_GearLow0_Torque2 100
-#define CONFIG_GearLow0_Torque3 100
-#define CONFIG_GearLow0_Torque4 100
-#define CONFIG_GearLow0_Torque5 100
-#define CONFIG_GearLow0_Torque6 100
-#define CONFIG_GearLow0_Torque7 100
-#define CONFIG_GearLow0_Torque8 0
-#define CONFIG_GearLow0_Torque9 0
-#define CONFIG_GearLow1_MaxSpeed 1000
-#define CONFIG_GearLow1_MaxTorque 100
-#define CONFIG_GearLow1_MaxIdc 30
-#define CONFIG_GearLow1_ZeroAccl 500
-#define CONFIG_GearLow1_NormalAccl 100
-#define CONFIG_GearLow1_Torque0 100
-#define CONFIG_GearLow1_Torque1 100
-#define CONFIG_GearLow1_Torque2 100
-#define CONFIG_GearLow1_Torque3 100
-#define CONFIG_GearLow1_Torque4 100
-#define CONFIG_GearLow1_Torque5 100
-#define CONFIG_GearLow1_Torque6 100
-#define CONFIG_GearLow1_Torque7 100
-#define CONFIG_GearLow1_Torque8 0
-#define CONFIG_GearLow1_Torque9 0
-#define CONFIG_GearLow2_MaxSpeed 1000
-#define CONFIG_GearLow2_MaxTorque 100
-#define CONFIG_GearLow2_MaxIdc 30
-#define CONFIG_GearLow2_ZeroAccl 500
-#define CONFIG_GearLow2_NormalAccl 100
-#define CONFIG_GearLow2_Torque0 100
-#define CONFIG_GearLow2_Torque1 100
-#define CONFIG_GearLow2_Torque2 100
-#define CONFIG_GearLow2_Torque3 100
-#define CONFIG_GearLow2_Torque4 100
-#define CONFIG_GearLow2_Torque5 100
-#define CONFIG_GearLow2_Torque6 100
-#define CONFIG_GearLow2_Torque7 100
-#define CONFIG_GearLow2_Torque8 0
-#define CONFIG_GearLow2_Torque9 0
-#define CONFIG_GearLow3_MaxSpeed 1000
-#define CONFIG_GearLow3_MaxTorque 100
-#define CONFIG_GearLow3_MaxIdc 30
-#define CONFIG_GearLow3_ZeroAccl 500
-#define CONFIG_GearLow3_NormalAccl 100
-#define CONFIG_GearLow3_Torque0 100
-#define CONFIG_GearLow3_Torque1 100
-#define CONFIG_GearLow3_Torque2 100
-#define CONFIG_GearLow3_Torque3 100
-#define CONFIG_GearLow3_Torque4 100
-#define CONFIG_GearLow3_Torque5 100
-#define CONFIG_GearLow3_Torque6 100
-#define CONFIG_GearLow3_Torque7 100
-#define CONFIG_GearLow3_Torque8 0
-#define CONFIG_GearLow3_Torque9 0
 #define CONFIG_GearLow0_MaxSpeed 1000
 #define CONFIG_GearLow0_MaxTorque 100
 #define CONFIG_GearLow0_MaxIdc 30

+ 8 - 5
Applications/foc/commands.c

@@ -272,7 +272,7 @@ static void process_foc_command(foc_cmd_body_t *command) {
 			int config_len = mc_conf_get_gear(mode, data + 3);
 			data[0] = command->cmd;
 			data[1] = CAN_MY_ADDRESS;
-			data[2] = 0;
+			data[2] = mode;
 			can_send_response(command->can_src, data, config_len + 3);
 			os_free(data);
 			return;
@@ -338,7 +338,7 @@ static void process_foc_command(foc_cmd_body_t *command) {
 			}
 			pid_t pid;
 			u8 id =   decode_u8((u8 *)command->data);
-			memcpy((char *)&pid, (char *)command->data + 1, sizeof(pid_conf_t));
+			memcpy((char *)&pid, (char *)command->data + 1, sizeof(pid_t));
 			sys_debug("set id = %d, kp = %f, ki = %f, kd = %f\n", id, pid.kp, pid.ki, pid.kd);
 			PMSM_FOC_SetPid(id, pid.kp, pid.ki, pid.kd);
 			mc_conf_set_pid(id, &pid);
@@ -489,6 +489,7 @@ static void process_foc_command(foc_cmd_body_t *command) {
 		case Foc_Set_Config:
 		{
 			u8 conf_cmd = decode_u8(command->data);
+			sys_debug("cmd %d\n", conf_cmd);
 			if (conf_cmd == 1) { //start
 				int len = decode_u16((u8 *)command->data+1);
 				if (!mc_conf_begin_recv(len)) {
@@ -503,6 +504,7 @@ static void process_foc_command(foc_cmd_body_t *command) {
 				}
 			}else if (conf_cmd == 0) { //recv config data
 				int offset = decode_u16((u8 *)command->data+1);
+				sys_debug("offset = %d\n", offset);
 				if (!mc_conf_recv_data((u8 *)command->data + 3, offset, command->len - 3)){
 					erroCode = FOC_Param_Err;
 				}
@@ -516,17 +518,18 @@ static void process_foc_command(foc_cmd_body_t *command) {
 			
 			int offset = decode_u16((u8 *)command->data);
 			if (offset == 0) {
-				response[4] = 1;
+				response[3] = 1;
 			}else {
-				response[4] = 0;
+				response[3] = 0;
 			}
+			len += 1;
 			int ret = mc_conf_send_data(response + 4, offset, sizeof(response) - 4);
 			if (ret == -2) {
 				erroCode = FOC_MEM_Err;
 			}else if (ret == -1) {
 				erroCode = FOC_Param_Err;
 			}else if (ret == 0){
-				response[4] = 2;
+				response[3] = 2;
 				len += mc_conf_finish_send(response + 4);
 			}else {
 				len += ret;

+ 0 - 6
Applications/foc/commands.h

@@ -151,12 +151,6 @@ typedef struct {
 	u32 step_time;//second
 }hall_offset_cali_cmd_t;
 
-typedef struct {
-	float kp;
-	float ki;
-	float kd;
-}pid_conf_t;
-
 #pragma pack(pop)
 
 void foc_command_init(void);

+ 24 - 10
Applications/foc/mc_config.c

@@ -2,6 +2,7 @@
 #include "libs/utils.h"
 #include "libs/crc16.h"
 #include "app/nv_storage.h"
+#include "libs/logger.h"
 
 mc_config conf;
 
@@ -56,7 +57,8 @@ bool mc_conf_finish_recv(u16 crc) {
 static u16 crc16;
 bool mc_conf_begin_send(void) {
 	if (conf_buff) {
-		return true;
+		os_free(conf_buff);
+		conf_buff = NULL;
 	}
 	
 	conf_idx = 0;
@@ -65,11 +67,13 @@ bool mc_conf_begin_send(void) {
 		conf_len = mc_conf_encode_configs(conf_buff);
 		crc16 = crc16_get(conf_buff, conf_len);
 	}
+	sys_debug("%x, %d, %d\n", conf_buff, conf_len, crc16);
 	return conf_buff != NULL;
 }
 
 
 int mc_conf_send_data(u8 *buff, int offset, int len) {
+	sys_debug("off %d, %d\n", offset, conf_idx);
 	if (offset == 0 && !mc_conf_begin_send()) {
 		return -2;
 	}
@@ -105,21 +109,28 @@ void mc_conf_load(void) {
 	u16 crc16 = crc16_get((u8 *)&temp, (u8 *)&temp.crc - (u8 *)&temp);
 	bool idx0_success = crc16 == temp.crc;
 	if (idx0_success) {
+		sys_debug("conf block 0 OK!\n");
 		memcpy(&conf, &temp, sizeof(temp));
 	}
 	nv_read_config_block(1, (u8 *)&temp, sizeof(temp));
 	crc16 = crc16_get((u8 *)&temp, (u8 *)&temp.crc - (u8 *)&temp);
 	if (crc16 == temp.crc && !idx0_success) {
+		sys_debug("conf block0 Bad!\n");
+		sys_debug("conf block 1 OK!\n");
 		memcpy(&conf, &temp, sizeof(temp));
 		nv_write_config_block(0, (u8 *)&temp, sizeof(temp));
 	}else if (crc16 != temp.crc) {
 		if (idx0_success) {
+			sys_debug("conf block1 Bad!\n");
 			nv_write_config_block(1, (u8 *)&conf, sizeof(conf));
 		}else {
+			sys_debug("conf block0&1 Bad!\n");
 			mc_conf_default();
 			nv_write_config_block(0, (u8 *)&conf, sizeof(conf));
 			nv_write_config_block(1, (u8 *)&conf, sizeof(conf));
 		}
+	}else {
+		sys_debug("conf block 1 OK!\n");
 	}
 }
 
@@ -135,7 +146,7 @@ void mc_conf_load(void) {
 		Gear_torque(l,5);Gear_torque(l,6);Gear_torque(l,7);Gear_torque(l,8);Gear_torque(l,9); \
 	}while(0);
 
-#define GearLow_torque(l, t) conf.g_n[l].torque[t] = CONFIG_GearLow##l##_Torque##t
+#define GearLow_torque(l, t) conf.g_l[l].torque[t] = CONFIG_GearLow##l##_Torque##t
 #define GearLow_Config_default(l) \
 		do { \
 			conf.g_l[l].max_speed = CONFIG_GearLow##l##_MaxSpeed;\
@@ -162,11 +173,12 @@ void mc_conf_load(void) {
 	}while(0);
 
 void mc_conf_default(void) {
-	conf.version = 0;
+	conf.version = CONFIG_Version;
 	conf.m.poles = CONFIG_Motor_Poles;
 	conf.m.ld = CONFIG_Motor_Ld;
 	conf.m.lq = CONFIG_Motor_Lq;
 	conf.m.rs = CONFIG_Motor_Rs;
+	conf.m.flux = CONFIG_Motor_Flux;
 	conf.m.nor_pll_band = CONFIG_Motor_PLLBand;
 	conf.m.epm_pll_band = CONFIG_Motor_EpmPLL;
 	conf.m.pos_pll_band = CONFIG_Motor_PosPLL;
@@ -210,8 +222,8 @@ void mc_conf_default(void) {
 	conf.c.pid[PID_Vel_ID].kd = CONFIG_Foc_PID_VelCtrl_Kd;
 
 	conf.c.pid[PID_AutoHold_ID].kp = CONFIG_Foc_PID_Autohold_Kp;
-	conf.c.pid[PID_AutoHold_ID].kp = CONFIG_Foc_PID_Autohold_Ki;
-	conf.c.pid[PID_AutoHold_ID].ki = CONFIG_Foc_PID_Autohold_Kd;
+	conf.c.pid[PID_AutoHold_ID].ki = CONFIG_Foc_PID_Autohold_Ki;
+	conf.c.pid[PID_AutoHold_ID].kd = CONFIG_Foc_PID_Autohold_Kd;
 
 	conf.c.pid[PID_IDCLim_ID].kp = CONFIG_Foc_PID_IDCLim_Kp;
 	conf.c.pid[PID_IDCLim_ID].ki = CONFIG_Foc_PID_IDCLim_Ki;
@@ -291,7 +303,7 @@ int mc_conf_encode_motor(u8 *buff) {
 	encode_u16(buff, conf.m.velocity_w);buff += 2;
 	encode_u16(buff, conf.m.wheel_c);buff += 2;
 	encode_float(buff, conf.m.gear_ratio);buff += 4;
-	encode_u16(buff, conf.m.max_fw_id); buff += 4;
+	encode_u16(buff, conf.m.max_fw_id); buff += 2;
 	return buff - ori;
 }
 
@@ -419,6 +431,7 @@ int mc_conf_decode_limiter(limiter_t *l, int size, u8 *buff) {
 		l->enter_pointer = decode_s16(buff);buff += 2;
 		l->exit_pointer = decode_s16(buff);buff += 2;
 		l->limit_value = decode_s16(buff);buff += 2;
+		l++;
 	}
 	return buff - ori;
 }
@@ -429,6 +442,7 @@ int mc_conf_encode_limiter(limiter_t *l, int size, u8 *buff) {
 		encode_s16(buff, l->enter_pointer);buff += 2;
 		encode_s16(buff, l->exit_pointer);buff += 2;
 		encode_s16(buff, l->limit_value);buff += 2;
+		l++;
 	}
 	return buff - ori;
 }
@@ -505,7 +519,7 @@ int mc_conf_get_gear(u8 mode, u8 *data) {
 	if (mode == 0) {
 		g = conf.g_l;
 	}
-	return mc_conf_decode_gear(g, data);
+	return mc_conf_encode_gear(g, data);
 }
 
 bool mc_conf_set_limter(u8 *data, int len) {
@@ -517,8 +531,8 @@ bool mc_conf_set_limter(u8 *data, int len) {
 
 int mc_conf_get_limter(u8 *data) {
 	u8 *ori = data;
-	data += mc_conf_decode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, data);
-	data += mc_conf_decode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, data);
-	data += mc_conf_decode_limiter(&conf.p_vol, 1, data);
+	data += mc_conf_encode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, data);
+	data += mc_conf_encode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, data);
+	data += mc_conf_encode_limiter(&conf.p_vol, 1, data);
 	return data-ori;
 }