#ifndef  _CAN_USE_H
#define _CAN_USE_H

#include <cmsis_os2.h>
#include "bsp_can.h"
#include "cmd.h"

typedef enum {
  CAN_M3508_M1_ID = 0x201, /* 1 */
  CAN_M3508_M2_ID = 0x202, /* 2 */
  CAN_M3508_M3_ID = 0x203, /* 3 */
  CAN_M3508_M4_ID = 0x204, /* 4 */

	CAN_G6020_AgvM1 =0x205,
	CAN_G6020_AgvM2 =0x206,
	CAN_G6020_AgvM3 =0x207,
	CAN_G6020_AgvM4 =0x208,
	
	CAN_G6020_Pitch =0x209,

	CAN_SICK_ID0 =0x120,
  CAN_SICK_ID1 =0x121,
  CAN_SICK_ID2 =0x122,
  CAN_SICK_ID3 =0x123,
  CAN_SICK_ID4 =0x124,
	
  CAN_VESC5065_M1_MSG1 =0x90b,           //vesc的数据回传使用了扩展id，[0:7]为驱动器id，[8:15]为帧类型
	CAN_VESC5065_M2_MSG1 =0x90c,
	CAN_VESC5065_M3_MSG1 =0x90d,
	CAN_VSEC5065_M4_MSG1 =0x90e,
	
} CAN_MotorId_t;


typedef enum {
  CAN_MOTOR_CHASSIS6020 = 0,
	CAN_MOTOR_PITCH6020,
  CAN_MOTOR_CHASSIS5065,
	CAN_MOTOR_3508,
  CAN_MOTOR_GROUT_NUM,
} CAN_MotorGroup_e;

typedef union {
  float as_array[4];
  struct {
    float m1;
    float m2;
    float m3;
    float m4;
  } named;
} CAN_DJIOutput_t;


typedef union {
  int erpm[4];
  struct {
    int m1;
    int m2;
    int m3;
    int m4;
  } named;
} CAN_VescOutput_t;

typedef union{
		int as_array;
		struct{
		int8_t byte1;
		int8_t byte2;
		int8_t byte3;
		int8_t byte4;
		}byte;
}Vesc_ByteGet;

typedef struct {
  CAN_DJIOutput_t chassis6020;
  CAN_VescOutput_t chassis5065;
	CAN_DJIOutput_t motor3508;
	CAN_DJIOutput_t pitch6020;
} CAN_Output_t;


typedef struct {
  CAN_RxHeaderTypeDef rx_header;
  uint8_t rx_data[8];
} CAN_RawRx_t;

typedef struct {
  CAN_TxHeaderTypeDef tx_header;
  uint8_t tx_data[8];
} CAN_RawTx_t;

typedef struct {
  BSP_CAN_t chassis6020;
  BSP_CAN_t chassis5065;
	BSP_CAN_t motor3508;
	BSP_CAN_t pitch6020;
} CAN_Params_t;


/* 电机反馈信息 */
typedef struct {
  float rotor_angle;
  float rotor_speed;
  float torque_current;
  float temp;
} CAN_MotorFeedback_t;

typedef struct {
  uint16_t sick_distance;
}CAN_SickFeedback_t;

typedef union {
  CAN_SickFeedback_t as_array[4];
  struct {
    CAN_SickFeedback_t s1;
    CAN_SickFeedback_t s2;
    CAN_SickFeedback_t s3;
    CAN_SickFeedback_t s4;
  } named;
} CAN_Sick_t;

typedef struct {
  uint16_t sick_distance[4];
}CAN_SickFeedback_mini_t;

typedef union {
  CAN_MotorFeedback_t as_array[4];
  struct {
    CAN_MotorFeedback_t m1;
    CAN_MotorFeedback_t m2;
    CAN_MotorFeedback_t m3;
    CAN_MotorFeedback_t m4;
  } named;
} CAN_ChassisMotor_t;

typedef union {
  CAN_MotorFeedback_t as_array[4];
  struct {
    CAN_MotorFeedback_t m1;
    CAN_MotorFeedback_t m2;
    CAN_MotorFeedback_t m3;
    CAN_MotorFeedback_t m4;
  } named;
} CAN_VescMotor_t;


typedef struct {
  CAN_ChassisMotor_t chassis6020;
  CAN_ChassisMotor_t chassis5065;
	CAN_ChassisMotor_t motor3508;
	CAN_ChassisMotor_t pit6020;
} CAN_Motor_t;

typedef struct {
  uint32_t recive_flag;
	float vesc_damp;  //阻尼标志位
  CAN_Motor_t motor;
  CAN_Sick_t sickfed;
  CAN_SickFeedback_mini_t sicked_mini;
  const CAN_Params_t *param;
  struct {
    uint32_t chassis;
  } mailbox;
  osMessageQueueId_t msgq_raw;
} CAN_t;

int8_t CAN_Init(CAN_t *can,const CAN_Params_t *param);

int8_t CAN_StoreMsg(CAN_t *can,CAN_RawRx_t *can_rx);

bool_t CAN_CheckFlag(CAN_t *can,uint32_t flag);

int8_t CAN_ClearFlag(CAN_t *can,uint32_t flag);

int8_t CAN_DJIMotor_Control(CAN_MotorGroup_e group,CAN_Output_t *output,CAN_t *can);

int8_t CAN_VESC_Control(int id,CAN_MotorGroup_e group, CAN_Output_t *output,CAN_t *can, CAN_Output_t* can_out);


#endif
