#include "imu_sensor_thing.h" #include "esp_log.h" #include #include #define TAG "ImuSensorThing" namespace iot { ImuSensorThing::ImuSensorThing(QMI8658A* sensor) : Thing("ImuSensor", "姿态传感器"), imu_sensor_(sensor), motion_detected_(false), motion_threshold_(1.5f) { // 初始化数据 memset(&latest_data_, 0, sizeof(latest_data_)); // 定义属性:加速度计数据 properties_.AddNumberProperty("accel_x", "X轴加速度 (mg)", [this]() -> int { return static_cast(latest_data_.acc_x * 1000); }); properties_.AddNumberProperty("accel_y", "Y轴加速度 (mg)", [this]() -> int { return static_cast(latest_data_.acc_y * 1000); }); properties_.AddNumberProperty("accel_z", "Z轴加速度 (mg)", [this]() -> int { return static_cast(latest_data_.acc_z * 1000); }); // 定义属性:陀螺仪数据 properties_.AddNumberProperty("gyro_x", "X轴角速度 (mdps)", [this]() -> int { return static_cast(latest_data_.gyro_x * 1000); }); properties_.AddNumberProperty("gyro_y", "Y轴角速度 (mdps)", [this]() -> int { return static_cast(latest_data_.gyro_y * 1000); }); properties_.AddNumberProperty("gyro_z", "Z轴角速度 (mdps)", [this]() -> int { return static_cast(latest_data_.gyro_z * 1000); }); // 定义属性:运动检测状态 properties_.AddBooleanProperty("motion_detected", "是否检测到运动", [this]() -> bool { return motion_detected_; }); // 定义属性:传感器状态 properties_.AddBooleanProperty("sensor_available", "传感器是否可用", [this]() -> bool { return imu_sensor_ != nullptr; }); // 定义方法:校准传感器 methods_.AddMethod("Calibrate", "校准传感器", ParameterList(), [this](const ParameterList& parameters) { if (imu_sensor_) { ESP_LOGI(TAG, "开始校准IMU传感器"); // 这里可以添加校准逻辑 } }); // 定义方法:设置运动检测阈值 methods_.AddMethod("SetMotionThreshold", "设置运动检测阈值", ParameterList({ Parameter("threshold", "运动检测阈值 (g)", kValueTypeNumber, true) }), [this](const ParameterList& parameters) { float threshold = static_cast(parameters["threshold"].number()) / 1000.0f; if (threshold > 0.1f && threshold < 10.0f) { motion_threshold_ = threshold; ESP_LOGI(TAG, "设置运动检测阈值为: %.3f g", motion_threshold_); } else { ESP_LOGW(TAG, "运动检测阈值超出范围 (0.1-10.0 g)"); } }); // 定义方法:获取传感器信息 methods_.AddMethod("GetSensorInfo", "获取传感器信息", ParameterList(), [this](const ParameterList& parameters) { if (imu_sensor_) { ESP_LOGI(TAG, "IMU传感器: QMI8658A"); ESP_LOGI(TAG, "当前运动阈值: %.3f g", motion_threshold_); } }); } void ImuSensorThing::UpdateData(const qmi8658a_data_t& data) { latest_data_ = data; // 计算加速度幅值来检测运动 float accel_magnitude = sqrt(data.acc_x * data.acc_x + data.acc_y * data.acc_y + data.acc_z * data.acc_z); // 检测运动(排除重力影响,1g ≈ 9.8m/s²) float motion_level = fabs(accel_magnitude - 1.0f); bool current_motion = motion_level > motion_threshold_; if (current_motion != motion_detected_) { motion_detected_ = current_motion; ESP_LOGI(TAG, "运动状态变化: %s (幅值: %.3f g)", motion_detected_ ? "检测到运动" : "静止", motion_level); } } void ImuSensorThing::SetMotionDetected(bool detected) { if (motion_detected_ != detected) { motion_detected_ = detected; ESP_LOGI(TAG, "运动检测状态更新: %s", detected ? "运动中" : "静止"); } } } // namespace iot