- 实现循环缓冲区队列管理CAN消息接收 - 添加缓冲区满/空状态检查函数 - 实现消息出队列dequeue_rx_message功能 - 在中断处理程序中添加消息入队列机制 - 优化CAN消息处理流程,支持批量处理 refactor(motor): 优化电机控制类结构和安全机制 - 重命名PID控制器参数变量名以提高可读性 - 添加过热关机标志位thermal_shutdown和相关处理函数 - 实现过热关机shut_down_thermal和过热恢复resume_from_thermal功能 - 调整关机逻辑,添加drv_stop调用确保电机完全停止 - 修正电流环控制中的占空比设置,改为单相保持 feat(motor): 实现FOC控制算法改进和零点校准功能 - 实现电机零点校准函数calibrate_zero_position用于位置补偿 - 修正FOC控制器中的电气角度计算,集成KTH7823校准角度补偿 - 新增d轴电流计算功能 - 重构PID参数初始化方式,使用统一的电压限制配置 - 更新SVPWM相电压设置算法,改进角度归一化处理 - 修改缺省扇区输出占空比从0.0f改为0.5f以提高稳定性 fix(control): 修复控制回路方向和频率计算问题 - 修正角度控制回路中的误差计算方向 - 修正开环速度控制中的频率参数使用 - 修正速度环控制中的误差计算方向 - 修正MIT模式下多级PID控制器的参数设置和误差计算 refactor(filter): 修正编码器速度计算算法 - 移除速度计算中的冗余乘法因子2 feat(pid): 扩展PID控制器功能 - 添加reset方法用于重置PID内部状态 chore(build): 更新开发工具版本和ADC配置 - 更新csolution版本至2.13.0 - 调整ADC采样时间配置以优化性能
150 lines
4.7 KiB
C++
150 lines
4.7 KiB
C++
#ifndef _CAN_H
|
|
#define _CAN_H
|
|
|
|
#include <stdint.h>
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <cstdint>
|
|
#include "gd32c10x_can.h"
|
|
|
|
// 定义一个联合体类型用于float和byte数组之间的转换
|
|
typedef union {
|
|
float f;
|
|
u8 b[4];
|
|
} float_union_t;
|
|
|
|
// 添加辅助函数用于将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数组中解包short类型数据
|
|
static inline short unpack_bytes_to_short(u8 *buffer, u16 offset) {
|
|
short value = (short)(buffer[offset] | (buffer[offset + 1] << 8));
|
|
return value;
|
|
}
|
|
|
|
// 添加辅助函数用于从u8数组中解包u16类型数据
|
|
static inline u16 unpack_bytes_to_u16(u8 *buffer, u16 offset) {
|
|
u16 value = (u16)(buffer[offset] | (buffer[offset + 1] << 8));
|
|
return value;
|
|
}
|
|
|
|
// 添加辅助函数用于从u8数组中解包float类型数据
|
|
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];
|
|
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发送)
|
|
void jumpToBOOT();
|
|
|
|
void can_loopback_init(void);
|
|
void can_loopback_test(void);
|
|
|
|
/*!
|
|
\brief CAN通信类
|
|
\note 封装CAN发送和接收功能
|
|
*/
|
|
class CAN_transmiter {
|
|
|
|
private:
|
|
static const uint8_t CAN_RX_BUFFER_SIZE = 8; // CAN接收缓冲区大小
|
|
|
|
bool is_sleep = false; // 是否休眠
|
|
|
|
u8 Target_Control_Mode = 0; // 目标控制模式
|
|
u8 controller_Mode = 0; // 当前模式
|
|
short mcu_temp = 0; // MCU温度
|
|
short motor_temp = 0; // 电机温度
|
|
u16 Power_Voltage = 0; // 电源电压
|
|
|
|
u16 limit_voltage = 0; // 限制电压
|
|
|
|
float_union_t rt_angle_union = {0}; // 实时角度
|
|
float_union_t rt_velocity_union = {0}; // 实时速度
|
|
float_union_t rt_current_union = {0}; // 实时电流
|
|
|
|
float_union_t target_angle_union = {0}; // 目标角度
|
|
float_union_t target_velocity_union = {0}; // 目标速度
|
|
float_union_t target_current_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相电流
|
|
|
|
bool MotorStatus = 0; // 电机状态码
|
|
u8 EEPROMStatus = 0; // EEPROM状态码
|
|
|
|
can_trasnmit_message_struct transmit_message; // 发送消息结构体
|
|
u8 transmit_mailbox = CAN_NOMAILBOX; // 初始化为无mailbox状态
|
|
u8 transmit_data[64]; // CANFD最大64字节
|
|
|
|
// CAN接收消息队列
|
|
can_receive_message_struct rx_message_buffer[CAN_RX_BUFFER_SIZE];
|
|
volatile uint8_t rx_buffer_head = 0;
|
|
volatile uint8_t rx_buffer_tail = 0;
|
|
|
|
void update(); // 更新数据
|
|
u8 CAN_Receive(); // CAN接收
|
|
u8 CANFD_Receive(); // CANFD接收
|
|
bool is_rx_buffer_full();
|
|
bool is_rx_buffer_empty();
|
|
bool dequeue_rx_message(can_receive_message_struct& message);
|
|
|
|
public:
|
|
// 添加构造函数声明
|
|
u8 init(void);
|
|
|
|
can_receive_message_struct receive_message; // 接收消息结构体
|
|
FlagStatus can0_send_flag; // CAN0发送标志
|
|
float_union_t testfloat_union = {0}; // 测试用
|
|
u16 test_u16 = 0;
|
|
|
|
bool MagStatus = 0; // 磁状态码
|
|
bool ThermalStatus = 0; // 热能状态码
|
|
void communication_check(); // 通信检查
|
|
u8 CAN_Sleep(); // CAN进入睡眠
|
|
u8 CAN_Wakeup(); // CAN进入睡眠
|
|
u8 CAN_AsyncSend(u32 ID, u8 length, u8 *data); // CAN异步发送(使用DMA)
|
|
u8 CANFD_AsyncSend(u32 ID, u8 length, u8 *data); // CANFD异步发送(使用DMA)
|
|
u8 CAN_all_Send(); // CAN发送
|
|
u8 CANFD_all_Send(); // CANFD发送
|
|
|
|
// 中断处理函数需要访问的公共接口
|
|
void enqueue_rx_message_from_isr(const can_receive_message_struct& message) {
|
|
if (!is_rx_buffer_full()) {
|
|
rx_message_buffer[rx_buffer_head] = message;
|
|
rx_buffer_head = (rx_buffer_head + 1) % CAN_RX_BUFFER_SIZE;
|
|
}
|
|
// 如果缓冲区满,丢弃新消息
|
|
}
|
|
};
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif |