From eefa6daf3803ef81ddc16b8c7ddef8fbf73e77fd Mon Sep 17 00:00:00 2001 From: liumin Date: Tue, 8 Jul 2025 21:07:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BD=AC=E5=90=91=E6=89=AD?= =?UTF-8?q?=E7=9F=A9=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app_differential_drive.c | 209 ++- app/app_differential_drive.h | 169 +- interface_can.c | 2853 +++++++++++++++++----------------- main.c | 2 +- 4 files changed, 1673 insertions(+), 1560 deletions(-) diff --git a/app/app_differential_drive.c b/app/app_differential_drive.c index 8513240..567238c 100644 --- a/app/app_differential_drive.c +++ b/app/app_differential_drive.c @@ -19,6 +19,43 @@ DiffData diff_data; PID_t speed_pid; PID_t yaw_rate_pid; + + + +/** + * @brief 根据挡位和输入转矩计算输出转矩值 + * @param gear 挡位状态(STATE_FORWARD/STATE_BACKWARD/其他) + * @param input_torque 输入转矩值 + * @return 处理后的转矩值(已包含偏移量和系数) + */ +float calculateTorqueOutput(uint8_t gear, float input_torque) +{ + const float OFFSET = 300.0f; // 偏移量常量 + const float SCALE_FACTOR = 100.0f; // 缩放系数 + const float DEFAULT_VALUE = 30000.0f; // 默认输出值 + + float output_torque; + + if (gear == STATE_FORWARD) + { + output_torque = (input_torque + OFFSET) * SCALE_FACTOR; + } + else if (gear == STATE_BACKWARD) + { + output_torque = (-input_torque + OFFSET) * SCALE_FACTOR; + } + else + { + output_torque = DEFAULT_VALUE; + } + + return output_torque; +} + + + + + // 设置电机输出 void setMotorOutput(float *out_torq, float max_torque, uint16_t feed_power, uint16_t discharge_power) { @@ -29,35 +66,54 @@ void setMotorOutput(float *out_torq, float max_torque, uint16_t feed_power, uint float abs_right_rear_speed = 0; // 档位 - un_motor_output1.bit_data.gear = diff_data.state; // 1 表示前进,2 表示后退,0空挡 - un_motor_output2.bit_data.gear = diff_data.state; - un_motor_output3.bit_data.gear = diff_data.state; - un_motor_output4.bit_data.gear = diff_data.state; + if(diff_data.state != STATE_STATIC_TURN) + { + un_motor_output1.bit_data.gear = diff_data.state; // 1 表示前进,2 表示后退,0空挡 + un_motor_output2.bit_data.gear = diff_data.state; + un_motor_output3.bit_data.gear = diff_data.state; // 1 表示前进,2 表示后退,0空挡 + un_motor_output4.bit_data.gear = diff_data.state; + } + else//原地转向状态的话,根据扭矩大小确定方向 + { + un_motor_output1.bit_data.gear = (out_torq[0] < 0) ? STATE_BACKWARD : STATE_FORWARD; + un_motor_output2.bit_data.gear = (out_torq[1] < 0) ? STATE_BACKWARD : STATE_FORWARD; + un_motor_output3.bit_data.gear = (out_torq[2] < 0) ? STATE_BACKWARD : STATE_FORWARD; + un_motor_output4.bit_data.gear = (out_torq[3] < 0) ? STATE_BACKWARD : STATE_FORWARD; + } - //增加系数以及偏移量 - if(diff_data.state == STATE_FORWARD)//根据挡位来判断,扭矩的正负 + abs_left_front_speed = calculateTorqueOutput(un_motor_output1.bit_data.gear, out_torq[0]); //根据挡位增加转矩方向 + abs_right_front_speed = calculateTorqueOutput(un_motor_output2.bit_data.gear, out_torq[1]); + abs_left_rear_speed = calculateTorqueOutput(un_motor_output3.bit_data.gear, out_torq[2]); + abs_right_rear_speed = calculateTorqueOutput(un_motor_output4.bit_data.gear, out_torq[3]); + + + if(STATE_FORWARD == un_motor_output3.bit_data.gear)//把后两台电机反相 { - abs_left_front_speed = (out_torq[0] + 300.0f) *100.0f; - abs_right_front_speed = (out_torq[1] + 300.0f) *100.0f; - abs_left_rear_speed = (out_torq[2] + 300.0f) *100.0f; - abs_right_rear_speed = (out_torq[3] + 300.0f) *100.0f; + un_motor_output3.bit_data.gear = STATE_BACKWARD; } - else if(diff_data.state == STATE_BACKWARD)//倒挡直接修改为负扭矩发送出去 + else if(STATE_BACKWARD == un_motor_output3.bit_data.gear) { - abs_left_front_speed = (-out_torq[0] + 300.0f) *100.0f; - abs_right_front_speed = (-out_torq[1] + 300.0f) *100.0f; - abs_left_rear_speed = (-out_torq[2] + 300.0f) *100.0f; - abs_right_rear_speed = (-out_torq[3] + 300.0f) *100.0f; + un_motor_output3.bit_data.gear = STATE_FORWARD; } - else//空挡直接发0 + else { - abs_left_front_speed = 0; - abs_right_front_speed = 0; - abs_left_rear_speed = 0; - abs_right_rear_speed = 0; + un_motor_output3.bit_data.gear = STATE_INIT; + } + + if(STATE_FORWARD == un_motor_output4.bit_data.gear) + { + un_motor_output4.bit_data.gear = STATE_BACKWARD; } - + else if(STATE_BACKWARD == un_motor_output4.bit_data.gear) + { + un_motor_output4.bit_data.gear = STATE_FORWARD; + } + else + { + un_motor_output4.bit_data.gear = STATE_INIT; + } + // 设置左右电机期望转速 // un_motor_output1.bit_data.set_rotation_speed = ((uint16_t)roundf(abs_left_speed) + 30000); // 20240921 增加偏移量 30000 @@ -69,13 +125,18 @@ void setMotorOutput(float *out_torq, float max_torque, uint16_t feed_power, uint un_motor_output3.bit_data.mode = MOTOR_MODE; un_motor_output4.bit_data.mode = MOTOR_MODE; - // 设置最大扭矩 + // 设置扭矩 un_motor_output1.bit_data.set_torque = (uint16_t)( (int16_t)abs_left_front_speed ); un_motor_output2.bit_data.set_torque = (uint16_t)( (int16_t)abs_right_front_speed ); un_motor_output3.bit_data.set_torque = (uint16_t)( (int16_t)abs_left_rear_speed ); un_motor_output4.bit_data.set_torque = (uint16_t)( (int16_t)abs_right_rear_speed ); - + //设定转速 + un_motor_output1.bit_data.set_rotation_speed = 30000; + un_motor_output2.bit_data.set_rotation_speed = 30000; + un_motor_output3.bit_data.set_rotation_speed = 30000; + un_motor_output4.bit_data.set_rotation_speed = 30000; + // 设置馈电功率 un_motor_output1.bit_data.feed_power = feed_power; un_motor_output2.bit_data.feed_power = feed_power; @@ -325,8 +386,7 @@ void computeInverseKinematics(float linear_velocity_x, float yaw_rate, float max linear_velocity_x = constrain(linear_velocity_x, -max_torque, max_torque); yaw_rate = constrain(yaw_rate, -2*max_torque, 2*max_torque); - - + float left_speed_mps = linear_velocity_x + yaw_rate; float right_speed_mps = linear_velocity_x - yaw_rate; @@ -361,9 +421,9 @@ void computeInverseKinematics(float linear_velocity_x, float yaw_rate, float max motor_speed[1] = right_speed_mps; motor_speed[3] = right_speed_mps; - adjust_torque_by_speed_diff( diff_data.left_front_motor_speed,diff_data.left_rear_motor_speed, &motor_speed[0], &motor_speed[2],100, 5); - adjust_torque_by_speed_diff( diff_data.right_front_motor_speed,diff_data.right_rear_motor_speed, &motor_speed[1], &motor_speed[3],100, 5); - +// adjust_torque_by_speed_diff( diff_data.left_front_motor_speed,diff_data.left_rear_motor_speed, &motor_speed[0], &motor_speed[2],100, 5); +// adjust_torque_by_speed_diff( diff_data.right_front_motor_speed,diff_data.right_rear_motor_speed, &motor_speed[1], &motor_speed[3],100, 5); +// // printf("speed: FL=%.1f FR=%.1f RL=%.1f RR=%.1f\n", diff_data.left_front_motor_speed, diff_data.right_front_motor_speed, diff_data.left_rear_motor_speed, diff_data.right_rear_motor_speed); @@ -490,7 +550,11 @@ void handleVehicleState(DiffData *ctx) if (ctx->desired_speed < 0.0f) { ctx->state = STATE_BACKWARD; - } + } + else if( (ctx->desired_speed == 0.0f) && (ctx->desired_curvature != 0) )//无速度有转向 + { + ctx->state = STATE_STATIC_TURN; + } else { ctx->state = STATE_FORWARD; @@ -503,15 +567,19 @@ void handleVehicleState(DiffData *ctx) //------------------------------------------- case STATE_FORWARD: { - if ((ctx->desired_speed < 0.0f) && (ctx->speed == 0.0f)) + if( (ctx->desired_speed < 0.0f) && (ctx->speed == 0.0f) ) { ctx->state = STATE_BACKWARD; // 零速时允许切换方向 } - else if ((ctx->desired_speed < 0.0f) && (ctx->speed != 0.0f)) + else if( (ctx->desired_speed < 0.0f) && (ctx->speed != 0.0f) ) { ctx->desired_speed = 0.0f; // 非零速时清空期望速度 ctx->state = STATE_FORWARD; // 显式保持当前状态 } + else if( (ctx->desired_speed == 0.0f) && (ctx->speed == 0.0f) && (ctx->desired_curvature != 0.0f) ) + { + ctx->state = STATE_STATIC_TURN; // 原地转向 + } else { ctx->state = STATE_FORWARD; // 新增:其他情况保持前进状态 @@ -533,12 +601,38 @@ void handleVehicleState(DiffData *ctx) ctx->desired_speed = 0.0f; // 非零速时清空期望速度 ctx->state = STATE_BACKWARD; // 显式保持当前状态 } + else if( (ctx->desired_speed == 0.0f) && (ctx->speed == 0.0f) && (ctx->desired_curvature != 0.0f) ) + { + ctx->state = STATE_STATIC_TURN; // 原地转向 + } else { ctx->state = STATE_BACKWARD; // 新增:其他情况保持倒车状态 } break; } + + //------------------------------------------- + // 原地转向状态 + //------------------------------------------- + case STATE_STATIC_TURN: + { + if ((ctx->desired_speed > 0.0f) && (ctx->speed == 0.0f)) + { + ctx->state = STATE_FORWARD; // 零速时允许切换方向 + } + else if ((ctx->desired_speed < 0.0f) && (ctx->speed == 0.0f)) + { + ctx->state = STATE_BACKWARD; // 显式保持当前状态 + } + else + { + ctx->state = STATE_STATIC_TURN; // 原地转向 + } + break; + } + + default:; } } @@ -580,6 +674,8 @@ static void diffProcess(void *signal_id) float output_speed = calculatePidOutput(&speed_pid, diff_data.desired_speed, diff_data.speed, 0.0f, dt); float output_yaw_rate = calculatePidOutput(&yaw_rate_pid, diff_data.desired_yaw_rate, diff_data.yaw_rate, 0.0f, dt); + + // 计算最大加速度,用函数计算 float max_acceleration = calculateMaxAcceleration(); // 限制输出速度在当前速度和最大加速度计算出来的速度之间 @@ -707,6 +803,8 @@ static void diffInput(void *signal_id) diff_data.desired_speed = mapRemoteControlSpeed(diff_data.desired_speed, 0.1, 20, 5, 5, 0.5); diff_data.desired_curvature = mapRemoteControlSpeed(diff_data.desired_curvature, 0.1, 2, 2, 1, 0.5); + diff_data.desired_curvature = -diff_data.desired_curvature; + if(diff_data.desired_speed >= 0)//20250320 增加根据速度大小来决定方向,解决后退时转弯反向的问题 { diff_data.desired_curvature = diff_data.desired_curvature; @@ -743,6 +841,9 @@ static void diffInput(void *signal_id) { diff_data.left_front_motor_speed = (float)((int16_t)(un_motor_input1.bit_data.speed - 30000));//20240921 增加偏移量 diff_data.left_rear_motor_speed = (float)((int16_t)(un_motor_input3.bit_data.speed - 30000));//20240921 增加偏移量 + diff_data.left_rear_motor_speed = - diff_data.left_rear_motor_speed;//20250708 增加反相 + + if(fabs(diff_data.left_rear_motor_speed) > fabs(diff_data.left_front_motor_speed))//取速度较小的轮速 { @@ -759,6 +860,8 @@ static void diffInput(void *signal_id) diff_data.right_front_motor_speed = (float)((int16_t)(un_motor_input2.bit_data.speed - 30000)); // 20250502 1号控制器增加反相 diff_data.right_rear_motor_speed = (float)((int16_t)(un_motor_input4.bit_data.speed - 30000)); + diff_data.right_rear_motor_speed = - diff_data.right_rear_motor_speed;//20250708 增加反相 + if(fabs(diff_data.right_front_motor_speed) > fabs(diff_data.right_rear_motor_speed))//取速度较小的轮速 { motor_speed_temp = diff_data.right_rear_motor_speed; @@ -787,18 +890,18 @@ static void diffInput(void *signal_id) diff_data.desired_curvature = 0.0; } - if (diff_data.emergency_stop_state == 1)//刹车 20241017 增加的扭矩限制 - { - diff_data.max_Torq = 5;//20240403修改。刹车就是5N - } - else if ((0 == diff_data.desired_speed) && (0 == diff_data.desired_curvature) && (diff_data.left_motor_speed > -100) && (diff_data.left_motor_speed < 100)&& (((diff_data.right_motor_speed > -100) && (diff_data.right_motor_speed < 100))))//20240330只有当手柄回中,然后当前已经停止的状态才设置为最小停车扭矩 - { - diff_data.max_Torq = 5;//停车 就为0 20250425 修改为5,解决手柄回中,震荡问题 - } - else - { - diff_data.max_Torq = (uint16_t)getParam("maxTorq");//参数读取设定最大扭矩 - } +// if (diff_data.emergency_stop_state == 1)//刹车 20241017 增加的扭矩限制 +// { +// diff_data.max_Torq = 5;//20240403修改。刹车就是5N +// } +// else if ((0 == diff_data.desired_speed) && (0 == diff_data.desired_curvature) && (diff_data.left_motor_speed > -100) && (diff_data.left_motor_speed < 100)&& (((diff_data.right_motor_speed > -100) && (diff_data.right_motor_speed < 100))))//20240330只有当手柄回中,然后当前已经停止的状态才设置为最小停车扭矩 +// { +// diff_data.max_Torq = 5;//停车 就为0 20250425 修改为5,解决手柄回中,震荡问题 +// } +// else +// { +// diff_data.max_Torq = (uint16_t)getParam("maxTorq");//参数读取设定最大扭矩 +// } diffProcess(&diff_data);//计算左右电机期望转速 } @@ -814,8 +917,11 @@ void preChargeFinish(void *signal_id) setMotorOutput(out_torq, (uint16_t)getParam("maxTorq"), (uint16_t)getParam("feedPwr"), (uint16_t)getParam("dispPwr")); // 档位 -// un_motor_output1.bit_data.gear = 0; // 0表示空挡 -// un_motor_output2.bit_data.gear = 0; + un_motor_output1.bit_data.gear = 0; // 0表示空挡 + un_motor_output2.bit_data.gear = 0; + un_motor_output3.bit_data.gear = 0; // 0表示空挡 + un_motor_output4.bit_data.gear = 0; + publishMessage(&un_motor_output1, 1); publishMessage(&un_motor_output2, 1); publishMessage(&un_motor_output3, 1); @@ -866,15 +972,14 @@ void diffParametersInit(void *signal_id) diff_data.min_Torq = (uint16_t)getParam("minTorq");//参数读取设定最大扭矩 - // printf("desired_speed: %f, desired_yaw_rate: %f\n", diff_data.desired_speed, diff_data.desired_yaw_rate); - // printf("speed: %f, yaw_rate: %f\n", diff_data.speed, diff_data.yaw_rate); -// printf("speed: %f, yaw_rate: %f\n", diff_data.speed, diff_data.yaw_rate); + printf("desired_speed: %f, desired_yaw_rate: %f\n", diff_data.desired_speed, diff_data.desired_yaw_rate); + printf("speed: %f, yaw_rate: %f\n", diff_data.speed, diff_data.yaw_rate); +// printf("speed: %f, yaw_rate: %f\n", diff_data.speed, diff_data.yaw_rate); - // printf("left_motor_speed = %f\n",diff_data.left_motor_speed); - // printf("right_motor_speed = %f\n",diff_data.right_motor_speed); + printf("LF_speed = %f,RF_speed = %f,LR_speed = %f,RR_speed = %f\n",diff_data.left_front_motor_speed,diff_data.right_front_motor_speed,diff_data.left_rear_motor_speed,diff_data.right_rear_motor_speed); // printf("speed: FL=%.1f FR=%.1f RL=%.1f RR=%.1f\n", diff_data.left_front_motor_speed, diff_data.right_front_motor_speed, diff_data.left_rear_motor_speed, diff_data.right_rear_motor_speed); -// printf("torq: FL=%.1fNm FR=%.1fNm RL=%.1fNm RR=%.1fNm\n", diff_data.out_torq[0], diff_data.out_torq[1], diff_data.out_torq[2], diff_data.out_torq[3]); + printf("torq: FL=%.1fNm FR=%.1fNm RL=%.1fNm RR=%.1fNm\n", diff_data.out_torq[0], diff_data.out_torq[1], diff_data.out_torq[2], diff_data.out_torq[3]); float deffspeed = (float)((int16_t)(un_remote_control_input.bit_data.speed)); float deffcurvature = (float)((int16_t)(un_remote_control_input.bit_data.curvature)); @@ -882,7 +987,7 @@ void diffParametersInit(void *signal_id) deffspeed = deffspeed * 0.01f; deffcurvature = deffcurvature * 0.0001f; - printf("remote speed = %f, remote curvature = %f\n", deffspeed, deffcurvature); + printf(" car state = %d\n", diff_data.state); timerStart(&diff_app_timer,1000,1);//1s调用一次 } diff --git a/app/app_differential_drive.h b/app/app_differential_drive.h index 73041d5..9afb129 100644 --- a/app/app_differential_drive.h +++ b/app/app_differential_drive.h @@ -1,84 +1,85 @@ -#ifndef APP_DIFFERENTIAL_DRIVE_H -#define APP_DIFFERENTIAL_DRIVE_H - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include "app_config.h" - -#define SPEED_FITER_NUM 6 - - -#define SPEED_PID_MODE 0 -#define THROTTLE_PID_MODE 1 - - - -#define SPEED_MODE 0x01 -#define TORQUE_MODE 0x02 - -#define MOTOR_MODE TORQUE_MODE - - - -#define ALPHA 0.1f // 滤波系数α∈[0.01,0.3],0.2对应截止频率约10Hz(假设采样周期10ms) -#define LOWPASS_FILTER(speed, prev) (ALPHA * (speed) + (1 - ALPHA) * (prev)) - -// 状态机内部状态 -typedef enum -{ - STATE_INIT, ///< 初始状态(转速为0且等待扭矩方向判定) - STATE_FORWARD, ///< 正向旋转状态(扭矩为正) - STATE_BACKWARD, ///< 反向旋转状态(扭矩为负) -} MotorState; - -typedef enum -{ - MODE_MANUAL, // 手动模式 - MODE_AUTO // 自动模式 -} ControlMode; - -typedef struct DiffData -{ - ControlMode mode ; // 控制模式 - MotorState state; //当前状态机状态 - float desired_speed; // 期望速度 - float desired_curvature; // 期望曲率 - float left_motor_speed; // 当前左电机速度 - float right_motor_speed; // 当前右电机速度 - float left_front_motor_speed; // 当前左前电机速度 - float right_front_motor_speed; // 当前右前电机速度 - float left_rear_motor_speed; // 当前左后电机速度 - float right_rear_motor_speed; // 当前右后电机速度 - float speed; // 当前车速 - float curvature; // 当前曲率 - float yaw_rate; // 当前角速度 - float desired_yaw_rate; // 期望角速度 - float acceleration; // 当前加速度 - float deceleration; // 当前减速度 - float max_speed; // 最大速度 - float desired_acceleration; // 期望加速度 - float desired_deceleration; // 期望减速度 - uint8_t emergency_stop_switch; // 急停开关 - uint8_t remote_emergency_stop; // 遥控器急停开关 - uint8_t emergency_stop_state; // 急停状态 - float out_left_motor_speed; // 输出左电机速度 - float out_right_motor_speed; // 输出右电机速度 - float out_torq[4]; //4个电机扭矩 - float max_Torq; // 最大扭矩限制 - float min_Torq; // 最小扭矩限制 -} DiffData; - - -// 声明外部变量 -extern DiffData diff_data; - -void diffAppInit(void); - -#ifdef __cplusplus -} -#endif - -#endif // APP_DIFFERENTIAL_DRIVE_H +#ifndef APP_DIFFERENTIAL_DRIVE_H +#define APP_DIFFERENTIAL_DRIVE_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "app_config.h" + +#define SPEED_FITER_NUM 6 + + +#define SPEED_PID_MODE 0 +#define THROTTLE_PID_MODE 1 + + + +#define SPEED_MODE 0x01 +#define TORQUE_MODE 0x02 + +#define MOTOR_MODE TORQUE_MODE + + + +#define ALPHA 0.1f // 滤波系数α∈[0.01,0.3],0.2对应截止频率约10Hz(假设采样周期10ms) +#define LOWPASS_FILTER(speed, prev) (ALPHA * (speed) + (1 - ALPHA) * (prev)) + +// 状态机内部状态 +typedef enum +{ + STATE_INIT, ///< 初始状态(转速为0且等待扭矩方向判定) + STATE_FORWARD, ///< 前进 + STATE_BACKWARD, ///< 后退 + STATE_STATIC_TURN ///< 原地转向 +} MotorState; + +typedef enum +{ + MODE_MANUAL, // 手动模式 + MODE_AUTO // 自动模式 +} ControlMode; + +typedef struct DiffData +{ + ControlMode mode ; // 控制模式 + MotorState state; //当前车辆状态 + float desired_speed; // 期望速度 + float desired_curvature; // 期望曲率 + float left_motor_speed; // 当前左电机速度 + float right_motor_speed; // 当前右电机速度 + float left_front_motor_speed; // 当前左前电机速度 + float right_front_motor_speed; // 当前右前电机速度 + float left_rear_motor_speed; // 当前左后电机速度 + float right_rear_motor_speed; // 当前右后电机速度 + float speed; // 当前车速 + float curvature; // 当前曲率 + float yaw_rate; // 当前角速度 + float desired_yaw_rate; // 期望角速度 + float acceleration; // 当前加速度 + float deceleration; // 当前减速度 + float max_speed; // 最大速度 + float desired_acceleration; // 期望加速度 + float desired_deceleration; // 期望减速度 + uint8_t emergency_stop_switch; // 急停开关 + uint8_t remote_emergency_stop; // 遥控器急停开关 + uint8_t emergency_stop_state; // 急停状态 + float out_left_motor_speed; // 输出左电机速度 + float out_right_motor_speed; // 输出右电机速度 + float out_torq[4]; //4个电机扭矩 + float max_Torq; // 最大扭矩限制 + float min_Torq; // 最小扭矩限制 +} DiffData; + + +// 声明外部变量 +extern DiffData diff_data; + +void diffAppInit(void); + +#ifdef __cplusplus +} +#endif + +#endif // APP_DIFFERENTIAL_DRIVE_H diff --git a/interface_can.c b/interface_can.c index 4c23167..a356b85 100644 --- a/interface_can.c +++ b/interface_can.c @@ -1,1423 +1,1430 @@ - -#include -#include "debug.h" -#include "interface_config.h" -#include "app/app_config.h" -#define TX_PADDING_VAL (0xA5) -#include "sdrv_vic.h" -#include "intrinsics.h" -#include "app/app_differential_drive.h" -#include "app/app_param_manage.h" -#include "app/app_ultrasonic.h" - - - -uint32_t OTA_CANTxID = 0x02;//ĬϷIDΪ0x02 -uint32_t OTA_CANRxID = 0x01;//ĬϽIDΪ0x01 -uint32_t WDT_CANRxID = 0x03;//ĬϽIDΪ0x03 - - -Timer can_timer_interface1; -Timer can_timer_interface2; - -bool ecu_online = 0;//0ߣ1 - - -UnCanFault can_fault_info = { - .bit_data.navigator_count = 0, //Ǽ - .bit_data.motor1_count = 0, //ֶݼ - .bit_data.motor2_count = 0, //ֶݼ - .bit_data.bms_count = 0, //ֶݼ - .bit_data.temperature_count = 0, //¶ȼ - .bit_data.remote_count = 0, //ңؼ - - .bit_data.navigator_state = 0, //Զݼ - .bit_data.motor1_state = 0, //ֶݼ - .bit_data.motor2_state = 0, //ֶݼ - .bit_data.bms_state = 0, //ֶݼ - .bit_data.temperature_state = 0, //¶״̬ - .bit_data.remote_state = 0, //ң״̬ -}; - - - -// -//StrCanFifoQueue can0_Queue;//8֡can棬Ͷ -//StrCanFifoQueue can1_Queue; -//StrCanFifoQueue can2_Queue; -//StrCanFifoQueue can3_Queue; -//StrCanFifoQueue can4_Queue; -//StrCanFifoQueue can5_Queue; -//StrCanFifoQueue can6_Queue; -//StrCanFifoQueue can7_Queue; - - -//bool busoff_occur_flag = false;//busoff־ - -bool can_0_busoff_flag = false;//busoff־ -bool can_1_busoff_flag = false;//busoff־ -bool can_2_busoff_flag = false;//busoff־ -bool can_3_busoff_flag = false;//busoff־ -bool can_4_busoff_flag = false;//busoff־ -bool can_5_busoff_flag = false;//busoff־ -bool can_6_busoff_flag = false;//busoff־ -bool can_7_busoff_flag = false;//busoff־ - - -flexcan_handle_t can_handle_0;//canʼṹ -flexcan_handle_t can_handle_1; -flexcan_handle_t can_handle_2; -flexcan_handle_t can_handle_3; -flexcan_handle_t can_handle_4; -flexcan_handle_t can_handle_5; -flexcan_handle_t can_handle_6; -flexcan_handle_t can_handle_7; - - - -static uint8_t fifo_Data_0[8]; -static flexcan_frame_t fifo_Buf_0; - -static uint8_t fifo_Data_1[8]; -static flexcan_frame_t fifo_Buf_1; - -static uint8_t fifo_Data_2[8]; -static flexcan_frame_t fifo_Buf_2; - -static uint8_t fifo_Data_3[8]; -static flexcan_frame_t fifo_Buf_3; - -static uint8_t fifo_Data_4[8]; -static flexcan_frame_t fifo_Buf_4; - -static uint8_t fifo_Data_5[8]; -static flexcan_frame_t fifo_Buf_5; - -static uint8_t fifo_Data_6[8]; -static flexcan_frame_t fifo_Buf_6; - -static uint8_t fifo_Data_7[8]; -static flexcan_frame_t fifo_Buf_7; - - -uint8_t flexcan_config_rx_fifo(flexcan_handle_t *handle, - flexcan_rx_fifo_config_t *config) -{ - flexcan_frame_t *rx_fifo_frame; -//------------------------------------------------------------------------------ - if(CAN_INDEX_0 == (handle->controller_id))//ѡͬжϺԼַ - { - rx_fifo_frame = &fifo_Buf_0; - rx_fifo_frame->dataBuffer = &fifo_Data_0[0]; - } - else if(CAN_INDEX_1 == (handle->controller_id)) - { - rx_fifo_frame = &fifo_Buf_1; - rx_fifo_frame->dataBuffer = &fifo_Data_1[0]; - } - else if(CAN_INDEX_2 == (handle->controller_id)) - { - rx_fifo_frame = &fifo_Buf_2; - rx_fifo_frame->dataBuffer = &fifo_Data_2[0]; - } - else if(CAN_INDEX_3 == (handle->controller_id)) - { - rx_fifo_frame = &fifo_Buf_3; - rx_fifo_frame->dataBuffer = &fifo_Data_3[0]; - } - else if(CAN_INDEX_4 == (handle->controller_id)) - { - rx_fifo_frame = &fifo_Buf_4; - rx_fifo_frame->dataBuffer = &fifo_Data_4[0]; - } - else if(CAN_INDEX_5 == (handle->controller_id)) - { - rx_fifo_frame = &fifo_Buf_5; - rx_fifo_frame->dataBuffer = &fifo_Data_5[0]; - } - else if(CAN_INDEX_6 == (handle->controller_id)) - { - rx_fifo_frame = &fifo_Buf_6; - rx_fifo_frame->dataBuffer = &fifo_Data_6[0]; - } - else if(CAN_INDEX_7 == (handle->controller_id)) - { - rx_fifo_frame = &fifo_Buf_7; - rx_fifo_frame->dataBuffer = &fifo_Data_7[0]; - } - else - { - return 0; - } - - flexcan_set_rx_fifo_config(handle, config);//fifo - - for (uint8_t i = 0; i < USED_MB_FOR_FIFO; - i++) { - flexcan_set_rx_individual_mask(handle, i, config->filter_tab[i].filter_mask); - } - - /* rx by interrupt */ - flexcan_fifo_transfer_t Xfer = {rx_fifo_frame}; - flexcan_receive_fifo_nonblocking(handle, &Xfer); - - return 1; -} - - -/********************************************************************* - * @fn CAN_Send_Msg - * - * @brief CAN Transmit function. - * - * @param CANx- CAN1 or CAN2 - * ID - Message ID - * IDE - CAN_Id_Standard or CAN_Id_Extended - * RTR - CAN_RTR_Data or CAN_RTR_Remote - * msg - Transmit data buffer. - * len - Data length. - * - * @return 0 - Send successful. - * 1 - Send failed. - */ -uint8_t CAN_Send_Msg( flexcan_handle_t *handle, uint32_t ID, flexcan_frame_format_e ide, flexcan_frame_type_e rtr, uint8_t *msg, uint8_t len, uint8_t tx_index) -{ - uint16_t i = 0; -//------------------------------------------------------------------------------ - - flexcan_frame_t frame = {.id = ID, - .length = len, - .type = rtr, - .format = ide, - .dataBuffer = msg}; - flexcan_mb_transfer_t xfer = {&frame, tx_index}; - - while( ( flexcan_get_mb_state(handle, tx_index) != FLEXCAN_StateIdle ) && (i < 0xFFF) ) - { - i++; - } - - if( i == 0xFFF ) - { - return 1; - - } - else - { - flexcan_send_nonblocking(handle, &xfer, TX_PADDING_VAL); - return 0; - } -} - - - -/********************************************************************* - * @fn flexcan_Receive_callback - * - * @brief erase Data-Flash block, minimal block is 256B - * - * @param Page_Address - the address of the page being erased. - * Length - Erased data length - * - * @return none - */ -void flexcan_Receive_callback_0(flexcan_handle_t *handle, - flexcan_status_e status, uint32_t result, - void *userData) -{ - flexcan_frame_t *buf = (flexcan_frame_t *)userData; - uint8_t i = 0; -//-------------------------------------------------------------- - switch (status) - { - case FLEXCAN_RX_IDLE: - break; - - case FLEXCAN_RX_FIFO_IDLE: - - if(TEMP_MODULE_INPUT_ID_1 == (buf->id))//¶ - { - for(i = 0; i < (buf->length); i++) - { - un_sw_sample.arr[i] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_sw_sample, 1);//ź - } - break; - - case FLEXCAN_TX_IDLE: - break; - - case FLEXCAN_WAKEUP_TIMEOUT: - break; - - case FLEXCAN_WAKEUP_MATCH: - break; - - case FLEXCAN_ERROR_STATUS: - can_0_busoff_flag = true; - break; - - default: - break; - } -} - - - - - - -/********************************************************************* - * @fn flexcan_Receive_callback - * - * @brief erase Data-Flash block, minimal block is 256B - * - * @param Page_Address - the address of the page being erased. - * Length - Erased data length - * - * @return none - */ - -//uint32_t time_elapsed = 0; -//uint32_t time_elapsed1 = 0; -void flexcan_Receive_callback_1(flexcan_handle_t *handle, - flexcan_status_e status, uint32_t result, - void *userData) -{ - flexcan_frame_t *buf = (flexcan_frame_t *)userData; - - uint8_t i = 0; -//-------------------------------------------------------------- - switch (status) - { - case FLEXCAN_RX_IDLE: - break; - - case FLEXCAN_RX_FIFO_IDLE: - if(LEFT_FRONT_MOTOR_INPUT1 == (buf->id)) - { - can_fault_info.bit_data.motor1_count ++; - for(i = 0; i < (buf->length); i++) - { - un_motor_input1.arr[i] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_motor_input1, 1); - } - else if( LEFT_FRONT_MOTOR_INPUT2 == (buf->id) ) - { - for(i = 0; i < (buf->length); i++) - { - un_motor_temp1.arr[i] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_motor_temp1, 1);//ź - } - else if( LEFT_REAR_MOTOR2_INPUT1 == (buf->id) )// - { - can_fault_info.bit_data.motor3_count ++; - for(i = 0; i < (buf->length); i++) - { - un_motor_input3.arr[i] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_motor_input3, 1); - } - else if( LEFT_REAR_MOTOR2_INPUT2 == (buf->id) ) - { - for(i = 0; i < (buf->length); i++) - { - un_motor_temp3.arr[i] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_motor_temp3, 1);//ź - } - else{} - break; - - case FLEXCAN_TX_IDLE: - break; - - case FLEXCAN_WAKEUP_TIMEOUT: - break; - - case FLEXCAN_WAKEUP_MATCH: - break; - - case FLEXCAN_ERROR_STATUS: - can_1_busoff_flag = true; - break; - - default: - break; - } -} - - - -/********************************************************************* - * @fn flexcan_Receive_callback - * - * @brief erase Data-Flash block, minimal block is 256B - * - * @param Page_Address - the address of the page being erased. - * Length - Erased data length - * - * @return none - */ -void flexcan_Receive_callback_2(flexcan_handle_t *handle, - flexcan_status_e status, uint32_t result, - void *userData) -{ - flexcan_frame_t *buf = (flexcan_frame_t *)userData; - uint8_t i = 0; -//-------------------------------------------------------------- - switch (status) - { - case FLEXCAN_RX_IDLE: - break; - - case FLEXCAN_RX_FIFO_IDLE: - if(RIGHT_FRONT_MOTOR_INPUT1 == (buf->id)) - { - can_fault_info.bit_data.motor2_count ++; - for(i = 0; i < (buf->length); i++) - { - un_motor_input2.arr[i] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_motor_input2, 1); - } - else if( RIGHT_FRONT_MOTOR_INPUT2 == (buf->id) ) - { - for(i = 0; i < (buf->length); i++) - { - un_motor_temp2.arr[i] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_motor_temp2, 1);//ź - } - else if( RIGHT_REAR_MOTOR_INPUT1 == (buf->id) )// - { - can_fault_info.bit_data.motor4_count ++; - for(i = 0; i < (buf->length); i++) - { - un_motor_input4.arr[i] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_motor_input4, 1); - } - else if( RIGHT_REAR_MOTOR_INPUT2 == (buf->id) ) - { - for(i = 0; i < (buf->length); i++) - { - un_motor_temp4.arr[i] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_motor_temp4, 1);//ź - } - else{} - break; - - case FLEXCAN_TX_IDLE: - break; - - case FLEXCAN_WAKEUP_TIMEOUT: - break; - - case FLEXCAN_WAKEUP_MATCH: - break; - - case FLEXCAN_ERROR_STATUS: - can_2_busoff_flag = true; - break; - - default: - break; - } -} - - - -/********************************************************************* - * @fn flexcan_Receive_callback - * - * @brief erase Data-Flash block, minimal block is 256B - * - * @param Page_Address - the address of the page being erased. - * Length - Erased data length - * - * @return none - */ -void flexcan_Receive_callback_3(flexcan_handle_t *handle, - flexcan_status_e status, uint32_t result, - void *userData) -{ - flexcan_frame_t *buf = (flexcan_frame_t *)userData; - uint8_t i = 0; -//-------------------------------------------------------------- - switch (status) - { - case FLEXCAN_RX_IDLE: - break; - - case FLEXCAN_RX_FIFO_IDLE: - - if(BMS_INPUT_ID1 == (buf->id))//BMS - { - can_fault_info.bit_data.bms_count ++; - for(i = 0; i < (buf->length); i++) - { - un_bms_input.arr[i] = buf->dataBuffer[i]; - } - publishMessage(&un_bms_input, 1); - } - else if(BMS_INPUT_ID2 == (buf->id))//BMS - { - for(i = 0; i < (buf->length); i++) - { - un_bms_input.arr[i+8] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_bms_input, 1); - } - else{} - - break; - - case FLEXCAN_TX_IDLE: - break; - - case FLEXCAN_WAKEUP_TIMEOUT: - break; - - case FLEXCAN_WAKEUP_MATCH: - break; - - case FLEXCAN_ERROR_STATUS: - can_3_busoff_flag = true; - break; - - default: - break; - } -} - -/********************************************************************* - * @fn flexcan_Receive_callback - * - * @brief erase Data-Flash block, minimal block is 256B - * - * @param Page_Address - the address of the page being erased. - * Length - Erased data length - * - * @return none - */ -void flexcan_Receive_callback_4(flexcan_handle_t *handle, - flexcan_status_e status, uint32_t result, - void *userData) -{ -//-------------------------------------------------------------- - switch (status) - { - case FLEXCAN_RX_IDLE: - break; - - case FLEXCAN_RX_FIFO_IDLE: - - break; - - case FLEXCAN_TX_IDLE: - break; - - case FLEXCAN_WAKEUP_TIMEOUT: - break; - - case FLEXCAN_WAKEUP_MATCH: - break; - - case FLEXCAN_ERROR_STATUS: - can_4_busoff_flag = true; - break; - - default: - break; - } -} - -/********************************************************************* - * @fn flexcan_Receive_callback - * - * @brief erase Data-Flash block, minimal block is 256B - * - * @param Page_Address - the address of the page being erased. - * Length - Erased data length - * - * @return none - */ -void flexcan_Receive_callback_5(flexcan_handle_t *handle, - flexcan_status_e status, uint32_t result, - void *userData) -{ -//-------------------------------------------------------------- - switch (status) - { - case FLEXCAN_RX_IDLE: - break; - - case FLEXCAN_RX_FIFO_IDLE: - - break; - - case FLEXCAN_TX_IDLE: - break; - - case FLEXCAN_WAKEUP_TIMEOUT: - break; - - case FLEXCAN_WAKEUP_MATCH: - break; - - case FLEXCAN_ERROR_STATUS: - can_5_busoff_flag = true; - break; - - default: - break; - } -} - -/********************************************************************* - * @fn flexcan_Receive_callback - * - * @brief erase Data-Flash block, minimal block is 256B - * - * @param Page_Address - the address of the page being erased. - * Length - Erased data length - * - * @return none - */ -void flexcan_Receive_callback_6(flexcan_handle_t *handle, - flexcan_status_e status, uint32_t result, - void *userData) -{ - flexcan_frame_t *buf = (flexcan_frame_t *)userData; - uint8_t i = 0; -//-------------------------------------------------------------- - switch (status) - { - case FLEXCAN_RX_IDLE: - break; - - case FLEXCAN_RX_FIFO_IDLE: - if(OTA_CANRxID == (buf->id))//IDΪ1 boot - { - boot_can_flag = true; - FrameHeader = ( (buf->dataBuffer[0] << 8) | (buf->dataBuffer[1]) ); - } - else if(WDT_CANRxID == (buf->id))//յιID - { - if(FEEDDOG_HEADER == ( (buf->dataBuffer[0] << 8) | (buf->dataBuffer[1]) )) - { - WDTReFresh_flag = true;//ι־ - printf("Feed dog flag received %d\n",getCurrentTime()); - } - } - else if(ULTRASONIC_ID_1 == (buf->id)) - { - for(i = 0; i < (buf->length); i++) - { - un_ultrasonic_input1.arr[i] = buf->dataBuffer[i]; - } - publishMessage(&un_ultrasonic_input1, 1); - } - else{} - break; - - case FLEXCAN_TX_IDLE: - break; - - case FLEXCAN_WAKEUP_TIMEOUT: - break; - - case FLEXCAN_WAKEUP_MATCH: - break; - - case FLEXCAN_ERROR_STATUS: - can_6_busoff_flag = true; - break; - - default: - break; - } -} - - - -/********************************************************************* - * @fn flexcan_Receive_callback - * - * @brief erase Data-Flash block, minimal block is 256B - * - * @param Page_Address - the address of the page being erased. - * Length - Erased data length - * - * @return none - */ -void flexcan_Receive_callback_7(flexcan_handle_t *handle, - flexcan_status_e status, uint32_t result, - void *userData) -{ - flexcan_frame_t *buf = (flexcan_frame_t *)userData; - uint8_t i = 0; - //uintptr_t -//-------------------------------------------------------------- - switch (status) - { - case FLEXCAN_RX_IDLE: - break; - - case FLEXCAN_RX_FIFO_IDLE: - if(REMOTE_ID == (buf->id))//ңCAN - { - can_fault_info.bit_data.remote_count ++; - for(i = 0; i < (buf->length); i++) - { - un_remote_control_input.arr[i] = buf->dataBuffer[i]; - } - //ź - publishMessage(&un_remote_control_input, 1); - -// printf("ori_remote_stop: %d\n", un_remote_control_input.bit_data.switch_b); - } - else - { - } - break; - - case FLEXCAN_TX_IDLE: - break; - - case FLEXCAN_WAKEUP_TIMEOUT: - break; - - case FLEXCAN_WAKEUP_MATCH: - break; - - case FLEXCAN_ERROR_STATUS: - can_7_busoff_flag = true; - break; - - default: - break; - } -} - - -//fifo_config ˲ -//can_config Լ -//call_back жϻص -uint8_t initialization_Flexcan(uint8_t can_index, const flexcan_config_t *can_config, flexcan_rx_fifo_config_t *fifo_config, flexcan_transfer_callback_t call_back) -{ - flexcan_handle_t *handle; - uint8_t can_irq_num = 0; - uint32_t can_reg_base = 0; -//------------------------------------------------------------------ - if(FLEXCAN7 == can_index)//ѡͬжϺԼַ - { - handle = &can_handle_0; - can_irq_num = CANFD7_CANFD_INTR_NUM; - can_reg_base = APB_CANFD7_BASE; - } - else if(FLEXCAN21 == can_index) - { - handle = &can_handle_1; - can_irq_num = CANFD21_CANFD_INTR_NUM; - can_reg_base = APB_CANFD21_BASE; - } - else if(FLEXCAN16 == can_index) - { - handle = &can_handle_2; - can_irq_num = CANFD16_CANFD_INTR_NUM; - can_reg_base = APB_CANFD16_BASE; - } - else if(FLEXCAN3 == can_index) - { - handle = &can_handle_3; - can_irq_num = CANFD3_CANFD_INTR_NUM; - can_reg_base = APB_CANFD3_BASE; - } - else if(FLEXCAN23 == can_index) - { - handle = &can_handle_4; - can_irq_num = CANFD23_CANFD_INTR_NUM; - can_reg_base = APB_CANFD23_BASE; - } - else if(FLEXCAN5 == can_index) - { - handle = &can_handle_5; - can_irq_num = CANFD5_CANFD_INTR_NUM; - can_reg_base = APB_CANFD5_BASE; - } - else if(FLEXCAN6 == can_index) - { - handle = &can_handle_6; - can_irq_num = CANFD6_CANFD_INTR_NUM; - can_reg_base = APB_CANFD6_BASE; - } - else if(FLEXCAN4 == can_index) - { - handle = &can_handle_7; - can_irq_num = CANFD4_CANFD_INTR_NUM; - can_reg_base = APB_CANFD4_BASE; - } - else - { - return 0; - } - - //initialize the FlexCAN handle - flexcan_create_handle(handle, can_index, (void *)can_reg_base, can_irq_num, call_back, NULL); - - /* initialize flexCAN. */ - flexcan_init(handle, can_config); - - /* unfreeze the flexCAN by demo. */ - flexcan_freeze(handle, false); - - /* enable busoff interrupt. */ - flexcan_enable_interrupts(handle, FLEXCAN_BusOffInterruptEnable);// - - /* enable irq. */ - irq_attach(handle->irq_num, flexcan_irq_handler, handle); - irq_enable(handle->irq_num); - - /* assign fifo only for rx. */ - flexcan_config_rx_fifo(handle, fifo_config); - - /* assign mailbox14-20 for tx. */ - for (uint8_t i = TX_MB_INDEX; i < (TX_MB_INDEX + TX_MAILBOX_NUM); i++) - { - flexcan_set_tx_mb_config(handle, i, true); - } - - return 1; -} - -/* reinitialize the controller. */ -void flexcan_Busoff_Recovery(void) -{ -//---------------------------------------------------------- - if (can_0_busoff_flag == true) //busoff - { - printf("can0 busoff !\n"); - flexcan_deinit(&can_handle_0); - - initialization_Flexcan(CAN_INDEX_0, &can_0_config, &can_0_fifo_config, flexcan_Receive_callback_0); - - can_0_busoff_flag = false; - } - - if (can_1_busoff_flag == true) //busoff - { - printf("can1 busoff !\n"); - flexcan_deinit(&can_handle_1); - - initialization_Flexcan(CAN_INDEX_1, &can_1_config, &can_1_fifo_config, flexcan_Receive_callback_1); - - can_1_busoff_flag = false; - } - - if (can_2_busoff_flag == true) //busoff - { - printf("can2 busoff !\n"); - flexcan_deinit(&can_handle_2); - - initialization_Flexcan(CAN_INDEX_2, &can_2_config, &can_2_fifo_config, flexcan_Receive_callback_2); - - can_2_busoff_flag = false; - } - - if (can_3_busoff_flag == true) //busoff - { - printf("can3 busoff !\n"); - flexcan_deinit(&can_handle_3); - - initialization_Flexcan(CAN_INDEX_3, &can_3_config, &can_3_fifo_config, flexcan_Receive_callback_3); - - can_3_busoff_flag = false; - } - - if (can_4_busoff_flag == true) //busoff - { - printf("can4 busoff !\n"); - flexcan_deinit(&can_handle_4); - - initialization_Flexcan(CAN_INDEX_4, &can_4_config, &can_4_fifo_config, flexcan_Receive_callback_4); - - can_4_busoff_flag = false; - } - - if (can_5_busoff_flag == true) //busoff - { - printf("can5 busoff !\n"); - flexcan_deinit(&can_handle_5); - - initialization_Flexcan(CAN_INDEX_5, &can_5_config, &can_5_fifo_config, flexcan_Receive_callback_5); - - can_5_busoff_flag = false; - } - - if (can_6_busoff_flag == true) //busoff - { - printf("can6 busoff !\n"); - flexcan_deinit(&can_handle_6); - - initialization_Flexcan(CAN_INDEX_6, &can_6_config, &can_6_fifo_config, flexcan_Receive_callback_6); - - can_6_busoff_flag = false; - } - - if (can_7_busoff_flag == true) //busoff - { - printf("can7 busoff !\n"); - flexcan_deinit(&can_handle_7); - - initialization_Flexcan(CAN_INDEX_7, &can_7_config, &can_7_fifo_config, flexcan_Receive_callback_7); - - can_7_busoff_flag = false; - } -} - - - -/* initialization_all_flexcan. */ -void initialization_All_Flexcan(void) -{ - sdrv_gpio_set_pin_output_level(GPIO_H12, 0); //CANʹ - - initialization_Flexcan(CAN_INDEX_0, &can_0_config, &can_0_fifo_config, flexcan_Receive_callback_0);//250k - - initialization_Flexcan(CAN_INDEX_1, &can_1_config, &can_1_fifo_config, flexcan_Receive_callback_1);//250k - - initialization_Flexcan(CAN_INDEX_2, &can_2_config, &can_2_fifo_config, flexcan_Receive_callback_2);//250k - - initialization_Flexcan(CAN_INDEX_3, &can_3_config, &can_3_fifo_config, flexcan_Receive_callback_3);//1M - - initialization_Flexcan(CAN_INDEX_4, &can_4_config, &can_4_fifo_config, flexcan_Receive_callback_4);//250k - - initialization_Flexcan(CAN_INDEX_5, &can_5_config, &can_5_fifo_config, flexcan_Receive_callback_5);//250k - - initialization_Flexcan(CAN_INDEX_6, &can_6_config, &can_6_fifo_config, flexcan_Receive_callback_6);//1M - - initialization_Flexcan(CAN_INDEX_7, &can_7_config, &can_7_fifo_config, flexcan_Receive_callback_7);//250k -} - - - - - - -//100ms һ -void canTimerProcess(void *signal_id) -{ - static uint8_t can_timer[5] = {0,0,0,0,0}; - static uint8_t can_temp[10] = {0,0,0,0,0,0,0,0,0,0};//мжֵ - uint8_t temp = 0; - - can_timer[0] ++; - if(can_timer[0] >= 1)//100msжһ - { - can_timer[0] = 0; - - if(can_fault_info.bit_data.motor1_count == can_temp[0])//һʾ - { - can_fault_info.bit_data.motor1_state = FAULT; - } - else - { - can_fault_info.bit_data.motor1_state = NORMAL; - can_temp[0] = can_fault_info.bit_data.motor1_count;//ݸ - } - - if(can_fault_info.bit_data.motor1_state != can_temp[1]) - { - can_temp[1] = can_fault_info.bit_data.motor1_state; - temp ++; - } - - - if(can_fault_info.bit_data.motor2_count == can_temp[2])//һʾ - { - can_fault_info.bit_data.motor2_state = FAULT; - } - else - { - can_fault_info.bit_data.motor2_state = NORMAL; - can_temp[2] = can_fault_info.bit_data.motor2_count;//ݸ - } - - if(can_fault_info.bit_data.motor2_state != can_temp[3]) - { - can_temp[3] = can_fault_info.bit_data.motor2_state; - temp ++; - } - } - - can_timer[1] ++;//BMSж - if(can_timer[1] >= 20)//2sжһ - { - can_timer[1] = 0; - //BMS - if(can_fault_info.bit_data.bms_count == can_temp[4])//һʾ - { - can_fault_info.bit_data.bms_state = FAULT; - } - else - { - can_fault_info.bit_data.bms_state = NORMAL; - can_temp[4] = can_fault_info.bit_data.bms_count;//ݸ - } - if(can_fault_info.bit_data.bms_state != can_temp[5]) - { - can_temp[5] = can_fault_info.bit_data.bms_state; - temp ++; - } - - //¶ģ - if(can_fault_info.bit_data.temperature_count == can_temp[6])//һʾ - { - can_fault_info.bit_data.temperature_state = FAULT; - } - else - { - can_fault_info.bit_data.temperature_state = NORMAL; - can_temp[6] = can_fault_info.bit_data.temperature_count;//ݸ - } - if(can_fault_info.bit_data.temperature_state != can_temp[7]) - { - can_temp[7] = can_fault_info.bit_data.temperature_state; - temp ++; - } - } - - can_timer[2] ++;//ңж - if(can_timer[2] >= 5)//500msжһ - { - can_timer[2] = 0; - //ң - if(can_fault_info.bit_data.remote_count == can_temp[8])//һʾ - { - can_fault_info.bit_data.remote_state = FAULT; - } - else - { - can_fault_info.bit_data.remote_state = NORMAL; - can_temp[8] = can_fault_info.bit_data.remote_count;//ݸ - } - if(can_fault_info.bit_data.remote_state != can_temp[9]) - { - can_temp[9] = can_fault_info.bit_data.remote_state; - temp ++; - } - } - - can_timer[4] ++;//ڷź - if( (0 != temp) || (can_timer[4] >= 5) )//500źţ仯 - { - can_timer[4] = 0; - publishMessage(&can_fault_info, 1);// - } - - //busoffж - can_timer[3] ++; - if(can_timer[3] >= 10)//100*100*10 = 1sһ - { - can_timer[3] = 0; - - flexcan_Busoff_Recovery();//BUSS OFF - } -} - - -//static void processSdoOutput1(void *signal_id) -//{ -// (void)signal_id; // DZΪʹã -// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output1, 8, 15);// -//} -// -//static void processSdoOutput2(void *signal_id) -//{ -// (void)signal_id; // DZΪʹã -// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output2, 8, 16); -//} -// -//static void processSdoOutput3(void *signal_id) -//{ -// (void)signal_id; // DZΪʹã -// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output3, 8, 17); -//} -// -//static void processSdoOutput4(void *signal_id) -//{ -// (void)signal_id; // DZΪʹã -// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output4, 8, 18); -//} -// -//static void processSdoOutput5(void *signal_id) -//{ -// (void)signal_id; // DZΪʹã -// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output5, 8, 19); -//} -// -// -//static void processSdoOutput7(void *signal_id) -//{ -// (void)signal_id; // DZΪʹã -// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output7, 8, 20); -//} - - - -static void processMotorOutput1(void *signal_id) -{ - (void)signal_id; // DZΪʹã - CAN_Send_Msg(&can_handle_1, LEFT_FRONT_MOTOR_OUTPUT1, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output1, 8, 15);//1Ťغת - CAN_Send_Msg(&can_handle_1, LEFT_REAR_MOTOR_OUTPUT1, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output3, 8, 16);//1Ťغת -} - -static void processMotorOutput2(void *signal_id) -{ - (void)signal_id; // DZΪʹã - CAN_Send_Msg(&can_handle_2, RIGHT_FRONT_MOTOR_OUTPUT1, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output2, 8, 15);//2Ťغת - CAN_Send_Msg(&can_handle_2, RIGHT_REAR_MOTOR_OUTPUT1, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output4, 8, 16);//2Ťغת -} - -static void processMotorOutput3(void *signal_id) -{ - (void)signal_id; // DZΪʹã - - CAN_Send_Msg(&can_handle_1, LEFT_FRONT_MOTOR_OUTPUT2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output1.arr[8], 8, 17);//1 - CAN_Send_Msg(&can_handle_1, LEFT_REAR_MOTOR_OUTPUT2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output3.arr[8], 8, 17);//1 - -} - -static void processMotorOutput4(void *signal_id) -{ - (void)signal_id; // DZΪʹã - CAN_Send_Msg(&can_handle_2, RIGHT_FRONT_MOTOR_OUTPUT2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output2.arr[8], 8, 17);//2 - CAN_Send_Msg(&can_handle_2, RIGHT_FRONT_MOTOR_OUTPUT2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output4.arr[8], 8, 17);//2 -} - -static void processKgfOutput1(void *signal_id) -{ - (void)signal_id; // DZΪʹã - CAN_Send_Msg(&can_handle_0, 0x11000002, FLEXCAN_EXTEND_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_inf_can_kgf_output1, 8, 15);//KGF1 -} - -static void processKgfOutput2(void *signal_id) -{ - (void)signal_id; // DZΪʹã - if(1 == ecu_online)//20250318 ޸Ϊ߲ܷͣɼģE3ͬʱ - { - un_inf_can_kgf_output2.bit_data.can_rx5 = 0xE3; - CAN_Send_Msg(&can_handle_0, 0x14000002, FLEXCAN_EXTEND_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_inf_can_kgf_output2, 8, 16);//KGF2 - } - -} - -//static void processKgfOutput2(void *signal_id) -//{ -// (void)signal_id; // DZΪʹã -// if(1 == ecu_online)//20250318 ޸Ϊ߲ܷͣɼģE3ͬʱ -// { -// un_inf_can_kgf_output2.bit_data.can_rx5 = 0xE3; -// CAN_Send_Msg(&can_handle_0, 0x14000002, FLEXCAN_EXTEND_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_inf_can_kgf_output2, 8, 16);//KGF2 -// } -// -//} - - - -static void processWheelSpeedOutput(void *signal_id) -{ - float CANPressSpeedTemp = 0.0;//ٶȼмֵ - (void)signal_id; // DZΪʹã - - CANPressSpeedTemp = (diff_data.left_motor_speed*(float)getParam("whl_dia") * M_PI*60)/1000.0/(float)getParam("gRatio");// ת/ӡkm/h һȦ1.8 20241016 Լٱ - CANPressSpeedTemp = CANPressSpeedTemp*100;//,Ҫ100 ,ϵ0.01 - un_wheel_wpeed_output.bit_data.left_front = (uint16_t)((int16_t)(CANPressSpeedTemp));//ת*1.8*60*100/1000/ ת/ӡkm/h һȦ1.8,Ҫ100ϵΪ0.01תΪ޷ͣ 20240629 ٱȲҪѾ - un_wheel_wpeed_output.bit_data.left_rear = (uint16_t)((int16_t)(CANPressSpeedTemp));//תΪintתΪ޷ - - CANPressSpeedTemp = (diff_data.right_motor_speed*(float)getParam("whl_dia") * M_PI*60)/1000.0/(float)getParam("gRatio");// ת/ӡkm/h һȦ1.8 - CANPressSpeedTemp = CANPressSpeedTemp*100;//,Ҫ100 ,ϵ0.01 - un_wheel_wpeed_output.bit_data.right_front = (uint16_t)((int16_t)(CANPressSpeedTemp));//ת*1.8*60*100/1000/ ת/ӡkm/h һȦ1.8,Ҫ100ϵΪ0.01תΪ޷ͣ 20240629 ٱȲҪѾ - un_wheel_wpeed_output.bit_data.right_rear = (uint16_t)((int16_t)(CANPressSpeedTemp));//תΪintתΪ޷ - - CAN_Send_Msg(&can_handle_4, 0x98, FLEXCAN_EXTEND_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_wheel_wpeed_output, 8, 15);// -} - -static void processHBridgeOutput(void *signal_id) -{ - (void)signal_id; // DZΪʹã - CAN_Send_Msg(&can_handle_5, 0x7F2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_h_bridge_output, 8, 17);//H - CAN_Send_Msg(&can_handle_5, 0x722, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_h_bridge_output1, 8, 19);//H -} - -static void processUnGatherOutput(void *signal_id) -{ - (void)signal_id; // DZΪʹã - CAN_Send_Msg(&can_handle_0, 0x15000002, FLEXCAN_EXTEND_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_gather_output, 8, 18);//ɼ -} - - - -static void processUltrasonicOutput(void *signal_id) -{ - (void)signal_id; // DZΪʹã - CAN_Send_Msg(&can_handle_6, ULTRASONIC_ID_1, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_ultrasonic_output1, 8, 15);// -} - -//static void processHBridgeOutput2(void *signal_id) -//{ -// (void)signal_id; // DZΪʹã -// CAN_Send_Msg(&can_handle_5, 0x722, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_h_bridge_output2, 8, 16);//H -//} - - -//static void processLifterOutput(void *signal_id) -//{ -// (void)signal_id; // DZΪʹã -// CAN_Send_Msg(&can_handle_5, 0x6F2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_lifter_output, 8, 17);//Ƹ -//} - - - - -//can 100msһ -void canSendAll(void *signal_id) -{ - static uint16_t wheel_speed_cnt = 0; -// static uint8_t kgf_cnt = 0; - static uint16_t bms_cnt1 = 0; - static uint16_t bms_cnt2 = 0; -// static uint16_t motor_speed_cnt = 0; - static uint16_t motor_power_cnt = 0; -// static uint8_t h_bridge_cnt = 0; - uint8_t CanData[8] = {0,0,0,0,0,0,0,0}; -//------------------------------------------------------------------------- - (void)signal_id; // DZΪʹã - - bms_cnt1 ++; - if(bms_cnt1 >= 1000)//1s - { - bms_cnt1 = 0; - - CanData[0] = 0x5A; - - CAN_Send_Msg(&can_handle_3, 0x100, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 1, 15);//BMS - } - - bms_cnt2 ++; - if(bms_cnt2 >= 1100)//1s - { - bms_cnt2 = 0; - - CanData[0] = 0x5A; - - CAN_Send_Msg(&can_handle_3, 0x101, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 1, 16);//BMS - } - - - motor_power_cnt ++; - if(motor_power_cnt >= 1000)//1sһ - { - motor_power_cnt = 0; - processMotorOutput3(CanData); - processMotorOutput4(CanData); - } - -// motor_power_cnt ++; -// if(motor_power_cnt >= 10)//ȡת 20ms -// { -// motor_power_cnt = 0; -// -// un_motor_output1.bit_data.MotCon_1Signal6 = 0x84; -// un_motor_output1.bit_data.MotCon_1Signal7 = 0x02; -// un_motor_output2.bit_data.MotCon_1Signal6 = 0x84; -// un_motor_output2.bit_data.MotCon_1Signal7 = 0x02; -// -// processMotorOutput3(CanData); -// processMotorOutput4(CanData); -// } - -// kgf_cnt ++; -// if(kgf_cnt >= 5)//20210312 CANͨѶģ 20210710޸Ϊ50ms -// { -// kgf_cnt = 0; -// -// processKgfOutput1(CanData); -// processKgfOutput2(CanData); -// } -// - //ٷ - wheel_speed_cnt ++; - if(wheel_speed_cnt >= 100) - { - wheel_speed_cnt = 0; - - processWheelSpeedOutput(CanData); - -// CAN_Send_Msg(&can_handle_0, 0x120, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 16);//BMS -// CAN_Send_Msg(&can_handle_1, 0x121, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 17);//BMS -// CAN_Send_Msg(&can_handle_2, 0x122, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 17);//BMS -// CAN_Send_Msg(&can_handle_3, 0x123, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 16);//BMS -// CAN_Send_Msg(&can_handle_4, 0x124, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 16);//BMS -// CAN_Send_Msg(&can_handle_5, 0x125, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 16);//BMS - CAN_Send_Msg(&can_handle_6, 0x126, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, &un_can_debug_output.arr[0], 8, 16);//BMS -// CAN_Send_Msg(&can_handle_7, 0x127, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 16);//BMS - } -// - - //Hݷ -// h_bridge_cnt ++; -// if(h_bridge_cnt >= 5) -// { -// h_bridge_cnt = 0; -// -// processHBridgeOutput(CanData); -// } - - timerStart(&can_timer_interface1,1,1);//1msһ -} - - - - -// canʱźŴ -static void canInterfaceTimerProcess(void *signal_id) -{ - (void)signal_id; // DZΪʹã -// uint32_t time_boot = getCurrentTime();//¼ǰʱ - - canTimerProcess(signal_id); - -// printf("motor1 interval:%d,motor2 interval:%d\n",time_elapsed,time_elapsed1);//ӡ12ռ - - timerStart(&can_timer_interface2,100,1);//100msһ - -// printf("canInterface spend time:%d\n",getCurrentTime() - time_boot);//app˶೤ʱ -} - -static void canInterfaceInput(void *signal_id) -{ - if (signal_id == &un_sw_sample) - { - ecu_online = un_sw_sample.bit_data.flg_wake_ecu; - } -} - - - -// APPģijʼ -void canInterfaceInit(void) -{ - memset(&un_motor_input1, 0, sizeof(UnMotorInput)); - memset(&un_motor_input2, 0, sizeof(UnMotorInput)); - memset(&un_bms_input , 0, sizeof(UnBmsInput)); - memset(&un_temp_module_input, 0, sizeof(UnTempModuleInput)); - - memset(&un_motor_output1, 0, sizeof(UnMotorOutput)); - memset(&un_motor_output2, 0, sizeof(UnMotorOutput)); - -// un_motor_output1.bit_data.mode = 0x05; -// un_motor_output1.bit_data.gear = 0x01; -// un_motor_output1.bit_data.can_gear = 0x01; -// un_motor_output1.bit_data.motor_direction = 0x01; -// un_motor_output1.bit_data.control_data1 = 0x0; -// un_motor_output1.bit_data.control_data2 = 0x0; -// -// un_motor_output2.bit_data.mode = 0x05; -// un_motor_output2.bit_data.gear = 0x01; -// un_motor_output2.bit_data.can_gear = 0x01; -// un_motor_output2.bit_data.motor_direction = 0x00; -// un_motor_output2.bit_data.control_data1 = 0x0; -// un_motor_output2.bit_data.control_data2 = 0x0; -// -// un_motor_output3.bit_data.mode = 0x05; -// un_motor_output3.bit_data.gear = 0x01; -// un_motor_output3.bit_data.can_gear = 0x01; -// un_motor_output3.bit_data.motor_direction = 0x01; -// un_motor_output3.bit_data.control_data1 = 0x0; -// un_motor_output3.bit_data.control_data2 = 0x0; -// -// un_motor_output4.bit_data.mode = 0x05; -// un_motor_output4.bit_data.gear = 0x01; -// un_motor_output4.bit_data.can_gear = 0x01; -// un_motor_output4.bit_data.motor_direction = 0x00; -// un_motor_output4.bit_data.control_data1 = 0x0; -// un_motor_output4.bit_data.control_data2 = 0x0; - - - -// un_motor_output1.bit_data.set_torque = 30000;//ƫʼ -// un_motor_output1.bit_data.set_rotation_speed = 30000; -// un_motor_output1.bit_data.mode = 0; -// un_motor_output2.bit_data.set_torque = 30000; -// un_motor_output2.bit_data.set_rotation_speed = 30000; -// un_motor_output2.bit_data.mode = 0; -// can_fault_info.bit_data.navigator_state = 1; - - memset(&un_inf_can_kgf_output1, 0, sizeof(UnInfCanKGFOutput)); - memset(&un_inf_can_kgf_output2, 0, sizeof(UnInfCanKGFOutput)); - memset(&un_h_bridge_output, 0, sizeof(UnHBridgeOutput)); - memset(&un_wheel_wpeed_output, 0, sizeof(UnWheelSpeedOutput)); - memset(&un_remote_control_input, 0, sizeof(UnRemoteControlInput)); - - un_inf_can_kgf_output2.bit_data.KGF04 = 1;//ɱǿԼϵͨ - - - // ʼʱʹ brake_timer ĵַΪźID - timerInit(&can_timer_interface1); - timerInit(&can_timer_interface2); - - subscribe(&can_timer_interface1, canSendAll); - subscribe(&can_timer_interface2, canInterfaceTimerProcess); - subscribe(&un_motor_output1, processMotorOutput1); - subscribe(&un_motor_output2, processMotorOutput2); - subscribe(&un_inf_can_kgf_output1, processKgfOutput1); - subscribe(&un_inf_can_kgf_output2, processKgfOutput2); - subscribe(&un_wheel_wpeed_output, processWheelSpeedOutput); - subscribe(&un_h_bridge_output, processHBridgeOutput); - subscribe(&un_gather_output, processUnGatherOutput); - subscribe(&un_sw_sample, canInterfaceInput); // ͣءѹ ѱ־ - subscribe(&un_ultrasonic_output1, processUltrasonicOutput); // - - - - - - - - -// subscribe(&un_h_bridge_output2, processHBridgeOutput2); -// subscribe(&un_lifter_output, processLifterOutput); - -// subscribe(&un_sdo_output1, processSdoOutput1); -// subscribe(&un_sdo_output2, processSdoOutput2); -// subscribe(&un_sdo_output3, processSdoOutput3); -// subscribe(&un_sdo_output4, processSdoOutput4); -// subscribe(&un_sdo_output5, processSdoOutput5); -// subscribe(&un_sdo_output7, processSdoOutput7); - - timerStart(&can_timer_interface1,1,1);//1msһ - - timerStart(&can_timer_interface2,100,1);//100msһ - - printf( "canInterface: initial OK %d\n",getCurrentTime()); -} - - - + +#include +#include "debug.h" +#include "interface_config.h" +#include "app/app_config.h" +#define TX_PADDING_VAL (0xA5) +#include "sdrv_vic.h" +#include "intrinsics.h" +#include "app/app_differential_drive.h" +#include "app/app_param_manage.h" +#include "app/app_ultrasonic.h" + + + +uint32_t OTA_CANTxID = 0x02;//ĬϷIDΪ0x02 +uint32_t OTA_CANRxID = 0x01;//ĬϽIDΪ0x01 +uint32_t WDT_CANRxID = 0x03;//ĬϽIDΪ0x03 + + +Timer can_timer_interface1; +Timer can_timer_interface2; + +bool ecu_online = 0;//0ߣ1 + + +UnCanFault can_fault_info = { + .bit_data.navigator_count = 0, //Ǽ + .bit_data.motor1_count = 0, //ֶݼ + .bit_data.motor2_count = 0, //ֶݼ + .bit_data.bms_count = 0, //ֶݼ + .bit_data.temperature_count = 0, //¶ȼ + .bit_data.remote_count = 0, //ңؼ + + .bit_data.navigator_state = 0, //Զݼ + .bit_data.motor1_state = 0, //ֶݼ + .bit_data.motor2_state = 0, //ֶݼ + .bit_data.bms_state = 0, //ֶݼ + .bit_data.temperature_state = 0, //¶״̬ + .bit_data.remote_state = 0, //ң״̬ +}; + + + +// +//StrCanFifoQueue can0_Queue;//8֡can棬Ͷ +//StrCanFifoQueue can1_Queue; +//StrCanFifoQueue can2_Queue; +//StrCanFifoQueue can3_Queue; +//StrCanFifoQueue can4_Queue; +//StrCanFifoQueue can5_Queue; +//StrCanFifoQueue can6_Queue; +//StrCanFifoQueue can7_Queue; + + +//bool busoff_occur_flag = false;//busoff־ + +bool can_0_busoff_flag = false;//busoff־ +bool can_1_busoff_flag = false;//busoff־ +bool can_2_busoff_flag = false;//busoff־ +bool can_3_busoff_flag = false;//busoff־ +bool can_4_busoff_flag = false;//busoff־ +bool can_5_busoff_flag = false;//busoff־ +bool can_6_busoff_flag = false;//busoff־ +bool can_7_busoff_flag = false;//busoff־ + + +flexcan_handle_t can_handle_0;//canʼṹ +flexcan_handle_t can_handle_1; +flexcan_handle_t can_handle_2; +flexcan_handle_t can_handle_3; +flexcan_handle_t can_handle_4; +flexcan_handle_t can_handle_5; +flexcan_handle_t can_handle_6; +flexcan_handle_t can_handle_7; + + + +static uint8_t fifo_Data_0[8]; +static flexcan_frame_t fifo_Buf_0; + +static uint8_t fifo_Data_1[8]; +static flexcan_frame_t fifo_Buf_1; + +static uint8_t fifo_Data_2[8]; +static flexcan_frame_t fifo_Buf_2; + +static uint8_t fifo_Data_3[8]; +static flexcan_frame_t fifo_Buf_3; + +static uint8_t fifo_Data_4[8]; +static flexcan_frame_t fifo_Buf_4; + +static uint8_t fifo_Data_5[8]; +static flexcan_frame_t fifo_Buf_5; + +static uint8_t fifo_Data_6[8]; +static flexcan_frame_t fifo_Buf_6; + +static uint8_t fifo_Data_7[8]; +static flexcan_frame_t fifo_Buf_7; + + +uint8_t flexcan_config_rx_fifo(flexcan_handle_t *handle, + flexcan_rx_fifo_config_t *config) +{ + flexcan_frame_t *rx_fifo_frame; +//------------------------------------------------------------------------------ + if(CAN_INDEX_0 == (handle->controller_id))//ѡͬжϺԼַ + { + rx_fifo_frame = &fifo_Buf_0; + rx_fifo_frame->dataBuffer = &fifo_Data_0[0]; + } + else if(CAN_INDEX_1 == (handle->controller_id)) + { + rx_fifo_frame = &fifo_Buf_1; + rx_fifo_frame->dataBuffer = &fifo_Data_1[0]; + } + else if(CAN_INDEX_2 == (handle->controller_id)) + { + rx_fifo_frame = &fifo_Buf_2; + rx_fifo_frame->dataBuffer = &fifo_Data_2[0]; + } + else if(CAN_INDEX_3 == (handle->controller_id)) + { + rx_fifo_frame = &fifo_Buf_3; + rx_fifo_frame->dataBuffer = &fifo_Data_3[0]; + } + else if(CAN_INDEX_4 == (handle->controller_id)) + { + rx_fifo_frame = &fifo_Buf_4; + rx_fifo_frame->dataBuffer = &fifo_Data_4[0]; + } + else if(CAN_INDEX_5 == (handle->controller_id)) + { + rx_fifo_frame = &fifo_Buf_5; + rx_fifo_frame->dataBuffer = &fifo_Data_5[0]; + } + else if(CAN_INDEX_6 == (handle->controller_id)) + { + rx_fifo_frame = &fifo_Buf_6; + rx_fifo_frame->dataBuffer = &fifo_Data_6[0]; + } + else if(CAN_INDEX_7 == (handle->controller_id)) + { + rx_fifo_frame = &fifo_Buf_7; + rx_fifo_frame->dataBuffer = &fifo_Data_7[0]; + } + else + { + return 0; + } + + flexcan_set_rx_fifo_config(handle, config);//fifo + + for (uint8_t i = 0; i < USED_MB_FOR_FIFO; + i++) { + flexcan_set_rx_individual_mask(handle, i, config->filter_tab[i].filter_mask); + } + + /* rx by interrupt */ + flexcan_fifo_transfer_t Xfer = {rx_fifo_frame}; + flexcan_receive_fifo_nonblocking(handle, &Xfer); + + return 1; +} + + +/********************************************************************* + * @fn CAN_Send_Msg + * + * @brief CAN Transmit function. + * + * @param CANx- CAN1 or CAN2 + * ID - Message ID + * IDE - CAN_Id_Standard or CAN_Id_Extended + * RTR - CAN_RTR_Data or CAN_RTR_Remote + * msg - Transmit data buffer. + * len - Data length. + * + * @return 0 - Send successful. + * 1 - Send failed. + */ +uint8_t CAN_Send_Msg( flexcan_handle_t *handle, uint32_t ID, flexcan_frame_format_e ide, flexcan_frame_type_e rtr, uint8_t *msg, uint8_t len, uint8_t tx_index) +{ + uint16_t i = 0; +//------------------------------------------------------------------------------ + + flexcan_frame_t frame = {.id = ID, + .length = len, + .type = rtr, + .format = ide, + .dataBuffer = msg}; + flexcan_mb_transfer_t xfer = {&frame, tx_index}; + + while( ( flexcan_get_mb_state(handle, tx_index) != FLEXCAN_StateIdle ) && (i < 0xFFF) ) + { + i++; + } + + if( i == 0xFFF ) + { + return 1; + + } + else + { + flexcan_send_nonblocking(handle, &xfer, TX_PADDING_VAL); + return 0; + } +} + + + +/********************************************************************* + * @fn flexcan_Receive_callback + * + * @brief erase Data-Flash block, minimal block is 256B + * + * @param Page_Address - the address of the page being erased. + * Length - Erased data length + * + * @return none + */ +void flexcan_Receive_callback_0(flexcan_handle_t *handle, + flexcan_status_e status, uint32_t result, + void *userData) +{ + flexcan_frame_t *buf = (flexcan_frame_t *)userData; + uint8_t i = 0; +//-------------------------------------------------------------- + switch (status) + { + case FLEXCAN_RX_IDLE: + break; + + case FLEXCAN_RX_FIFO_IDLE: + + if(TEMP_MODULE_INPUT_ID_1 == (buf->id))//¶ + { + for(i = 0; i < (buf->length); i++) + { + un_sw_sample.arr[i] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_sw_sample, 1);//ź + } + break; + + case FLEXCAN_TX_IDLE: + break; + + case FLEXCAN_WAKEUP_TIMEOUT: + break; + + case FLEXCAN_WAKEUP_MATCH: + break; + + case FLEXCAN_ERROR_STATUS: + can_0_busoff_flag = true; + break; + + default: + break; + } +} + + + + + + +/********************************************************************* + * @fn flexcan_Receive_callback + * + * @brief erase Data-Flash block, minimal block is 256B + * + * @param Page_Address - the address of the page being erased. + * Length - Erased data length + * + * @return none + */ + +//uint32_t time_elapsed = 0; +//uint32_t time_elapsed1 = 0; +void flexcan_Receive_callback_1(flexcan_handle_t *handle, + flexcan_status_e status, uint32_t result, + void *userData) +{ + flexcan_frame_t *buf = (flexcan_frame_t *)userData; + + uint8_t i = 0; +//-------------------------------------------------------------- + switch (status) + { + case FLEXCAN_RX_IDLE: + break; + + case FLEXCAN_RX_FIFO_IDLE: + if(LEFT_FRONT_MOTOR_INPUT1 == (buf->id)) + { + can_fault_info.bit_data.motor1_count ++; + for(i = 0; i < (buf->length); i++) + { + un_motor_input1.arr[i] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_motor_input1, 1); + } + else if( LEFT_FRONT_MOTOR_INPUT2 == (buf->id) ) + { + for(i = 0; i < (buf->length); i++) + { + un_motor_temp1.arr[i] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_motor_temp1, 1);//ź + } + else if( LEFT_REAR_MOTOR2_INPUT1 == (buf->id) )// + { + can_fault_info.bit_data.motor3_count ++; + for(i = 0; i < (buf->length); i++) + { + un_motor_input3.arr[i] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_motor_input3, 1); + } + else if( LEFT_REAR_MOTOR2_INPUT2 == (buf->id) ) + { + for(i = 0; i < (buf->length); i++) + { + un_motor_temp3.arr[i] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_motor_temp3, 1);//ź + } + else{} + break; + + case FLEXCAN_TX_IDLE: + break; + + case FLEXCAN_WAKEUP_TIMEOUT: + break; + + case FLEXCAN_WAKEUP_MATCH: + break; + + case FLEXCAN_ERROR_STATUS: + can_1_busoff_flag = true; + break; + + default: + break; + } +} + + + +/********************************************************************* + * @fn flexcan_Receive_callback + * + * @brief erase Data-Flash block, minimal block is 256B + * + * @param Page_Address - the address of the page being erased. + * Length - Erased data length + * + * @return none + */ +void flexcan_Receive_callback_2(flexcan_handle_t *handle, + flexcan_status_e status, uint32_t result, + void *userData) +{ + flexcan_frame_t *buf = (flexcan_frame_t *)userData; + uint8_t i = 0; +//-------------------------------------------------------------- + switch (status) + { + case FLEXCAN_RX_IDLE: + break; + + case FLEXCAN_RX_FIFO_IDLE: + if(RIGHT_FRONT_MOTOR_INPUT1 == (buf->id)) + { + can_fault_info.bit_data.motor2_count ++; + for(i = 0; i < (buf->length); i++) + { + un_motor_input2.arr[i] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_motor_input2, 1); + } + else if( RIGHT_FRONT_MOTOR_INPUT2 == (buf->id) ) + { + for(i = 0; i < (buf->length); i++) + { + un_motor_temp2.arr[i] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_motor_temp2, 1);//ź + } + else if( RIGHT_REAR_MOTOR_INPUT1 == (buf->id) )// + { + can_fault_info.bit_data.motor4_count ++; + for(i = 0; i < (buf->length); i++) + { + un_motor_input4.arr[i] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_motor_input4, 1); + } + else if( RIGHT_REAR_MOTOR_INPUT2 == (buf->id) ) + { + for(i = 0; i < (buf->length); i++) + { + un_motor_temp4.arr[i] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_motor_temp4, 1);//ź + } + else{} + break; + + case FLEXCAN_TX_IDLE: + break; + + case FLEXCAN_WAKEUP_TIMEOUT: + break; + + case FLEXCAN_WAKEUP_MATCH: + break; + + case FLEXCAN_ERROR_STATUS: + can_2_busoff_flag = true; + break; + + default: + break; + } +} + + + +/********************************************************************* + * @fn flexcan_Receive_callback + * + * @brief erase Data-Flash block, minimal block is 256B + * + * @param Page_Address - the address of the page being erased. + * Length - Erased data length + * + * @return none + */ +void flexcan_Receive_callback_3(flexcan_handle_t *handle, + flexcan_status_e status, uint32_t result, + void *userData) +{ + flexcan_frame_t *buf = (flexcan_frame_t *)userData; + uint8_t i = 0; +//-------------------------------------------------------------- + switch (status) + { + case FLEXCAN_RX_IDLE: + break; + + case FLEXCAN_RX_FIFO_IDLE: + + if(BMS_INPUT_ID1 == (buf->id))//BMS + { + can_fault_info.bit_data.bms_count ++; + for(i = 0; i < (buf->length); i++) + { + un_bms_input.arr[i] = buf->dataBuffer[i]; + } + publishMessage(&un_bms_input, 1); + } + else if(BMS_INPUT_ID2 == (buf->id))//BMS + { + for(i = 0; i < (buf->length); i++) + { + un_bms_input.arr[i+8] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_bms_input, 1); + } + else{} + + break; + + case FLEXCAN_TX_IDLE: + break; + + case FLEXCAN_WAKEUP_TIMEOUT: + break; + + case FLEXCAN_WAKEUP_MATCH: + break; + + case FLEXCAN_ERROR_STATUS: + can_3_busoff_flag = true; + break; + + default: + break; + } +} + +/********************************************************************* + * @fn flexcan_Receive_callback + * + * @brief erase Data-Flash block, minimal block is 256B + * + * @param Page_Address - the address of the page being erased. + * Length - Erased data length + * + * @return none + */ +void flexcan_Receive_callback_4(flexcan_handle_t *handle, + flexcan_status_e status, uint32_t result, + void *userData) +{ +//-------------------------------------------------------------- + switch (status) + { + case FLEXCAN_RX_IDLE: + break; + + case FLEXCAN_RX_FIFO_IDLE: + + break; + + case FLEXCAN_TX_IDLE: + break; + + case FLEXCAN_WAKEUP_TIMEOUT: + break; + + case FLEXCAN_WAKEUP_MATCH: + break; + + case FLEXCAN_ERROR_STATUS: + can_4_busoff_flag = true; + break; + + default: + break; + } +} + +/********************************************************************* + * @fn flexcan_Receive_callback + * + * @brief erase Data-Flash block, minimal block is 256B + * + * @param Page_Address - the address of the page being erased. + * Length - Erased data length + * + * @return none + */ +void flexcan_Receive_callback_5(flexcan_handle_t *handle, + flexcan_status_e status, uint32_t result, + void *userData) +{ +//-------------------------------------------------------------- + switch (status) + { + case FLEXCAN_RX_IDLE: + break; + + case FLEXCAN_RX_FIFO_IDLE: + + break; + + case FLEXCAN_TX_IDLE: + break; + + case FLEXCAN_WAKEUP_TIMEOUT: + break; + + case FLEXCAN_WAKEUP_MATCH: + break; + + case FLEXCAN_ERROR_STATUS: + can_5_busoff_flag = true; + break; + + default: + break; + } +} + +/********************************************************************* + * @fn flexcan_Receive_callback + * + * @brief erase Data-Flash block, minimal block is 256B + * + * @param Page_Address - the address of the page being erased. + * Length - Erased data length + * + * @return none + */ +void flexcan_Receive_callback_6(flexcan_handle_t *handle, + flexcan_status_e status, uint32_t result, + void *userData) +{ + flexcan_frame_t *buf = (flexcan_frame_t *)userData; + uint8_t i = 0; +//-------------------------------------------------------------- + switch (status) + { + case FLEXCAN_RX_IDLE: + break; + + case FLEXCAN_RX_FIFO_IDLE: + if(OTA_CANRxID == (buf->id))//IDΪ1 boot + { + boot_can_flag = true; + FrameHeader = ( (buf->dataBuffer[0] << 8) | (buf->dataBuffer[1]) ); + } + else if(WDT_CANRxID == (buf->id))//յιID + { + if(FEEDDOG_HEADER == ( (buf->dataBuffer[0] << 8) | (buf->dataBuffer[1]) )) + { + WDTReFresh_flag = true;//ι־ + printf("Feed dog flag received %d\n",getCurrentTime()); + } + } + else if(ULTRASONIC_ID_1 == (buf->id)) + { + for(i = 0; i < (buf->length); i++) + { + un_ultrasonic_input1.arr[i] = buf->dataBuffer[i]; + } + publishMessage(&un_ultrasonic_input1, 1); + } + else{} + break; + + case FLEXCAN_TX_IDLE: + break; + + case FLEXCAN_WAKEUP_TIMEOUT: + break; + + case FLEXCAN_WAKEUP_MATCH: + break; + + case FLEXCAN_ERROR_STATUS: + can_6_busoff_flag = true; + break; + + default: + break; + } +} + + + +/********************************************************************* + * @fn flexcan_Receive_callback + * + * @brief erase Data-Flash block, minimal block is 256B + * + * @param Page_Address - the address of the page being erased. + * Length - Erased data length + * + * @return none + */ +void flexcan_Receive_callback_7(flexcan_handle_t *handle, + flexcan_status_e status, uint32_t result, + void *userData) +{ + flexcan_frame_t *buf = (flexcan_frame_t *)userData; + uint8_t i = 0; + //uintptr_t +//-------------------------------------------------------------- + switch (status) + { + case FLEXCAN_RX_IDLE: + break; + + case FLEXCAN_RX_FIFO_IDLE: + if(REMOTE_ID == (buf->id))//ңCAN + { + can_fault_info.bit_data.remote_count ++; + for(i = 0; i < (buf->length); i++) + { + un_remote_control_input.arr[i] = buf->dataBuffer[i]; + } + //ź + publishMessage(&un_remote_control_input, 1); + +// printf("ori_remote_stop: %d\n", un_remote_control_input.bit_data.switch_b); + } + else + { + } + break; + + case FLEXCAN_TX_IDLE: + break; + + case FLEXCAN_WAKEUP_TIMEOUT: + break; + + case FLEXCAN_WAKEUP_MATCH: + break; + + case FLEXCAN_ERROR_STATUS: + can_7_busoff_flag = true; + break; + + default: + break; + } +} + + +//fifo_config ˲ +//can_config Լ +//call_back жϻص +uint8_t initialization_Flexcan(uint8_t can_index, const flexcan_config_t *can_config, flexcan_rx_fifo_config_t *fifo_config, flexcan_transfer_callback_t call_back) +{ + flexcan_handle_t *handle; + uint8_t can_irq_num = 0; + uint32_t can_reg_base = 0; +//------------------------------------------------------------------ + if(FLEXCAN7 == can_index)//ѡͬжϺԼַ + { + handle = &can_handle_0; + can_irq_num = CANFD7_CANFD_INTR_NUM; + can_reg_base = APB_CANFD7_BASE; + } + else if(FLEXCAN21 == can_index) + { + handle = &can_handle_1; + can_irq_num = CANFD21_CANFD_INTR_NUM; + can_reg_base = APB_CANFD21_BASE; + } + else if(FLEXCAN16 == can_index) + { + handle = &can_handle_2; + can_irq_num = CANFD16_CANFD_INTR_NUM; + can_reg_base = APB_CANFD16_BASE; + } + else if(FLEXCAN3 == can_index) + { + handle = &can_handle_3; + can_irq_num = CANFD3_CANFD_INTR_NUM; + can_reg_base = APB_CANFD3_BASE; + } + else if(FLEXCAN23 == can_index) + { + handle = &can_handle_4; + can_irq_num = CANFD23_CANFD_INTR_NUM; + can_reg_base = APB_CANFD23_BASE; + } + else if(FLEXCAN5 == can_index) + { + handle = &can_handle_5; + can_irq_num = CANFD5_CANFD_INTR_NUM; + can_reg_base = APB_CANFD5_BASE; + } + else if(FLEXCAN6 == can_index) + { + handle = &can_handle_6; + can_irq_num = CANFD6_CANFD_INTR_NUM; + can_reg_base = APB_CANFD6_BASE; + } + else if(FLEXCAN4 == can_index) + { + handle = &can_handle_7; + can_irq_num = CANFD4_CANFD_INTR_NUM; + can_reg_base = APB_CANFD4_BASE; + } + else + { + return 0; + } + + //initialize the FlexCAN handle + flexcan_create_handle(handle, can_index, (void *)can_reg_base, can_irq_num, call_back, NULL); + + /* initialize flexCAN. */ + flexcan_init(handle, can_config); + + /* unfreeze the flexCAN by demo. */ + flexcan_freeze(handle, false); + + /* enable busoff interrupt. */ + flexcan_enable_interrupts(handle, FLEXCAN_BusOffInterruptEnable);// + + /* enable irq. */ + irq_attach(handle->irq_num, flexcan_irq_handler, handle); + irq_enable(handle->irq_num); + + /* assign fifo only for rx. */ + flexcan_config_rx_fifo(handle, fifo_config); + + /* assign mailbox14-20 for tx. */ + for (uint8_t i = TX_MB_INDEX; i < (TX_MB_INDEX + TX_MAILBOX_NUM); i++) + { + flexcan_set_tx_mb_config(handle, i, true); + } + + return 1; +} + +/* reinitialize the controller. */ +void flexcan_Busoff_Recovery(void) +{ +//---------------------------------------------------------- + if (can_0_busoff_flag == true) //busoff + { + printf("can0 busoff !\n"); + flexcan_deinit(&can_handle_0); + + initialization_Flexcan(CAN_INDEX_0, &can_0_config, &can_0_fifo_config, flexcan_Receive_callback_0); + + can_0_busoff_flag = false; + } + + if (can_1_busoff_flag == true) //busoff + { + printf("can1 busoff !\n"); + flexcan_deinit(&can_handle_1); + + initialization_Flexcan(CAN_INDEX_1, &can_1_config, &can_1_fifo_config, flexcan_Receive_callback_1); + + can_1_busoff_flag = false; + } + + if (can_2_busoff_flag == true) //busoff + { + printf("can2 busoff !\n"); + flexcan_deinit(&can_handle_2); + + initialization_Flexcan(CAN_INDEX_2, &can_2_config, &can_2_fifo_config, flexcan_Receive_callback_2); + + can_2_busoff_flag = false; + } + + if (can_3_busoff_flag == true) //busoff + { + printf("can3 busoff !\n"); + flexcan_deinit(&can_handle_3); + + initialization_Flexcan(CAN_INDEX_3, &can_3_config, &can_3_fifo_config, flexcan_Receive_callback_3); + + can_3_busoff_flag = false; + } + + if (can_4_busoff_flag == true) //busoff + { + printf("can4 busoff !\n"); + flexcan_deinit(&can_handle_4); + + initialization_Flexcan(CAN_INDEX_4, &can_4_config, &can_4_fifo_config, flexcan_Receive_callback_4); + + can_4_busoff_flag = false; + } + + if (can_5_busoff_flag == true) //busoff + { + printf("can5 busoff !\n"); + flexcan_deinit(&can_handle_5); + + initialization_Flexcan(CAN_INDEX_5, &can_5_config, &can_5_fifo_config, flexcan_Receive_callback_5); + + can_5_busoff_flag = false; + } + + if (can_6_busoff_flag == true) //busoff + { + printf("can6 busoff !\n"); + flexcan_deinit(&can_handle_6); + + initialization_Flexcan(CAN_INDEX_6, &can_6_config, &can_6_fifo_config, flexcan_Receive_callback_6); + + can_6_busoff_flag = false; + } + + if (can_7_busoff_flag == true) //busoff + { + printf("can7 busoff !\n"); + flexcan_deinit(&can_handle_7); + + initialization_Flexcan(CAN_INDEX_7, &can_7_config, &can_7_fifo_config, flexcan_Receive_callback_7); + + can_7_busoff_flag = false; + } +} + + + +/* initialization_all_flexcan. */ +void initialization_All_Flexcan(void) +{ + sdrv_gpio_set_pin_output_level(GPIO_H12, 0); //CANʹ + + initialization_Flexcan(CAN_INDEX_0, &can_0_config, &can_0_fifo_config, flexcan_Receive_callback_0);//250k + + initialization_Flexcan(CAN_INDEX_1, &can_1_config, &can_1_fifo_config, flexcan_Receive_callback_1);//250k + + initialization_Flexcan(CAN_INDEX_2, &can_2_config, &can_2_fifo_config, flexcan_Receive_callback_2);//250k + + initialization_Flexcan(CAN_INDEX_3, &can_3_config, &can_3_fifo_config, flexcan_Receive_callback_3);//1M + + initialization_Flexcan(CAN_INDEX_4, &can_4_config, &can_4_fifo_config, flexcan_Receive_callback_4);//250k + + initialization_Flexcan(CAN_INDEX_5, &can_5_config, &can_5_fifo_config, flexcan_Receive_callback_5);//250k + + initialization_Flexcan(CAN_INDEX_6, &can_6_config, &can_6_fifo_config, flexcan_Receive_callback_6);//1M + + initialization_Flexcan(CAN_INDEX_7, &can_7_config, &can_7_fifo_config, flexcan_Receive_callback_7);//250k +} + + + + + + +//100ms һ +void canTimerProcess(void *signal_id) +{ + static uint8_t can_timer[5] = {0,0,0,0,0}; + static uint8_t can_temp[10] = {0,0,0,0,0,0,0,0,0,0};//мжֵ + uint8_t temp = 0; + + can_timer[0] ++; + if(can_timer[0] >= 1)//100msжһ + { + can_timer[0] = 0; + + if(can_fault_info.bit_data.motor1_count == can_temp[0])//һʾ + { + can_fault_info.bit_data.motor1_state = FAULT; + } + else + { + can_fault_info.bit_data.motor1_state = NORMAL; + can_temp[0] = can_fault_info.bit_data.motor1_count;//ݸ + } + + if(can_fault_info.bit_data.motor1_state != can_temp[1]) + { + can_temp[1] = can_fault_info.bit_data.motor1_state; + temp ++; + } + + + if(can_fault_info.bit_data.motor2_count == can_temp[2])//һʾ + { + can_fault_info.bit_data.motor2_state = FAULT; + } + else + { + can_fault_info.bit_data.motor2_state = NORMAL; + can_temp[2] = can_fault_info.bit_data.motor2_count;//ݸ + } + + if(can_fault_info.bit_data.motor2_state != can_temp[3]) + { + can_temp[3] = can_fault_info.bit_data.motor2_state; + temp ++; + } + } + + can_timer[1] ++;//BMSж + if(can_timer[1] >= 20)//2sжһ + { + can_timer[1] = 0; + //BMS + if(can_fault_info.bit_data.bms_count == can_temp[4])//һʾ + { + can_fault_info.bit_data.bms_state = FAULT; + } + else + { + can_fault_info.bit_data.bms_state = NORMAL; + can_temp[4] = can_fault_info.bit_data.bms_count;//ݸ + } + if(can_fault_info.bit_data.bms_state != can_temp[5]) + { + can_temp[5] = can_fault_info.bit_data.bms_state; + temp ++; + } + + //¶ģ + if(can_fault_info.bit_data.temperature_count == can_temp[6])//һʾ + { + can_fault_info.bit_data.temperature_state = FAULT; + } + else + { + can_fault_info.bit_data.temperature_state = NORMAL; + can_temp[6] = can_fault_info.bit_data.temperature_count;//ݸ + } + if(can_fault_info.bit_data.temperature_state != can_temp[7]) + { + can_temp[7] = can_fault_info.bit_data.temperature_state; + temp ++; + } + } + + can_timer[2] ++;//ңж + if(can_timer[2] >= 5)//500msжһ + { + can_timer[2] = 0; + //ң + if(can_fault_info.bit_data.remote_count == can_temp[8])//һʾ + { + can_fault_info.bit_data.remote_state = FAULT; + } + else + { + can_fault_info.bit_data.remote_state = NORMAL; + can_temp[8] = can_fault_info.bit_data.remote_count;//ݸ + } + if(can_fault_info.bit_data.remote_state != can_temp[9]) + { + can_temp[9] = can_fault_info.bit_data.remote_state; + temp ++; + } + } + + can_timer[4] ++;//ڷź + if( (0 != temp) || (can_timer[4] >= 5) )//500źţ仯 + { + can_timer[4] = 0; + publishMessage(&can_fault_info, 1);// + } + + //busoffж + can_timer[3] ++; + if(can_timer[3] >= 10)//100*100*10 = 1sһ + { + can_timer[3] = 0; + + flexcan_Busoff_Recovery();//BUSS OFF + } +} + + +//static void processSdoOutput1(void *signal_id) +//{ +// (void)signal_id; // DZΪʹã +// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output1, 8, 15);// +//} +// +//static void processSdoOutput2(void *signal_id) +//{ +// (void)signal_id; // DZΪʹã +// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output2, 8, 16); +//} +// +//static void processSdoOutput3(void *signal_id) +//{ +// (void)signal_id; // DZΪʹã +// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output3, 8, 17); +//} +// +//static void processSdoOutput4(void *signal_id) +//{ +// (void)signal_id; // DZΪʹã +// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output4, 8, 18); +//} +// +//static void processSdoOutput5(void *signal_id) +//{ +// (void)signal_id; // DZΪʹã +// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output5, 8, 19); +//} +// +// +//static void processSdoOutput7(void *signal_id) +//{ +// (void)signal_id; // DZΪʹã +// CAN_Send_Msg(&can_handle_3, 0x601, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_sdo_output7, 8, 20); +//} + + + +static void processMotorOutput1(void *signal_id) +{ + (void)signal_id; // DZΪʹã + CAN_Send_Msg(&can_handle_1, LEFT_FRONT_MOTOR_OUTPUT1, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output1, 8, 15);//1Ťغת + CAN_Send_Msg(&can_handle_1, LEFT_REAR_MOTOR_OUTPUT1, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output3, 8, 16);//1Ťغת +} + +static void processMotorOutput2(void *signal_id) +{ + (void)signal_id; // DZΪʹã + CAN_Send_Msg(&can_handle_2, RIGHT_FRONT_MOTOR_OUTPUT1, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output2, 8, 15);//2Ťغת + CAN_Send_Msg(&can_handle_2, RIGHT_REAR_MOTOR_OUTPUT1, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output4, 8, 16);//2Ťغת +} + +static void processMotorOutput3(void *signal_id) +{ + (void)signal_id; // DZΪʹã + + CAN_Send_Msg(&can_handle_1, LEFT_FRONT_MOTOR_OUTPUT2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output1.arr[8], 8, 17);//1 + CAN_Send_Msg(&can_handle_1, LEFT_REAR_MOTOR_OUTPUT2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output3.arr[8], 8, 18);//1 + +} + +static void processMotorOutput4(void *signal_id) +{ + (void)signal_id; // DZΪʹã + CAN_Send_Msg(&can_handle_2, RIGHT_FRONT_MOTOR_OUTPUT2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output2.arr[8], 8, 17);//2 + CAN_Send_Msg(&can_handle_2, RIGHT_REAR_MOTOR_OUTPUT2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_motor_output4.arr[8], 8, 18);//2 +} + +static void processKgfOutput1(void *signal_id) +{ + (void)signal_id; // DZΪʹã + + if(1 == ecu_online)//20250318 ޸Ϊ߲ܷͣɼģE3ͬʱ + { + un_inf_can_kgf_output1.bit_data.can_rx5 = 0xE3; + CAN_Send_Msg(&can_handle_0, 0x11000002, FLEXCAN_EXTEND_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_inf_can_kgf_output1, 8, 15);//KGF1 + } +} + +static void processKgfOutput2(void *signal_id) +{ + (void)signal_id; // DZΪʹã + if(1 == ecu_online)//20250318 ޸Ϊ߲ܷͣɼģE3ͬʱ + { + un_inf_can_kgf_output2.bit_data.can_rx5 = 0xE3; + CAN_Send_Msg(&can_handle_0, 0x14000002, FLEXCAN_EXTEND_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_inf_can_kgf_output2, 8, 16);//KGF2 + } + +} + +//static void processKgfOutput2(void *signal_id) +//{ +// (void)signal_id; // DZΪʹã +// if(1 == ecu_online)//20250318 ޸Ϊ߲ܷͣɼģE3ͬʱ +// { +// un_inf_can_kgf_output2.bit_data.can_rx5 = 0xE3; +// CAN_Send_Msg(&can_handle_0, 0x14000002, FLEXCAN_EXTEND_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_inf_can_kgf_output2, 8, 16);//KGF2 +// } +// +//} + + + +static void processWheelSpeedOutput(void *signal_id) +{ + float CANPressSpeedTemp = 0.0;//ٶȼмֵ + (void)signal_id; // DZΪʹã + + CANPressSpeedTemp = (diff_data.left_motor_speed*(float)getParam("whl_dia") * M_PI*60)/1000.0/(float)getParam("gRatio");// ת/ӡkm/h һȦ1.8 20241016 Լٱ + CANPressSpeedTemp = CANPressSpeedTemp*100;//,Ҫ100 ,ϵ0.01 + un_wheel_wpeed_output.bit_data.left_front = (uint16_t)((int16_t)(CANPressSpeedTemp));//ת*1.8*60*100/1000/ ת/ӡkm/h һȦ1.8,Ҫ100ϵΪ0.01תΪ޷ͣ 20240629 ٱȲҪѾ + un_wheel_wpeed_output.bit_data.left_rear = (uint16_t)((int16_t)(CANPressSpeedTemp));//תΪintתΪ޷ + + CANPressSpeedTemp = (diff_data.right_motor_speed*(float)getParam("whl_dia") * M_PI*60)/1000.0/(float)getParam("gRatio");// ת/ӡkm/h һȦ1.8 + CANPressSpeedTemp = CANPressSpeedTemp*100;//,Ҫ100 ,ϵ0.01 + un_wheel_wpeed_output.bit_data.right_front = (uint16_t)((int16_t)(CANPressSpeedTemp));//ת*1.8*60*100/1000/ ת/ӡkm/h һȦ1.8,Ҫ100ϵΪ0.01תΪ޷ͣ 20240629 ٱȲҪѾ + un_wheel_wpeed_output.bit_data.right_rear = (uint16_t)((int16_t)(CANPressSpeedTemp));//תΪintתΪ޷ + + CAN_Send_Msg(&can_handle_4, 0x98, FLEXCAN_EXTEND_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_wheel_wpeed_output, 8, 15);// +} + +static void processHBridgeOutput(void *signal_id) +{ + (void)signal_id; // DZΪʹã + CAN_Send_Msg(&can_handle_5, 0x7F2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_h_bridge_output, 8, 17);//H + CAN_Send_Msg(&can_handle_5, 0x722, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_h_bridge_output1, 8, 19);//H +} + +static void processUnGatherOutput(void *signal_id) +{ + (void)signal_id; // DZΪʹã + CAN_Send_Msg(&can_handle_0, 0x15000002, FLEXCAN_EXTEND_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_gather_output, 8, 18);//ɼ +} + + + +static void processUltrasonicOutput(void *signal_id) +{ + (void)signal_id; // DZΪʹã + CAN_Send_Msg(&can_handle_6, ULTRASONIC_ID_1, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_ultrasonic_output1, 8, 15);// +} + +//static void processHBridgeOutput2(void *signal_id) +//{ +// (void)signal_id; // DZΪʹã +// CAN_Send_Msg(&can_handle_5, 0x722, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_h_bridge_output2, 8, 16);//H +//} + + +//static void processLifterOutput(void *signal_id) +//{ +// (void)signal_id; // DZΪʹã +// CAN_Send_Msg(&can_handle_5, 0x6F2, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, (uint8_t *)&un_lifter_output, 8, 17);//Ƹ +//} + + + + +//can 100msһ +void canSendAll(void *signal_id) +{ + static uint16_t wheel_speed_cnt = 0; +// static uint8_t kgf_cnt = 0; + static uint16_t bms_cnt1 = 0; + static uint16_t bms_cnt2 = 0; +// static uint16_t motor_speed_cnt = 0; + static uint16_t motor_power_cnt = 0; +// static uint8_t h_bridge_cnt = 0; + uint8_t CanData[8] = {0,0,0,0,0,0,0,0}; +//------------------------------------------------------------------------- + (void)signal_id; // DZΪʹã + + bms_cnt1 ++; + if(bms_cnt1 >= 1000)//1s + { + bms_cnt1 = 0; + + CanData[0] = 0x5A; + + CAN_Send_Msg(&can_handle_3, 0x100, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 1, 15);//BMS + } + + bms_cnt2 ++; + if(bms_cnt2 >= 1100)//1s + { + bms_cnt2 = 0; + + CanData[0] = 0x5A; + + CAN_Send_Msg(&can_handle_3, 0x101, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 1, 16);//BMS + } + + + motor_power_cnt ++; + if(motor_power_cnt >= 1000)//1sһ + { + motor_power_cnt = 0; + processMotorOutput3(CanData); + processMotorOutput4(CanData); + } + +// motor_power_cnt ++; +// if(motor_power_cnt >= 10)//ȡת 20ms +// { +// motor_power_cnt = 0; +// +// un_motor_output1.bit_data.MotCon_1Signal6 = 0x84; +// un_motor_output1.bit_data.MotCon_1Signal7 = 0x02; +// un_motor_output2.bit_data.MotCon_1Signal6 = 0x84; +// un_motor_output2.bit_data.MotCon_1Signal7 = 0x02; +// +// processMotorOutput3(CanData); +// processMotorOutput4(CanData); +// } + +// kgf_cnt ++; +// if(kgf_cnt >= 5)//20210312 CANͨѶģ 20210710޸Ϊ50ms +// { +// kgf_cnt = 0; +// +// processKgfOutput1(CanData); +// processKgfOutput2(CanData); +// } +// + //ٷ + wheel_speed_cnt ++; + if(wheel_speed_cnt >= 100) + { + wheel_speed_cnt = 0; + + processWheelSpeedOutput(CanData); + +// CAN_Send_Msg(&can_handle_0, 0x120, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 16);//BMS +// CAN_Send_Msg(&can_handle_1, 0x121, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 17);//BMS +// CAN_Send_Msg(&can_handle_2, 0x122, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 17);//BMS +// CAN_Send_Msg(&can_handle_3, 0x123, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 16);//BMS +// CAN_Send_Msg(&can_handle_4, 0x124, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 16);//BMS +// CAN_Send_Msg(&can_handle_5, 0x125, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 16);//BMS + CAN_Send_Msg(&can_handle_6, 0x126, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, &un_can_debug_output.arr[0], 8, 16);//BMS +// CAN_Send_Msg(&can_handle_7, 0x127, FLEXCAN_STANDARD_FRAME, FLEXCAN_FrameTypeData, CanData, 8, 16);//BMS + } +// + + //Hݷ +// h_bridge_cnt ++; +// if(h_bridge_cnt >= 5) +// { +// h_bridge_cnt = 0; +// +// processHBridgeOutput(CanData); +// } + + timerStart(&can_timer_interface1,1,1);//1msһ +} + + + + +// canʱźŴ +static void canInterfaceTimerProcess(void *signal_id) +{ + (void)signal_id; // DZΪʹã +// uint32_t time_boot = getCurrentTime();//¼ǰʱ + + canTimerProcess(signal_id); + +// printf("motor1 interval:%d,motor2 interval:%d\n",time_elapsed,time_elapsed1);//ӡ12ռ + + timerStart(&can_timer_interface2,100,1);//100msһ + +// printf("canInterface spend time:%d\n",getCurrentTime() - time_boot);//app˶೤ʱ +} + +static void canInterfaceInput(void *signal_id) +{ + if (signal_id == &un_sw_sample) + { + ecu_online = un_sw_sample.bit_data.flg_wake_ecu; + } +} + + + +// APPģijʼ +void canInterfaceInit(void) +{ + memset(&un_motor_input1, 0, sizeof(UnMotorInput)); + memset(&un_motor_input2, 0, sizeof(UnMotorInput)); + memset(&un_bms_input , 0, sizeof(UnBmsInput)); + memset(&un_temp_module_input, 0, sizeof(UnTempModuleInput)); + + memset(&un_motor_output1, 0, sizeof(UnMotorOutput)); + memset(&un_motor_output2, 0, sizeof(UnMotorOutput)); + +// un_motor_output1.bit_data.mode = 0x05; +// un_motor_output1.bit_data.gear = 0x01; +// un_motor_output1.bit_data.can_gear = 0x01; +// un_motor_output1.bit_data.motor_direction = 0x01; +// un_motor_output1.bit_data.control_data1 = 0x0; +// un_motor_output1.bit_data.control_data2 = 0x0; +// +// un_motor_output2.bit_data.mode = 0x05; +// un_motor_output2.bit_data.gear = 0x01; +// un_motor_output2.bit_data.can_gear = 0x01; +// un_motor_output2.bit_data.motor_direction = 0x00; +// un_motor_output2.bit_data.control_data1 = 0x0; +// un_motor_output2.bit_data.control_data2 = 0x0; +// +// un_motor_output3.bit_data.mode = 0x05; +// un_motor_output3.bit_data.gear = 0x01; +// un_motor_output3.bit_data.can_gear = 0x01; +// un_motor_output3.bit_data.motor_direction = 0x01; +// un_motor_output3.bit_data.control_data1 = 0x0; +// un_motor_output3.bit_data.control_data2 = 0x0; +// +// un_motor_output4.bit_data.mode = 0x05; +// un_motor_output4.bit_data.gear = 0x01; +// un_motor_output4.bit_data.can_gear = 0x01; +// un_motor_output4.bit_data.motor_direction = 0x00; +// un_motor_output4.bit_data.control_data1 = 0x0; +// un_motor_output4.bit_data.control_data2 = 0x0; + + + +// un_motor_output1.bit_data.set_torque = 30000;//ƫʼ +// un_motor_output1.bit_data.set_rotation_speed = 30000; +// un_motor_output1.bit_data.mode = 0; +// un_motor_output2.bit_data.set_torque = 30000; +// un_motor_output2.bit_data.set_rotation_speed = 30000; +// un_motor_output2.bit_data.mode = 0; +// can_fault_info.bit_data.navigator_state = 1; + + memset(&un_inf_can_kgf_output1, 0, sizeof(UnInfCanKGFOutput)); + memset(&un_inf_can_kgf_output2, 0, sizeof(UnInfCanKGFOutput)); + memset(&un_h_bridge_output, 0, sizeof(UnHBridgeOutput)); + memset(&un_wheel_wpeed_output, 0, sizeof(UnWheelSpeedOutput)); + memset(&un_remote_control_input, 0, sizeof(UnRemoteControlInput)); + + un_inf_can_kgf_output2.bit_data.KGF04 = 1;//ɱǿԼϵͨ + + + // ʼʱʹ brake_timer ĵַΪźID + timerInit(&can_timer_interface1); + timerInit(&can_timer_interface2); + + subscribe(&can_timer_interface1, canSendAll); + subscribe(&can_timer_interface2, canInterfaceTimerProcess); + + subscribe(&un_motor_output1, processMotorOutput1); + subscribe(&un_motor_output2, processMotorOutput2); + + subscribe(&un_inf_can_kgf_output1, processKgfOutput1); + subscribe(&un_inf_can_kgf_output2, processKgfOutput2); + subscribe(&un_wheel_wpeed_output, processWheelSpeedOutput); + subscribe(&un_h_bridge_output, processHBridgeOutput); + subscribe(&un_gather_output, processUnGatherOutput); + subscribe(&un_sw_sample, canInterfaceInput); // ͣءѹ ѱ־ + subscribe(&un_ultrasonic_output1, processUltrasonicOutput); // + + + + + + + + +// subscribe(&un_h_bridge_output2, processHBridgeOutput2); +// subscribe(&un_lifter_output, processLifterOutput); + +// subscribe(&un_sdo_output1, processSdoOutput1); +// subscribe(&un_sdo_output2, processSdoOutput2); +// subscribe(&un_sdo_output3, processSdoOutput3); +// subscribe(&un_sdo_output4, processSdoOutput4); +// subscribe(&un_sdo_output5, processSdoOutput5); +// subscribe(&un_sdo_output7, processSdoOutput7); + + timerStart(&can_timer_interface1,1,1);//1msһ + + timerStart(&can_timer_interface2,100,1);//100msһ + + printf( "canInterface: initial OK %d\n",getCurrentTime()); +} + + + diff --git a/main.c b/main.c index 3eb9118..2d192de 100644 --- a/main.c +++ b/main.c @@ -115,7 +115,7 @@ int main(void) //打印版本号 - printf("version: V1.72 \n"); + printf("version: V1.75 \n"); // 初始化框架 放在最前面,解决电机can发送信号累积不处理的问题。 testAppInit();