249 lines
6.8 KiB
C++
249 lines
6.8 KiB
C++
#include "sensor/imu_processor.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
namespace borla {
|
|
namespace {
|
|
|
|
float Magnitude(const ImuSample& sample)
|
|
{
|
|
return std::sqrt(
|
|
sample.ax * sample.ax +
|
|
sample.ay * sample.ay +
|
|
sample.az * sample.az);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
ImuProcessor::ImuProcessor()
|
|
{
|
|
Reset();
|
|
}
|
|
|
|
void ImuProcessor::Reset()
|
|
{
|
|
filter_initialized_ = false;
|
|
filtered_ = {};
|
|
still_ = false;
|
|
still_frame_count_ = 0;
|
|
pending_batch_ = {};
|
|
next_sequence_ = 0;
|
|
next_gravity_sequence_ = 0;
|
|
freefall_candidate_ = false;
|
|
freefall_confirmed_ = false;
|
|
freefall_started_ms_ = 0;
|
|
last_freefall_active_ms_ = 0;
|
|
landing_window_ = false;
|
|
landing_deadline_ms_ = 0;
|
|
landing_duration_ms_ = 0;
|
|
landing_peak_g_ = 0.0F;
|
|
last_gravity_event_ms_.fill(0);
|
|
}
|
|
|
|
ImuProcessorOutput ImuProcessor::Process(
|
|
const ImuSample& raw,
|
|
uint64_t now_ms)
|
|
{
|
|
ImuProcessorOutput output{};
|
|
const ImuSample current = Filter(raw);
|
|
const float deviation_g = std::fabs(Magnitude(current) - 1.0F);
|
|
|
|
if (still_) {
|
|
if (deviation_g >= kResumeDeviationG) {
|
|
still_ = false;
|
|
still_frame_count_ = 0;
|
|
}
|
|
} else if (deviation_g < kStillDeviationG) {
|
|
still_frame_count_++;
|
|
if (still_frame_count_ >= kStillFrameCount) {
|
|
still_ = true;
|
|
still_frame_count_ = 0;
|
|
FlushPartialBatch(&output);
|
|
}
|
|
} else {
|
|
still_frame_count_ = 0;
|
|
}
|
|
|
|
if (!still_) {
|
|
UpdateBatch(current, now_ms, &output);
|
|
}
|
|
UpdateGravity(raw, now_ms, &output);
|
|
return output;
|
|
}
|
|
|
|
ImuSample ImuProcessor::Filter(const ImuSample& raw)
|
|
{
|
|
if (!filter_initialized_) {
|
|
filtered_ = raw;
|
|
filter_initialized_ = true;
|
|
return filtered_;
|
|
}
|
|
const float incoming = 1.0F - kFilterAlpha;
|
|
filtered_.ax = kFilterAlpha * filtered_.ax + incoming * raw.ax;
|
|
filtered_.ay = kFilterAlpha * filtered_.ay + incoming * raw.ay;
|
|
filtered_.az = kFilterAlpha * filtered_.az + incoming * raw.az;
|
|
filtered_.gx = kFilterAlpha * filtered_.gx + incoming * raw.gx;
|
|
filtered_.gy = kFilterAlpha * filtered_.gy + incoming * raw.gy;
|
|
filtered_.gz = kFilterAlpha * filtered_.gz + incoming * raw.gz;
|
|
return filtered_;
|
|
}
|
|
|
|
void ImuProcessor::UpdateBatch(
|
|
const ImuSample& filtered,
|
|
uint64_t now_ms,
|
|
ImuProcessorOutput* output)
|
|
{
|
|
if (pending_batch_.count == 0) {
|
|
pending_batch_.sequence = next_sequence_++;
|
|
pending_batch_.timestamp_ms_base = now_ms;
|
|
pending_batch_.sample_rate_hz = 50;
|
|
}
|
|
pending_batch_.samples[pending_batch_.count++] = filtered;
|
|
if (pending_batch_.count == pending_batch_.samples.size()) {
|
|
output->has_batch = true;
|
|
output->batch = pending_batch_;
|
|
pending_batch_ = {};
|
|
}
|
|
}
|
|
|
|
void ImuProcessor::FlushPartialBatch(ImuProcessorOutput* output)
|
|
{
|
|
if (pending_batch_.count == 0 || output->has_batch) {
|
|
return;
|
|
}
|
|
output->has_batch = true;
|
|
output->batch = pending_batch_;
|
|
pending_batch_ = {};
|
|
}
|
|
|
|
void ImuProcessor::UpdateGravity(
|
|
const ImuSample& raw,
|
|
uint64_t now_ms,
|
|
ImuProcessorOutput* output)
|
|
{
|
|
const float magnitude_g = Magnitude(raw);
|
|
|
|
if (magnitude_g < kFreefallThresholdG) {
|
|
if (!freefall_candidate_) {
|
|
freefall_candidate_ = true;
|
|
freefall_started_ms_ = now_ms;
|
|
}
|
|
const uint64_t duration = now_ms - freefall_started_ms_;
|
|
if (!freefall_confirmed_ && duration >= kFreefallConfirmMs) {
|
|
freefall_confirmed_ = true;
|
|
last_freefall_active_ms_ = now_ms;
|
|
TryEmitGravity(
|
|
GravityAction::kFreefall,
|
|
now_ms,
|
|
static_cast<uint32_t>(duration),
|
|
magnitude_g,
|
|
output);
|
|
} else if (freefall_confirmed_ &&
|
|
now_ms - last_freefall_active_ms_ >=
|
|
kFreefallActivePeriodMs) {
|
|
last_freefall_active_ms_ = now_ms;
|
|
TryEmitGravity(
|
|
GravityAction::kFreefallActive,
|
|
now_ms,
|
|
static_cast<uint32_t>(duration),
|
|
magnitude_g,
|
|
output);
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (freefall_candidate_) {
|
|
const uint64_t duration = now_ms - freefall_started_ms_;
|
|
if (freefall_confirmed_) {
|
|
landing_window_ = true;
|
|
landing_deadline_ms_ = now_ms + kLandingWindowMs;
|
|
landing_duration_ms_ = static_cast<uint32_t>(duration);
|
|
landing_peak_g_ = magnitude_g;
|
|
}
|
|
freefall_candidate_ = false;
|
|
freefall_confirmed_ = false;
|
|
}
|
|
|
|
if (landing_window_) {
|
|
landing_peak_g_ = std::max(landing_peak_g_, magnitude_g);
|
|
if (landing_peak_g_ >= kSevereThresholdG) {
|
|
landing_window_ = false;
|
|
TryEmitGravity(
|
|
GravityAction::kSevereImpact,
|
|
now_ms,
|
|
landing_duration_ms_,
|
|
landing_peak_g_,
|
|
output);
|
|
return;
|
|
}
|
|
if (landing_peak_g_ >= kDroppedThresholdG) {
|
|
landing_window_ = false;
|
|
TryEmitGravity(
|
|
GravityAction::kDropped,
|
|
now_ms,
|
|
landing_duration_ms_,
|
|
landing_peak_g_,
|
|
output);
|
|
return;
|
|
}
|
|
if (now_ms >= landing_deadline_ms_) {
|
|
landing_window_ = false;
|
|
if (landing_peak_g_ >= kCaughtThresholdG) {
|
|
TryEmitGravity(
|
|
GravityAction::kCaught,
|
|
now_ms,
|
|
landing_duration_ms_,
|
|
landing_peak_g_,
|
|
output);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (magnitude_g >= kSevereThresholdG) {
|
|
TryEmitGravity(
|
|
GravityAction::kSevereImpact,
|
|
now_ms,
|
|
0,
|
|
magnitude_g,
|
|
output);
|
|
} else if (magnitude_g >= kDroppedThresholdG) {
|
|
TryEmitGravity(
|
|
GravityAction::kImpact,
|
|
now_ms,
|
|
0,
|
|
magnitude_g,
|
|
output);
|
|
}
|
|
}
|
|
|
|
void ImuProcessor::TryEmitGravity(
|
|
GravityAction action,
|
|
uint64_t now_ms,
|
|
uint32_t duration_ms,
|
|
float peak_g,
|
|
ImuProcessorOutput* output)
|
|
{
|
|
if (output->has_gravity_event) {
|
|
return;
|
|
}
|
|
const size_t index = static_cast<size_t>(action);
|
|
const uint64_t previous = last_gravity_event_ms_[index];
|
|
if (previous != 0 && now_ms - previous < kActionCooldownMs) {
|
|
return;
|
|
}
|
|
last_gravity_event_ms_[index] = now_ms;
|
|
output->has_gravity_event = true;
|
|
output->gravity_event = {
|
|
.action = action,
|
|
.sequence = next_gravity_sequence_++,
|
|
.timestamp_ms = now_ms,
|
|
.duration_ms = duration_ms,
|
|
.magnitude_g = peak_g,
|
|
.peak_g = peak_g,
|
|
};
|
|
}
|
|
|
|
} // namespace borla
|