/** * @file device/ang_sensor.h * @brief 底层设备驱动与外设封装源文件 * @note 本文件仅补充 Doxygen 文档注释,不改变代码逻辑。 */ #ifndef _angsensor_H #define _angsensor_H #ifdef __cplusplus extern "C" { #endif #include "gd32c10x_eval.h" #include #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