57 lines
2.8 KiB
C++
57 lines
2.8 KiB
C++
#pragma once
|
|||
|
|
// How a PoTi time reference from the phone moves the micrOBU's ITS clock. Honest: the caller passes the
|
||
|
|
// measured drift and local time and applies the returned step.
|
||
|
|
#include <cstdint>
|
||
|
|
|
||
|
|
namespace microbu::its_clock {
|
||
|
|
|
||
|
|
/// @brief What a PoTi update does to the station's ITS clock.
|
||
|
|
enum class Step {
|
||
|
|
start, ///< first reference: synchronise the clock (and restart the station at real time)
|
||
|
|
forward, ///< the reference is ahead: step the clock forward to it
|
||
|
|
hold, ///< less than 1 s behind: keep the local clock, later updates catch up
|
||
|
|
refuse_stale, ///< more than 1 s behind: a late-delivered update, refuse it and keep the clock
|
||
|
|
restart, ///< consistently behind over >= 5 updates and >= 2 s: the reference really moved back
|
||
|
|
slew_back, ///< even the freshest reference of >= 5 updates over >= 2 s is behind: the clock runs ahead,
|
||
|
|
///< slow it down by Lag::slew_us (monotonic, EN 302 890-2 clause 6.3.4 |error| < 20 ms)
|
||
|
|
};
|
||
|
|
|
||
|
|
/// @brief Backward offsets below 1 s seen while holding (state owned by the caller). The freshest one (the largest
|
||
|
|
/// drift) is the reference least delayed by the link: if even it is behind, the clock itself is ahead.
|
||
|
|
struct Lag {
|
||
|
|
int count = 0;
|
||
|
|
std::int64_t freshest_us = 0;
|
||
|
|
std::int64_t first_local_us = 0;
|
||
|
|
std::int64_t slew_us = 0; ///< set with Step::slew_back: how far to slow the clock down
|
||
|
|
};
|
||
|
|
|
||
|
|
/// @brief Clock ahead by less than this is left alone (well inside pPoTiMaxTimeDiff = 20 ms).
|
||
|
|
constexpr std::int64_t slew_deadband_us = 5000;
|
||
|
|
|
||
|
|
/// @brief Slew rate: the clock runs 1/slew_divisor slower until a pending correction is applied (10 %).
|
||
|
|
constexpr std::int64_t slew_divisor = 10;
|
||
|
|
|
||
|
|
/// @brief Part of a pending slew applied after `elapsed_us` of local time.
|
||
|
|
constexpr std::int64_t slew_applied_us(std::int64_t pending_us, std::int64_t elapsed_us) {
|
||
|
|
const std::int64_t possible = elapsed_us / slew_divisor;
|
||
|
|
return possible < pending_us ? possible : pending_us;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// @brief Backward offsets seen so far (state owned by the caller).
|
||
|
|
struct Regression {
|
||
|
|
int count = 0;
|
||
|
|
std::int64_t offset_us = 0;
|
||
|
|
std::int64_t first_local_us = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
/// @brief Decide the clock step for one PoTi update. Over BLE, updates delivered late (10-15 s observed)
|
||
|
|
/// arrive in bursts with a shrinking offset and never look like a consistent backward move.
|
||
|
|
/// @param synchronised Whether the clock already follows a reference.
|
||
|
|
/// @param drift_us Reference time minus the station's ITS time.
|
||
|
|
/// @param local_us Local monotonic time of this update.
|
||
|
|
/// @param regression Backward-offset tracker, updated in place.
|
||
|
|
/// @param lag Freshest-backward-offset tracker for Step::slew_back, updated in place.
|
||
|
|
Step next_step(bool synchronised, std::int64_t drift_us, std::int64_t local_us, Regression& regression, Lag& lag);
|
||
|
|
|
||
|
|
} // namespace microbu::its_clock
|