SatCat5
ptp_tracking.h
Go to the documentation of this file.
1 // Copyright 2023-2024 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
22 
23 #pragma once
24 
25 #include <satcat5/cfgbus_pps.h>
26 #include <satcat5/list.h>
27 #include <satcat5/polling.h>
28 #include <satcat5/ptp_filters.h>
30 #include <satcat5/ptp_source.h>
31 #include <satcat5/ptp_time.h>
32 #include <satcat5/timeref.h>
33 #include <satcat5/utils.h>
34 
35 namespace satcat5 {
36  namespace ptp {
42  constexpr s64 RATE_ONE_PPM = 1000 * satcat5::ptp::RATE_ONE_PPB;
43  constexpr s64 RATE_ONE_PPK = 1000 * satcat5::ptp::RATE_ONE_PPM;
45 
52  class TrackingClock {
53  public:
56 
61  const satcat5::ptp::Time& amount) = 0;
62 
66  virtual void clock_rate(s64 offset) = 0;
67 
70  s64 get_rate() const {return m_offset;}
71 
72  protected:
74  constexpr TrackingClock() : m_offset(0), m_next(0) {}
75  s64 m_offset;
76 
77  private:
78  // Pointer to the next element in a linked list.
81  };
82 
90  : public satcat5::ptp::Callback
92  public:
97  satcat5::ptp::Source* source = 0);
98 
101  void ptp_ready(const satcat5::ptp::Measurement& data) override;
102 
108  { m_clocks.push_front(clock); }
111  { m_clocks.remove(clock); }
112 
118  inline void add_filter(satcat5::ptp::Filter* filter)
119  { m_filters.push_back(filter); }
120 
123  inline void remove_filter(satcat5::ptp::Filter* filter)
124  { m_filters.remove(filter); }
125 
128  void reset(bool linear=false);
129 
133  inline void reacquire() {
134  if (m_lock_state == LockState::TRACK) {
135  m_lock_state = LockState::ACQUIRE;
136  }
137  }
138 
142  void update(const satcat5::ptp::Time& delta);
143 
146  inline bool is_locked() const
147  { return m_lock_state == LockState::TRACK ||
148  m_lock_state == LockState::LINEAR; }
149 
150  protected:
151  // Coarse acquisition and lock/unlock state.
152  s64 coarse(const satcat5::ptp::Time& delta);
153 
154  // Run each filter one timestep.
155  void filter(u32 elapsed_usec, s64 delta_subns);
156 
157  // Notify each registered clock object.
158  // The primary clock (first added) sets the return value.
159  satcat5::ptp::Time clock_adjust(const satcat5::ptp::Time& amount);
160  void clock_rate(s64 offset);
161 
162  // PPS events immediately trigger an update from observed delta.
163  inline void pps_event(
164  satcat5::ptp::Time& ts, satcat5::ptp::Time& delta) override
165  { update(delta); }
166 
167  // Internal state for fast-acquisition mode.
168  enum class LockState {RESET, ACQUIRE, TRACK, LINEAR};
169 
170  // Internal state.
173  satcat5::util::TimeVal m_tref;
174  s64 m_freq_accum;
175  u32 m_lock_alarm;
176  u32 m_lock_usec;
177  satcat5::ptp::TrackingController::LockState m_lock_state;
178  };
179 
185  public:
189  satcat5::ptp::Source* source);
190 
191  protected:
194  };
195 
202  public:
206  satcat5::ptp::Source* source);
207 
209  void ptp_ready(const satcat5::ptp::Measurement& data) override;
210 
211  protected:
212  // Internal state.
214  };
215  }
216 }
Pulse-per-second (PPS) input and output.
Callback interface for cfg::PpsInput events.
Definition: cfgbus_pps.h:23
Amplitude-based outlier rejection.
Definition: ptp_filters.h:135
PTP callback accepts each complete measurement from the Source.
Definition: ptp_source.h:38
Loop-filter for a proportional-double-integral (PII) controller.
Definition: ptp_filters.h:349
Define the basic chain-of-filters API.
Definition: ptp_filters.h:38
A source for ptp::Measurement events, usually a ptp::Client.
Definition: ptp_source.h:19
High-precision timestamp for use with PTP / IEEE1588.
Definition: ptp_time.h:41
Generic interface to a numerically-controlled reference clock.
Definition: ptp_tracking.h:52
virtual satcat5::ptp::Time clock_now()=0
Return the current time if available, TIME_ZERO otherwise.
constexpr TrackingClock()
Protected constructor should only be called by the child.
Definition: ptp_tracking.h:74
s64 get_rate() const
Read the current clock-tuning parameter.
Definition: ptp_tracking.h:70
virtual void clock_rate(s64 offset)=0
Adjust rate by a normalized frequency offset.
virtual satcat5::ptp::Time clock_adjust(const satcat5::ptp::Time &amount)=0
Make a one-time adjustment of the specified magnitude.
Bang-bang alternative to TrackingController.
Definition: ptp_tracking.h:201
void ptp_ready(const satcat5::ptp::Measurement &data) override
Event handler for ptp::Callback.
TrackingCoarse(satcat5::ptp::TrackingClock *clk, satcat5::ptp::Source *source)
Constructor links to a specific TrackingClock target.
Clock controller handling coarse and fine discipline.
Definition: ptp_tracking.h:91
TrackingController(satcat5::ptp::TrackingClock *clk, satcat5::ptp::Source *source=0)
Constructor links to a specific TrackingClock target.
Definition: ptp_tracking.cc:36
void update(const satcat5::ptp::Time &delta)
Update filter state with a new measurement from the PTP client.
Definition: ptp_tracking.cc:73
void add_clock(satcat5::ptp::TrackingClock *clock)
Add to the list of attached clocks.
Definition: ptp_tracking.h:107
bool is_locked() const
Check if the controller is currently locked.
Definition: ptp_tracking.h:146
void ptp_ready(const satcat5::ptp::Measurement &data) override
Event handler for ptp::Callback.
Definition: ptp_tracking.cc:48
void reacquire()
Indicate to the controller than an external event has happened that should trigger a re-acquisition w...
Definition: ptp_tracking.h:133
void remove_clock(satcat5::ptp::TrackingClock *clock)
Remove an item from the list of attached clocks.
Definition: ptp_tracking.h:110
void add_filter(satcat5::ptp::Filter *filter)
Add to the chain of processing filters.
Definition: ptp_tracking.h:118
void reset(bool linear=false)
Reset tracking filter(s) and begin free-wheeling.
Definition: ptp_tracking.cc:53
void remove_filter(satcat5::ptp::Filter *filter)
Remove a filter from the chain, leaving others as-is.
Definition: ptp_tracking.h:123
Simple all-in-one implementation of TrackingController.
Definition: ptp_tracking.h:184
TrackingSimple(satcat5::ptp::TrackingClock *clk, satcat5::ptp::Source *source)
Constructor links to a specific TrackingClock target.
Helper functions for manipulating singly-linked lists.
Definition: list.h:52
void push_back(T *item)
Add a new item at the tail of the list.
Definition: list.h:273
void remove(T *item)
Remove the designated item from the list.
Definition: list.h:277
void push_front(T *item)
Add a new item at the head of the list.
Definition: list.h:269
Templated functions for manipulating singly-linked lists.
Core event-processing loop for SatCat5 software.
Chainable filters for use with ptp::TrackingController.
Define the data structure for a two-way time transfer handshake.
Define the API for generating ptp::Measurement events.
High-precision "Time" object for use with PTP / IEEE1588.
constexpr s64 SUBNS_PER_NSEC
Define commonly used scaling factors.
Definition: ptp_time.h:28
constexpr s64 RATE_ONE_PPK
Define rate offsets for the TrackingClock::clock_rate() method.
Definition: ptp_tracking.h:43
constexpr s64 RATE_ONE_PPM
Define rate offsets for the TrackingClock::clock_rate() method.
Definition: ptp_tracking.h:42
constexpr s64 RATE_ONE_PPB
Define rate offsets for the TrackingClock::clock_rate() method.
Definition: ptp_tracking.h:41
Timestamps and metadata for a two-way time-transfer handshake.
Timestamp for measuring elapsed time.
Definition: timeref.h:48
TimeRef and TimeVal define the API for monotonic timers.
Miscellaneous mathematical utility functions.