添加了完整的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文档生成。
111 lines
2.5 KiB
C++
111 lines
2.5 KiB
C++
/**
|
|
* @file device/ang_sensor.h
|
|
* @brief 底层设备驱动与外设封装源文件
|
|
* @note 本文件仅补充 Doxygen 文档注释,不改变代码逻辑。
|
|
*/
|
|
|
|
#ifndef _angsensor_H
|
|
#define _angsensor_H
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
#include "gd32c10x_eval.h"
|
|
#include <cstring>
|
|
#include "config.h"
|
|
|
|
/**
|
|
* @brief spi_rw_half_word 函数
|
|
* @param[in] spi_periph 参数
|
|
* @param[in] i_HalfWord 参数
|
|
* @return u16 返回值
|
|
*/
|
|
u16 spi_rw_half_word(u32 spi_periph, const u16 i_HalfWord);
|
|
/**
|
|
* @brief spi_rw_half_word_dma 函数
|
|
* @param[in] spi_periph 参数
|
|
* @param[in] tx_data 参数
|
|
* @param[in] rx_data 参数
|
|
* @return int 返回值
|
|
*/
|
|
int spi_rw_half_word_dma(uint32_t spi_periph, uint16_t tx_data,
|
|
uint16_t *rx_data);
|
|
|
|
|
|
/**
|
|
* @brief angle_sensor 类或结构体
|
|
*/
|
|
class angle_sensor {
|
|
public:
|
|
float calibration_angle = ANGLESENS_CALIBRATION_ANGLE;
|
|
/**
|
|
* @brief 初始化对象或模块
|
|
* @return u8 返回值
|
|
*/
|
|
u8 init(void);
|
|
/**
|
|
* @brief 读取 read_sensorAngle 相关数据
|
|
* @return float 返回值
|
|
*/
|
|
float read_sensorAngle(void);
|
|
/**
|
|
* @brief 更新对象内部状态或采样数据
|
|
* @return float 返回值
|
|
*/
|
|
float update(void);
|
|
/**
|
|
* @brief 复位对象内部状态
|
|
* @return void
|
|
*/
|
|
void reset(void);
|
|
/**
|
|
* @brief 获取 getAngle 对应的数据或状态
|
|
* @return float 返回值
|
|
*/
|
|
float getAngle(void) const;
|
|
/**
|
|
* @brief 获取 getRawAngle 对应的数据或状态
|
|
* @return float 返回值
|
|
*/
|
|
float getRawAngle(void) const;
|
|
/**
|
|
* @brief 获取 getcurrent_sensorangle 对应的数据或状态
|
|
* @return float 返回值
|
|
*/
|
|
float getcurrent_sensorangle(void) const;
|
|
/**
|
|
* @brief angle_set 函数
|
|
* @param[in] angle 参数
|
|
* @return u8 返回值
|
|
*/
|
|
u8 angle_set(float angle);
|
|
/**
|
|
* @brief 设置 set_calibration 对应的参数或状态
|
|
* @param[in] angle 参数
|
|
* @return u8 返回值
|
|
*/
|
|
u8 set_calibration(float angle);
|
|
|
|
protected:
|
|
float m_cumulativeAngle;
|
|
float current_angle_deg;
|
|
float current_angle_rad;
|
|
bool m_firstUpdate;
|
|
float m_lastAngle;
|
|
float m_lastcumulativeAngle;
|
|
float velocity[VEL_FILTER_SIZE] = {0.0f};
|
|
};
|
|
/**
|
|
* @brief 快速角度归一化到 [0, 360)
|
|
* 用 while 减法替代 fmodf,在角度变化量很小时(电机控制场景)
|
|
* 通常只需 0~1 次减法,远快于 fmodf 的 ~30-50 周期开销。
|
|
*/
|
|
inline float normalizeAngle360(float angle) {
|
|
while (angle >= 360.0f) angle -= 360.0f;
|
|
while (angle < 0.0f) angle += 360.0f;
|
|
return angle;
|
|
}
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif |