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

支持CAN老协议

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

+ 3 - 1
Applications/app/app.c

@@ -95,6 +95,8 @@ static u32 _app_report_task(void *p) {
 	can_report_foc_status(0x45);
 	can_report_phase_voltage(0x45);
 	can_report_mpta_values(0x45);
+	can_report_ext_status(0x43);
+	
 	if (++loop % 10 == 0) {
 		can_report_pid_value(0x45, PID_TRQ_id);
 		//sys_debug("max exec time %d, %d\n", g_meas_foc.exec_max_error_time, g_meas_foc.exec_time);
@@ -102,7 +104,7 @@ static u32 _app_report_task(void *p) {
 		encoder_log();
 		err_code_log();
 	}
-	return 500;
+	return 200;
 }
 
 

+ 23 - 11
Applications/bsp/can.c

@@ -40,6 +40,9 @@ void can_rx_poll(void){
 
 void can_tx_poll(void){
 	can_trasnmit_message_struct can_tr_m;
+	if (CAN_ERR(CAN0) & CAN_ERR_BOERR){
+		shark_can0_reset();
+	}
 	while (can_get_mailbox(CAN0) != CAN_NOMAILBOX) {
 		if (circle_get_data(&g_tx_circle, (uint8_t * )&can_tr_m, sizeof(can_tr_m)) != sizeof(can_tr_m)) {
 			break;
@@ -126,18 +129,11 @@ static void shark_can0_config(void)
   	nvic_irq_enable(USBD_LP_CAN0_RX0_IRQn,CAN_IRQ_PRIORITY,0);	
     /* enable can receive FIFO0 not empty interrupt */
     can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0);
-	/* recv broadcast, fifo1 */
-	can_filter_mask_mode_init(CAN_NODE_ADDR_FF, CAN_FILTER_DEST_MASK, CAN_EXTENDED_FIFO1, 1);
-	/* recv ccu aux fifo2*/
-	can_filter_mask_mode_init(CAN_NODE_ADDR_CCU_AUX, CAN_FILTER_DEST_MASK, CAN_EXTENDED_FIFO1, 2);
-	/* recv ble fifo3*/
-	can_filter_mask_mode_init(CAN_NODE_ADDR_BLE, CAN_FILTER_DEST_MASK, CAN_EXTENDED_FIFO1, 3);
-	
-	can_filter_mask_mode_init(CAN_NODE_ADDR_ACU, CAN_FILTER_DEST_MASK, CAN_EXTENDED_FIFO1, 4);
-	
-	nvic_irq_enable(CAN0_RX1_IRQn,CAN_IRQ_PRIORITY,0);	
+
+	//can_filter_mask_mode_init(0x1A0, CAN_FILTER_DEST_MASK, CAN_EXTENDED_FIFO1, 1);
+	//nvic_irq_enable(CAN0_RX1_IRQn,CAN_IRQ_PRIORITY,0);	
     /* enable can receive FIFO1 not empty interrupt */
-    can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE1);
+    //can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE1);
 }
 
 
@@ -200,6 +196,22 @@ int shark_can0_send_message(uint32_t can_id, const void*buff, int len){
 
 }
 
+int shark_can0_send_ext_message(uint32_t can_id, const void*buff, int len){
+	can_trasnmit_message_struct trasnmit_msg;
+	
+	trasnmit_msg.tx_sfid	= 0;
+	trasnmit_msg.tx_efid	= can_id;				
+	trasnmit_msg.tx_ft	= CAN_FT_DATA;
+	trasnmit_msg.tx_ff	= CAN_FF_EXTENDED;
+	trasnmit_msg.tx_dlen = min(CAN_DLC_LENGTH,len);
+	memcpy((char *)trasnmit_msg.tx_data, (char *)buff, trasnmit_msg.tx_dlen);
+
+	if (shark_send_can0_data(&trasnmit_msg) != CAN_SEND_OK){
+		return CAN_SEND_ERROR;
+	}
+	return CAN_SEND_OK;
+}
+
 
 void shark_can0_reset(void){
 	shark_can0_txrx_pin_config();

+ 1 - 1
Applications/bsp/can.h

@@ -66,7 +66,7 @@ static __inline__ void encoder_can_key(uint8_t *buff, uint16_t key) {
 
 int shark_can0_send_message(uint32_t can_id, const void*buff, int len);
 void shark_can0_route_message(uint32_t can_id, const void *buff, int len);
-
+int shark_can0_send_ext_message(uint32_t can_id, const void*buff, int len);
 void shark_can0_init(void);
 void shark_can0_reset(void);
 void shark_can0_deinit(void);

+ 59 - 1
Applications/foc/commands.c

@@ -45,12 +45,70 @@ static u32 foc_command_task(void *args) {
 	}
 	return 0;
 }
+u8 ext_gear = 0;
+static void process_ext_command(foc_cmd_body_t *command) {
+	if (command->ext_key == 0x1A01) {
+		return;
+	}else if (command->ext_key == 0x1A02) {
+		u8 b0 = decode_u8(command->data);
+		u8 p_mode = decode_8bits(b0, 0, 1);
+		if (p_mode == 1) {
+			mc_start(CTRL_MODE_TRQ);
+		}else if (p_mode == 2) {
+			mc_stop();
+		}
+		ext_gear = decode_8bits(b0, 5, 7);
+
+		u8 b1 = decode_u8((u8 *)command->data + 1);
+		u8 cruise = decode_8bits(b1, 0, 1);
+		if (cruise == 2) {
+			PMSM_FOC_EnableCruise(true);
+		}else if (cruise == 1) {
+			PMSM_FOC_EnableCruise(false);
+		}
+
+		u8 epm = decode_8bits(b1, 2, 3);
+		if (epm == 1) {
+			mc_start_epm(true);
+		}else if(epm == 2) {
+			mc_start_epm(false);
+		}
+
+		u8 m_4896 = decode_8bits(b1, 4, 5);
+
+		u8 epm_dir = decode_8bits(b1, 6, 7);
+		if (epm_dir == 0) {
+			mc_start_epm_move(EPM_Dir_None, true);
+		}else if (epm_dir == 1) {
+			mc_start_epm_move(EPM_Dir_Back, true);
+		}else if (epm_dir == 2) {
+			mc_start_epm_move(EPM_Dir_Forward, true);
+		}
+		u8 response[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
+		response[0] &= 0xFC;
+		response[0] |= (mc_is_start()?1:2);
+		response[0] |= (ext_gear << 5);
+		
+		response[1] &= 0xC0;
+		response[1] |= (PMSM_FOC_Is_CruiseEnabled()?2:1);
+		response[1] |= (mc_is_epm()?1:2) << 2;
+		response[1] |= m_4896<<4;
+
+		shark_can0_send_ext_message(0x1A024D43, response, sizeof(response));
+	}else if (command->ext_key == 0x1A05) {
+		shark_can0_send_ext_message(0x1A054D43, command->data, command->len);
+	}
+	
+}
 
 static void process_foc_command(foc_cmd_body_t *command) {
 	u8 erroCode = 0;
 	u8 response[32];
 	int len = 3;
-	sys_debug("command %d\n", command->cmd);
+	if ((command->ext_key != 0) && (command->cmd == 0)) {
+		process_ext_command(command);
+		return;
+	}
 	switch (command->cmd) {
 		case Foc_Start_Motor:
 		{

+ 2 - 1
Applications/foc/commands.h

@@ -41,7 +41,7 @@ typedef enum {
 	Foc_Report_Pid,
 	Foc_Report_Status,
 	Foc_Report_MTPA_DQ_Angle,
-	Foc_Cmd_Max
+	Foc_Cmd_Max = 255
 }foc_cmd_t;
 #define CMD_2_CAN_KEY(cmd) ((u16)(((u16)cmd) | (CAN_MY_ADDRESS<<8)))
 typedef enum {
@@ -51,6 +51,7 @@ typedef enum {
 
 typedef struct {
 	foc_cmd_t cmd;
+	u16       ext_key;
 	u8        can_src;
 	u8        len;
 	void      *data;

+ 1 - 1
Applications/foc/core/e_ctrl.c

@@ -61,7 +61,7 @@ bool eCtrl_enable_eBrake(bool enable) {
 }
 
 void _eCtrl_clear_ramp(void) {
-	eRamp_init(&g_eCtrl.current, 500, 500);
+	eRamp_init(&g_eCtrl.current, CONFIG_EBRK_RAMP_TIME, CONFIG_EBRK_RAMP_TIME);
 	eRamp_init(&g_eCtrl.speed, g_eCtrl.accl_time, g_eCtrl.dec_time);
 	eRamp_init(&g_eCtrl.torque, g_eCtrl.accl_time, g_eCtrl.dec_time);
 	g_eCtrl.current_shadow = 0.0f;

+ 1 - 1
Applications/foc/foc_config.h

@@ -37,7 +37,7 @@
 #define CONFIG_eCTRL_Brake_TIME 1500     /* 捏住刹车的时间,超过这个时间启动ebrake,单位 ms */
 #define CONFIG_ACC_TIME 3000
 #define CONFIG_DEC_TIME 2000
-
+#define CONFIG_EBRK_RAMP_TIME 500
 #define CONFIG_AUTOHOLD_DETECT_TIME 3000
 
 #define CONFIG_LIMIT_RAMP_TIME (10 * 1000)

+ 1 - 0
Applications/foc/motor/motor.c

@@ -585,6 +585,7 @@ static void mc_autohold_process(void) {
 		}else {
 			if (get_delta_ms(motor.n_autohold_time) >= CONFIG_AUTOHOLD_DETECT_TIME) {
 				mc_auto_hold(true);
+				gpio_beep(50);
 			}
 		}
 	}else {

+ 2 - 1
Applications/libs/utils.h

@@ -113,7 +113,8 @@ static __inline__ float decode_float(const u8 *buff)
 	return value;
 }
 
-
+#define u8_bits(start, end)             ((0xFFU << (start)) & (0xFFU >> (7U - (uint32_t)(end)))) 
+#define decode_8bits(v, start, end)    (((v) & u8_bits((start),(end))) >> (start))
 
 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
 #define min(a,b) ((a)>(b)?(b):(a))

+ 15 - 0
Applications/prot/can_foc_msg.c

@@ -102,3 +102,18 @@ void can_report_mpta_values(u8 can) {
 	encode_s16(data + 6, (s16)(PMSM_FOC_Get()->in.s_targetIdq.q * 10.0f));
 	can_send_message(get_indicator_can_id(can), data, sizeof(data), 0);
 }
+extern u8 ext_gear;
+void can_report_ext_status(u8 can) {
+	u8 data[8] = {0x0, 0x0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
+	data[0] = (get_vbus_int()>60?1:0) << 5;
+	data[0] |= (mc_is_epm()?1:0) << 6;
+	data[1] = mc_is_start()?0:1;
+	data[1] |= (PMSM_FOC_Is_CruiseEnabled()?1:0) << 3;
+	data[1] |= ext_gear << 6;
+	encode_s16(data + 2, (s16)PMSM_FOC_GetSpeed());
+	float vDC = get_vbus_float();
+	encode_s16(data + 4, (s16)(vDC*10));
+	float iDC = PMSM_FOC_GetVbusCurrent();
+	encode_s16(data + 6, (s16)(iDC*10));
+	shark_can0_send_ext_message(0x1A014D43, data, sizeof(data));
+}

+ 2 - 0
Applications/prot/can_foc_msg.h

@@ -11,5 +11,7 @@ void can_report_power(u8 can);
 void can_report_pid_value(u8 can, u8 id);
 void can_report_foc_status(u8 can);
 void can_report_mpta_values(u8 can);
+void can_report_ext_status(u8 can);
+
 #endif	/*_Can_Foc_Msg_H__ */
 

+ 33 - 1
Applications/prot/can_message.c

@@ -65,7 +65,38 @@ s32 can_send_message(uint32_t can_id, u8 *data, int len, s32 timeout){
 
 }
 
+static bool handle_can_ext_frame(u32 id, uint8_t *data, int len) {
+	switch (id) {
+		case 0x1A01434D:
+		case 0x1A02434D:
+		case 0x1A03434D:
+		case 0x1A04434D:
+		case 0x1A05434D:
+		case 0x1A06434D:
+		{
+			foc_cmd_body_t command;
+			command.cmd = (foc_cmd_t)0;
+			command.ext_key = (id >> 16) & 0xFFFF;
+			command.can_src = 0x43;
+			command.len = len;
+			command.data = os_alloc(len);
+			if (command.data != NULL) {
+				memcpy(command.data, data, len);
+				foc_send_command(&command);
+			}
+			return true;
+		}
+		default:
+			break;
+	}
+	return false;
+}
+
 void handle_can_frame(can_id_t id, uint8_t *data, int len){
+	//sys_debug("id = %0x\n", id.id);
+	if (handle_can_ext_frame(id.id, data, len)) {
+		return;
+	}
 	can_message_t *message = get_message_by_id(id);
 	if (message == NULL) {
 		return ;
@@ -114,12 +145,13 @@ void handle_can_frame(can_id_t id, uint8_t *data, int len){
 
 
 static void can_process_message(can_message_t *message){
-	sys_debug("can %x [%x -> %x], len = %d\n", message->key, message->src, message->dest, message->len);
+	//sys_debug("can %x [%x -> %x], len = %d\n", message->key, message->src, message->dest, message->len);
 	if ((message->key & 0xFF) >= 0xF0) {
 		can_process_iap_message(message);
 	}else{ //只处理后控的指令
 		if ((message->key & 0xFF) < Foc_Cmd_Max){
 			foc_cmd_body_t command;
+			command.ext_key = 0;
 			command.cmd = (foc_cmd_t)(message->key & 0xFF);
 			command.can_src = message->src;
 			command.len = message->len;