Rework radar system

This commit is contained in:
Ayzen
2026-10-06 12:32:26 +03:00
parent 552cdc688a
commit 5bb86b7165
299 changed files with 13537 additions and 27763 deletions
@@ -2,10 +2,10 @@
#include "channel_bundle_support.hpp"
#include "data_preprocessor.hpp"
#include "locator/tcp_server.hpp"
#include "object_approach_filter.hpp"
#include "processing_live_config.hpp"
#include "runtime_wait.hpp"
#include "shm_ring.hpp"
#include "sweep_orchestrator.hpp"
#include "tty_protocol_writer.h"
#include <sys/mman.h>
@@ -13,6 +13,7 @@
#include <sys/wait.h>
#include <unistd.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <chrono>
@@ -65,9 +66,9 @@ struct RingName {
std::string name;
explicit RingName(const std::string& suffix = {})
: name("/radar_native_test_" + std::to_string(::getpid()) + suffix) {
ipc::ShmRing::unlink_ring(name);
::shm_unlink(name.c_str());
}
~RingName() { ipc::ShmRing::unlink_ring(name); }
~RingName() { ::shm_unlink(name.c_str()); }
};
auto save_bundle(const Workspace& workspace, const char* name, const ipc::RawSweepCollection& collection)
@@ -84,13 +85,15 @@ void write_u32(std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint32
std::memcpy(bytes.data() + offset, &value, sizeof(value));
}
void test_wire_decoders() {
void test_wire_format() {
ipc::RawSweepCollection raw{.collection_id = 7, .monotonic_ns = 42,
.capture_start_ns = 21, .capture_end_ns = 40,
.traces = {{.combo = {1, 2}, .frequency_hz = {1, 2},
.s11 = {{1, 2}, {3, 4}}, .s21 = {{5, 6}, {7, 8}},
.s21 = {{5, 6}, {7, 8}},
.capture_start_ns = 25, .capture_end_ns = 39}}};
ipc::ResultCollection results{.collection_id = 7, .processing_duration_ns = 17,
ipc::ResultCollection results{.collection_id = 7, .monotonic_ns = 0x0102030405060708ULL,
.processing_duration_ns = 17, .settings_revision = 0xfedcba9876543210ULL,
.capture_start_ns = 21, .capture_end_ns = 40, .speed_m_s = -1.5F,
.collection_payloads = {
{.kind = ipc::ResultKind::TraceComplex, .frequency_hz = {1}, .trace = {{2, 3}}},
{.kind = ipc::ResultKind::ScalarF32, .scalar_value = 4},
@@ -103,32 +106,39 @@ void test_wire_decoders() {
const auto raw_bytes = ipc::serialize_raw_collection(raw);
const auto pre_bytes = ipc::serialize_preprocessed_collection(raw);
const auto result_bytes = ipc::serialize_result_collection(results);
const std::array<std::uint8_t, 64> result_header{
'R', 'S', 'L', '3', 7, 0, 0, 0, 0, 0, 0, 0,
8, 7, 6, 5, 4, 3, 2, 1, 17, 0, 0, 0, 0, 0, 0, 0,
0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe,
21, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0xc0, 0xbf, 6, 0, 0, 0, 1, 0, 0, 0,
};
require(std::equal(result_header.begin(), result_header.end(), result_bytes.begin()),
"RSL3 must preserve exact acquisition identity and settings in its 64-byte header");
require(std::memcmp(raw_bytes.data(), "RAW3", 4) == 0 &&
std::memcmp(pre_bytes.data(), "PRP3", 4) == 0,
"trace collections must use the S21-only version 3 format");
// Collection header + one trace header + two 12-byte points + capture trailer.
require(raw_bytes.size() == 24 + 12 + 2 * 12 + 16 + 4 + 16 && pre_bytes.size() == raw_bytes.size(),
"trace wire format must contain only frequency and S21 samples");
auto old_raw = raw_bytes;
auto old_pre = pre_bytes;
write_u32(old_raw, 0, 0x32574152U);
write_u32(old_pre, 0, 0x32525050U);
expect_error([&] { (void)ipc::deserialize_raw_collection(old_raw); });
expect_error([&] { (void)ipc::deserialize_preprocessed_collection(old_pre); });
require(ipc::serialize_raw_collection(ipc::deserialize_raw_collection(raw_bytes)) == raw_bytes,
"raw roundtrip changed data");
require(ipc::serialize_preprocessed_collection(ipc::deserialize_preprocessed_collection(pre_bytes)) == pre_bytes,
"preprocessed roundtrip changed data");
require(ipc::serialize_result_collection(ipc::deserialize_result_collection(result_bytes)) == result_bytes,
"result roundtrip changed data or empty shape");
for (std::size_t size = 0; size < result_bytes.size(); ++size) {
expect_error([&] { (void)ipc::deserialize_result_collection(std::span(result_bytes).first(size)); });
}
for (std::size_t count_offset : {28U, 32U, 39U}) {
auto corrupt = result_bytes;
write_u32(corrupt, count_offset, std::numeric_limits<std::uint32_t>::max());
expect_error([&] { (void)ipc::deserialize_result_collection(corrupt); });
}
for (auto kind : {ipc::ResultKind::ImageF32, ipc::ResultKind::TableF32}) {
ipc::ResultCollection empty{.collection_payloads = {{.kind = kind}}};
auto corrupt = ipc::serialize_result_collection(empty);
write_u32(corrupt, 39, std::numeric_limits<std::uint32_t>::max());
write_u32(corrupt, 43, std::numeric_limits<std::uint32_t>::max());
expect_error([&] { (void)ipc::deserialize_result_collection(corrupt); });
for (const std::size_t tail : {36U, 20U}) {
expect_error([&] { (void)ipc::deserialize_raw_collection(std::span(raw_bytes).first(raw_bytes.size() - tail)); });
expect_error([&] { (void)ipc::deserialize_preprocessed_collection(std::span(pre_bytes).first(pre_bytes.size() - tail)); });
}
// Deterministic random mutations include wire-compatible valid frames as seeds.
std::mt19937 random(0x52414452U);
for (unsigned iteration = 0; iteration < 10'000U; ++iteration) {
auto bytes = iteration % 3U == 0U ? raw_bytes : iteration % 3U == 1U ? pre_bytes : result_bytes;
auto bytes = iteration % 2U == 0U ? raw_bytes : pre_bytes;
for (unsigned edit = 0; edit < 1U + random() % 5U; ++edit) {
bytes[random() % bytes.size()] = static_cast<std::uint8_t>(random());
}
@@ -136,9 +146,8 @@ void test_wire_decoders() {
bytes.resize(random() % bytes.size());
}
try {
if (iteration % 3U == 0U) { (void)ipc::deserialize_raw_collection(bytes); }
else if (iteration % 3U == 1U) { (void)ipc::deserialize_preprocessed_collection(bytes); }
else { (void)ipc::deserialize_result_collection(bytes); }
if (iteration % 2U == 0U) { (void)ipc::deserialize_raw_collection(bytes); }
else { (void)ipc::deserialize_preprocessed_collection(bytes); }
} catch (const std::bad_alloc&) { throw; }
catch (const std::runtime_error&) {}
}
@@ -151,12 +160,35 @@ void test_configuration(const std::string& seed_path, const Workspace& workspace
const auto save = [&](const Json& config) { std::ofstream(config_path) << config; };
save(seed);
const auto config = config::load_run_config(config_path.string());
auto no_taps = seed;
no_taps["rings"]["preprocessed_tap"]["name"] = "";
save(no_taps);
const auto disabled = config::load_run_config(config_path.string()).rings;
require(disabled.preprocessed_tap.name.empty(),
"explicit empty tap names must disable the optional rings");
for (const auto* key : {"raw", "preprocessed", "results"}) {
auto invalid = no_taps;
invalid["rings"][key]["name"] = "";
save(invalid);
expect_error([&] { (void)config::load_run_config(config_path.string()); });
}
for (const auto* port : {"port1", "port2"}) {
auto with_driver = seed;
with_driver["switches"][port]["driver"] = "h7992";
save(with_driver);
(void)config::load_run_config(config_path.string());
for (const auto& driver : {Json("hmc349a"), Json("unknown"), Json(7)}) {
with_driver["switches"][port]["driver"] = driver;
save(with_driver);
expect_error([&] { (void)config::load_run_config(config_path.string()); });
}
}
auto with_object_calibration = seed;
with_object_calibration["preprocess"]["s21"]["object_calibration"] =
Json{{"set_name", "reflector"}, {"bundle_path", "corrections.bin"}};
save(with_object_calibration);
const auto selected = config::load_run_config(config_path.string()).preprocess.s21.object_calibration;
require(selected.set_name == "reflector" && selected.bundle_path == "corrections.bin",
require(selected.bundle_path == "corrections.bin",
"object calibration asset did not load");
with_object_calibration["preprocess"]["s21"].erase("object_calibration");
save(with_object_calibration);
@@ -167,10 +199,8 @@ void test_configuration(const std::string& seed_path, const Workspace& workspace
save(with_object_calibration);
expect_error([&] { (void)config::load_run_config(config_path.string()); });
}
for (const auto& [pointer, value] : std::array<std::pair<const char*, double>, 8>{{
{"/radar/sweep/start_hz", -1}, {"/radar/sweep/stop_hz", 1e100},
{"/radar/sweep/if_bandwidth_hz", 0}, {"/radar/sweep/points", 0},
{"/radar/sweep/points", 4294967296.0}, {"/run/combos/0/input", 0.0000001},
for (const auto& [pointer, value] : std::array<std::pair<const char*, double>, 3>{{
{"/run/combos/0/input", 0.0000001},
{"/rings/raw/slot_size_bytes", 65}, {"/gpr/rx_geometry/0/x_m", 1e100}}}) {
auto invalid = seed;
invalid[Json::json_pointer(pointer)] = value;
@@ -190,11 +220,6 @@ void test_configuration(const std::string& seed_path, const Workspace& workspace
require(current.bscan_gain == 2 && loader.revision() == revision,
"invalid live config replaced the last valid settings");
}
const std::array<float, 1> nan_axis{std::numeric_limits<float>::quiet_NaN()};
require(!preprocessing::frequency_axes_match(nan_axis, nan_axis), "NaN frequency axes matched");
preprocessing::ChannelTrace invalid_trace{.frequency_hz = {1},
.samples = {{std::numeric_limits<float>::infinity(), 0}}};
expect_error([&] { preprocessing::validate_channel_trace_layout(invalid_trace, "test", {}); });
}
void test_ring() {
@@ -245,38 +270,16 @@ void test_ring() {
}
}
void test_partial_driver_open() {
struct Radar final : drivers::RadarDriver {
unsigned closes = 0;
void open() override {}
void close() override { ++closes; }
auto acquire_sweep() -> drivers::SweepTrace override { return {}; }
} radar;
struct Switch final : drivers::SwitchDriver {
std::atomic<bool>& stop;
bool fail_open = false;
bool fail_close = false;
unsigned closes = 0;
explicit Switch(std::atomic<bool>& stop_flag) : stop(stop_flag) {}
void open() override {
if (fail_open) { stop.store(true); throw std::runtime_error("open failure"); }
}
void close() override { ++closes; if (fail_close) { throw std::runtime_error("close failure"); } }
auto position_count() const -> std::uint32_t override { return 1; }
void switch_to(std::uint32_t) override {}
};
std::atomic<bool> stop{false};
Switch input(stop), output(stop);
input.fail_open = true;
output.fail_close = true;
config::RunConfig config;
config.radar.sweep.points = 1;
config.run_combos = {{0, 0}};
RingName name;
auto ring = ipc::ShmRing::open_or_create(name.name, 2, 256);
acq::SweepOrchestrator(config, radar, input, output, ring).run(stop);
require(radar.closes == 1 && input.closes == 1 && output.closes == 1,
"partial driver startup did not close each driver exactly once");
void test_acquisition_identity() {
processing::ObjectApproachFilter filter;
const std::vector<processing::ObjectApproachFilter::Point> points{{0, 2}};
require(!filter.confirm(points, 1, 1'000'000'000ULL, 0, 0, 2).front(), "first frame confirmed a track");
require(filter.confirm(points, 1, 2'000'000'000ULL, 0, 0, 2).front(),
"reused collection id replaced a distinct acquisition");
require(filter.confirm(points, 1, 2'000'000'000ULL, 0, 0, 2).front(),
"reprocessing duplicated a frame or lost its predecessor");
require(!filter.confirm(points, 7, 1'000'000'000ULL, 0, 0, 2).front(),
"an earlier capture time failed to reset replay history");
}
void test_interruptible_wait() {
@@ -291,6 +294,19 @@ void test_interruptible_wait() {
"large wait delayed shutdown");
}
void test_cosine_notch_taper() {
// Band 2.0-2.1 GHz with 0.1 GHz shoulders: 1 at the outer edges, 0.5 halfway,
// 0 from the band edges inwards.
const std::vector<float> frequency_hz{1.85e9F, 1.9e9F, 1.95e9F, 2.0e9F, 2.05e9F,
2.1e9F, 2.15e9F, 2.2e9F, 2.25e9F};
const std::vector<float> expected{1.0F, 1.0F, 0.5F, 0.0F, 0.0F, 0.0F, 0.5F, 1.0F, 1.0F};
const auto mask = preprocessing::build_notch_mask(frequency_hz,
{.enabled = true, .bands_hz = {{2.0e9F, 2.1e9F}}, .taper_width_hz = 1e8F, .taper_type = "cosine"});
for (std::size_t index = 0U; index < expected.size(); ++index) {
require(std::abs(mask[index] - expected[index]) < 1e-3F, "cosine notch shoulders are not 1 -> 0 towards the band");
}
}
void test_csv_failures(const Workspace& workspace) {
const auto csv = workspace.path / "capture.csv";
const auto spool = workspace.path / "capture.capture_spool.bin";
@@ -328,37 +344,32 @@ void test_csv_failures(const Workspace& workspace) {
void test_asset_reload_preserves_data(const Workspace& workspace) {
const auto save = [&](const char* name, float sample) {
return save_bundle(workspace, name, {.traces = {
{.frequency_hz = {1}, .s11 = {{sample, 0}}, .s21 = {{sample, 0}}}}});
{.frequency_hz = {1}, .s21 = {{sample, 0}}}}});
};
const auto open = save("open.bin", 1);
const auto short_standard = save("short.bin", -1);
const auto load = save("load.bin", 0);
const auto nan = save("nan.bin", std::numeric_limits<float>::quiet_NaN());
preprocessing::S21ReferenceBundle s21_reference;
s21_reference.load(open);
expect_error([&] { s21_reference.load(nan); });
require(s21_reference.resolve({}).samples.front().re == 1, "failed S21 reload lost previous reference");
preprocessing::S21CalibrationBundle s21_calibration;
s21_calibration.load(open);
s21_calibration.load(save("through.bin", 2));
expect_error([&] { s21_calibration.load(nan); });
require(s21_calibration.is_enabled(), "failed S21 reload disabled previous calibration");
preprocessing::S11ReferenceBundle s11_reference;
s11_reference.load(open);
expect_error([&] { s11_reference.load(nan); });
require(s11_reference.resolve({}).samples.front().re == 1, "failed S11 reload lost previous reference");
preprocessing::S11CalibrationBundle s11_calibration;
s11_calibration.load(open, short_standard, load);
expect_error([&] { s11_calibration.load(open, open, load); });
const std::array<float, 1> axis{1};
require(s11_calibration.apply({}, axis, {{0.5, 0}}).front().re == 0.5F,
"failed OSL reload lost previous calibration");
const std::array<float, 1> frequency{1};
std::array<ipc::Complex32, 1> samples{{{6, 8}}};
s21_calibration.apply({}, frequency, samples);
require(samples[0].re == 3 && samples[0].im == 4, "failed S21 reload changed previous calibration");
const std::array<float, 1> nan_axis{std::numeric_limits<float>::quiet_NaN()};
expect_error([&] { s21_calibration.apply({}, nan_axis, samples); });
preprocessing::S21ReferenceBundle reference;
reference.load(open, s21_calibration, {{}});
expect_error([&] { reference.load(nan, s21_calibration, {{}}); });
ipc::SweepTraceBlock trace{.frequency_hz = {1}, .s21 = {{2, 0}}};
reference.apply(trace);
require(trace.s21[0].re == 1.5F, "failed reference reload changed the calibrated reference");
}
void test_object_calibration(const Workspace& workspace) {
const ipc::SweepTraceBlock first{.combo = {7, 3}, .frequency_hz = {1e9F, 2e9F},
.s11 = {{1, 2}, {3, 4}}, .s21 = {{2, 3}, {4, 5}}, .capture_start_ns = 5, .capture_end_ns = 15};
.s21 = {{2, 3}, {4, 5}}, .capture_start_ns = 5, .capture_end_ns = 15};
const ipc::SweepTraceBlock second{.combo = {1, 9}, .frequency_hz = {3e9F, 4e9F, 5e9F},
.s11 = {{}, {}, {}}, .s21 = {{6, 7}, {8, 9}, {10, 11}}};
.s21 = {{6, 7}, {8, 9}, {10, 11}}};
auto factors_first = first;
factors_first.s21 = {{0, 2}, {2, 0}};
auto factors_second = second;
@@ -390,7 +401,7 @@ void test_object_calibration(const Workspace& workspace) {
trace = first;
correction.apply(trace);
require(ipc::serialize_raw_collection({.traces = {trace}}) == ipc::serialize_raw_collection({.traces = {expected}}),
"object calibration changed S11, timing, pair identity, or multiplication after a failed reload");
"object calibration changed timing, pair identity, or multiplication after a failed reload");
trace = second;
correction.apply(trace);
require(trace.s21[0].re == 13 && trace.s21[0].im == 1 && trace.s21[1].re == -8 && trace.s21[1].im == -9 &&
@@ -406,19 +417,18 @@ void test_object_calibration(const Workspace& workspace) {
void test_object_calibration_pipeline(const Workspace& workspace) {
const ipc::SweepTraceBlock trace{.combo = {7, 3}, .frequency_hz = {1e9F, 2e9F},
.s11 = {{7, 3}, {8, 4}}, .s21 = {{2, 4}, {6, 8}}, .capture_start_ns = 5, .capture_end_ns = 15};
.s21 = {{2, 4}, {6, 8}}, .capture_start_ns = 5, .capture_end_ns = 15};
auto standard = trace;
standard.s21 = {{2, 0}, {2, 0}};
auto reference = trace;
reference.s21 = {{1, 2}, {2, 2}};
auto factors = trace;
factors.s21 = {{0, 2}, {5, 1}};
preprocessing::CalibrationMaster calibration(preprocessing::make_s21_through_calibrator());
calibration.load_s21_calibration_bundle(save_bundle(workspace, "pipeline_through.bin", {.traces = {standard}}));
preprocessing::ReferenceMaster references;
references.load_s21_reference_bundle(save_bundle(workspace, "pipeline_reference.bin", {.traces = {reference}}));
references.prepare_calibrated(calibration, {trace.combo});
config::RunConfig config;
config.preprocess.s21.calibration.bundle_path =
save_bundle(workspace, "pipeline_through.bin", {.traces = {standard}});
config.preprocess.s21.reference.bundle_path =
save_bundle(workspace, "pipeline_reference.bin", {.traces = {reference}});
config.run_combos = {trace.combo};
config.preprocess.s21.object_calibration.bundle_path =
save_bundle(workspace, "pipeline_object.bin", {.traces = {factors}});
@@ -426,7 +436,7 @@ void test_object_calibration_pipeline(const Workspace& workspace) {
RingName raw_name("_object_raw"), pre_name("_object_pre");
auto raw_ring = ipc::ShmRing::open_or_create(raw_name.name, 2, 4096);
auto pre_ring = ipc::ShmRing::open_or_create(pre_name.name, 2, 4096);
preprocessing::DataPreprocessor preprocessor(config, calibration, references, raw_ring, pre_ring);
preprocessing::DataPreprocessor preprocessor(config, raw_ring, pre_ring);
require(raw_ring.push(ipc::serialize_raw_collection({.collection_id = 41, .capture_start_ns = 5,
.capture_end_ns = 15, .traces = {trace}})), "test raw ring could not hold a sweep");
std::atomic<bool> stop{false};
@@ -449,8 +459,7 @@ void test_object_calibration_pipeline(const Workspace& workspace) {
"object calibration lost capture identity or timing");
require(output.s21[0].re == -2 && output.s21[0].im == 1 && output.s21[1].re == 0 && output.s21[1].im == 0,
"object calibration ran before existing preprocessing or was applied more than once");
require(output.s11[0].re == 7 && output.s11[0].im == 3 && output.s11[1].re == 0 && output.s11[1].im == 0,
"object calibration changed S11 or bypassed the notch filter");
}
void test_locator_packet_errors() {
@@ -506,14 +515,15 @@ int main(int argc, char** argv) {
try {
require(argc == 2, "Pass the simulator config fixture path");
Workspace workspace;
test_wire_decoders();
test_wire_format();
test_configuration(argv[1], workspace);
test_asset_reload_preserves_data(workspace);
test_object_calibration(workspace);
test_object_calibration_pipeline(workspace);
test_ring();
test_partial_driver_open();
test_acquisition_identity();
test_interruptible_wait();
test_cosine_notch_taper();
test_csv_failures(workspace);
test_locator_packet_errors();
test_unread_tty_shutdown(workspace);