添加了完整的Doxygen文档注释到FOC算法模块的所有函数、类和变量,提升了代码可读性和维护性。同时优化了SVPWM算法实现,将atan2f替换为符号法扇区判断,显著提升计算效率(约15周期vs atan2f 300周期),并改进了arm_sin_cos_f32替代sqrtf提高性能。 feat(FOC): 统一电气角度计算避免重复运算 重构代码以统一电气角度计算,在各个控制循环中只计算一次电气角度并传递给需要的函数,避免了多次重复的fmodf运算和角度归一化操作,提升了实时控制性能。 refactor(Filter): 改进α-β观测器角度处理精度 修改角度卡尔曼滤波器在归一化角度空间[0,360)内工作,避免大累积角度的浮点精度损失问题,确保静止状态下不会产生虚假速度,同时保持与下游代码的兼容性。 refactor: 为算法模块添加完整文档注释 为所有算法模块(FOC、Filter、pid、thermal)添加了详细的Doxygen文档注释,包括函数说明、参数描述、返回值说明等,便于代码理解和API文档生成。
267 lines
7.0 KiB
C++
267 lines
7.0 KiB
C++
/**
|
|
* @file Algorithm/FOC.h
|
|
* @brief 算法模块源文件
|
|
* @note 本文件仅补充 Doxygen 文档注释,不改变代码逻辑。
|
|
*/
|
|
|
|
// 函数声明
|
|
#ifndef _FOC_H
|
|
#define _FOC_H
|
|
#include "config.h"
|
|
#include "pid.h"
|
|
#include <cstdint>
|
|
|
|
/**
|
|
* @brief FOC_Controller 类或结构体
|
|
*/
|
|
class FOC_Controller {
|
|
|
|
public:
|
|
FOC_Controller();
|
|
|
|
// 初始化函数
|
|
/**
|
|
* @brief 初始化对象或模块
|
|
* @return u8 返回值
|
|
*/
|
|
u8 init();
|
|
|
|
u8 control_mode = 0;
|
|
float limit_voltage = 0.0f;
|
|
|
|
|
|
float target_angle = 0.0f;
|
|
float target_velocity = 0.0f;
|
|
float target_current = 0.0f;
|
|
|
|
|
|
float rt_angle = 0.0f;
|
|
float rt_velocity = 0.0f;
|
|
float rt_current = 0.0f;
|
|
|
|
bool over_current = false;
|
|
bool voltage_error = false;
|
|
bool is_shut_down = false;
|
|
bool thermal_shutdown = false; // 过热关机标志
|
|
bool emergency_shutdown = false; // 紧急电压跌落关机标志
|
|
|
|
float voltage_power_supply = 24.0f; // 电源电压
|
|
float v_safe = voltage_power_supply / 1.05f;
|
|
float duty_a = 0, duty_b = 0, duty_c = 0;
|
|
float Ua = 0, Ub = 0, Uc = 0;
|
|
|
|
// 快速电压检测相关成员(基于跌幅检测)
|
|
float vbus_reference = 0.0f; // 基准电压(峰值检测,跟踪当前额定电压)
|
|
float last_vbus_fast = 0.0f; // 上次电压值(快速检测用)
|
|
u32 vbus_drop_count = 0; // 电压跌落计数器(防抖)
|
|
|
|
float cur_loop_p = DEFAULT_current_loop_p;
|
|
float vel_loop_p = DEFAULT_vel_loop_p;
|
|
float ang_loop_p = DEFAULT_angle_loop_p;
|
|
|
|
float vel_angloop_p = DEFAULT_vel_angloop_p;
|
|
float ang_angloop_p = DEFAULT_angle_angloop_p;
|
|
|
|
float ang_MIT_p = DEFAULT_angle_MIT_p;
|
|
float vel_MIT_p = DEFAULT_vel_MIT_p;
|
|
float cur_MIT_p = DEFAULT_current_MIT_p;
|
|
|
|
/**
|
|
* @brief velocity_open_loop 函数
|
|
* @return float 返回值
|
|
*/
|
|
float velocity_open_loop(void);
|
|
/**
|
|
* @brief angle_control 函数
|
|
* @return float 返回值
|
|
*/
|
|
float angle_control(void);
|
|
/**
|
|
* @brief angle_control_with_velocity_limit 函数
|
|
* @return float 返回值
|
|
*/
|
|
float angle_control_with_velocity_limit(void);
|
|
/**
|
|
* @brief MIT_Mode 函数
|
|
* @return float 返回值
|
|
*/
|
|
float MIT_Mode(void);
|
|
/**
|
|
* @brief velocity_control 函数
|
|
* @return float 返回值
|
|
*/
|
|
float velocity_control(void);
|
|
/**
|
|
* @brief current_control 函数
|
|
* @return float 返回值
|
|
*/
|
|
float current_control(void);
|
|
|
|
/**
|
|
* @brief 更新 power 相关状态
|
|
* @param[in] voltage 参数
|
|
* @return void
|
|
*/
|
|
void update_power(float voltage);
|
|
/**
|
|
* @brief DFOC_alignSensor 函数
|
|
* @param[in] _PP 参数
|
|
* @return void
|
|
*/
|
|
void DFOC_alignSensor(int _PP);
|
|
/**
|
|
* @brief shut_down 函数
|
|
* @return void
|
|
*/
|
|
void shut_down(void);
|
|
/**
|
|
* @brief resume 函数
|
|
* @return void
|
|
*/
|
|
void resume(void);
|
|
/**
|
|
* @brief drv_stop 函数
|
|
* @return void
|
|
*/
|
|
void drv_stop(void);
|
|
/**
|
|
* @brief drv_hold 函数
|
|
* @return float 返回值
|
|
*/
|
|
float drv_hold(void);
|
|
/**
|
|
* @brief PWM_out 函数
|
|
* @return float 返回值
|
|
*/
|
|
float PWM_out(void);
|
|
/**
|
|
* @brief shut_down_thermal 函数
|
|
* @return void
|
|
*/
|
|
void shut_down_thermal(void); // 过热关机
|
|
/**
|
|
* @brief resume_from_thermal 函数
|
|
* @return void
|
|
*/
|
|
void resume_from_thermal(void); // 过热恢复
|
|
/**
|
|
* @brief fast_voltage_check 函数
|
|
* @return void
|
|
*/
|
|
void fast_voltage_check(void); // 快速电压检测(100us周期调用)
|
|
|
|
// 齿槽转矩补偿
|
|
bool cogging_comp_enabled = false;
|
|
float cogging_comp_scale = COGGING_COMP_SCALE;
|
|
float cogging_table[COGGING_TABLE_SIZE] = {0}; // 齿槽补偿表-正转 (mA)
|
|
float cogging_table_rev[COGGING_TABLE_SIZE] = {0}; // 齿槽补偿表-反转 (mA)
|
|
/**
|
|
* @brief 获取 get_cogging_compensation 对应的数据或状态
|
|
* @param[in] angle_el 参数
|
|
* @return float 返回值
|
|
*/
|
|
inline float get_cogging_compensation(float angle_el);
|
|
|
|
// === 校准相关(DEBUG,平时不启用)===
|
|
// 启用方法:config.h 中 FOC_DEBUG 改为 1
|
|
#if FOC_DEBUG
|
|
volatile bool cogging_calib_request = false;
|
|
/**
|
|
* @brief calibrate_cogging_tick 函数
|
|
* @return void
|
|
*/
|
|
void calibrate_cogging_tick(void);
|
|
/**
|
|
* @brief CogCalibState 枚举定义
|
|
*/
|
|
enum class CogCalibState : u8 {
|
|
IDLE = 0,
|
|
SETTLING,
|
|
SAMPLING,
|
|
STOPPING,
|
|
DONE
|
|
};
|
|
CogCalibState cog_calib_state = CogCalibState::IDLE;
|
|
u8 cog_calib_run = 0;
|
|
u32 cog_calib_sample_count = 0;
|
|
u32 cog_calib_settle_tick = 0;
|
|
u32 cog_calib_stop_tick = 0;
|
|
u8 cog_calib_saved_mode = 0;
|
|
float cog_calib_saved_vel = 0;
|
|
static float cog_calib_run_data[6][COGGING_TABLE_SIZE]; // 3正+3反
|
|
static float cog_calib_accum[COGGING_TABLE_SIZE];
|
|
static u32 cog_calib_count[COGGING_TABLE_SIZE];
|
|
#endif // FOC_DEBUG
|
|
|
|
// 此处默认值不作数
|
|
PIDController vel_loop =
|
|
PIDController{DEFAULT_vel_loop_p * 100 / limit_voltage, vel_loop_Ki,
|
|
vel_loop_Kd, 10000, voltage_power_supply}; // 12V
|
|
PIDController angle_loop =
|
|
PIDController{DEFAULT_angle_loop_p * 100 / limit_voltage, 100.0f, 0.2f,
|
|
10000000, voltage_power_supply}; // 12V
|
|
PIDController current_loop = PIDController{
|
|
DEFAULT_current_loop_p * 100 / limit_voltage, current_loop_Ki,
|
|
current_loop_Kd, 1500, voltage_power_supply}; // 12V
|
|
PIDController vel_inVelAngloop =
|
|
PIDController{DEFAULT_vel_angloop_p * 100, vel_angloop_Ki,
|
|
vel_angloop_Kd, 1e6, v_safe};
|
|
PIDController ang_inVelAngloop =
|
|
PIDController{DEFAULT_angle_angloop_p * 100 / voltage_power_supply,
|
|
ang_angloop_Ki, ang_angloop_Kd, 1e4, 100};
|
|
|
|
PIDController ang_inMITloop =
|
|
PIDController{DEFAULT_angle_MIT_p * 100 / limit_voltage, angle_MIT_Ki,
|
|
angle_MIT_Kd, 1e5, 100};
|
|
PIDController vel_inMITloop = PIDController{
|
|
DEFAULT_vel_MIT_p * 100, vel_MIT_Ki, vel_MIT_Kd, 1e9, 1000};
|
|
PIDController current_inMITloop = PIDController{
|
|
DEFAULT_current_loop_p * 100 / limit_voltage, current_loop_Ki,
|
|
current_loop_Kd, 1500, voltage_power_supply}; // 12V
|
|
|
|
/**
|
|
* @brief 更新 pid_limits 相关状态
|
|
* @return void
|
|
*/
|
|
void update_pid_limits() {
|
|
vel_loop.limit = voltage_power_supply;
|
|
angle_loop.limit = voltage_power_supply;
|
|
current_loop.limit = voltage_power_supply;
|
|
vel_inVelAngloop.limit = voltage_power_supply;
|
|
}
|
|
|
|
protected:
|
|
float shaft_angle = 0;
|
|
float Ualpha, Ubeta = 0;
|
|
float zero_electric_angle = 0;
|
|
float v_half = voltage_power_supply * 0.5f; // 电源电压的一半
|
|
float startup_ramp_val = 0.0f; // 软启动系数
|
|
float startup_ramp_step = 0.01f; // 启动斜坡步长,可调节以控制启动速度
|
|
/**
|
|
* @brief 设置 setSVPWM_PhaseVoltage 对应的参数或状态
|
|
* @param[in] Uq 参数
|
|
* @param[in] Ud 参数
|
|
* @param[in] angle_el 参数
|
|
* @return void
|
|
*/
|
|
void setSVPWM_PhaseVoltage(float Uq, float Ud, float angle_el);
|
|
/**
|
|
* @brief cal_Iq_Id 函数
|
|
* @param[in] current_a 参数
|
|
* @param[in] current_b 参数
|
|
* @param[in] current_c 参数
|
|
* @param[in] angle_el 参数
|
|
* @return float 返回值
|
|
*/
|
|
float cal_Iq_Id(float current_a, float current_b, float current_c, float angle_el);
|
|
};
|
|
|
|
// 定义函数指针类型
|
|
typedef float (FOC_Controller::*FOCFunctionPtr)(void);
|
|
|
|
/**
|
|
* @brief calibrate_zero_position 函数
|
|
* @return void
|
|
*/
|
|
void calibrate_zero_position(void);
|
|
#endif |