Files
Motor_v2/Robot_V2/device/Can.h
T

125 lines
4.0 KiB
C++
Raw Normal View History

2025-09-23 00:27:51 +08:00
#ifndef _CAN_H
#define _CAN_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "gd32c10x_can.h"
#include "stdint.h"
#include <stdio.h>
// 定义一个联合体类型用于float和byte数组之间的转换
typedef union {
float f;
u8 b[4];
} float_union_t;
2025-10-02 02:52:07 +08:00
// 添加辅助函数用于将u16类型数据打包到u8数组中
2025-11-23 18:31:47 +08:00
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);
2025-10-02 02:52:07 +08:00
}
// 添加辅助函数用于将float类型数据打包到u8数组中
2025-11-23 18:31:47 +08:00
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];
2025-10-02 02:52:07 +08:00
}
2025-10-16 21:43:34 +08:00
// 添加辅助函数用于从u8数组中解包short类型数据
2025-11-23 18:31:47 +08:00
static inline short unpack_bytes_to_short(u8 *buffer, u16 offset) {
short value = (short)(buffer[offset] | (buffer[offset + 1] << 8));
return value;
2025-10-16 21:43:34 +08:00
}
// 添加辅助函数用于从u8数组中解包u16类型数据
2025-11-23 18:31:47 +08:00
static inline u16 unpack_bytes_to_u16(u8 *buffer, u16 offset) {
u16 value = (u16)(buffer[offset] | (buffer[offset + 1] << 8));
return value;
2025-10-16 21:43:34 +08:00
}
// 添加辅助函数用于从u8数组中解包float类型数据
2025-11-23 18:31:47 +08:00
static inline float_union_t unpack_bytes_to_float(u8 *buffer, u16 offset) {
float_union_t float_union;
float_union.b[0] = buffer[offset];
float_union.b[1] = buffer[offset + 1];
float_union.b[2] = buffer[offset + 2];
float_union.b[3] = buffer[offset + 3];
2025-10-16 21:43:34 +08:00
return float_union;
}
u8 can_gpio_init(void); // CAN引脚初始化
void can_config(void); // CAN初始化
void canfd_config(void); // CANFD初始化
void can_dma_init(void); // CAN DMA初始化
void CAN0_RX0_IRQHandler(void); // CAN接收中断处理函数
void DMA0_Channel4_IRQHandler(void); // DMA通道4中断处理函数(CAN接收)
void DMA0_Channel5_IRQHandler(void); // DMA通道5中断处理函数(CAN发送)
2025-10-14 00:49:58 +08:00
2025-10-02 02:52:07 +08:00
void can_loopback_init(void);
void can_loopback_test(void);
2025-09-23 00:27:51 +08:00
/*!
\brief CAN通信类
\note 封装CAN发送和接收功能
*/
class CAN_transmiter {
private:
bool is_sleep = false; // 是否休眠
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}; // 实时速度
bool MotorStatus = 0; // 电机状态码
float_union_t target_angle_union = {0}; // 目标角度
u8 Target_Control_Mode = 0; // 目标控制模式
u8 EEPROMStatus = 0; // EEPROM状态码
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相电流
2025-09-23 00:27:51 +08:00
2025-10-14 00:49:58 +08:00
can_trasnmit_message_struct transmit_message; // 发送消息结构体
u8 transmit_mailbox = CAN_NOMAILBOX; // 初始化为无mailbox状态
u8 transmit_data[64]; // CANFD最大64字节
2025-09-23 00:27:51 +08:00
void update(); // 更新数据
2025-10-14 00:49:58 +08:00
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发送
2025-10-14 00:49:58 +08:00
u8 CANFD_all_Send(); // CANFD发送
2025-09-23 00:27:51 +08:00
public:
// 添加构造函数声明
2025-12-08 02:48:59 +08:00
u8 init(void);
2025-10-14 00:49:58 +08:00
can_receive_message_struct receive_message; // 接收消息结构体
FlagStatus can0_receive_flag; // CAN0接收标志
FlagStatus can0_send_flag; // CAN0发送标志
float_union_t testfloat_union = {0}; // 测试用
2025-10-14 00:49:58 +08:00
bool MagStatus = 0; // 磁状态码
bool ThermalStatus = 0; // 热能状态码
void communication_check(); // 通信检查
u8 CAN_Sleep(); // CAN进入睡眠
u8 CAN_Wakeup(); // CAN进入睡眠
2025-09-23 00:27:51 +08:00
};
#ifdef __cplusplus
}
#endif
#endif