Files
Motor_v2/Robot_V2/device/ang_sensor.h
T

111 lines
2.5 KiB
C++
Raw Normal View History

/**
* @file device/ang_sensor.h
* @brief 底层设备驱动与外设封装源文件
* @note 本文件仅补充 Doxygen 文档注释,不改变代码逻辑。
*/
2025-10-31 01:17:52 +08:00
#ifndef _angsensor_H
#define _angsensor_H
2025-09-23 00:27:51 +08:00
#ifdef __cplusplus
extern "C" {
#endif
#include "gd32c10x_eval.h"
2026-01-24 01:50:39 +08:00
#include <cstring>
#include "config.h"
2025-09-23 00:27:51 +08:00
/**
* @brief spi_rw_half_word 函数
* @param[in] spi_periph 参数
* @param[in] i_HalfWord 参数
* @return u16 返回值
*/
2025-09-23 00:27:51 +08:00
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 返回值
*/
2025-12-08 02:48:59 +08:00
u8 init(void);
/**
* @brief 读取 read_sensorAngle 相关数据
* @return float 返回值
*/
2025-11-02 01:54:01 +08:00
float read_sensorAngle(void);
/**
* @brief 更新对象内部状态或采样数据
* @return float 返回值
*/
2025-11-02 01:54:01 +08:00
float update(void);
/**
* @brief 复位对象内部状态
* @return void
*/
2025-11-02 01:54:01 +08:00
void reset(void);
/**
* @brief 获取 getAngle 对应的数据或状态
* @return float 返回值
*/
2025-11-02 01:54:01 +08:00
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;
2025-11-02 01:54:01 +08:00
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;
}
2025-09-23 00:27:51 +08:00
#ifdef __cplusplus
}
#endif
2025-11-01 21:03:57 +08:00
#endif