refactor(FOC): 移除代码冲突标记并优化FOC控制器实现

- 移除了所有git冲突标记(<<<<<<< HEAD, =======, >>>>>>>)
- 清理了校准步骤相关注释代码
- 修复了Iq,Id计算中的角度处理逻辑
- 优化了PID控制器参数设置和初始化流程
- 重构了SVPWM相电压设置方法,移除了重复的相电压设置函数
- 修复了关闭功能和电源电压安全限制的计算

feat(CAN): 增强CAN通信协议支持多种控制模式

- 实现了目标电流、速度、角度的多参数控制
- 添加了PID参数动态配置功能
- 支持7种不同控制模式的切换
- 实现了控制模式切换时PID状态重置机制

refactor(pid): 扩展PID控制器功能接口

- 添加了set_P方法替代原有的set_P_percent
- 新增set_limit方法用于输出限幅控制
- 实现了reset方法用于PID状态重置
- 优化了PID参数设置接口

fix(timer): 优化定时器任务调度机制

- 重构了任务调度的时间片管理逻辑
- 实现了基于查找表的任务分配方式
- 优化了100us、1ms、10ms、100ms等不同周期的任务调度
- 提高了系统实时性表现

perf(config): 优化系统配置和数学常量定义

- 添加了必要的GD32头文件包含
- 重新组织了PID参数命名规范
- 定义了常用数学常量如三角函数相关数值
- 优化了定时器频率和周期计算公式
This commit is contained in:
cbb
2026-03-09 01:48:12 +08:00
parent 1ee2f68a89
commit 2b93a4537c
8 changed files with 212 additions and 399 deletions
+45 -8
View File
@@ -1,5 +1,6 @@
#ifndef __CONFIG_H
#define __CONFIG_H
#include "gd32c10x_eval.h"
#include <cstdint>
// 如果使用CMSIS DSP库,需要定义ARM_MATH_CM4
@@ -54,7 +55,7 @@ constexpr u32 OTA_AVAL = 0x00000000; // 全零表示可用
constexpr u8 PP = 11;
constexpr float DIR = -1.0f;
constexpr u16 REDUCTION_RATIO = 25;
constexpr float CURRENT_OFFSET = -3.31f/2.0f; // 当前电流传感器的零点偏移
constexpr float CURRENT_OFFSET = -3.31f / 2.0f; // 当前电流传感器的零点偏移
constexpr float HOLD_PWM_DUTY = 0.1f;
constexpr u16 ANGLESENS_OFFSET = +15;
constexpr float KE_FACTOR = 0.0f;
@@ -87,17 +88,49 @@ constexpr float CURRENT_KALMAN_PROCESS_NOISE_Q = 10.0f; // 降低过程噪声
constexpr float CURRENT_KALMAN_MEASUREMENT_NOISE_R = 5e-5f; // 增大测量噪声
// PID配置
constexpr float current_loop_p = 6e-2f / 100 * 12; // 电流P
constexpr float vel_loop_p = 0.01f / 100 * 12; // 速度P
constexpr float angle_loop_p = 20.0f / 100 * 12; // 角度P
constexpr float vel_angloop_p = 0.12f / 100 * 12; // 速度 速度-角度P
constexpr float ang_angloop_p = 100.0f / 100 * 12; // 角度 速度-角度P
constexpr float DEFAULT_current_loop_p = 0.1f / 100 * 12; // 电流P
constexpr float current_loop_Ki = 0.55f; // 电流P
constexpr float current_loop_Kd = 3e-4f; // 电流P
constexpr float DEFAULT_vel_loop_p = 0.018f / 100 * 12; // 速度P
constexpr float vel_loop_Ku = 3.600000e-03 * 0.7 * 100 / 12;
constexpr float vel_loop_Tu = 17.0f * 1e-3f;
constexpr float vel_loop_Ki = 1.2 * vel_loop_Ku / vel_loop_Tu;
constexpr float vel_loop_Kd = 0.075 * vel_loop_Ku * vel_loop_Tu * 2;
constexpr float DEFAULT_vel_angloop_p = 0.4f / 100; // 速度 速度-角度P
constexpr float vel_angloop_Ki = 5.0f; // 速度 速度-角度I
constexpr float vel_angloop_Kd = 5e-05f; // 速度 速度-角度D
constexpr float DEFAULT_angle_angloop_p = 150.0f / 100 * 12; // 角度 速度-角度P
constexpr float ang_angloop_Ki = 0.00f; // 角度 速度-角度I
constexpr float ang_angloop_Kd = 8e-04f; // 角度 速度-角度D
constexpr float DEFAULT_angle_loop_p = 60.0f / 100 * 12; // 角度P
constexpr float DEFAULT_angle_MIT_p = 100.0f / 100 * 12; // 角度 MIT P
constexpr float angle_MIT_Ki = 0.0f;
constexpr float angle_MIT_Kd = 8e-04f;
constexpr float DEFAULT_vel_MIT_p = 5.0f / 100; // 速度 MIT P
constexpr float vel_MIT_Ki = 15.0f;
constexpr float vel_MIT_Kd = 1e-04f;
constexpr float DEFAULT_current_MIT_p = 0.2f / 100 * 12; // 电流 MIT P
constexpr float current_MIT_Ki = 0.1f;
constexpr float current_MIT_Kd = 3e-4f;
// 常量定义
constexpr u8 SIZE_FLOAT = sizeof(float);
constexpr u8 SIZE_U32 = sizeof(uint32_t);
constexpr float ADC_MAX = 65536.0f;
constexpr float PI = 3.14159265358979323846f;
constexpr float INV_PI_3 = 3.0f / PI;
constexpr float PI_3 = PI / 3.0f;
constexpr float _1_SQRT3 = 0.57735026919f; // 1/sqrt(3)
constexpr float _3PI_2 = 4.71238898038f;
constexpr float SQRT3 = 1.7320508075688772f;
constexpr float SQRT3DV2 = 0.8660254037844386f; // sqrt(3)/2
constexpr float COS_PI_3 = 0.5f;
constexpr float ANGLE_FACTOR = 25.0f / 180.0f * PI;
constexpr float DEG2RAD = 1.0f / 180.0f * PI;
constexpr float RAD2DEG = 180.0f / PI; // 修正RAD2DEG定义,应该是180/π
@@ -149,8 +182,12 @@ constexpr float ANGLESENS_FACTOR = 1.0f / ADC_MAX * 360.0f;
// TIMER2-任务调度
constexpr int TIMER2_PRESCALER = 119; // 120MHz / 120 = 1MHz
constexpr int TIMER2_PERIOD = 49; // 1MHz / 50 = 50us
constexpr float TIMER2_FREQUENCY = 1 / 5e-5;
constexpr int TIMER2_PERIOD = 49; // 1 / 1MHz * 50 = 50us
constexpr int MOTOR_PERIOD = 2; // 100us/50us = 2
constexpr float TIME_SLICE =
(TIMER2_PERIOD + 1) * (TIMER2_PRESCALER + 1) / 120000000.0f; // 周期
constexpr float TIMER2_FREQUENCY = 1 / TIME_SLICE; // 频率
constexpr float MOTOR_FREQUENCY = 1 / TIME_SLICE / MOTOR_PERIOD; // 频率
// TIMER1-PWM
constexpr int TIMER1_PRESCALER = 0; // 不分频,计数器时钟为120MHz