refactor(timer): 引入任务调度机制替代硬编码定时操作

通过定义 `TaskScheduler` 结构体和相关枚举类型,
将原TIMER2中断中耦合的操作解耦成多个独立任务标志,
便于后续维护和扩展,并提高系统实时响应能力。

feat(FOC): 优化电压安全范围与PID参数配置
将电压计算中的 `voltage_power_supply` 替换为更安全的 `v_safe`,
并引入电压有效性检测逻辑。同时调整PID控制器参数以适配12V供电场景。
此外,统一布尔变量初始化方式,提升代码可读性和安全性。

chore(build): 更新CMSIS工具链版本依赖
更新vcpkg配置文件中CMSIS Toolbox的版本要求,
从2.11.0升级至2.12.0,确保构建环境兼容最新功能及修复。
This commit is contained in:
cbb
2025-11-13 02:04:58 +08:00
parent db2d08032c
commit 67a1c04458
13 changed files with 161 additions and 110 deletions
+18 -14
View File
@@ -4,12 +4,9 @@
#include "Timer.h"
#include "adc.h"
#include "ang_sensor.h"
#include "gd32c10x_dbg.h"
#include "initial.h"
#include "pid.h"
#include "systick.h"
#include <cmath>
#include <cstdio>
#include <gd32c10x.h>
#include <stdint.h>
@@ -76,13 +73,13 @@ inline void FOC_Controlroller::setPhaseVoltage(float Uq, float Ud) {
Uc = (-Ualpha - sqrt3 * Ubeta) * 0.5f + v_half;
// 计算占空比
float dc_a = _constrain(Ua / voltage_power_supply, 0.0f, 1.0f);
float dc_b = _constrain(Ub / voltage_power_supply, 0.0f, 1.0f);
float dc_c = _constrain(Uc / voltage_power_supply, 0.0f, 1.0f);
float dc_a = _constrain(Ua / v_safe, 0.0f, 1.0f);
float dc_b = _constrain(Ub / v_safe, 0.0f, 1.0f);
float dc_c = _constrain(Uc / v_safe, 0.0f, 1.0f);
// 设置PWM输出
dc_a = dc_a * limit_voltage / voltage_power_supply;
dc_b = dc_b * limit_voltage / voltage_power_supply;
dc_c = dc_c * limit_voltage / voltage_power_supply;
dc_a = dc_a * limit_voltage / v_safe;
dc_b = dc_b * limit_voltage / v_safe;
dc_c = dc_c * limit_voltage / v_safe;
Set_3Pwm(dc_a, dc_b, dc_c);
// std::printf("PWM:%f,%f,%f\n", dc_a, dc_b, dc_c);
}
@@ -101,9 +98,9 @@ void FOC_Controlroller::shut_down() {
*
*/
void FOC_Controlroller::resume() {
thermal_uncontrol = 0;
voltage_error = 0;
is_shut_down = 0;
thermal_uncontrol = false;
voltage_error = false;
is_shut_down = false;
gpio_bit_set(GPIOB, GPIO_PIN_12);
}
/**
@@ -112,7 +109,14 @@ void FOC_Controlroller::resume() {
*/
void FOC_Controlroller::update_power(void) {
voltage_power_supply = sampler.get_vbus_voltage();
v_half = voltage_power_supply * 0.5f;
if (voltage_power_supply < 36.0f and voltage_power_supply > 8.0f) {
voltage_error = false;
v_half = voltage_power_supply * 0.5f;
v_safe = voltage_power_supply * 0.95f;
}
else {
voltage_error = true;
}
}
/**
@@ -158,7 +162,7 @@ float FOC_Controlroller::velocity_open_loop() {
float FOC_Controlroller::angle_control(float target_angle) {
// PID 控制计算
float Uq = angle_loop(target_angle - rt_angle);
// 获取电机轴角度
shaft_angle = KTH7823.getcurrent_sensorangle() * DEG2RAD;
// 设置相电压
+13 -7
View File
@@ -14,10 +14,10 @@ class FOC_Controlroller {
float target_velocity = 0.0f;
float rt_angle = 0.0f;
float rt_velocity = 0.0f;
bool thermal_uncontrol = 0;
bool voltage_error = 0;
bool is_shut_down = 0;
float voltage_power_supply = 24.0f; // 电源电压
bool thermal_uncontrol = false;
bool voltage_error = false;
bool is_shut_down = false;
float voltage_power_supply = 24.0f; // 电源电压
float velocity_open_loop();
float angle_control(float target_angle);
@@ -36,10 +36,16 @@ class FOC_Controlroller {
float Ualpha, Ubeta = 0, Ua = 0, Ub = 0, Uc = 0;
float zero_electric_angle = 0;
float v_half = voltage_power_supply * 0.5f; // 电源电压的一半
float v_safe = voltage_power_supply * 0.95f;
PIDController vel_loop =PIDController{1.8f, 1.2f, 0.0002f, 1000000, voltage_power_supply};
PIDController angle_loop =
PIDController{140.0f, 80.0f, 0.005f, 10000000, voltage_power_supply};
// PIDController vel_loop =PIDController{1.8f, 1.2f, 0.0002f, 1000000,
// voltage_power_supply};//8V
// PIDController angle_loop = PIDController{140.0f, 80.0f, 0.005f, 10000000,
// voltage_power_supply}; // 8V
PIDController vel_loop = PIDController{1.0f, 0.85f, 0.0002f, 1000000,
voltage_power_supply}; // 12V
PIDController angle_loop = PIDController{70.0f, 75.0f, 0.0001f, 10000000,
voltage_power_supply}; // 12V
void setPhaseVoltage(float Uq, float Ud);
};
+1
View File
@@ -2,6 +2,7 @@
#include "ang_sensor.h"
#include <algorithm>
#include <array>
#include <cstdio>
#include <cstring>
#include <stdint.h>
+1 -1
View File
@@ -1,6 +1,7 @@
#ifndef _FILTER_H
#define _FILTER_H
#include <cstdio>
#ifdef __cplusplus
extern "C" {
#endif
@@ -95,7 +96,6 @@ template <typename T> class KalmanFilter {
x += K * (measurement - x); // 更新状态估计
P = (1 - K) * P; // 更新误差协方差
return x;
}
-1
View File
@@ -1,5 +1,4 @@
#include "pid.h"
#include "Timer.h"
#define _constrain(amt, low, high) \
((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))
#include "Can.h"
+2 -3
View File
@@ -2,7 +2,6 @@
#include "Can.h"
#include "DengFOC.h"
#include "adc.h"
#include <cstdio>
extern sampler sampler;
extern CAN_transmiter CAN0_transmiter;
@@ -12,7 +11,7 @@ extern FOC_Controlroller FOC;
((a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c)))
constexpr u16 PROTECT_TIME = 6000; // 过热保护时间,单位*10ms
u16 ThermalController::update() {
u32 ThermalController::update() {
thermal_value = thermal_value * thermalRate;
thermal_value +=
SQUARE(sampler.get_drv_temp()) + SQUARE(sampler.get_mcu_temp());
@@ -38,5 +37,5 @@ u16 ThermalController::update() {
remainingTime10Ms = PROTECT_TIME; // 过热停机10秒
return remainingTime10Ms;
}
return remainingTime10Ms;
return thermal_value;
}
+1 -1
View File
@@ -17,7 +17,7 @@ class ThermalController {
u16 gapTimeMs = 10; // 单次计算间隔ms
u16 remainingTime10Ms = 0; // 过热冷却剩余ms
public:
u16 update();
u32 update();
void reset() {
thermal_value = 0;
remainingTime10Ms = 0;