feat(can): 增强CAN通信缓冲区管理机制
- 实现循环缓冲区队列管理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采样时间配置以优化性能
This commit is contained in:
@@ -81,7 +81,7 @@ void adc_config(void) {
|
||||
adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 6);
|
||||
/* 配置ADC规则通道 */
|
||||
adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_16,
|
||||
ADC_SAMPLETIME_239POINT5);
|
||||
ADC_SAMPLETIME_55POINT5);
|
||||
adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_9, ADC_SAMPLETIME_28POINT5);
|
||||
adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_3, ADC_SAMPLETIME_28POINT5);
|
||||
adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_4, ADC_SAMPLETIME_28POINT5);
|
||||
|
||||
+79
-33
@@ -19,8 +19,6 @@
|
||||
#include <random>
|
||||
|
||||
constexpr u16 CANX = CAN0;
|
||||
|
||||
CAN_transmiter CAN0_transmiter; // CAN初始化
|
||||
extern FOC_Controller FOC;
|
||||
extern sampler sampler;
|
||||
extern angle_sensor KTH7823;
|
||||
@@ -35,6 +33,51 @@ dma_parameter_struct dma_tx_parameter;
|
||||
uint8_t can_rx_buffer[8]; // CAN接收缓冲区
|
||||
uint8_t can_tx_buffer[8]; // CAN发送缓冲区
|
||||
|
||||
CAN_transmiter CAN0_transmiter; // CAN初始化
|
||||
|
||||
// CAN接收缓冲区相关函数实现
|
||||
/*!
|
||||
\brief 检查CAN接收缓冲区是否已满
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval bool 返回true表示缓冲区已满,false表示未满
|
||||
\note 通过比较头指针的下一个位置和尾指针来判断缓冲区是否已满
|
||||
使用循环缓冲区策略,当(rx_buffer_head + 1) %
|
||||
CAN_RX_BUFFER_SIZE等于rx_buffer_tail时, 表示缓冲区已达到最大容量
|
||||
*/
|
||||
bool CAN_transmiter::is_rx_buffer_full() {
|
||||
return ((rx_buffer_head + 1) % CAN_RX_BUFFER_SIZE) == rx_buffer_tail;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief 检查CAN接收缓冲区是否为空
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval bool 返回true表示缓冲区为空,false表示缓冲区不为空
|
||||
\note 通过比较头指针和尾指针是否相等来判断缓冲区是否为空
|
||||
*/
|
||||
bool CAN_transmiter::is_rx_buffer_empty() {
|
||||
return rx_buffer_head == rx_buffer_tail;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 从接收缓冲区中取出一条CAN消息
|
||||
*
|
||||
* 该函数检查接收缓冲区是否为空,如果不为空则取出缓冲区队列头部的消息,
|
||||
* 并更新队列尾部索引。如果缓冲区为空,则返回false。
|
||||
*
|
||||
* @param message 引用类型的输出参数,用于存储取出的CAN接收消息结构体
|
||||
* @return bool 返回操作是否成功,true表示成功取出消息,false表示缓冲区为空
|
||||
*/
|
||||
bool CAN_transmiter::dequeue_rx_message(can_receive_message_struct &message) {
|
||||
if (!is_rx_buffer_empty()) {
|
||||
message = rx_message_buffer[rx_buffer_tail];
|
||||
rx_buffer_tail = (rx_buffer_tail + 1) % CAN_RX_BUFFER_SIZE;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief initialize CAN GPIO
|
||||
\param[in] none
|
||||
@@ -521,30 +564,32 @@ void CAN_transmiter::communication_check() {
|
||||
can0_send_flag = RESET;
|
||||
}
|
||||
|
||||
// CAN0 接收数据标志
|
||||
if (SET == can0_receive_flag) {
|
||||
// printf("\r\n can0 %X receive data:", receive_message.rx_efid);
|
||||
// for (i = 0; i < receive_message.rx_dlen; i++) {
|
||||
// printf(" %02x", receive_message.rx_data[i]);
|
||||
// 处理所有接收到的CAN消息(从环形缓冲区中)
|
||||
can_receive_message_struct current_message;
|
||||
while (dequeue_rx_message(current_message)) {
|
||||
// printf("\r\n can0 %X receive data:", current_message.rx_efid);
|
||||
// for (i = 0; i < current_message.rx_dlen; i++) {
|
||||
// printf(" %02x", current_message.rx_data[i]);
|
||||
// }
|
||||
if (receive_message.rx_efid == CANFD_CMD_ID) {
|
||||
if (current_message.rx_efid == CANFD_CMD_ID) {
|
||||
// 控制模式
|
||||
FOC.control_mode = receive_message.rx_data[0];
|
||||
FOC.control_mode = current_message.rx_data[0];
|
||||
// 目标电压限制
|
||||
FOC.limit_voltage =
|
||||
float(unpack_bytes_to_u16(receive_message.rx_data, 1)) / 1000;
|
||||
float(unpack_bytes_to_u16(current_message.rx_data, 1)) / 1000;
|
||||
// 目标电流
|
||||
target_current_union.f =
|
||||
unpack_bytes_to_float(receive_message.rx_data, 3).f;
|
||||
unpack_bytes_to_float(current_message.rx_data, 3).f;
|
||||
FOC.current_loop.set_limit(target_current_union.f);
|
||||
FOC.vel_inMITloop.set_limit(target_current_union.f);
|
||||
// 目标速度
|
||||
target_velocity_union.f =
|
||||
unpack_bytes_to_float(receive_message.rx_data, 7).f;
|
||||
unpack_bytes_to_float(current_message.rx_data, 7).f;
|
||||
FOC.ang_inVelAngloop.set_limit(target_velocity_union.f);
|
||||
FOC.ang_inMITloop.set_limit(target_velocity_union.f);
|
||||
// 目标角度
|
||||
target_angle_union.f =
|
||||
unpack_bytes_to_float(receive_message.rx_data, 11).f;
|
||||
unpack_bytes_to_float(current_message.rx_data, 11).f;
|
||||
// 赋值
|
||||
FOC.target_angle = target_angle_union.f;
|
||||
FOC.target_velocity = target_velocity_union.f;
|
||||
@@ -553,26 +598,26 @@ void CAN_transmiter::communication_check() {
|
||||
// 解析PID参数
|
||||
float voltage = FOC.limit_voltage;
|
||||
// PID参数
|
||||
u16 P_config = unpack_bytes_to_u16(receive_message.rx_data, 15);
|
||||
FOC.current_loop_p = DEFAULT_current_loop_p * P_config / voltage;
|
||||
u16 P_config = unpack_bytes_to_u16(current_message.rx_data, 15);
|
||||
FOC.cur_loop_p = DEFAULT_current_loop_p * P_config / voltage;
|
||||
FOC.cur_MIT_p = DEFAULT_current_MIT_p * P_config / voltage;
|
||||
|
||||
P_config = unpack_bytes_to_u16(receive_message.rx_data, 17);
|
||||
P_config = unpack_bytes_to_u16(current_message.rx_data, 17);
|
||||
FOC.vel_loop_p = DEFAULT_vel_loop_p * P_config / voltage;
|
||||
|
||||
P_config = unpack_bytes_to_u16(receive_message.rx_data, 19);
|
||||
FOC.angle_loop_p = DEFAULT_angle_loop_p * P_config / voltage;
|
||||
FOC.ang_angloop_p = DEFAULT_ang_angloop_p * P_config / voltage;
|
||||
|
||||
P_config = unpack_bytes_to_u16(receive_message.rx_data, 21);
|
||||
FOC.vel_angloop_p = DEFAULT_vel_angloop_p * P_config;
|
||||
FOC.vel_MIT_p = DEFAULT_vel_MIT_p * P_config;
|
||||
|
||||
} else if (receive_message.rx_efid == CANFD_RESET_ID) {
|
||||
u8 first_byte = receive_message.rx_data[0];
|
||||
u8 eigth_byte = receive_message.rx_data[7];
|
||||
P_config = unpack_bytes_to_u16(current_message.rx_data, 19);
|
||||
FOC.ang_loop_p = DEFAULT_angle_loop_p * P_config / voltage;
|
||||
FOC.ang_angloop_p = DEFAULT_angle_angloop_p * P_config / voltage;
|
||||
FOC.ang_MIT_p = DEFAULT_angle_MIT_p * P_config / voltage;
|
||||
} else if (current_message.rx_efid == CANFD_RESET_ID) {
|
||||
u8 first_byte = current_message.rx_data[0];
|
||||
u8 eigth_byte = current_message.rx_data[7];
|
||||
// 电机重置
|
||||
if (first_byte & 0x01) {
|
||||
motor_reset();
|
||||
testfloat_union.f = KTH7823.getcurrent_sensorangle();
|
||||
}
|
||||
// EEPROM重置
|
||||
if (first_byte >> 1 & 0x01) {
|
||||
@@ -593,16 +638,17 @@ void CAN_transmiter::communication_check() {
|
||||
app_prepare_deep_sleep(true);
|
||||
// CAN_Sleep();
|
||||
}
|
||||
} else if (receive_message.rx_efid == CANFD_VERSION_ID) {
|
||||
} else if (current_message.rx_efid == CANFD_VERSION_ID) {
|
||||
// 处理版本号请求,发送当前系统版本信息
|
||||
Version.sendVersionInfoViaCan();
|
||||
} else if (receive_message.rx_efid == CANFD_BOOT_ID) {
|
||||
} else if (current_message.rx_efid == CANFD_BOOT_ID) {
|
||||
motor_sleep();
|
||||
flashEEP.save_system_config(false);
|
||||
fmc_unlock();
|
||||
OTA_SET(n_APP_LATEST_EN_ADDR, OTA_NOT_AVAL); // 设置APP为不激活
|
||||
fmc_lock();
|
||||
jumpToBOOT();
|
||||
} else if (receive_message.rx_efid == 0x00000102) {
|
||||
} else if (current_message.rx_efid == 0x00000102) {
|
||||
volatile u32 *flash_ptr = (volatile u32 *)n_APP_LATEST_EN_ADDR;
|
||||
u32 app_enable = *flash_ptr;
|
||||
flash_ptr = (volatile u32 *)n_APP_LATEST_CHECK_ADDR;
|
||||
@@ -614,9 +660,8 @@ void CAN_transmiter::communication_check() {
|
||||
CANFD_AsyncSend(CANFD_OTA_ACK_ID, 2, transmit_data);
|
||||
}
|
||||
} else {
|
||||
printf("Unknown CAN ID: %X\n", receive_message.rx_efid);
|
||||
printf("Unknown CAN ID: %X\n", current_message.rx_efid);
|
||||
}
|
||||
can0_receive_flag = RESET;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -662,10 +707,11 @@ void CAN0_RX0_IRQHandler(void) {
|
||||
dma_channel_enable(DMA0, DMA_CH4);
|
||||
|
||||
// 读取接收到的数据
|
||||
can_message_receive(CAN0, CAN_FIFO0, &CAN0_transmiter.receive_message);
|
||||
can_receive_message_struct temp_message;
|
||||
can_message_receive(CAN0, CAN_FIFO0, &temp_message);
|
||||
|
||||
// 接收标志位置1
|
||||
CAN0_transmiter.can0_receive_flag = SET;
|
||||
// 将接收到的消息加入环形缓冲区
|
||||
CAN0_transmiter.enqueue_rx_message_from_isr(temp_message);
|
||||
}
|
||||
|
||||
// 检查其他可能的中断源
|
||||
|
||||
+19
-1
@@ -71,6 +71,8 @@ void can_loopback_test(void);
|
||||
class CAN_transmiter {
|
||||
|
||||
private:
|
||||
static const uint8_t CAN_RX_BUFFER_SIZE = 8; // CAN接收缓冲区大小
|
||||
|
||||
bool is_sleep = false; // 是否休眠
|
||||
|
||||
u8 Target_Control_Mode = 0; // 目标控制模式
|
||||
@@ -100,16 +102,23 @@ class CAN_transmiter {
|
||||
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_receive_flag; // CAN0接收标志
|
||||
FlagStatus can0_send_flag; // CAN0发送标志
|
||||
float_union_t testfloat_union = {0}; // 测试用
|
||||
u16 test_u16 = 0;
|
||||
@@ -123,6 +132,15 @@ class CAN_transmiter {
|
||||
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
|
||||
|
||||
+122
-114
@@ -9,7 +9,7 @@
|
||||
#include "gd32c10x_timer.h"
|
||||
#include "initial.h"
|
||||
|
||||
volatile u32 total_us100_counter = 0; // 总计数器,每100微秒增加1
|
||||
volatile u32 total_us100_counter = 0; // 总计数器,每 100 微秒增加 1
|
||||
extern u16 adc_value[7];
|
||||
extern FOC_Controller FOC;
|
||||
extern TaskScheduler scheduler;
|
||||
@@ -17,25 +17,56 @@ extern CAN_transmiter CAN0_transmiter;
|
||||
// 添加系统运行时间计数器
|
||||
extern u32 cnt_test;
|
||||
|
||||
// 任务调度查找表 - 每 100us 执行的任务 (1ms 周期内)
|
||||
static const u32 task_schedule_100us[10] = {
|
||||
(1u << TASK_MCU_TEMP_UPDATE), // 0: MCU 温度更新
|
||||
(1u << TASK_DRV_TEMP_UPDATE), // 1: 驱动温度更新
|
||||
(1u << TASK_VBUS_UPDATE), // 2: 电压更新
|
||||
0, // 3: 空闲
|
||||
0, // 4: 空闲
|
||||
0, // 5: 空闲
|
||||
0, // 6: 空闲
|
||||
0, // 7: 空闲
|
||||
0, // 8: 空闲
|
||||
(1u << TASK_CAN_TRANSMIT) // 9: CAN 发送
|
||||
};
|
||||
|
||||
// 任务调度查找表 - 每 1ms 执行的任务 (10ms 周期内)
|
||||
static const u32 task_schedule_1ms[10] = {
|
||||
(1u << TASK_THERMAL_CONTROL), // 0: 热量控制
|
||||
(1u << TASK_MAGNETIC_CHECK), // 1: 磁感应强度检测
|
||||
0, 0, 0, 0, 0, 0, 0, 0 // 空闲
|
||||
};
|
||||
|
||||
// 任务调度查找表 - 每 10ms 执行的任务 (100ms 周期内)
|
||||
static const u32 task_schedule_10ms[10] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 当前无任务,预留扩展
|
||||
};
|
||||
|
||||
// 任务调度查找表 - 每 100ms 执行的任务 (1s 周期内)
|
||||
static const u32 task_schedule_100ms[10] = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 当前无任务,预留扩展
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 设置引脚中断优先级
|
||||
* @return void
|
||||
*
|
||||
*设置引脚中断优先级
|
||||
* 设置引脚中断优先级
|
||||
*/
|
||||
void nvic_config(void) {
|
||||
/* 设置NVIC优先级分组,1位抢占优先级,3位响应优先级 */
|
||||
/* 设置 NVIC 优先级分组,1 位抢占优先级,3 位响应优先级 */
|
||||
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
|
||||
/* 使能TIMER2中断,设置抢占优先级为1,子优先级为1 */
|
||||
/* 使能 TIMER2 中断,设置抢占优先级为 1,子优先级为 1 */
|
||||
nvic_irq_enable(TIMER2_IRQn, 0, 1);
|
||||
nvic_irq_enable(TIMER1_IRQn, 0, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 配置定时器2-任务管理
|
||||
* @brief 配置定时器 2-任务管理
|
||||
* @return void
|
||||
*
|
||||
*配置定时器2,用于定时执行FOC控制、采样、CAN发送等任务
|
||||
* 配置定时器 2,用于定时执行 FOC 控制、采样、CAN 发送等任务
|
||||
*/
|
||||
void TIMER2_config(void) {
|
||||
/* ----------------------------------------------------------------------------
|
||||
@@ -46,21 +77,21 @@ void TIMER2_config(void) {
|
||||
*/
|
||||
timer_parameter_struct timer_struct;
|
||||
|
||||
/* 使能TIMER2时钟 */
|
||||
/* 使能 TIMER2 时钟 */
|
||||
rcu_periph_clock_enable(RCU_TIMER2);
|
||||
|
||||
/* 复位TIMER2外设到默认状态 */
|
||||
/* 复位 TIMER2 外设到默认状态 */
|
||||
timer_deinit(TIMER2);
|
||||
/* 初始化TIMER初始化参数结构体 */
|
||||
/* 初始化 TIMER 初始化参数结构体 */
|
||||
timer_struct_para_init(&timer_struct);
|
||||
/* TIMER2配置参数设置 */
|
||||
/* TIMER2 配置参数设置 */
|
||||
timer_struct.prescaler =
|
||||
TIMER2_PRESCALER; /* 预分频值:120分频(从0开始计数) */
|
||||
TIMER2_PRESCALER; /* 预分频值:120 分频 (从 0 开始计数) */
|
||||
timer_struct.alignedmode = TIMER_COUNTER_EDGE; /* 边沿对齐模式 */
|
||||
timer_struct.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */
|
||||
timer_struct.period =
|
||||
TIMER2_PERIOD; /* 自动重装载值:100次计数(从0开始计数) */
|
||||
timer_struct.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1分频 */
|
||||
TIMER2_PERIOD; /* 自动重装载值:100 次计数 (从 0 开始计数) */
|
||||
timer_struct.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1 分频 */
|
||||
timer_init(TIMER2, &timer_struct);
|
||||
|
||||
/* 清除更新中断标志位 */
|
||||
@@ -68,108 +99,85 @@ void TIMER2_config(void) {
|
||||
/* 使能更新中断 */
|
||||
timer_interrupt_enable(TIMER2, TIMER_INT_UP);
|
||||
|
||||
/* 使能TIMER2 */
|
||||
/* 使能 TIMER2 */
|
||||
timer_enable(TIMER2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 配置TIMER2-任务管理中断
|
||||
* @brief 配置 TIMER2-任务管理中断
|
||||
* @return void
|
||||
*/
|
||||
void User_timer_init(void) {
|
||||
/* 配置TIMER2 */
|
||||
/* 配置 TIMER2 */
|
||||
TIMER2_config();
|
||||
/* 配置TIMER2中断 */
|
||||
/* 配置 TIMER2 中断 */
|
||||
nvic_config();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIMER2-任务管理中断处理函数
|
||||
* @brief TIMER2-任务管理中断处理函数 (优化版)
|
||||
* @return void
|
||||
* @note 使用查找表替代多层 switch-case,减少代码复杂度和执行时间
|
||||
* 中断周期:50us
|
||||
* - 每 50us: 电机控制任务 (角度、速度更新)
|
||||
* - 每 100us: 传感器采样、CAN 通信
|
||||
* - 每 1ms: 热量控制、磁检
|
||||
* - 每 10ms/100ms/1s: 预留扩展任务
|
||||
*/
|
||||
void TIMER2_IRQHandler(void) {
|
||||
// 检查更新中断标志并处理
|
||||
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_UP) != RESET) {
|
||||
// 清除更新中断标志
|
||||
timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
|
||||
// 快速路径:检查并清除中断标志
|
||||
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_UP) == RESET) {
|
||||
return;
|
||||
}
|
||||
timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
|
||||
|
||||
// 每50us执行一次的操作 - 电机控制
|
||||
scheduler.taskFlags |=
|
||||
(1 << TASK_ANGLE_UPDATE) | (1 << TASK_VELOCITY_UPDATE);
|
||||
// 固定任务:每 50us 执行电机控制
|
||||
scheduler.taskFlags |= (1u << TASK_ANGLE_UPDATE) |
|
||||
(1u << TASK_VELOCITY_UPDATE) |
|
||||
(1u << TASK_MOTOR_CONTROL);
|
||||
|
||||
// 使用标志位控制,每100us执行一次其他任务
|
||||
static bool isEvenCall = false;
|
||||
isEvenCall = !isEvenCall;
|
||||
|
||||
if (isEvenCall) {
|
||||
// 每100us执行一次的操作 - 保持原有频率
|
||||
|
||||
scheduler.taskFlags |= (1 << TASK_MOTOR_CONTROL);
|
||||
switch (total_us100_counter) { // 每1ms执行的代码,负载均衡
|
||||
case 0: {
|
||||
scheduler.taskFlags |= (1 << TASK_MCU_TEMP_UPDATE);
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
scheduler.taskFlags |= (1 << TASK_DRV_TEMP_UPDATE);
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
scheduler.taskFlags |= (1 << TASK_VBUS_UPDATE);
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
break;
|
||||
}
|
||||
case 9: {
|
||||
scheduler.taskFlags |= (1 << TASK_CAN_TRANSMIT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
total_us100_counter++;
|
||||
if (total_us100_counter > 9) {
|
||||
total_us100_counter = 0;
|
||||
// 可以添加每1ms执行的代码,无负载均衡
|
||||
static u8 ms1_counter = 0;
|
||||
|
||||
switch (ms1_counter) { // 每10ms执行的代码,负载均衡
|
||||
case 0: {
|
||||
scheduler.taskFlags |= (1 << TASK_THERMAL_CONTROL);
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
scheduler.taskFlags |= (1 << TASK_MAGNETIC_CHECK);
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ms1_counter++;
|
||||
if (ms1_counter > 9) {
|
||||
ms1_counter = 0;
|
||||
// 可以添加每10ms执行的代码,无负载均衡
|
||||
static u8 ms10_counter = 0;
|
||||
ms10_counter++;
|
||||
if (ms10_counter > 9) {
|
||||
ms10_counter = 0;
|
||||
// 可以添加每100ms执行的代码
|
||||
static u8 sec_counter = 0;
|
||||
sec_counter++;
|
||||
if (sec_counter > 9) {
|
||||
sec_counter = 0;
|
||||
// 可以添加每1s执行的代码
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 每 100us 执行一次的任务(使用位运算替代布尔变量)
|
||||
static u32 call_counter = 0;
|
||||
call_counter++;
|
||||
|
||||
if ((call_counter & 1u) == 0u) { // 偶数次调用(每 100us)
|
||||
// 使用查找表设置 1ms 周期内的任务
|
||||
scheduler.taskFlags |= task_schedule_100us[total_us100_counter];
|
||||
|
||||
total_us100_counter++;
|
||||
if (total_us100_counter >= 10u) {
|
||||
total_us100_counter = 0;
|
||||
|
||||
// 每 1ms 执行的任务
|
||||
static u32 ms1_counter = 0;
|
||||
ms1_counter++;
|
||||
if (ms1_counter >= 10u) {
|
||||
ms1_counter = 0;
|
||||
|
||||
// 每 10ms 执行的任务
|
||||
static u32 ms10_counter = 0;
|
||||
ms10_counter++;
|
||||
if (ms10_counter >= 10u) {
|
||||
ms10_counter = 0;
|
||||
|
||||
// 每 100ms 执行的任务
|
||||
static u32 ms100_counter = 0;
|
||||
ms100_counter++;
|
||||
if (ms100_counter >= 10u) {
|
||||
ms100_counter = 0;
|
||||
// 每 1s 任务预留位置
|
||||
}
|
||||
scheduler.taskFlags |= task_schedule_100ms[ms100_counter];
|
||||
}
|
||||
scheduler.taskFlags |= task_schedule_10ms[ms10_counter];
|
||||
}
|
||||
scheduler.taskFlags |= task_schedule_1ms[ms1_counter];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIMER1-PWM GPIO配置函数
|
||||
* @brief TIMER1-PWM GPIO 配置函数
|
||||
*/
|
||||
void timer1_gpio_config(void) {
|
||||
// 时钟使能
|
||||
@@ -178,7 +186,7 @@ void timer1_gpio_config(void) {
|
||||
rcu_periph_clock_enable(RCU_AF);
|
||||
rcu_periph_clock_enable(RCU_TIMER1);
|
||||
|
||||
// TIMER1 GPIO初始化
|
||||
// TIMER1 GPIO 初始化
|
||||
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
|
||||
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
|
||||
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
|
||||
@@ -188,7 +196,7 @@ void timer1_gpio_config(void) {
|
||||
gpio_bit_set(GPIOB, GPIO_PIN_0);
|
||||
gpio_bit_set(GPIOB, GPIO_PIN_12);
|
||||
|
||||
/* 配置TIMER1完全重映射*/
|
||||
/* 配置 TIMER1 完全重映射*/
|
||||
gpio_pin_remap_config(GPIO_TIMER1_FULL_REMAP, DISABLE);
|
||||
}
|
||||
|
||||
@@ -199,13 +207,13 @@ void timer1_pwm_config(void) {
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_oc_parameter_struct timer_ocinitpara;
|
||||
|
||||
// 使能TIMER1时钟
|
||||
// 使能 TIMER1 时钟
|
||||
rcu_periph_clock_enable(RCU_TIMER1);
|
||||
|
||||
// 复位TIMER1
|
||||
// 复位 TIMER1
|
||||
timer_deinit(TIMER1);
|
||||
|
||||
// 配置TIMER1基本参数
|
||||
// 配置 TIMER1 基本参数
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
timer_initpara.prescaler = TIMER1_PRESCALER; // 预分频器
|
||||
timer_initpara.counterdirection = TIMER_COUNTER_UP; // 向上计数
|
||||
@@ -218,23 +226,23 @@ void timer1_pwm_config(void) {
|
||||
timer_ocinitpara.outputstate = TIMER_CCX_ENABLE;
|
||||
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
|
||||
|
||||
// 配置三个通道为PWM模式
|
||||
// 配置三个通道为 PWM 模式
|
||||
timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocinitpara);
|
||||
timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocinitpara);
|
||||
timer_channel_output_config(TIMER1, TIMER_CH_2, &timer_ocinitpara);
|
||||
|
||||
// 配置第四个通道(CH3)用于在PWM周期x%位置触发中断
|
||||
// 配置第四个通道 (CH3) 用于在 PWM 周期 x% 位置触发中断
|
||||
timer_ocinitpara.outputstate =
|
||||
TIMER_CCX_DISABLE; // 不输出PWM波形,仅用于触发中断
|
||||
TIMER_CCX_DISABLE; // 不输出 PWM 波形,仅用于触发中断
|
||||
timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
|
||||
timer_channel_output_config(TIMER1, TIMER_CH_3, &timer_ocinitpara);
|
||||
|
||||
// 设置PWM模式和占空比
|
||||
// 设置 PWM 模式和占空比
|
||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_0, TIMER_OC_MODE_PWM0);
|
||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0);
|
||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_2, TIMER_OC_MODE_PWM0);
|
||||
timer_channel_output_mode_config(TIMER1, TIMER_CH_3,
|
||||
TIMER_OC_MODE_PWM0); // CH3也设为PWM模式
|
||||
TIMER_OC_MODE_PWM0); // CH3 也设为 PWM 模式
|
||||
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0,
|
||||
TIMER1_DEFAULT_PULSE);
|
||||
@@ -242,7 +250,7 @@ void timer1_pwm_config(void) {
|
||||
TIMER1_DEFAULT_PULSE);
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_2,
|
||||
TIMER1_DEFAULT_PULSE);
|
||||
// 设置CH3比较值为PWM周期x%位置
|
||||
// 设置 CH3 比较值为 PWM 周期 x% 位置
|
||||
timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_3,
|
||||
TIMER1_CH3_COMPARE_VALUE);
|
||||
|
||||
@@ -250,26 +258,26 @@ void timer1_pwm_config(void) {
|
||||
timer_primary_output_config(TIMER1, ENABLE);
|
||||
timer_auto_reload_shadow_enable(TIMER1);
|
||||
|
||||
// 使能CH3比较中断
|
||||
// 使能 CH3 比较中断
|
||||
timer_interrupt_enable(TIMER1, TIMER_INT_CH3);
|
||||
|
||||
// 启动TIMER1
|
||||
// 启动 TIMER1
|
||||
timer_enable(TIMER1);
|
||||
}
|
||||
|
||||
// 添加TIMER1-PWM中断处理函数
|
||||
// 添加 TIMER1-PWM 中断处理函数
|
||||
/**
|
||||
* @brief TIMER1中断处理函数
|
||||
* @brief TIMER1 中断处理函数
|
||||
* @return void
|
||||
*/
|
||||
void TIMER1_IRQHandler(void) {
|
||||
// 检查CH3比较中断标志并处理
|
||||
// 检查 CH3 比较中断标志并处理
|
||||
if (timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_CH3) != RESET) {
|
||||
// 清除CH3比较中断标志
|
||||
// 清除 CH3 比较中断标志
|
||||
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_CH3);
|
||||
|
||||
scheduler.taskFlags |= (1 << TASK_CURRENT_UPDATE);
|
||||
// 例如触发ADC采样或其他操作
|
||||
// 例如触发 ADC 采样或其他操作
|
||||
}
|
||||
}
|
||||
|
||||
@@ -305,4 +313,4 @@ void Set_3Pwm(float duty_cycle0, float duty_cycle1, float duty_cycle2) {
|
||||
TIMER_CH0CV(TIMER1) = (uint32_t)(duty_cycle0 * TIMER1_PERIOD);
|
||||
TIMER_CH1CV(TIMER1) = (uint32_t)(duty_cycle1 * TIMER1_PERIOD);
|
||||
TIMER_CH2CV(TIMER1) = (uint32_t)(duty_cycle2 * TIMER1_PERIOD);
|
||||
}
|
||||
}
|
||||
@@ -6,10 +6,10 @@
|
||||
#include "gd32c10x_dma.h"
|
||||
#include "gd32c10x_gpio.h"
|
||||
#include "gd32c10x_spi.h"
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <stdint.h>
|
||||
#define ANG_SENS_DEBUG 1
|
||||
|
||||
#define ANG_SPI_CS_ENABLE() gpio_bit_reset(GPIOA, GPIO_PIN_15)
|
||||
#define ANG_SPI_CS_DISABLE() gpio_bit_set(GPIOA, GPIO_PIN_15)
|
||||
@@ -199,13 +199,14 @@ u8 angle_sensor::init(void) {
|
||||
spi_dma_init(); // DMA初始化
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ 角度传感器类 设置当前角度
|
||||
* @param angle 输出级角度值
|
||||
* @return 0成功/1失败
|
||||
*/
|
||||
u8 angle_sensor::angle_set(float angle) {
|
||||
m_cumulativeAngle = angle * REDUCTION_RATIO; // 从Flash读取上次保存的角度
|
||||
m_cumulativeAngle = angle * REDUCTION_RATIO;
|
||||
m_firstUpdate = true;
|
||||
return 0;
|
||||
}
|
||||
@@ -237,6 +238,7 @@ inline float angle_sensor::read_sensorAngle(void) {
|
||||
|
||||
return (float)FAULT_RECEIVE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 更新传感器角度(输出级)
|
||||
* @return 累积角度值
|
||||
@@ -286,6 +288,6 @@ float angle_sensor::getRawAngle(void) const { return m_cumulativeAngle; }
|
||||
* @brief 获取角度值(原始级)
|
||||
* @return 角度值(原始级)
|
||||
*/
|
||||
float angle_sensor::getcurrent_sensorangle(void) const {
|
||||
float angle_sensor::getcurrent_sensorangle(void) const{
|
||||
return current_angle_deg;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ int spi_rw_half_word_dma(uint32_t spi_periph, uint16_t tx_data,
|
||||
|
||||
class angle_sensor {
|
||||
public:
|
||||
float calibration_angle = 195.0f;
|
||||
u8 init(void);
|
||||
float read_sensorAngle(void);
|
||||
float update(void);
|
||||
@@ -22,6 +23,7 @@ class angle_sensor {
|
||||
float getRawAngle(void) const;
|
||||
float getcurrent_sensorangle(void) const;
|
||||
u8 angle_set(float angle);
|
||||
u8 set_calibration(float angle);
|
||||
|
||||
protected:
|
||||
float m_cumulativeAngle;
|
||||
|
||||
Reference in New Issue
Block a user