fix:修复任务调度不准确的问题,

feat:优化电流采样速率
feat:优化PIDramp效率
feat:优化角度采样效率
fix:回滚CAN打包逻辑
feat:去除timer3
feat(FOC): 优化帕克逆变换与克拉克逆变换计算方式
将原有的三角函数计算替换为查表法,提高运算效率。同时添加了 sqrt(3)/2 的预计算常量,并优化了电压限制部分的计算逻辑,减少重复运算。
This commit is contained in:
cbb
2025-11-23 18:31:47 +08:00
parent 59f161bd73
commit 871e33e10f
14 changed files with 503 additions and 189 deletions
+34 -110
View File
@@ -8,6 +8,7 @@
#include "gd32c10x_rcu.h"
#include "gd32c10x_timer.h"
#include "initial.h"
#include <cstdio>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@@ -43,10 +44,8 @@ void nvic_config(void) {
/* 设置NVIC优先级分组,1位抢占优先级,3位响应优先级 */
nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
/* 使能TIMER2中断,设置抢占优先级为1,子优先级为1 */
nvic_irq_enable(TIMER2_IRQn, 1, 1);
nvic_irq_enable(TIMER3_IRQn, 1, 2);
nvic_irq_enable(TIMER1_IRQn, 2,
1); // 使能TIMER1中断,抢占优先级2,子优先级1
nvic_irq_enable(TIMER2_IRQn, 0, 1);
nvic_irq_enable(TIMER1_IRQn, 0, 1);
}
/**
@@ -90,47 +89,6 @@ void TIMER2_config(void) {
timer_enable(TIMER2);
}
/**
* @brief 配置定时器3
* @return void
*
*配置定时器3,用于诊断恢复
*/
void TIMER3_config(void) {
/* ----------------------------------------------------------------------------
TIMER3 Configuration:
TIMER3CLK = SystemCoreClock/120 = 1MHz, the period is 50us(50/1000000 =
50us).
----------------------------------------------------------------------------
*/
timer_parameter_struct timer_struct;
/* 使能TIMER3时钟 */
rcu_periph_clock_enable(RCU_TIMER3);
/* 复位TIMER3外设到默认状态 */
timer_deinit(TIMER3);
/* 初始化TIMER初始化参数结构体 */
timer_struct_para_init(&timer_struct);
/* TIMER3配置参数设置 */
timer_struct.prescaler =
TIMER3_PRESCALER; /* 预分频值:120分频(从0开始计数) */
timer_struct.alignedmode = TIMER_COUNTER_EDGE; /* 边沿对齐模式 */
timer_struct.counterdirection = TIMER_COUNTER_UP; /* 向上计数模式 */
timer_struct.period =
TIMER3_PERIOD; /* 自动重装载值:50次计数(从0开始计数) */
timer_struct.clockdivision = TIMER_CKDIV_DIV1; /* 时钟分频值:1分频 */
timer_init(TIMER3, &timer_struct);
/* 清除更新中断标志位 */
timer_interrupt_flag_clear(TIMER3, TIMER_INT_FLAG_UP);
/* 使能更新中断 */
timer_interrupt_enable(TIMER3, TIMER_INT_UP);
/* 使能TIMER3 */
timer_enable(TIMER3);
}
/**
* @brief 配置TIMER2中断
* @return void
@@ -155,76 +113,71 @@ uint64_t get_system_runtime_us(void) { return system_runtime_us; }
* @return void
*/
void TIMER2_IRQHandler(void) {
static u8 mode = 0;
// 检查更新中断标志并处理
if (timer_interrupt_flag_get(TIMER2, TIMER_INT_UP) != RESET) {
// 清除更新中断标志
timer_interrupt_flag_clear(TIMER2, TIMER_INT_UP);
// 每100us执行一次的操作
total_us100_counter++;
scheduler.taskFlags |= (1 << TASK_ANGLE_UPDATE) |
(1 << TASK_VELOCITY_UPDATE) |
(1 << TASK_MOTOR_CONTROL);
switch (total_us100_counter) { // 每1ms执行的代码,负载均衡
case 1: {
case 0: {
scheduler.taskFlags |= (1 << TASK_MCU_TEMP_UPDATE);
break;
}
case 2: {
case 1: {
scheduler.taskFlags |= (1 << TASK_DRV_TEMP_UPDATE);
break;
}
case 3: {
case 2: {
scheduler.taskFlags |= (1 << TASK_VBUS_UPDATE);
break;
}
case 4: {
case 3: {
break;
}
case 9: {
CAN0_transmiter.can0_send_flag = SET;
scheduler.taskFlags |= (1 << TASK_CAN_TRANSMIT);
break;
}
case 9: {
mode = FOC.control_mode;
break;
}
}
if (total_us100_counter >= 10) {
total_us100_counter++;
if (total_us100_counter > 9) {
total_us100_counter = 0;
// 可以添加每1ms执行的代码,无负载均衡
static u8 ms10_counter = 0;
ms10_counter++;
switch (ms10_counter) { // 每10ms执行的代码,负载均衡
case 1: {
static u8 ms1_counter = 0;
switch (ms1_counter) { // 每10ms执行的代码,负载均衡
case 0: {
scheduler.taskFlags |= (1 << TASK_THERMAL_CONTROL);
break;
}
case 2: {
case 1: {
scheduler.taskFlags |= (1 << TASK_MAGNETIC_CHECK);
break;
}
case 2: {
break;
}
if (ms10_counter >= 10) {
ms10_counter = 0;
}
ms1_counter++;
if (ms1_counter > 9) {
ms1_counter = 0;
// 可以添加每10ms执行的代码,无负载均衡
static u8 ms100_counter = 0;
ms100_counter++;
switch (ms100_counter) { // 每100ms执行的代码,负载均衡
case 1: {
break;
}
if (ms100_counter >= 10) {
ms100_counter = 0;
// 可以添加每100ms执行的代码
static u8 sec_counter = 0;
sec_counter++;
if (sec_counter >= 10) {
sec_counter = 0;
// 可以添加每1s执行的代码
}
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执行的代码
}
}
}
@@ -232,33 +185,6 @@ void TIMER2_IRQHandler(void) {
}
}
/**
* @brief TIMER3中断处理函数
* @return void
*/
void TIMER3_IRQHandler(void) {
// 检查更新中断标志并处理
if (timer_interrupt_flag_get(TIMER3, TIMER_INT_UP) != RESET) {
// 清除更新中断标志
timer_interrupt_flag_clear(TIMER3, TIMER_INT_UP);
static u8 count = 0;
count++;
// 10ms测试代码
if (count >= 10) {
count = 0;
// 100ms测试代码
static u8 count_10 = 0;
count_10++;
if (count_10 >= 10) {
count_10 = 0;
// 1s测试代码
}
}
}
}
/**
* @brief TIMER1 GPIO配置函数
*/
@@ -343,8 +269,6 @@ void timer1_pwm_config(void) {
// 使能CH3比较中断
timer_interrupt_enable(TIMER1, TIMER_INT_CH3);
nvic_irq_enable(TIMER1_IRQn, 0,
2); // 使能TIMER1中断,抢占优先级2,子优先级1
// 启动TIMER1
timer_enable(TIMER1);