2025-09-23 00:27:51 +08:00
|
|
|
#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;
|
|
|
|
|
|
2025-10-02 02:52:07 +08:00
|
|
|
// 添加辅助函数用于将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);
|
|
|
|
|
void can_config(void);
|
|
|
|
|
void canfd_config(void);
|
|
|
|
|
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:
|
|
|
|
|
u8 controller_Mode;
|
|
|
|
|
float_union_t limit_velocity_union;
|
|
|
|
|
u16 limit_voltage;
|
|
|
|
|
short mcu_temp;
|
|
|
|
|
short motor_temp;
|
|
|
|
|
u16 Power_Voltage;
|
|
|
|
|
float_union_t rt_angle_union;
|
|
|
|
|
float_union_t rt_velocity_union;
|
|
|
|
|
u8 Status_code;
|
|
|
|
|
float_union_t target_angle_union;
|
|
|
|
|
u8 Target_Control_Mode;
|
|
|
|
|
float_union_t target_velocity_union;
|
|
|
|
|
float_union_t u_current_union;
|
|
|
|
|
float_union_t w_current_union;
|
|
|
|
|
float_union_t v_current_union;
|
|
|
|
|
|
|
|
|
|
u8 transmit_mailbox; // 初始化为无mailbox状态
|
|
|
|
|
u32 timeout; // 默认超时为0xFFFF
|
|
|
|
|
u8 transmit_data[64]; // CANFD最大64字节
|
|
|
|
|
|
|
|
|
|
can_trasnmit_message_struct transmit_message;
|
|
|
|
|
can_receive_message_struct receive_message;
|
|
|
|
|
|
|
|
|
|
can_fdframe_struct can_fd_parameter;
|
|
|
|
|
can_fd_tdc_struct can_fd_tdc_parameter;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// 添加构造函数声明
|
|
|
|
|
CAN_transmiter();
|
|
|
|
|
u8 CAN_Send(u32 ID, u8 length, u8 *data);
|
|
|
|
|
u8 CANFD_Send(u32 ID, u8 length, u8 *data);
|
|
|
|
|
void update();
|
|
|
|
|
u8 CAN_all_Send();
|
|
|
|
|
u8 CANFD_all_Send();
|
|
|
|
|
u8 CAN_Receive();
|
|
|
|
|
u8 CANFD_Receive();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endif
|