82 lines
2.3 KiB
C++
82 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include "sensor/sensor_types.h"
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
|
|
namespace borla {
|
|
|
|
struct ImuProcessorOutput {
|
|
bool has_batch = false;
|
|
ImuBatch batch{};
|
|
bool has_gravity_event = false;
|
|
GravityEvent gravity_event{};
|
|
};
|
|
|
|
class ImuProcessor final {
|
|
public:
|
|
ImuProcessor();
|
|
|
|
void Reset();
|
|
ImuProcessorOutput Process(
|
|
const ImuSample& raw,
|
|
uint64_t now_ms);
|
|
|
|
bool still() const { return still_; }
|
|
|
|
private:
|
|
static constexpr float kFilterAlpha = 0.75F;
|
|
static constexpr float kStillDeviationG = 0.010F;
|
|
static constexpr float kResumeDeviationG = 0.050F;
|
|
static constexpr uint16_t kStillFrameCount = 50;
|
|
static constexpr float kFreefallThresholdG = 0.30F;
|
|
static constexpr uint32_t kFreefallConfirmMs = 60;
|
|
static constexpr uint32_t kFreefallActivePeriodMs = 500;
|
|
static constexpr uint32_t kLandingWindowMs = 800;
|
|
static constexpr float kCaughtThresholdG = 1.40F;
|
|
static constexpr float kDroppedThresholdG = 4.50F;
|
|
static constexpr float kSevereThresholdG = 7.00F;
|
|
static constexpr uint32_t kActionCooldownMs = 1000;
|
|
|
|
ImuSample Filter(const ImuSample& raw);
|
|
void UpdateBatch(
|
|
const ImuSample& filtered,
|
|
uint64_t now_ms,
|
|
ImuProcessorOutput* output);
|
|
void FlushPartialBatch(ImuProcessorOutput* output);
|
|
void UpdateGravity(
|
|
const ImuSample& raw,
|
|
uint64_t now_ms,
|
|
ImuProcessorOutput* output);
|
|
void TryEmitGravity(
|
|
GravityAction action,
|
|
uint64_t now_ms,
|
|
uint32_t duration_ms,
|
|
float peak_g,
|
|
ImuProcessorOutput* output);
|
|
|
|
bool filter_initialized_ = false;
|
|
ImuSample filtered_{};
|
|
bool still_ = false;
|
|
uint16_t still_frame_count_ = 0;
|
|
ImuBatch pending_batch_{};
|
|
uint32_t next_sequence_ = 0;
|
|
uint32_t next_gravity_sequence_ = 0;
|
|
|
|
bool freefall_candidate_ = false;
|
|
bool freefall_confirmed_ = false;
|
|
uint64_t freefall_started_ms_ = 0;
|
|
uint64_t last_freefall_active_ms_ = 0;
|
|
bool landing_window_ = false;
|
|
uint64_t landing_deadline_ms_ = 0;
|
|
uint32_t landing_duration_ms_ = 0;
|
|
float landing_peak_g_ = 0.0F;
|
|
std::array<
|
|
uint64_t,
|
|
static_cast<size_t>(GravityAction::kCount)>
|
|
last_gravity_event_ms_{};
|
|
};
|
|
|
|
} // namespace borla
|