#include "soft_p_2_c.h"
#include "soft_uart.h"
#include "usart.h"
#include "stdbool.h"
#include "soft_timer.h"
#include "soft_adc.h"
#include "soft_flow.h"
#include "soft_crc.h"
#include <string.h>
#include "stdbool.h"
#include "tim.h"
#include "hard_led.h"
#include "soft_can.h"
#include "stdio.h"
#include "soft_bms.h"
#include "soft_terrain.h"
#include "soft_obstacle.h"
#include "soft_okcell.h"
#include "soft_adc.h"
#include "config.h"
#include "soft_calibration.h"
#include "soft_flash.h"
#include "bsp_serial.h"
#include "rkfifo.h"
#include "soft_engine.h"
#include "soft_eft.h"
#include "math.h"
#include "stdlib.h"
#include "soft_water_device.h"
#include "soft_seed_device.h"
#include "soft_test.h"
#include "can_debug.h"
#include "soft_version.h"
#include "soft_update.h"
#include "qingxie_bms.h"
#include "soft_flash.h"


uint8_t msg_buf[256] = {0};
uint16_t crc = 0;
pmu_data pmu;
plane_para planep = {.Candebug_flag = 0};

/**
  * @file    Update_ack_func
  * @brief   PMU回复ACK 回复升级工具
  * @param   组件ID，消息ID，ACK数据
  * @details 升级应答FMU透传，协议和升级工具一致,和FMU不一样
  * @author  Zhang Sir 
 **/
void Update_ack_func(uint8_t group_id, uint8_t msg_id, uint8_t *ackbuf)
{
    
    int index = 0;
    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = group_id;
    msg_buf[index++] = 21;

    msg_buf[index++] = msg_id;
    msg_buf[index++] = *ackbuf;
    msg_buf[index++] = *(ackbuf + 1);
    msg_buf[index++] = 1;

    msg_buf[1] = index - 6;
    uint16_t uart_crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &uart_crc, 2);
    index += 2;

    uart2_send_msg(msg_buf, index);
}


/**
  * @file    pmu_set_ack
  * @brief   PMU应答FMU
  * @param   命令 命令内容 123
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_set_ack(uint8_t id,short content1, short content2,short content3)
{
    pmu_send = PMU_SEND_ACK;
    ack_id = id;
    ack_content = content1;
    ack_content1 = content2;
    ack_content2 = content3;
}   


/**
  * @file    pmu_to_con_voltage_data
  * @brief   PMU发送电压、温度信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_con_voltage_data()
{
    int index = 0;
    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0; //组件计数
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = _MSGID_VOL;

    pmu.voltage = ADC_gather() / 10.0f + volinf.cal_vol * 10; // 获取当前板子电压
    memcpy(&msg_buf[index], &pmu.voltage, 2);
    index += 2;

    pmu.temperature = ADC_read_temptrue();
    memcpy(&msg_buf[index], &pmu.temperature, 2);
    index += 2;

    msg_buf[index++] = 0;
    msg_buf[index++] = 0;

    memcpy(&msg_buf[index], &uavinf.abnormal_outage_flag, 2);
    index += 2;

    msg_buf[1] = index - 6;

    crc = Get_Crc16(msg_buf, index);
    msg_buf[index++] = crc;
    msg_buf[index++] = (crc >> 8) & 0xff;

    uart2_send_msg(msg_buf, index); //10
    //for(int i = 0;i<10;i++){
    //printf("%d              ",msg_buf[6]);
    //printf("%d\n",msg_buf[7]);
    //}
}

/**
  * @file    get_radar_info
  * @brief   获取雷达信息
  * @param   Info_Type:避障雷达 X：1  Y：2
  * @details 
  * @author  Zhang Sir 
 **/
short get_radar_info(uint8_t Radar_Type,uint8_t Info_Type)
{
    uavr_terrain *Ptr_T = NULL;
    uavr_obs *Ptr_O = NULL;
    
    if(Radar_Type == MSGID_SET_T_RADAR)
    {
        if(mimo_ter_info.Link.connect_status != COMP_NOEXIST)
            Ptr_T = &mimo_ter_info;
        else if(DM_ter_info.Link.connect_status != COMP_NOEXIST)
            Ptr_T = &DM_ter_info;
        else if(uavr56_info.Link.connect_status != COMP_NOEXIST)
            Ptr_T = &uavr56_info;
        

        if(Ptr_T->Link.connect_status == COMP_LOST) {return -2;}
        else if(Ptr_T == NULL) {return -1;}
        else {return Ptr_T->height;}
    }
    else if(Radar_Type == MSGID_SET_F_RADAR)
    {
        if(uavr11_info.Link.connect_status != COMP_NOEXIST)
            Ptr_O = &uavr11_info;
        else if(mimo_f_info.Link.connect_status != COMP_NOEXIST)
            Ptr_O = &mimo_f_info;
        else if(DM_f_info.Link.connect_status != COMP_NOEXIST)
            Ptr_O = &DM_f_info;

        if(Ptr_O->Link.connect_status == COMP_LOST) {return -2;}
        else if(Ptr_O == NULL) {return -1;}
        else
        {
            if(Info_Type == OBS_X)
                return Ptr_O->distance_x;
            else if((Info_Type == OBS_Y))
                return Ptr_O->distance_y;
        }
    }
    else if(Radar_Type == MSGID_SET_B_RADAR)
    {
        if(uavr12_info.Link.connect_status != COMP_NOEXIST)
            Ptr_O = &uavr12_info;
        else if(mimo_b_info.Link.connect_status != COMP_NOEXIST)
            Ptr_O = &mimo_b_info;

        if(Ptr_O->Link.connect_status == COMP_LOST) {return -2;}
        else if(Ptr_O == NULL) {return -1;}
        else
        {
            if(Info_Type == OBS_X)
                return Ptr_O->distance_x;
            else if((Info_Type == OBS_Y))
                return Ptr_O->distance_y;
        }
    }
    return 0;
}

/**
  * @file    pmu_to_con_radar_data
  * @brief   PMU发送雷达信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_con_radar_data()
{
    int index = 0;
    //检查各种雷达连接状态
    check_radar_link_status();

    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 10;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = _MSGID_RADAR;

    pmu.radar_buf[0] = get_radar_info(MSGID_SET_T_RADAR,0);
    pmu.radar_buf[1] = get_radar_info(MSGID_SET_F_RADAR,OBS_Y);
    pmu.radar_buf[2] = get_radar_info(MSGID_SET_F_RADAR,OBS_X);
    pmu.radar_buf[3] = get_radar_info(MSGID_SET_B_RADAR,OBS_Y);
    pmu.radar_buf[4] = get_radar_info(MSGID_SET_B_RADAR,OBS_X);

    terrain_is_link = pmu.radar_buf[0] > -1? true:false;
    obs_f_is_link = pmu.radar_buf[1] > -1? true:false;
    obs_b_is_link = pmu.radar_buf[3] > -1? true:false;
    // //发送木牛或者恩曌数据或者莫之比仿地
    // if ((muniu_ter_info.muniu_time == 0) && (mimo_ter_info.heart_time == 0) && (uavr56_info.heart_time == 0)) //上电没通讯
    // {
    //     pmu.radar_buf[0] = -1;
    //     terrain_is_link = false;
    // }
    // else if ((muniu_ter_info.muniu_heart_flag == false) && (mimo_ter_info.heart_flag == false) &&
    //          (uavr56_info.heart_flag == false)) //持续3秒没有接收到数据  显示断开连接
    // {
    //     pmu.radar_buf[0] = -2;
    //     terrain_is_link = false;
    // }
    // else
    // {
    //     if (muniu_ter_info.muniu_heart_flag == true)
    //     {
    //         pmu.radar_buf[0] = muniu_ter_info.muniu_hight;
    //         terrain_height = muniu_ter_info.muniu_hight;
    //         //printf("%d\n", muniu_hight);
    //     }
    //     else if (uavr56_info.heart_flag == true)
    //     {
    //         pmu.radar_buf[0] = uavr56_info.height;
    //         terrain_height = uavr56_info.height;
    //         //		printf("%d\n",uavr56_info.height);
    //     }
    //     else if (mimo_ter_info.heart_flag == true)
    //     {
    //         pmu.radar_buf[0] = mimo_ter_info.height;
    //         terrain_height = mimo_ter_info.height;
    //         //	printf("%d  %d   %d  %d\n",mimo_ter_info.height,keep_time,ez_count,ez_count2);
    //     }
    //     terrain_is_link = true;
    // }

    // //前避障
    // if (uavr11_info.heart_time == 0 && mimo_f_info.heart_time == 0)
    // {
    //     pmu.radar_buf[1] = -1;
    //     obs_f_is_link = false;
    // }
    // else if (uavr11_info.heart_flag == false && mimo_f_info.heart_flag == false)
    // {
    //     pmu.radar_buf[1] = -2;
    //     obs_f_is_link = false;
    // }
    // else
    // {
    //     if (uavr11_info.heart_flag == true) 
    //     {
    //         if(Fobs_handle_function())
    //         {

    //             pmu.radar_buf[1] = uavr11_info.distance_y;
    //             pmu.radar_buf[2] = uavr11_info.distance_x;

    //         }
    //         else
    //         {
    //             pmu.radar_buf[1] = 0;
    //             pmu.radar_buf[2] = 0;                
    //         }
    //     }
    //     if (mimo_f_info.heart_flag == true)
    //     {
    //         if(Fobs_handle_function())
    //         {
    //             pmu.radar_buf[1] = mimo_f_info.distance_y;
    //             pmu.radar_buf[2] = mimo_f_info.distance_x;
    //         }
    //         else
    //         {
    //             pmu.radar_buf[1] = 0;
    //             pmu.radar_buf[2] = 0;                
    //         }
    //         //printf("%d\n",ez_obs_distance_y);
    //         // printf("%d  %d   %d  %d\n",mimo_f_info.distance_y,keep_time,ez_count,ez_count2);
    //     }
    //     obs_f_is_link = true;
    // }
    // //后避障
    // if (uavr12_info.heart_time == 0 && mimo_b_info.heart_time == 0)
    // {
    //     pmu.radar_buf[3] = -1;
    //     obs_b_is_link = false;
    // }
    // else if (uavr12_info.heart_flag == false && mimo_b_info.heart_flag == false)
    // {
    //     pmu.radar_buf[3] = -2;
    //     obs_b_is_link = false;
    // }
    // else
    // {   
    //     if (uavr12_info.heart_flag == true)
    //     {
    //         if(Bobs_handle_function())
    //         {
    //             pmu.radar_buf[3] = uavr12_info.distance_y;
    //             pmu.radar_buf[4] = uavr12_info.distance_x;
    //         }
    //         else
    //         {
    //             pmu.radar_buf[3] = 0;
    //             pmu.radar_buf[4] = 0;                
    //         }
    //     }
    //     else if(mimo_b_info.heart_flag == true)
    //     {
    //         if(Bobs_handle_function())
    //         {
    //             pmu.radar_buf[3] = mimo_b_info.distance_y;
    //             pmu.radar_buf[4] = mimo_b_info.distance_x;
    //         }
    //         else
    //         {
    //             pmu.radar_buf[3] = 0;
    //             pmu.radar_buf[4] = 0;
    //         }
    //     }  
        
    //     obs_b_is_link = true;
    // }


    memcpy(&msg_buf[index], &pmu.radar_buf[0], 10);
    index += 10;
    msg_buf[1] = index - 6;

    crc = Get_Crc16(msg_buf, index);
    msg_buf[index++] = crc;
    msg_buf[index++] = (crc >> 8) & 0xff;

    uart2_send_msg(msg_buf, index);

}



/**
  * @file    pmu_to_con_barttery_data
  * @brief   PMU发送智能电池信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_con_barttery_data()
{
    int index = 0;
    if (Dev.Bms_Link.connect_status == COMP_NORMAL)
    {
        uint8_t size_elong = 3 + 1 + 1 + bms_data.bms_num * 2; //前5字节:协议版本3 电池串数1 电池组数1
        uint8_t size_elong2 = 1 + bms_data2.bms_num * 2;       //电池串数 加 电压
        msg_buf[index++] = 0xFE;
        msg_buf[index++] = 0; // 31 电池ID后的变量字节数
        msg_buf[index++] = 0;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = _MSGID_BMS;

        bms_data.bms_version[0] = 'V';
        bms_data.bms_version[1] = 'K';
        bms_data.bms_version[2] = '3';

        bms_data.bms_group = group_num;

        //一组电池
        if (bms_data.bms_group == 1)
        {
            memcpy(&msg_buf[index], &bms_data, size_elong); //变长数组 赋值到最后一个电压
            index += size_elong;
            memcpy(&msg_buf[index], &bms_data.bms_ids, 45); //从id 到状态 31字节 + 2字节 厂商编号
            index += 45;                                    //新加32字节 33+12=45
            msg_buf[1] = index - 6;

            crc = Get_Crc16(msg_buf, index);
            msg_buf[index++] = crc;
            msg_buf[index++] = (crc >> 8) & 0xff;

            uart2_send_msg(msg_buf, index);
        }
        //两组电池
        else if (bms_data.bms_group == 2)
        {
            memcpy(&msg_buf[index], &bms_data, size_elong); //变长数组 赋值到最后一个电压
            index += size_elong;
            memcpy(&msg_buf[index], &bms_data.bms_ids, 45); //从id 到状态 31字节
            index += 45;
            memcpy(&msg_buf[index], &bms_data2.bms_num, size_elong2);
            index += size_elong2;
            memcpy(&msg_buf[index], &bms_data2.bms_ids, 45);
            index += 45;
            msg_buf[1] = index - 6;

            crc = Get_Crc16(msg_buf, index);
            msg_buf[index++] = crc;
            msg_buf[index++] = (crc >> 8) & 0xff;

            uart2_send_msg(msg_buf, index);
        }
    }
}


/**
  * @file    pmu_to_con_engin_data
  * @brief   PMU发送发动机信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_con_engine_data(void)
{
    uint8_t index = 0;
    if(engine_link_status == COMP_NORMAL)
    {
        msg_buf[index++] = 0xFE;
        msg_buf[index++] = 0;
        msg_buf[index++] = 0; //组件计数
        msg_buf[index++] = 0x00;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = _MSG_ENGIN;
        
        memcpy(&msg_buf[index],&engine_data.engine_type,sizeof(engine_data));
        index += (sizeof(engine_data));

        msg_buf[1] = index - 6;

        crc = Get_Crc16(msg_buf, index);
        msg_buf[index++] = crc;
        msg_buf[index++] = (crc >> 8) & 0xff;

        uart2_send_msg(msg_buf, index);
    }
}


// /**
//   * @file    pmu_to_con_radar360_data
//   * @brief   PMU发送360信息
//   * @param   none
//   * @details 
//   * @author  Zhang Sir 
//  **/
// void pmu_to_con_radar360_data(void)
// {
//     uint8_t index = 0;
//     uint32_t send_byte = 0;
//     if(mimo_360_info.connect_status == COMP_NORMAL)
//     {
//         msg_buf[index++] = 0xFE;
//         msg_buf[index++] = 0;
//         msg_buf[index++] = 0;
//         msg_buf[index++] = 0x00;
//         msg_buf[index++] = 0x00;
//         msg_buf[index++] = _MSGID_360RADAR;
        
//         radar360_proflag = 1;

//         send_byte = sizeof(mimo_360_data) * fmu_360info.total_tar + 2;
        
//         memcpy(&msg_buf[index],&fmu_360info,send_byte);
//         index += send_byte;
//         radar360_proflag = 0;

//         msg_buf[1] = index - 6;

//         crc = Get_Crc16(msg_buf, index);
//         msg_buf[index++] = crc;
//         msg_buf[index++] = (crc >> 8) & 0xff;

//         uart2_send_msg(msg_buf, index);
//     }
// }


/**
  * @file    pmu_to_con_radar360_data
  * @brief   PMU发送360信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
/**
 * 计算需要的包数
 * @param total_points 总点数 M
 * @param points_per_package 每包最大点数 n
 * @return 需要的包数
 */
int calculate_packages(int total_points, int points_per_package) {
    if (points_per_package <= 0) {
        return 0; // 每包容量必须大于0
    }
    
    if (total_points == 0) {
        return 1; // 没有点不需要包
    }
    
    // 使用整数除法向上取整的技巧：(a + b - 1) / b
    return (total_points + points_per_package - 1) / points_per_package;
}

void pmu_to_con_DMradar_data(void)
{
    uint8_t index = 0;
    if(DM_status.connect_status == COMP_NORMAL)
    {
        msg_buf[index++] = 0xFE;
        msg_buf[index++] = 0;
        msg_buf[index++] = 0;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = _MSGID_DMRADAR;
        
        DM_recv_flag = 1;

        memcpy(&msg_buf[index],&FMU_DM_info.target_num,3);
        index += 3;

        memcpy(&msg_buf[index],&FMU_DM_info.warn,2);
        index += 2;
        
        memcpy(&msg_buf[index],FMU_DM_info.buf,DM_T_info.target_num * 4);
        index += DM_T_info.target_num * 4;

        DM_recv_flag = 0;

        msg_buf[1] = index - 6;

        crc = Get_Crc16(msg_buf, index);
        msg_buf[index++] = crc;
        msg_buf[index++] = (crc >> 8) & 0xff;

        uart2_send_msg(msg_buf, index);
    }


}

void pmu_to_con_DM4DFradar_data(void)
{
    uint8_t index = 0;
    int8_t pack_count = 0;//需要发送包数
    int8_t remain_lastcount = 0; //余数
    uint8_t send_bytes = 0; //需要发送字节
    uint8_t send_pack = 1; //当前发送包数
    int16_t remain_target_num = 0; //剩余发送目标点
    int16_t complete_bytes_i= 0;
    
    DM4d_recv_flag = 1;

    remain_target_num = FMU_4D_info.target_num;
    pack_count = calculate_packages(remain_target_num,48);
    while (remain_target_num > 0)
    {
        index = 0;
        msg_buf[index++] = 0xFE;
        msg_buf[index++] = 0;
        msg_buf[index++] = 0;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = 0x00;
        msg_buf[index++] = _MSGID_F4DRADAR;

        remain_lastcount = remain_target_num % 48;
        if(remain_target_num > 0 && remain_lastcount == 0)
            remain_lastcount = 48;

        if(pack_count > send_pack)
            send_bytes = 48 * 5;
        else
            send_bytes = remain_lastcount * 5;

        msg_buf[index++] = send_pack; 
        msg_buf[index++] = FMU_4D_info.target_num;
        memcpy(&msg_buf[index],&DM_F4d.time_delay,2);
        index += 2;

        memcpy(&msg_buf[index],&FMU_4D_info.buf[complete_bytes_i],send_bytes);
        index += send_bytes;
        complete_bytes_i += send_bytes;

        msg_buf[1] = index - 6;

        crc = Get_Crc16(msg_buf, index);
        msg_buf[index++] = crc;
        msg_buf[index++] = (crc >> 8) & 0xff;

        uart2_send_msg(msg_buf, index);

        remain_target_num -= send_bytes / 5;
        if(send_bytes < 48 * 5)
            break;
        send_pack++;
    }
    DM4d_recv_flag = 0;
    
}


/**
  * @file    pmu_to_con_heart_data
  * @brief   PMU_发送心跳
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_con_heart_data()
{
    int index = 0;
    uint32_t time = 0;

    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00; 
    msg_buf[index++] = 21;

    msg_buf[index++] = _MSGID_HEART;
    ack_content = 0x56;
    memcpy(&msg_buf[index],&ack_content,2);
    index += 2;

    ack_content1 = uavinf.reset_reason;
    memcpy(&msg_buf[index],&ack_content1,2);
    index += 2;
    
    memcpy(&msg_buf[index],&ack_content2,2);
    index += 2;  
    
    time = HAL_GetTick();
    memcpy(&msg_buf[index],&time,4);
    index += 4;  

    msg_buf[1] = index - 6;

    crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;
    uart2_send_msg(msg_buf, index);
}


/**
  * @file    pmu_to_con_devtype_data
  * @brief   PMU发送播撒、称重、水泵、离心喷头、在位传感器信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
// void check_and_put_msg(uint8_t *buf, uint16_t len)
// {
//     buf[1] = len - 6;

//     uint16_t crc = Get_Crc16(buf, len);
//     msg_buf[len++] = crc;
//     msg_buf[len++] = (crc >> 8) & 0xff;

//     uart2_send_msg(msg_buf, len);
// }

void pmu_to_con_devtype_data(void)
{
    uint16_t index = 0;
    bool send_flag = true;
    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0; //组件计数
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = _MSGID_DEV_INFO;

    
    if((Dev.Bms_Link.connect_status == COMP_NORMAL || _Aqx_bms_link.connect_status == COMP_NORMAL) && 
        devinfo_time.bms == true)
    {
        if(Dev.Bms.facid == FAC_QX_BMS)
            msg_buf[index++] = DEV_QQ_BMS;
        else
            msg_buf[index++] = DEV_BMS;//设备类型
        
        memcpy(&msg_buf[index],&Dev.Bms.facid,Dev.Bms.index + 1);//厂家id
        index += Dev.Bms.index + 1;
        devinfo_time.bms = false;
    }
    else if(devinfo_time.flow == true)
    {
        msg_buf[index++] = DEV_FLOW;
        memcpy(&msg_buf[index],&Dev.Flow.facid,sizeof(Flow_info));
        index += sizeof(Flow_info);
        devinfo_time.flow = false;
    }
    else if (devinfo_time.radar == true)
    {
        msg_buf[index++] = DEV_RADAR;
        memcpy(&msg_buf[index],&Dev.Radar.facid_T,sizeof(Radar_info));
        index += sizeof(Radar_info);
        devinfo_time.radar = false;
    }
    else if(devinfo_time.checklow == true)
    {
        msg_buf[index++] = DEV_CHECKLOW;
        memcpy(&msg_buf[index],&Dev.Checklow.facid,sizeof(CheckLow_info));
        index += sizeof(CheckLow_info);
        devinfo_time.checklow = false;
    }
    else if(Dev.Part_Tradar_Link.connect_status == COMP_NORMAL && devinfo_time.part_radar == true)
    {
        msg_buf[index++] = DEV_PART_RADAR;
        memcpy(&msg_buf[index],&Dev.Part_radarT.facid,sizeof(Part_Tradar));
        index += sizeof(Part_Tradar);
        devinfo_time.part_radar = false;
    }
    else if(Dev.Part_Fradar_Link.connect_status == COMP_NORMAL && devinfo_time.part_Fradar == true)
    {
        msg_buf[index++] = DEV_PART_FRADAR;
        memcpy(&msg_buf[index],&Dev.Part_radarF.facid,sizeof(Part_Fradar));
        index += sizeof(Part_Fradar);
        devinfo_time.part_Fradar = false;
    }
    // else if(Dev.Bms_Link.connect_status == COMP_NORMAL && devinfo_time.bms == true)
    // {
    //     msg_buf[index++] = DEV_BMS;
    //     memcpy(&msg_buf[index],&Dev.Bms.facid,Dev.Bms.index + 1);
    //     index += Dev.Bms.index + 1;
    //     devinfo_time.bms = false;
    // }
    else if(Dev.L_pump1_Link.connect_status == COMP_NORMAL && devinfo_time.L_pump1 == true)
    {
        msg_buf[index++] = DEV_L_PUMP1;
        memcpy(&msg_buf[index],&Dev.L_pump1.facid,sizeof(Linear_pump_info));
        index += sizeof(Linear_pump_info);
        devinfo_time.L_pump1 = false;
    }
    else if(Dev.L_pump2_Link.connect_status == COMP_NORMAL && devinfo_time.L_pump2 == true)
    {
        msg_buf[index++] = DEV_L_PUMP2;
        memcpy(&msg_buf[index],&Dev.L_pump2.facid,sizeof(Linear_pump_info));
        index += sizeof(Linear_pump_info);
        devinfo_time.L_pump2 = false;
    }
    else if(Dev.Weight_Link.connect_status == COMP_NORMAL && devinfo_time.weight == true)
    {
        msg_buf[index++] = DEV_WEIGHT;
        memcpy(&msg_buf[index],&Dev.Weight.facid,sizeof(Weight_info));
        index += sizeof(Weight_info);
        devinfo_time.weight = false;
    }
    else if(Dev.Seed_Link.connect_status == COMP_NORMAL && devinfo_time.seed == true)
    {
        msg_buf[index++] = DEV_SEED;
        memcpy(&msg_buf[index],&Dev.Seed.facid,sizeof(Seed_info));
        index += sizeof(Seed_info);
        devinfo_time.seed = false;
    }
    else if (Dev.Pump_Link.connect_status == COMP_NORMAL && devinfo_time.pump == true)
    {
        msg_buf[index++] = DEV_PUMP;
        memcpy(&msg_buf[index],&Dev.Pump.facid,sizeof(Pump_info));
        index += sizeof(Pump_info);
        devinfo_time.pump = false;
    }
    else if (Dev.Nozzle_Link.connect_status == COMP_NORMAL && devinfo_time.nozzle == true)
    {
        msg_buf[index++] = DEV_NOZZLE;
        memcpy(&msg_buf[index],&Dev.Nozzle.facid,sizeof(Nozzle_info));
        index += sizeof(Nozzle_info);
        devinfo_time.nozzle = false;
    }
    else if (Dev.Arm_Link.connect_status == COMP_NORMAL && devinfo_time.arm == true)
    {
        msg_buf[index++] = DEV_ARM;
        memcpy(&msg_buf[index],&Dev.Arm.facid,sizeof(Arm_info));
        index += sizeof(Arm_info);  
        devinfo_time.arm = false;
    }
    else if(Dev.Current_Link.connect_status == COMP_NORMAL && devinfo_time.current == true)
    {
        msg_buf[index++] = DEV_CURRENT;
        memcpy(&msg_buf[index],&Dev.Current.facid,sizeof(Current_info));
        index += sizeof(Current_info);  
        devinfo_time.current = false;
    }
    else if(Dev.Temp_Sensor_Link.connect_status == COMP_NORMAL && devinfo_time.tempSensor == true)
    {
        msg_buf[index++] = DEV_TEMPSENSOR;
        memcpy(&msg_buf[index],&Dev.Temp_sensor.facid,sizeof(_Temp_sensor));
        index += sizeof(_Temp_sensor);  
        devinfo_time.tempSensor = false;
    }
    else
    {
        send_flag = false;
    }

    msg_buf[1] = index - 6;

    crc = Get_Crc16(msg_buf, index);
    msg_buf[index++] = crc;
    msg_buf[index++] = (crc >> 8) & 0xff;

    if(send_flag == true)
        uart2_send_msg(msg_buf, index);
}

/**
  * @file    radar_version_check
  * @brief   更改雷达版本格式
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void radar_version_check(void)
{   
    //前避障
    if(uavr11_info.Link.connect_status == COMP_NORMAL  || uavr11_info.Link.boot_flag == true)
    {
        if(uavr11_info.soft_verison == 0 && uavr11_info.get_radar_ver_flag == false)
        {
            uavr11_info.version[0] = 'M';
            uavr11_info.version[1] = '1';
            for(uint8_t i = 2;i < 10; i++)
            {
                uavr11_info.version[i] = '0';
            }
        }
        memcpy(&radar_version[0][0],&uavr11_info.version[0],10);
    }
    else if (mimo_f_info.Link.connect_status == COMP_NORMAL)
    {
        if(mimo_f_info.Link.boot_flag == true)
        {   
            memcpy(&mimo_f_info.version,"E100000000",10);
        }
        memcpy(&radar_version[0][0],&mimo_f_info.version[0],10);
    }
    else if(DM_f_info.Link.connect_status == COMP_NORMAL)
    {
        memcpy(&radar_version[0][0],&DM_f_info.version[0],10);
    }
    
    //后避障
    if(uavr12_info.Link.connect_status == COMP_NORMAL )
    {
        if(uavr12_info.soft_verison == 0 && uavr12_info.get_radar_ver_flag == false)
        {
            uavr12_info.version[0] = 'M';
            uavr12_info.version[1] = '2';
            for(uint8_t i = 2;i < 10; i++)
            {
                uavr12_info.version[i] = '0';
            }
        }
        memcpy(&radar_version[1][0],&uavr12_info.version[0],10);
    }
    else if(mimo_b_info.Link.connect_status == COMP_NORMAL)
    {
        if(mimo_b_info.Link.boot_flag == true)
        {   
            memcpy(&mimo_b_info.version,"E200000000",10);
        }
        memcpy(&radar_version[1][0],&mimo_b_info.version[0],10);
    }

    if(uavr56_info.Link.connect_status == COMP_NORMAL )
    {
        if(uavr56_info.soft_verison == 0 && uavr56_info.get_radar_ver_flag == false)
        {
            uavr56_info.version[0] = 'M';
            uavr56_info.version[1] = 'B';
            for(uint8_t i = 2;i < 10; i++)
            {
                uavr56_info.version[i] = '0';
            }
        }
        
        memcpy(&radar_version[2][0],&uavr56_info.version[0],10);
    }
    else if(mimo_ter_info.Link.connect_status == COMP_NORMAL)
    {
        if(mimo_ter_info.Link.boot_flag == true)
        {   
            memcpy(&mimo_ter_info.version,"EB00000000",10);
        }
        memcpy(&radar_version[2][0],&mimo_ter_info.version[0],10);
    }
    else if(DM_ter_info.Link.connect_status == COMP_NORMAL)
    {
        memcpy(&radar_version[2][0],&DM_ter_info.version[0],10);
    }
}


/**
  * @file    pmu_to_con_version_data
  * @brief   PMU发送版本信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
char radar_version[3][10] = {0}; //0 前避障  1后避障 2仿地
void pmu_to_con_version_data()
{
    //版本信息 0 - 5  硬件版本 IAP版本 APP版本
    uint32_t ver_msg_buf[4] = {0};
    
    int index = 0;
    ver_msg_buf[0] = serial.num; //硬件版本号
    ver_msg_buf[1] = IAP_VERSION;  //IAP版本号
    ver_msg_buf[2] = APP_VERSION;  //APP版本号
    ver_msg_buf[3] = serial.num;//serial.num;

    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = MSGID_REQ_VERSION;

    radar_version_check();

    memcpy(&msg_buf[index], ver_msg_buf, 16);
    index += 16;

    memcpy(&msg_buf[index], &radar_version[0][0], 10);
    index += 10;
    memcpy(&msg_buf[index], &radar_version[1][0], 10);
    index += 10;
    memcpy(&msg_buf[index], &radar_version[2][0], 10);
    index += 10;


    msg_buf[1] = index - 6;

    crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;
    uart2_send_msg(msg_buf, index);

}


/**
  * @file    pmu_to_fcu_version_data
  * @brief   版本信息发送，新协议还没用
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
void pmu_to_fcu_version_data()
{    
    dev_version_content *ptr = NULL;

    for(uint8_t i = 0;i < dev_num;i++)
    {
        ptr = dev_ptr[i];
        if(ptr->send_times > 0)
        {
            break;
        }
        if(i == dev_num - 1)
        {
            return;
        }
    }

    uint8_t index = 0;

    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = _MSGID_DEV_LIST;

    memcpy(&msg_buf[index],&ptr->num,sizeof(dev_version_content) - sizeof(regist_type) - 1);
    index += sizeof(dev_version_content) - sizeof(regist_type) - 1;

    msg_buf[1] = index - 6;

    crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;

    uart2_send_msg(msg_buf, index);
}


/**
  * @file    pmu_to_fcu_key_data
  * @brief   PMU发送秘钥信息
  * @param   none
  * @details 格式电池秘钥匹配
  * @author  Zhang Sir 
 **/
void pmu_to_fcu_key_data(void)
{
    uint8_t index = 0;
    
    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = _MSGID_SHA1;

    msg_buf[index++] = start_msg.Dev_type;
    msg_buf[index++] = start_msg.Id;
    msg_buf[index++] = start_msg.Id_content;
    // if(start_msg.Id == 4)
    // {
    //     start_msg.key_info_checking = false; //发送状态后结束发送
    // }

    memcpy(&msg_buf[index],&start_msg.key_data[0],20);
    index += 20;

    msg_buf[1] = index - 6;

    crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;

    uart2_send_msg(msg_buf, index);

}


/**
  * @file    pmu_to_con_request_data
  * @brief   PMU发送请求信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
char request_id = 0;
short request_1_content = 0;
int request_2_content = 0;
void pmu_to_con_request_data()
{
    int index = 0;

    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 20;

    msg_buf[index++] = request_id;
    memcpy(&msg_buf[index],&request_1_content,2);
    index += 2;
    memcpy(&msg_buf[index],&request_2_content,4);
    index += 4;

    msg_buf[1] = index - 6;

    crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;
    uart2_send_msg(msg_buf, index);
}


/**
  * @file    pmu_to_con_request_data
  * @brief   PMU发送应答信息
  * @param   none
  * @details 
  * @author  Zhang Sir 
 **/
char ack_id = 0;
short ack_content = 0;
short ack_content1 = 0;
short ack_content2 = 0;
void pmu_to_con_ack_data()
{
    int index = 0;

    msg_buf[index++] = 0xFE;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 0x00;
    msg_buf[index++] = 21;

    msg_buf[index++] = ack_id;
    memcpy(&msg_buf[index],&ack_content,2);
    index += 2;
    memcpy(&msg_buf[index],&ack_content1,2);
    index += 2;
    memcpy(&msg_buf[index],&ack_content2,2);
    index += 2;  
    
    msg_buf[1] = index - 6;

    crc = Get_Crc16(msg_buf, index);
    memcpy(&msg_buf[index], &crc, 2);
    index += 2;
    uart2_send_msg(msg_buf, index);
}


/******************void pmu_to_fcu()******************************
 * ****************PMU发送信息给主控********************************
 * ****************************************************************/
/**
  * @file    pmu_to_fcu
  * @brief   PMU发送信息给FMU
  * @param   none
  * @details UART2
  * @author  Zhang Sir 
 **/
uint8_t pmu_send = PMU_SEND_YAOCE;
uint32_t utc_time = 0;
void pmu_to_fcu()
{
    //串口阻塞 和雷达升级不发送
    if(uart_info.vk_dev_update_flag != true && EZup_par.update_flag != true && Rupdate.update_flag != true)
    {
        switch (pmu_send)
        {
        case PMU_SEND_YAOCE:
            //发送电压信息
            if (vol_flag == true)
            {
                pmu_to_con_voltage_data();
                vol_flag = false;
            }
            //发动机信息
            else if (engine_flag == true)
            {
                pmu_to_con_engine_data();
                engine_flag = false;
            } 
            // //mimo360测试
            // else if (mimo360_radar_flag == true)
            // {
            //     pmu_to_con_radar360_data();
            //     mimo360_radar_flag = false;
            // }
            //电目雷达测试
            else if(DM_radar_flag == true)
            {
                pmu_to_con_DMradar_data();
                DM_radar_flag = false;
            }
            //CAN调试信息
            else if(can_debug_flag == true)
            {
                Can_send_debug_to_app();
                can_debug_flag = false;
            }
            //设备SN号,软硬件号
            else if (dev_version_flag == true)
            {
                pmu_to_fcu_version_data();
                dev_version_flag = false;
            }
            //设备信息
            else if (devtype_flag == true)
            {
                pmu_to_con_devtype_data();
                devtype_flag = false;
            }
            break;
        case PMU_SEND_REQINFO:
            pmu_to_con_request_data();
            pmu_send = PMU_SEND_YAOCE;
            break;
        case PMU_SEND_ACK:
            pmu_to_con_ack_data();
            pmu_send = PMU_SEND_YAOCE;
            break;
        case PMU_SEND_VERSION:
            pmu_to_con_version_data();
            pmu_send = PMU_SEND_YAOCE;
            break;
        case PMU_SEND_SHA1:
            pmu_to_fcu_key_data();
            pmu_send = PMU_SEND_YAOCE;
            break;
        case PMU_SEND_DEV_INFO:
            pmu_to_fcu_version_data();
            pmu_send = PMU_SEND_YAOCE;
            break;
        default:
            break;
        }
        //心跳包单独发
        if(pmu_heart_flag == true)
        {
            pmu_to_con_heart_data();
            pmu_heart_flag = false;
        }
        if(F4d_send_flag == true)
        {
            pmu_to_con_DM4DFradar_data();
            F4d_send_flag = false;
        }
    }
}

void Check_Rst(void)
{
    if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET ) // NRST 引脚复位
    {
        uavinf.reset_reason = R_NRST;
        write_uav_information = true;
        __HAL_RCC_CLEAR_RESET_FLAGS();
    }
    if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET ) // 上电掉电复位
    {
        uavinf.reset_reason = R_POWER;
        write_uav_information = true;
        __HAL_RCC_CLEAR_RESET_FLAGS();
    }
    if(__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) != RESET ) //  软件复位
    {
        uavinf.reset_reason = R_SOFT;
        write_uav_information = true;
        __HAL_RCC_CLEAR_RESET_FLAGS();
    }
    if(__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET ) // 独立看门狗复位
    {
        uavinf.reset_reason = R_IWD;
        write_uav_information = true;
        __HAL_RCC_CLEAR_RESET_FLAGS();
    }
    if(__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST) != RESET ) // 窗口看门狗复位
    {
        uavinf.reset_reason = R_WWD;
        write_uav_information = true;
        __HAL_RCC_CLEAR_RESET_FLAGS();
    }
    if(__HAL_RCC_GET_FLAG(RCC_FLAG_LPWRRST) != RESET ) // 低功耗复位
    {
        uavinf.reset_reason = R_LOPWER;
        write_uav_information = true;
        __HAL_RCC_CLEAR_RESET_FLAGS();
    }
    
    
}
/******************void check_fmu_link()************************
 * ****************检查是否收到FMU信息************************************
 * ****************************************************************/
int time_heart[3] = {0};
int time_count[3] = {0};//test
static uint32_t fmu_link_time = 0;
void check_fmu_link()
{
    //FMU串口通讯连接检测
    if(HAL_GetTick() > 15000)
    {
        if(recv_fmu_data == true && HAL_GetTick() - fmu_link_time > 3000)
        {
            recv_fmu_data = false;
        }
    }

    //FMU异常断电检测
    if((planep.lock_status != STA_LOCK) && (recv_fmu_data == true) && (HAL_GetTick() - fmu_link_time > 500))
    {
        uavinf.abnormal_outage_flag = 1;
        write_uav_information = true;
        recv_fmu_data = false;
    }

    //Check_Rst();
}

/******************void uart_recv_con_msg()************************
 * ****************PMU收控制消息************************************
 * ****************************************************************/
_pmu_pin pmu_pin;
bool recv_fmu_data = false;
Set_info msgidset;
uint32_t DMJZ = 0 ;
void uart_recv_con_msg()
{
    check_fmu_link();

    check_uart_data(&recv_rkfifo);

    if (uart_info.fcu_buf_flag == true)
    {  
        uint32_t tem_32t = 0;
        FLASH_EraseInitTypeDef f;

        fmu_link_time = HAL_GetTick();
        recv_fmu_data = true;

        switch (fcu_protocol.msg_id)
        {
        case _MSGID_PWM:
            memcpy(&pmu_pin.pump1, &fcu_protocol.payload[6], sizeof(_pmu_pin));

            if(set_HWesc_ESCid == true && setESCidInfo.setESCidStep == SETESCID_SET)
            {
                memset(&pmu_pin.pump1,0,4);
                
                if(setESCidInfo.setESCidNum == 1)
                    pmu_pin.pump1 = 1000;
                else if(setESCidInfo.setESCidNum == 2)  
                    pmu_pin.pump2 = 1000;
            }

            __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, pmu_pin.pump1);
            __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, pmu_pin.pump2);
            
            if(planep.UAV_type == VK_ALL_IN_ONE)
            {
                __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, pmu_pin.nozz1_fm);
                __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, pmu_pin.nozz2_zp);
            }
            else
            {
                __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, pmu_pin.nozz1_fm);
                __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, pmu_pin.nozz2_zp);
            }

            if(planep.UAV_type == VK_ALL_IN_ONE)
            {
                //分电板离心喷头
                distributor.nozzle1 = pmu_pin.nozz1_fm; 
                distributor.nozzle2 = pmu_pin.nozz2_zp;
                
                //离心喷头34
                __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_4, pmu_pin.nozz3); // PB8 nozzle
                __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, pmu_pin.nozz4); // PB9 nozzle


                //前后灯
                if((pmu_pin.aux_light & 0x1) > 0)
                    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
                else
                    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);

                if((pmu_pin.aux_light & 0x2) > 0)
                    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
                else
                    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET); 

                //摄像头舵机
                __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, pmu_pin.aux_steer);
            }
            
            break;
        case _MSGID_LED:
            for(uint8_t i = 0; i < 7; i++)
            {
                if(planep.UAV_type != VK_ALL_IN_ONE ) //青岛机型不用LED
                {
                    led_set_blink(i, fcu_protocol.payload[i*3+6], fcu_protocol.payload[i*3+7], fcu_protocol.payload[i*3+8]);
                }
            }
            break;
        //飞机姿态信息
        case _MSGID_ATTITUDE:
            memcpy(&planep.yaw, &fcu_protocol.payload[6], sizeof(plane_para));

            if(uavinf.uavtype != planep.UAV_type)
            {
                write_uav_information = true;
            }
            break;
        case _MSGID_TIME:
            //tem_32t = 1684136124;
            memcpy(&utc_time, &fcu_protocol.payload[6], 4);
            covUnixTimeStp2Beijing(utc_time,&beijing_time);
            pmu_set_ack(_MSGID_TIME,0,0,0);
            break;
        //eft播撒器
        case _MSGID_EFT_CON:
            recv_fmu_seed_info = true;
            memcpy(&tppwm_value, &fcu_protocol.payload[6], 2);
            memcpy(&sow_rotate_value, &fcu_protocol.payload[8], 2);
            break;
        //请求信息包
        case _MSGID_REQ:
            switch (fcu_protocol.payload[6])
            {
            case MSGID_REQ_VERSION:
                pmu_send = PMU_SEND_VERSION;
                break;
            default:
                break;
            }
            break;
        //主控应答
        case _MSGID_ACK:
            msgidset.content1 = fcu_protocol.payload[7] + 256 * fcu_protocol.payload[8];
            switch (fcu_protocol.payload[6])
            {
            case MSGID_ACK_HEART:
                uavinf.abnormal_outage_flag = 0;
                write_uav_information = true;
                break;
            case MSGID_ACK_VERSION:
                start_msg.version_info = true;
                break;
            case MSGID_ACK_DEV:
                {
                    if(msgidset.content1 < DEVICE_END - 1)
                    {
                        dev_version_content *Pt = dev_ptr[msgidset.content1];
                        if(Pt->send_times > 0)
                            Pt->send_times--;
                        }
                    }
                break;
            default:
                break;
            }
            break;
        //设置雷达灵敏度
        case _MSGID_SET:
            msgidset.num  = fcu_protocol.payload[6];
            msgidset.content1 = fcu_protocol.payload[7] + 256 * fcu_protocol.payload[8];
            msgidset.content2 = fcu_protocol.payload[9] + 256 * fcu_protocol.payload[10];
            msgidset.content3 = fcu_protocol.payload[11] + 256 * fcu_protocol.payload[12];
            switch (msgidset.num)
            {
            //前雷达
            case MSGID_SET_F_RADAR:
                if(uavr12_info.fcu_set_sensi_flag != true && uavr56_info.fcu_set_sensi_flag != true && radar_update_flag == false)
                {
                    uavr11_info.fcu_set_sensi_flag = true;
                }
                memcpy(&obsfradar_sensitivity, &fcu_protocol.payload[7], 2);
                if(obsfradar_sensitivity == uavr11_info.get_radar_sensi)
                {
                    uavr11_info.fcu_set_sensi_flag = false;
                    pmu_set_ack(_MSGID_SET,MSGID_SET_F_RADAR,uavr11_info.get_radar_sensi,0);
                }
                break;
            //后雷达
            case MSGID_SET_B_RADAR:
                if(uavr11_info.fcu_set_sensi_flag != true && uavr56_info.fcu_set_sensi_flag != true && radar_update_flag == false)
                {
                    uavr12_info.fcu_set_sensi_flag = true;
                }
                memcpy(&obsbradar_sensitivity, &fcu_protocol.payload[7], 2);
                if(obsbradar_sensitivity == uavr12_info.get_radar_sensi)
                {
                    uavr12_info.fcu_set_sensi_flag = false;
                    pmu_set_ack(_MSGID_SET,MSGID_SET_B_RADAR,uavr12_info.get_radar_sensi,0);
                }
                break;
            //仿地
            case MSGID_SET_T_RADAR:
                if(uavr11_info.fcu_set_sensi_flag != true && uavr12_info.fcu_set_sensi_flag != true && radar_update_flag == false)
                {
                    uavr56_info.fcu_set_sensi_flag = true;
                }
                memcpy(&uavr56_info.fcu_set_sensi, &fcu_protocol.payload[7], 2);
                if(uavr56_info.fcu_set_sensi == uavr56_info.get_radar_sensi)
                {
                    uavr56_info.fcu_set_sensi_flag = false;
                    pmu_set_ack(_MSGID_SET,MSGID_SET_T_RADAR,uavr56_info.get_radar_sensi,0);
                }
                break;
            case MSGID_SET_TR_BLIND:
                {
                    uint8_t can_buf[8] = {0};
                    uint32_t can_id = 0;
                    if(msgidset.content1 == 0x11)
                        can_id = 0xA81300;
                    else if (msgidset.content1 == 0x56)
                        can_id = 0x981300;
                    put_date_to_can(can_buf,0x5,(msgidset.content2 & 0xff),((msgidset.content2 >> 8) & 0xff),0,0,0,0,0X7);
                    can_send_msg_normal(&can_buf[0], 8, can_id);
                }
                break;
            case MSGID_SET_BR_POWER:
                {
                    uint8_t can_buf[8] = {0};
                    uint32_t can_id = 0;
                    if(msgidset.content1 == 0x11)
                        can_id = 0xA81300;
                    else if (msgidset.content1 == 0x56)
                        can_id = 0x981300;
                    put_date_to_can(can_buf,0x7,(msgidset.content2 & 0xff),((msgidset.content2 >> 8) & 0xff),0,0,0,0,0X7);
                    can_send_msg_normal(&can_buf[0], 8, can_id);
                }
                break;
            case MSGID_SET_R_FUNC:
                {
                    uint8_t can_buf[8] = {0};
                    uint32_t can_id = 0;
                    if(msgidset.content1 == 3)
                        can_id = 0x981300;
                    else if(msgidset.content1 == 5)
                        can_id = 0xA81300;
                    put_date_to_can(can_buf,0x4,(msgidset.content1 & 0xff),((msgidset.content1 >> 8) & 0xff),0,0,0,0,0X7);
                    can_send_msg_normal(&can_buf[0], 8, can_id);
                }
                break;
            case MSGID_SET_RAW_SWITCH:
                {
                    uint8_t can_buf[8] = {0};
                    uint32_t can_id = 0;
                    if(msgidset.content1 == 0x11)
                        can_id = 0xA81300;
                    else if (msgidset.content1 == 0x56)
                        can_id = 0x981300;
                    put_date_to_can(can_buf,0xA,(msgidset.content2 & 0xff),((msgidset.content2 >> 8) & 0xff),0,0,0,0,0X7);
                    can_send_msg_normal(&can_buf[0], 8, can_id);
                }
                break;
            case MSGID_SET_GEELY:
                switch (fcu_protocol.payload[7])
                {
                case SET_START_OR_STOP:
                    if(0x1 == fcu_protocol.payload[9] + ((fcu_protocol.payload[10] << 8) & 0xff00))
                    {
                        if (geely_engin_link.connect_status == COMP_NORMAL && geely_data.engine_control == 0)
                        {
                            sendto_geely.ExtenderControl = 1;
                        }
                        start_engine = 0x80;
                    }
                    else if(0x0 == fcu_protocol.payload[9] + ((fcu_protocol.payload[10] << 8) & 0xff00))
                    {
                        if (geely_engin_link.connect_status == COMP_NORMAL && geely_data.engine_control == 1)
                        {
                            sendto_geely.ExtenderControl = 0;
                        }
                        start_engine = 0;
                    }
                    break;
                case SET_PROTECT_TIME:
                    if (geely_engin_link.connect_status == COMP_NORMAL)
                    {
                        geely_set_tcd = true;
                        
                        sendto_geely.SettingParameter = fcu_protocol.payload[9] + ((fcu_protocol.payload[10] << 8) & 0xff00);
                    }
                    break;
                case SET_LOCK_TIME:
                    if (geely_engin_link.connect_status == COMP_NORMAL)
                    {
                        geely_set_tlock = true;
                        
                        sendto_geely.SettingParameter = fcu_protocol.payload[9] + ((fcu_protocol.payload[10] << 8) & 0xff00);
                    }
                    break;
                case SET_LOWVOL_PROTECT:
                    if(0x1 == fcu_protocol.payload[9] + ((fcu_protocol.payload[10] << 8) & 0xff00))
                    {
                        if (geely_engin_link.connect_status == COMP_NORMAL)
                        {
                            sendto_geely.ExtenderControl = 1;
                            geely_set_protect = true;
                        }
                    }
                    else
                    {
                        geely_set_protect = false;
                    }
                    break;
                default:
                    break;
                }
                break;
            case MSGID_SET_VOL:
                set_cali_voltage = (fcu_protocol.payload[7] + 256 * fcu_protocol.payload[8]) - (int)(ADC_gather() /100.0f) ;
                write_vol_information = true;
                pmu_set_ack(_MSGID_SET,MSGID_SET_VOL,0,0);
                break;
            case MSGID_SET_MIMO_FLOW:
                if(msgidset.content1 != 0)
                {
                    if(Dev.Flow.facid == FAC_MIMO_SIG || Dev.Flow.facid == FAC_MIMO_DOU)
                    {
                        tem_32t = flow_mimo1.flow_k * msgidset.content1 / 100;
                        
                        if(tem_32t >= 500 && tem_32t <= 20000)
                        {
                            flow_mimo1.flow_calk = tem_32t;
                        }
                        else{
                            flow_mimo1.flow_calk = 10000;
                        }
                        flow_mimo1.send_k_count = 5;
                    }
                    else if(Dev.Flow.facid == FAC_QIFEI_SIG || Dev.Flow.facid == FAC_QIFEI_DOU)
                    {
                        tem_32t = flow_inf.ch1.k * msgidset.content1 / 100;
                        
                        if(tem_32t >= 500 && tem_32t <= 20000)
                        {
                            flow_inf.ch1.cal_k = tem_32t;
                        }
                        else{
                            flow_inf.ch1.cal_k = 10000;
                        }
                        flow_inf.ch1.set_k = true;
                    }
                }

                if(msgidset.content2 != 0)
                {
                    if(Dev.Flow.facid == FAC_MIMO_SIG || Dev.Flow.facid == FAC_MIMO_DOU)
                    {
                        tem_32t = flow_mimo2.flow_k * msgidset.content2 / 100;

                        if(tem_32t >= 500 && tem_32t <= 20000)
                        {
                            flow_mimo2.flow_calk = tem_32t;
                        }
                        else{
                            flow_mimo2.flow_calk = 10000;
                        }
                        flow_mimo2.send_k_count = 5;
                    }
                    else if(Dev.Flow.facid == FAC_QIFEI_SIG || Dev.Flow.facid == FAC_QIFEI_DOU)
                    {
                        tem_32t = flow_inf.ch2.k * msgidset.content2 / 100;
                        
                        if(tem_32t >= 500 && tem_32t <= 20000)
                        {
                            flow_inf.ch2.cal_k = tem_32t;
                        }
                        else{
                            flow_inf.ch2.cal_k = 10000;
                        }
                        flow_inf.ch1.set_k = true;
                    }
                }
                break;
            case MSGID_SET_FLOW_BACKGROUND:
                 if(Dev.Flow.facid == FAC_MIMO_SIG)
                 {
                    flow_inf.ch1.clear_background = true;
                 }
                 if(Dev.Flow.facid == FAC_MIMO_DOU)
                 {
                    flow_inf.ch1.clear_background = true;
                    flow_inf.ch2.clear_background = true;
                 }
                break;
            case MSGID_SET_WEIGHT_K:
                //calib_aux = msgidset.content1 + (msgidset.content2 << 2); //bit 0-1 编号 2-15K值
                weight_order.type = Weight_Set_K;
                weight_order.order_con1 = msgidset.content1;
                weight_order.order_con2 = msgidset.content2;
                break;
            case MSGID_SET_SEED_OUT_TYPE:
                recv_fmu_seed_info = true;
                seed_output_mode = msgidset.content1;
                pmu_set_ack(_MSGID_SET,MSGID_SET_SEED_OUT_TYPE,0,0);
                break;
            case MSGID_SET_SEED_CAL_TYPE:
                switch (msgidset.content1)
                {
                case Cal_Remove_Peel:
                    weight_order.type = Weight_Peer; 
                    break;
                case Cal_Weight:
                    weight_order.type = Weight_Kg;
                    weight_order.order_con2 =  msgidset.content2;
                    weight_order.order_con3 =  msgidset.content3;
                    break;
                case Cal_Seed_Back:
                    weight_order.type = Seed_Bcak;
                    break;
                default:
                    break;
                }
                break;
            case MSGID_SET_WEIGHT_MODE:
                weight_order.type = Weight_Mode;
                weight_order.order_con1 = msgidset.content1;
                break;
            case MSGID_SET_MAX_RATE:
                weight_order.type = Weight_Drug_Rate;
                weight_order.order_con1 = msgidset.content1;
                break;    
            case MSGID_SET_WEIGHT_RESETK:
                weight_order.type = Weight_Bcak;
                break;
            case MSGID_SET_WEIGHTFAC_CAL:
                weight_order.type = Weight_Fac_cal;
                weight_factory_cal_flag = true; //一直发送
                break;
            case MSGID_SET_PMU_SERIAL:
                set_pmu_serail = msgidset.content1 + ((msgidset.content2 << 16) & 0xffff0000);
                if(serial.num == PMU_SERIAL || set_pmu_serail == PMU_SERIAL)
                {
                    write_serial_information = true;
                    pmu_set_ack(_MSGID_SET,MSGID_SET_PMU_SERIAL,0,0);
                    dev_pmu.send_times += 5;
                }
                else if(set_pmu_serail == serial.num)
                {
                    pmu_set_ack(_MSGID_SET,MSGID_SET_PMU_SERIAL,0,0);
                }             
                break;
            case MSGID_SET_RADAR_FB:
                {
                uint8_t can_buf[8] = {0};
                if(msgidset.content1 == 0x11)
                {   
                    put_date_to_can(can_buf,0x55,0XAA,0X03,0X61,0X03,0X01,0X65,0X00);
                    can_send_msg_normalstd(can_buf,7,0xFA);
                }
                else if (msgidset.content1 == 0x12)
                {
                    put_date_to_can(can_buf,0x55,0XAA,0X03,0X61,0X03,0X02,0X66,0X00);
                    can_send_msg_normalstd(can_buf,7,0xFA);
                }
                else if(msgidset.content1 == 0x100)
                {
                    if(msgidset.content2 == 0x11 || msgidset.content2 == 0x12)
                    {
                        put_date_to_can(can_buf,0x55,0XAA,0X03,0X61,0X00,0X03,0X64,0X00);
                        can_send_msg_normalstd(can_buf,7,0xFA);
                    }
                    else if(msgidset.content2 == 0x56)
                    {
                        put_date_to_can(can_buf,0x55,0XAA,0X02,0X60,0X03,0X63,0X00,0X00);
                        can_send_msg_normalstd(can_buf,6,0xFA);
                    }
                }
                }
                break;
            case MSGID_SET_LACKLOSS_CAL:
                {
                uint8_t can_buf[8] = {0};
                if(Dev.Lackloss_Link.connect_status == COMP_NORMAL && Dev.Checklow.facid == FAC_VK)
                {
                    put_date_to_can(can_buf,0xFA,0x03,0x00,0xB2,0XE1,0x00,0X00,0X00);
                    can_buf[7] = (can_buf[1]+can_buf[2]+can_buf[3]+can_buf[4]+can_buf[5]+can_buf[6]) & 0xff;
                    can_send_msg_normalstd(can_buf,8,0xFA); 
                }
                else if(Dev.Lackloss_Link.connect_status == COMP_NORMAL && Dev.Checklow.facid == FAC_DM_CHECKLOW)
                {
                    if(HAL_GetTick()-DMJZ >= 6000)
                    {
                        put_date_to_can(can_buf,0x01,0x00,0x00,0x00,0X00,0x00,0X00,0X00);
                        can_send_msg_normal(can_buf,8,0x18F2AD0A);
                        DMJZ = HAL_GetTick();
                    }
                }
                }
                break;
            case MSGID_SET_PUMP_ID:
                set_HWesc_ESCid = true;
                setESCidInfo.setESCidNum = 1;
                setESCidInfo.setESCidStep = SETESCID_INIT;
                pmu_set_ack(_MSGID_SET,MSGID_SET_PUMP_ID,0,2);
                break;
            case MSGID_SET_RESIWIRE_BLOWN:
                {
                uint8_t can_buf[8] = {0};
                put_date_to_can(can_buf,0xF7,fcu_protocol.payload[7],0x00,0x00,0x00,0xF1,0XF3,0X00);
                can_send_msg_normal(&can_buf[0], 8, 0x88BB);
                }
                break;
            case MSGID_SET_FRADAR_SN:
                {
                    int radar_Sn = 0;
                    uint8_t can_buf[8] = {0};

                    radar_Sn = msgidset.content1 + (msgidset.content2 << 16);
                    can_buf[0] = 2;
                    memcpy(&can_buf[1],&radar_Sn,4);
                    can_buf[7] = 7;
                    can_send_msg_normal(can_buf,8,0xA81300);
                    DM_f_info.get_radar_ver_flag = false;
                    dev_obsf.regist.sn = false;
                    pmu_set_ack(_MSGID_SET,MSGID_SET_FRADAR_SN,msgidset.content1,msgidset.content2);
                }
                break;
            case MSGID_SET_BRADAR_SN:
                {
                    int radar_Sn = 0;
                    uint8_t can_buf[8] = {0};

                    radar_Sn = msgidset.content1 + (msgidset.content2 << 16);
                    can_buf[0] = 2;
                    memcpy(&can_buf[1],&radar_Sn,4);
                    can_buf[7] = 7;
                    can_send_msg_normal(can_buf,8,0xB81300);
                    dev_obsb.regist.sn = false;
                    //DM_b_info.get_radar_ver_flag = false;
                    pmu_set_ack(_MSGID_SET,MSGID_SET_BRADAR_SN,msgidset.content1,msgidset.content2);
                }
                break;
            case MSGID_SET_TRADAR_SN:
                {
                    int radar_Sn = 0;
                    uint8_t can_buf[8] = {0};

                    radar_Sn = msgidset.content1 + (msgidset.content2 << 16);
                    can_buf[0] = 2;
                    memcpy(&can_buf[1],&radar_Sn,4);
                    can_buf[7] = 7;
                    can_send_msg_normal(can_buf,8,0x981300);
                    DM_ter_info.get_radar_ver_flag = false;
                    dev_ter.regist.sn = false; //重新获取sn
                    pmu_set_ack(_MSGID_SET,MSGID_SET_TRADAR_SN,msgidset.content1,msgidset.content2);
                }
                break;
            default:
                break;
            }
            break;
        case _MSGID_HEART:
            time_heart[1] = HAL_GetTick();
            time_count[1]++;
            pmu_heart_flag = true;
            break;
        case _MSGID_SHA1:
            //智能电池秘钥
            if(fcu_protocol.payload[6] == 1)
            {
                //有秘钥
                if(fcu_protocol.payload[7] == 2 && fcu_protocol.payload[8] == 1)
                {
                    if(start_msg.key_info_checking == true)
                    {
                        memcpy(&start_msg.key_data[0],&fcu_protocol.payload[9],20);
                        start_msg.key_to_bms = true;//给电池发送秘钥
                        start_msg.key_info_checking = false;
                    }
                }
                //无秘钥
                else if(fcu_protocol.payload[7] == 2 && fcu_protocol.payload[8] == 2)
                {
                    start_msg.key_info_checking = false;
                }
                //回馈，防止PMU一直发
                else if(fcu_protocol.payload[7] == 3 || fcu_protocol.payload[7] == 4)
                {
                    start_msg.key_info_checking = false;
                }
            }
            break;
        //升级固件标志
        case _MSGID_UPDATA:
        {
            __disable_irq();
            HAL_FLASH_Unlock();
            __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGERR);

            f.TypeErase = FLASH_TYPEERASE_PAGES;
            f.PageAddress = UPDATE_FLAG;
            f.NbPages = 1;
            uint32_t PageError = 0;
            HAL_FLASHEx_Erase(&f, &PageError);

            uint16_t TempBuf = 0x01;
            HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, UPDATE_FLAG, TempBuf);

            HAL_FLASH_Lock();
            __enable_irq();

            HAL_NVIC_DisableIRQ(USART2_IRQn);
            HAL_Delay(100);

            //软件复位
            HAL_NVIC_SystemReset();
        }
            break;
        default:
            break;
        }

        uart_info.fcu_buf_flag = false;
        memset(fcu_protocol.payload, 0, 256);
    }
}

/******************void pmu_start_info()*******************
 * ****************上电时PMU发送的信息*******************************
 * ****************************************************************/
Start_info start_msg = {.key_info_checking = true,
                        .Dev_type = 1,
                        .Id = 1,
                        };
void pmu_start_info()
{
    static uint32_t circu_time_2hz = 0;

    if(Check_Timer_Ready(&circu_time_2hz,_2_HZ_))
    {
        if(HAL_GetTick() > 10000 && start_msg.version_info == false)
        {
            pmu_send = PMU_SEND_VERSION;
            start_msg.version_info = true;
        }
        else if (start_msg.key_info_checking == true && Device1.Vkbms_Link.connect_status == COMP_NORMAL)
        {
            pmu_send = PMU_SEND_SHA1;
        }
    }
}


