init commit

This commit is contained in:
Ayzen
2026-03-05 14:42:33 +03:00
commit fd4618b20d
964 changed files with 325114 additions and 0 deletions
@@ -0,0 +1,47 @@
#pragma once
#include <atomic>
#include <cstdint>
#include <vector>
#include "calibration_master.hpp"
#include "reference_master.hpp"
#include "run_config.hpp"
#include "shm_ring.hpp"
namespace radar::preprocessing {
class DataPreprocessor {
public:
DataPreprocessor(
const config::RunConfig& config,
CalibrationMaster& calibration_master,
ReferenceMaster& reference_master,
ipc::ShmRing& raw_ring,
ipc::ShmRing& preprocessed_ring,
ipc::ShmRing* preprocessed_tap_ring = nullptr
);
// Main processing loop. Runs until stop flag is set.
void run(const std::atomic<bool>& stop_requested);
private:
void validate_startup_prerequisites() const;
[[nodiscard]] auto try_pop_raw_collection(
std::vector<std::uint8_t>& serialized_raw,
ipc::RawSweepCollection* out_collection
) -> bool;
void publish_preprocessed_collection(const ipc::PreprocessedCollection& collection);
[[nodiscard]] auto preprocess_collection(const ipc::RawSweepCollection& raw_collection) const
-> ipc::PreprocessedCollection;
const config::RunConfig& config_;
CalibrationMaster& calibration_master_;
ReferenceMaster& reference_master_;
ipc::ShmRing& raw_ring_;
ipc::ShmRing& preprocessed_ring_;
ipc::ShmRing* preprocessed_tap_ring_ = nullptr;
};
} // namespace radar::preprocessing
@@ -0,0 +1,85 @@
#include "data_preprocessor.hpp"
#include <chrono>
#include <stdexcept>
#include <thread>
#include <vector>
namespace radar::preprocessing {
DataPreprocessor::DataPreprocessor(
const config::RunConfig& config,
CalibrationMaster& calibration_master,
ReferenceMaster& reference_master,
ipc::ShmRing& raw_ring,
ipc::ShmRing& preprocessed_ring,
ipc::ShmRing* preprocessed_tap_ring
)
: config_(config),
calibration_master_(calibration_master),
reference_master_(reference_master),
raw_ring_(raw_ring),
preprocessed_ring_(preprocessed_ring),
preprocessed_tap_ring_(preprocessed_tap_ring) {}
void DataPreprocessor::validate_startup_prerequisites() const {
calibration_master_.validate_combos(config_.run_combos);
reference_master_.validate_combos(config_.run_combos);
}
auto DataPreprocessor::try_pop_raw_collection(
std::vector<std::uint8_t>& serialized_raw,
ipc::RawSweepCollection* out_collection
) -> bool {
if (!raw_ring_.pop(serialized_raw)) {
std::this_thread::sleep_for(std::chrono::milliseconds(config_.runtime.idle_sleep_ms));
return false;
}
*out_collection = ipc::deserialize_raw_collection(serialized_raw);
return true;
}
void DataPreprocessor::publish_preprocessed_collection(const ipc::PreprocessedCollection& collection) {
const auto serialized_preprocessed = ipc::serialize_preprocessed_collection(collection);
if (!preprocessed_ring_.push(serialized_preprocessed)) {
throw std::runtime_error("Preprocessed ring slot is too small for serialized collection");
}
if (preprocessed_tap_ring_ != nullptr && !preprocessed_tap_ring_->push(serialized_preprocessed)) {
throw std::runtime_error("Preprocessed tap ring slot is too small for serialized collection");
}
}
void DataPreprocessor::run(const std::atomic<bool>& stop_requested) {
validate_startup_prerequisites();
std::vector<std::uint8_t> serialized_raw{};
ipc::RawSweepCollection raw_collection{};
while (!stop_requested.load(std::memory_order_relaxed)) {
if (!try_pop_raw_collection(serialized_raw, &raw_collection)) {
continue;
}
const auto preprocessed_collection = preprocess_collection(raw_collection);
publish_preprocessed_collection(preprocessed_collection);
}
}
auto DataPreprocessor::preprocess_collection(const ipc::RawSweepCollection& raw_collection) const
-> ipc::PreprocessedCollection {
ipc::PreprocessedCollection preprocessed{};
preprocessed.collection_id = raw_collection.collection_id;
preprocessed.monotonic_ns = ipc::current_monotonic_ns();
preprocessed.traces.reserve(raw_collection.traces.size());
for (const auto& raw_trace : raw_collection.traces) {
// Pipeline order is fixed: calibration first, then reference subtraction.
const auto calibrated = calibration_master_.apply(raw_trace);
const auto referenced = reference_master_.apply(calibrated);
preprocessed.traces.push_back(referenced);
}
return preprocessed;
}
} // namespace radar::preprocessing
@@ -0,0 +1,86 @@
#include <atomic>
#include <csignal>
#include <exception>
#include <iostream>
#include <string>
#include "calibration_master.hpp"
#include "data_preprocessor.hpp"
#include "reference_master.hpp"
#include "run_config.hpp"
#include "shm_ring.hpp"
namespace {
constexpr const char* kDefaultConfigPath = "run_config.json";
std::atomic<bool> g_stop_requested{false};
void signal_handler(int /*signal*/) {
g_stop_requested.store(true, std::memory_order_relaxed);
}
void install_signal_handlers() {
std::signal(SIGINT, signal_handler);
std::signal(SIGTERM, signal_handler);
}
[[nodiscard]] auto read_config_path(int argc, char** argv) -> std::string {
std::string config_path = kDefaultConfigPath;
for (int index = 1; index < argc; ++index) {
const std::string arg = argv[index];
if (arg == "--config" && (index + 1) < argc) {
config_path = argv[++index];
}
}
return config_path;
}
} // namespace
int main(int argc, char** argv) {
install_signal_handlers();
try {
const auto config_path = read_config_path(argc, argv);
const auto config = radar::config::load_run_config(config_path);
auto raw_ring = radar::ipc::ShmRing::open_or_create(
config.rings.raw.name,
config.rings.raw.capacity,
config.rings.raw.slot_size_bytes
);
auto preprocessed_ring = radar::ipc::ShmRing::open_or_create(
config.rings.preprocessed.name,
config.rings.preprocessed.capacity,
config.rings.preprocessed.slot_size_bytes
);
auto preprocessed_tap_ring = radar::ipc::ShmRing::open_or_create(
config.rings.preprocessed_tap.name,
config.rings.preprocessed_tap.capacity,
config.rings.preprocessed_tap.slot_size_bytes
);
// Load preprocessing assets once before entering run loop.
radar::preprocessing::CalibrationMaster calibration_master(radar::preprocessing::make_through_calibrator());
calibration_master.load_bundle(config.preprocess.calibration_bundle_path);
radar::preprocessing::ReferenceMaster reference_master;
reference_master.load_bundle(config.preprocess.reference_bundle_path);
reference_master.prepare_calibrated(calibration_master);
radar::preprocessing::DataPreprocessor preprocessor(
config,
calibration_master,
reference_master,
raw_ring,
preprocessed_ring,
&preprocessed_tap_ring
);
preprocessor.run(g_stop_requested);
return 0;
} catch (const std::exception& exception) {
std::cerr << "data_preprocessor error: " << exception.what() << '\n';
return 1;
}
}