101 lines
3.1 KiB
C++
101 lines
3.1 KiB
C++
#ifndef _CAN_H
|
|
#define _CAN_H
|
|
|
|
#include <stdint.h>
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include "adc.h"
|
|
#include "gd32c10x_can.h"
|
|
#include "stdint.h"
|
|
#include <stdio.h>
|
|
|
|
// 定义一个联合体类型用于float和byte数组之间的转换
|
|
typedef union {
|
|
float f;
|
|
u8 b[4];
|
|
} float_union_t;
|
|
|
|
// 添加辅助函数用于将short类型数据打包到u8数组中
|
|
static inline void pack_short_to_bytes(u8 *buffer, u16 offset, short value) {
|
|
buffer[offset] = (u8)(value & 0xFF);
|
|
buffer[offset + 1] = (u8)((value >> 8) & 0xFF);
|
|
}
|
|
|
|
// 添加辅助函数用于将u16类型数据打包到u8数组中
|
|
static inline void pack_u16_to_bytes(u8 *buffer, u16 offset, u16 value) {
|
|
buffer[offset] = (u8)(value & 0xFF);
|
|
buffer[offset + 1] = (u8)((value >> 8) & 0xFF);
|
|
}
|
|
|
|
// 添加辅助函数用于将float类型数据打包到u8数组中
|
|
static inline void pack_float_to_bytes(u8 *buffer, u16 offset,
|
|
float_union_t &float_union) {
|
|
buffer[offset] = float_union.b[0];
|
|
buffer[offset + 1] = float_union.b[1];
|
|
buffer[offset + 2] = float_union.b[2];
|
|
buffer[offset + 3] = float_union.b[3];
|
|
}
|
|
|
|
u8 can_gpio_init(void); // CAN引脚初始化
|
|
void can_config(void); // CAN初始化
|
|
void canfd_config(void);// CANFD初始化
|
|
void CAN0_RX0_IRQHandler(void);// CAN接收中断处理函数
|
|
|
|
void can_loopback_init(void);
|
|
void can_loopback_test(void);
|
|
|
|
/*!
|
|
\brief CAN通信类
|
|
\note 封装CAN发送和接收功能
|
|
*/
|
|
class CAN_transmiter {
|
|
|
|
private:
|
|
u8 controller_Mode = 0; // 电机模式
|
|
float_union_t limit_velocity_union = {0}; // 限制速度
|
|
u16 limit_voltage = 0; // 限制电压
|
|
short mcu_temp = 0; // MCU温度
|
|
short motor_temp = 0; // 电机温度
|
|
u16 Power_Voltage = 0; // 电源电压
|
|
float_union_t rt_angle_union = {0}; // 实时角度
|
|
float_union_t rt_velocity_union = {0}; // 实时速度
|
|
u8 Status_code = 0; // 状态码
|
|
float_union_t target_angle_union = {0}; // 目标角度
|
|
u8 Target_Control_Mode = 0; // 目标控制模式
|
|
float_union_t target_velocity_union = {0}; // 目标速度
|
|
float_union_t u_current_union = {0}; // U相电流
|
|
float_union_t w_current_union = {0}; // W相电流
|
|
float_union_t v_current_union = {0}; // V相电流
|
|
|
|
can_trasnmit_message_struct transmit_message; // 发送消息结构体
|
|
u8 transmit_mailbox = CAN_NOMAILBOX; // 初始化为无mailbox状态
|
|
u32 timeout = 0XFFFF; // 默认超时为0xFFFF
|
|
u8 transmit_data[64]; // CANFD最大64字节
|
|
|
|
void update(); // 更新数据
|
|
u8 CAN_Receive(); // CAN接收
|
|
u8 CANFD_Receive(); // CANFD接收
|
|
u8 CAN_Send(u32 ID, u8 length, u8 *data); // CAN发送
|
|
u8 CANFD_Send(u32 ID, u8 length, u8 *data); // CANFD发送
|
|
u8 CAN_all_Send(); // CAN发送
|
|
u8 CANFD_all_Send(); // CANFD发送
|
|
|
|
public:
|
|
// 添加构造函数声明
|
|
CAN_transmiter();
|
|
|
|
can_receive_message_struct receive_message; // 接收消息结构体
|
|
FlagStatus can0_receive_flag; // CAN0接收标志
|
|
FlagStatus can0_send_flag; // CAN0发送标志
|
|
|
|
void communication_check(); // 通信检查
|
|
};
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|