bolar_2 backup
This commit is contained in:
219
host_tests/sensor_pipeline_test.cc
Normal file
219
host_tests/sensor_pipeline_test.cc
Normal file
@@ -0,0 +1,219 @@
|
||||
#include "sensor/battery_estimator.h"
|
||||
#include "sensor/imu_processor.h"
|
||||
#include "sensor/sensor_message_codec.h"
|
||||
#include "sensor/touch_channel.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
namespace {
|
||||
|
||||
using borla::GravityAction;
|
||||
using borla::BatteryEstimator;
|
||||
using borla::ImuBatch;
|
||||
using borla::ImuProcessor;
|
||||
using borla::ImuSample;
|
||||
using borla::SensorMessageCodec;
|
||||
using borla::TouchAction;
|
||||
using borla::TouchChannel;
|
||||
using borla::TouchEvent;
|
||||
using borla::TouchTransition;
|
||||
using borla::TouchZone;
|
||||
|
||||
void TestTouchTransitionsArePaired()
|
||||
{
|
||||
TouchChannel channel;
|
||||
for (uint32_t value :
|
||||
{1000U, 1010U, 990U, 1005U, 995U, 1000U, 1002U, 998U}) {
|
||||
channel.AddCalibrationSample(value);
|
||||
}
|
||||
assert(channel.FinishCalibration());
|
||||
assert(channel.threshold() >= 1290.0F);
|
||||
|
||||
TouchTransition transition{};
|
||||
bool down = false;
|
||||
uint64_t down_at_ms = 0;
|
||||
for (uint64_t now_ms = 0; now_ms <= 400; now_ms += 50) {
|
||||
if (channel.Process(1800, now_ms, &transition)) {
|
||||
down = true;
|
||||
down_at_ms = now_ms;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert(down);
|
||||
assert(transition.action == TouchAction::kDown);
|
||||
assert(transition.timestamp_ms == down_at_ms);
|
||||
assert(down_at_ms <= 400);
|
||||
assert(channel.pressed());
|
||||
|
||||
assert(channel.Process(1000, down_at_ms + 500, &transition));
|
||||
assert(transition.action == TouchAction::kUp);
|
||||
assert(transition.duration_ms == 500);
|
||||
assert(!channel.pressed());
|
||||
}
|
||||
|
||||
void TestImuBatchAndStillness()
|
||||
{
|
||||
ImuProcessor processor;
|
||||
size_t full_batches = 0;
|
||||
size_t partial_batches = 0;
|
||||
for (uint64_t i = 0; i < 50; ++i) {
|
||||
const auto output = processor.Process(
|
||||
ImuSample{0.0F, 0.0F, 1.0F, 0.0F, 0.0F, 0.0F},
|
||||
i * 20);
|
||||
if (output.has_batch) {
|
||||
if (output.batch.count == 5) {
|
||||
full_batches++;
|
||||
} else {
|
||||
partial_batches++;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert(processor.still());
|
||||
assert(full_batches == 9);
|
||||
assert(partial_batches == 1);
|
||||
|
||||
const auto wake = processor.Process(
|
||||
ImuSample{0.0F, 0.0F, 1.5F, 0.0F, 0.0F, 0.0F},
|
||||
1000);
|
||||
assert(!processor.still());
|
||||
assert(!wake.has_batch);
|
||||
for (uint64_t i = 1; i <= 4; ++i) {
|
||||
const auto output = processor.Process(
|
||||
ImuSample{0.0F, 0.0F, 1.5F, 0.0F, 0.0F, 0.0F},
|
||||
1000 + i * 20);
|
||||
if (i < 4) {
|
||||
assert(!output.has_batch);
|
||||
} else {
|
||||
assert(output.has_batch);
|
||||
assert(output.batch.count == 5);
|
||||
assert(output.batch.sample_rate_hz == 50);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TestGravityStateMachine()
|
||||
{
|
||||
ImuProcessor processor;
|
||||
processor.Process(ImuSample{0.0F, 0.0F, 0.1F}, 1000);
|
||||
processor.Process(ImuSample{0.0F, 0.0F, 0.1F}, 1020);
|
||||
processor.Process(ImuSample{0.0F, 0.0F, 0.1F}, 1040);
|
||||
const auto freefall =
|
||||
processor.Process(ImuSample{0.0F, 0.0F, 0.1F}, 1060);
|
||||
assert(freefall.has_gravity_event);
|
||||
assert(
|
||||
freefall.gravity_event.action == GravityAction::kFreefall);
|
||||
assert(freefall.gravity_event.duration_ms == 60);
|
||||
|
||||
const auto dropped =
|
||||
processor.Process(ImuSample{0.0F, 0.0F, 5.0F}, 1080);
|
||||
assert(dropped.has_gravity_event);
|
||||
assert(dropped.gravity_event.action == GravityAction::kDropped);
|
||||
assert(std::fabs(dropped.gravity_event.peak_g - 5.0F) < 0.001F);
|
||||
|
||||
ImuProcessor impact_processor;
|
||||
const auto impact = impact_processor.Process(
|
||||
ImuSample{0.0F, 0.0F, 7.2F}, 3000);
|
||||
assert(impact.has_gravity_event);
|
||||
assert(
|
||||
impact.gravity_event.action ==
|
||||
GravityAction::kSevereImpact);
|
||||
}
|
||||
|
||||
void TestProtocolMessages()
|
||||
{
|
||||
char buffer[768]{};
|
||||
size_t size = 0;
|
||||
const TouchEvent touch{
|
||||
.zone = TouchZone::kBelly,
|
||||
.action = TouchAction::kUp,
|
||||
.timestamp_ms = 123456,
|
||||
.duration_ms = 800,
|
||||
};
|
||||
assert(SensorMessageCodec::BuildTouch(
|
||||
touch, buffer, sizeof(buffer), &size));
|
||||
assert(std::strstr(buffer, "\"event\":\"touch\"") != nullptr);
|
||||
assert(std::strstr(buffer, "\"touch_zone\":\"touch_5\"") != nullptr);
|
||||
assert(std::strstr(buffer, "\"duration_ms\":800") != nullptr);
|
||||
|
||||
ImuBatch batch{};
|
||||
batch.sequence = 42;
|
||||
batch.timestamp_ms_base = 9000;
|
||||
batch.count = 5;
|
||||
for (size_t i = 0; i < batch.count; ++i) {
|
||||
batch.samples[i] = ImuSample{
|
||||
0.1F * static_cast<float>(i),
|
||||
-0.2F,
|
||||
1.0F,
|
||||
2.0F,
|
||||
3.0F,
|
||||
4.0F,
|
||||
};
|
||||
}
|
||||
assert(SensorMessageCodec::BuildImu(
|
||||
batch, buffer, sizeof(buffer), &size));
|
||||
assert(std::strstr(buffer, "\"event\":\"imu\"") != nullptr);
|
||||
assert(std::strstr(buffer, "\"seq\":42") != nullptr);
|
||||
assert(std::strstr(buffer, "\"hz\":50") != nullptr);
|
||||
assert(!SensorMessageCodec::BuildImu(
|
||||
batch, buffer, 32, &size));
|
||||
|
||||
const borla::GravityEvent gravity{
|
||||
.action = GravityAction::kFreefall,
|
||||
.sequence = 12,
|
||||
.timestamp_ms = 123500,
|
||||
.duration_ms = 60,
|
||||
.magnitude_g = 0.22F,
|
||||
.peak_g = 0.22F,
|
||||
};
|
||||
assert(SensorMessageCodec::BuildGravity(
|
||||
gravity, buffer, sizeof(buffer), &size));
|
||||
assert(std::strstr(buffer, "\"seq\":12") != nullptr);
|
||||
assert(
|
||||
std::strstr(
|
||||
buffer,
|
||||
"\"gravity_action\":\"freefall\"") != nullptr);
|
||||
assert(std::strstr(buffer, "\"gravity_zone\":\"body\"") != nullptr);
|
||||
}
|
||||
|
||||
void TestBatteryEstimator()
|
||||
{
|
||||
assert(!BatteryEstimator::IsPlausibleVoltage(0));
|
||||
assert(!BatteryEstimator::IsPlausibleVoltage(39));
|
||||
assert(!BatteryEstimator::IsPlausibleVoltage(2499));
|
||||
assert(BatteryEstimator::IsPlausibleVoltage(2500));
|
||||
assert(BatteryEstimator::IsPlausibleVoltage(3700));
|
||||
assert(BatteryEstimator::IsPlausibleVoltage(4500));
|
||||
assert(!BatteryEstimator::IsPlausibleVoltage(4501));
|
||||
|
||||
assert(BatteryEstimator::VoltageToPercent(4090) == 100);
|
||||
assert(BatteryEstimator::VoltageToPercent(3000) == 0);
|
||||
assert(BatteryEstimator::VoltageToPercent(3570) == 20);
|
||||
assert(BatteryEstimator::VoltageToPercent(3520) == 15);
|
||||
|
||||
BatteryEstimator estimator;
|
||||
estimator.Update(3700);
|
||||
assert(estimator.initialized());
|
||||
assert(!estimator.charging());
|
||||
estimator.Update(3715);
|
||||
estimator.Update(3730);
|
||||
assert(estimator.charging());
|
||||
estimator.Update(3700);
|
||||
estimator.Update(3670);
|
||||
assert(!estimator.charging());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
TestTouchTransitionsArePaired();
|
||||
TestImuBatchAndStillness();
|
||||
TestGravityStateMachine();
|
||||
TestProtocolMessages();
|
||||
TestBatteryEstimator();
|
||||
std::cout << "sensor_pipeline_test passed\n";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user