CppGenerate/include/sensor_simulator.hpp

160 lines
4.0 KiB
C++
Raw Permalink Normal View History

2026-04-17 09:17:56 +00:00
#ifndef HSM_SENSOR_SIMULATOR_HPP
#define HSM_SENSOR_SIMULATOR_HPP
#include <cstdint>
#include <random>
#include <memory>
/**
* @brief
*/
struct SensorRawData {
float pressure; // 气压值hPa
uint32_t timestamp_ms; // 毫秒级时间戳
uint8_t flags; // 状态标志位
SensorRawData() : pressure(0.0f), timestamp_ms(0), flags(0) {}
SensorRawData(float p, uint32_t ts, uint8_t f = 0)
: pressure(p), timestamp_ms(ts), flags(f) {}
};
/**
* @brief
*
*
* 1.
* 2. 100
* 3. /
* 4.
*/
class SensorSimulator {
public:
/**
* @brief
* @param base_pressure hPa
* @param noise_level
*/
explicit SensorSimulator(float base_pressure = 1013.25f, float noise_level = 0.5f);
/**
* @brief
*/
~SensorSimulator();
/**
* @brief
* @return
*/
bool initialize();
/**
* @brief
* @return
*/
SensorRawData readSensor();
/**
* @brief
* @return hPa
*/
float getCurrentPressure() const;
/**
* @brief
* @param count
* @return
*/
std::vector<SensorRawData> getBufferData(size_t count) const;
/**
* @brief
*/
void clearBuffer();
/**
* @brief
* @param normal true=false=
*/
void setSensorStatus(bool normal);
/**
* @brief
* @return
*/
bool isSensorNormal() const { return sensor_normal_; }
/**
* @brief
* @param pressure
*/
void setBasePressure(float pressure);
/**
* @brief
* @param noise_level
*/
void setNoiseLevel(float noise_level);
/**
* @brief
* @param delta_height
*/
void simulateHeightChange(float delta_height);
/**
* @brief
* @return
*/
size_t getBufferSize() const { return BUFFER_SIZE; }
/**
* @brief
* @return
*/
size_t getDataCount() const { return buffer_count_; }
private:
static const size_t BUFFER_SIZE = 100; // 环形缓冲区容量
std::unique_ptr<SensorRawData[]> buffer_; // 环形缓冲区
size_t buffer_head_; // 缓冲区头指针
size_t buffer_tail_; // 缓冲区尾指针
size_t buffer_count_; // 当前数据点数
float base_pressure_; // 基础气压值
float current_pressure_; // 当前气压值
float noise_level_; // 噪声水平
bool sensor_normal_; // 传感器状态
// 随机数生成器
std::mt19937 rng_;
std::normal_distribution<float> noise_dist_;
/**
* @brief
* @return
*/
float generatePressure();
/**
* @brief
* @param data
*/
void addToBuffer(const SensorRawData& data);
/**
* @brief
* @param current
* @return
*/
size_t nextBufferIndex(size_t current) const;
/**
* @brief
* @param height
* @return
*/
float pressureChangeForHeight(float height) const;
};
#endif // HSM_SENSOR_SIMULATOR_HPP