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