PX4 Firmware
PX4 Autopilot Software http://px4.io
clock.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014, 2018 Pavel Kirienko <pavel.kirienko@gmail.com>
3  * Kinetis Port Author David Sidrane <david_s5@nscdg.com>
4  */
5 
6 #pragma once
7 
9 #include <uavcan/driver/system_clock.hpp>
10 
11 namespace uavcan_kinetis
12 {
13 
14 namespace clock
15 {
16 /**
17  * Starts the clock.
18  * Can be called multiple times, only the first call will be effective.
19  */
20 void init();
21 
22 /**
23  * Returns current monotonic time since the moment when clock::init() was called.
24  * This function is thread safe.
25  */
26 uavcan::MonotonicTime getMonotonic();
27 
28 /**
29  * Sets the driver's notion of the system UTC. It should be called
30  * at startup and any time the system clock is updated from an
31  * external source that is not the UAVCAN Timesync master.
32  * This function is thread safe.
33  */
34 void setUtc(uavcan::UtcTime time);
35 
36 /**
37  * Returns UTC time if it has been set, otherwise returns zero time.
38  * This function is thread safe.
39  */
40 uavcan::UtcTime getUtc();
41 
42 /**
43  * Performs UTC phase and frequency adjustment.
44  * The UTC time will be zero until first adjustment has been performed.
45  * This function is thread safe.
46  */
47 void adjustUtc(uavcan::UtcDuration adjustment);
48 
49 /**
50  * UTC clock synchronization parameters
51  */
52 struct UtcSyncParams {
53  float offset_p; ///< PPM per one usec error
54  float rate_i; ///< PPM per one PPM error for second
58  uavcan::UtcDuration lock_thres_offset;
59  uavcan::UtcDuration min_jump; ///< Min error to jump rather than change rate
60 
62  : offset_p(0.01F),
63  rate_i(0.02F),
64  rate_error_corner_freq(0.01F),
65  max_rate_correction_ppm(300.0F),
66  lock_thres_rate_ppm(2.0F),
67  lock_thres_offset(uavcan::UtcDuration::fromMSec(4)),
68  min_jump(uavcan::UtcDuration::fromMSec(10))
69  {
70  }
71 };
72 
73 /**
74  * Clock rate error.
75  * Positive if the hardware timer is slower than reference time.
76  * This function is thread safe.
77  */
79 
80 /**
81  * Number of non-gradual adjustments performed so far.
82  * Ideally should be zero.
83  * This function is thread safe.
84  */
85 uavcan::uint32_t getUtcJumpCount();
86 
87 /**
88  * Whether UTC is synchronized and locked.
89  * This function is thread safe.
90  */
91 bool isUtcLocked();
92 
93 /**
94  * UTC sync params get/set.
95  * Both functions are thread safe.
96  */
98 void setUtcSyncParams(const UtcSyncParams &params);
99 
100 }
101 
102 /**
103  * Adapter for uavcan::ISystemClock.
104  */
105 class SystemClock : public uavcan::ISystemClock
106  , uavcan::Noncopyable
107 {
109  {
110  }
111 
112  virtual void adjustUtc(uavcan::UtcDuration adjustment)
113  {
114  clock::adjustUtc(adjustment);
115  }
116 
117 public:
118  virtual uavcan::MonotonicTime getMonotonic() const
119  {
120  return clock::getMonotonic();
121  }
122  virtual uavcan::UtcTime getUtc() const
123  {
124  return clock::getUtc();
125  }
126 
127  /**
128  * Calls clock::init() as needed.
129  * This function is thread safe.
130  */
131  static SystemClock &instance();
132 };
133 
134 }
virtual void adjustUtc(uavcan::UtcDuration adjustment)
Definition: clock.hpp:112
void setUtcSyncParams(const UtcSyncParams &params)
virtual uavcan::UtcTime getUtc() const
Definition: clock.hpp:122
UtcSyncParams getUtcSyncParams()
UTC sync params get/set.
float rate_i
PPM per one PPM error for second.
Definition: clock.hpp:54
void init()
Starts the clock.
float getUtcRateCorrectionPPM()
Clock rate error.
LidarLite * instance
Definition: ll40ls.cpp:65
bool isUtcLocked()
Whether UTC is synchronized and locked.
uavcan::UtcTime getUtc()
Returns UTC time if it has been set, otherwise returns zero time.
UTC clock synchronization parameters.
Definition: clock.hpp:52
uavcan::MonotonicTime getMonotonic()
Returns current monotonic time since the moment when clock::init() was called.
uavcan::UtcDuration lock_thres_offset
Definition: clock.hpp:58
virtual uavcan::MonotonicTime getMonotonic() const
Definition: clock.hpp:118
uavcan::UtcDuration min_jump
Min error to jump rather than change rate.
Definition: clock.hpp:59
uavcan::uint32_t getUtcJumpCount()
Number of non-gradual adjustments performed so far.
float offset_p
PPM per one usec error.
Definition: clock.hpp:53
Adapter for uavcan::ISystemClock.
Definition: clock.hpp:105
void setUtc(uavcan::UtcTime time)
Sets the driver&#39;s notion of the system UTC.
void adjustUtc(uavcan::UtcDuration adjustment)
Performs UTC phase and frequency adjustment.