libuav original

Dependents:   UAVCAN UAVCAN_Subscriber

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers clock.hpp Source File

clock.hpp

00001 /*
00002  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
00003  */
00004 
00005 #pragma once
00006 
00007 #include <uavcan_stm32/build_config.hpp>
00008 #include <uavcan/driver/system_clock.hpp>
00009 
00010 namespace uavcan_stm32
00011 {
00012 
00013 namespace clock
00014 {
00015 /**
00016  * Starts the clock.
00017  * Can be called multiple times, only the first call will be effective.
00018  */
00019 void init();
00020 
00021 /**
00022  * Returns current monotonic time since the moment when clock::init() was called.
00023  * This function is thread safe.
00024  */
00025 uavcan::MonotonicTime getMonotonic();
00026 
00027 /**
00028  * Sets the driver's notion of the system UTC. It should be called
00029  * at startup and any time the system clock is updated from an
00030  * external source that is not the UAVCAN Timesync master.
00031  * This function is thread safe.
00032  */
00033 void setUtc(uavcan::UtcTime time);
00034 
00035 /**
00036  * Returns UTC time if it has been set, otherwise returns zero time.
00037  * This function is thread safe.
00038  */
00039 uavcan::UtcTime getUtc();
00040 
00041 /**
00042  * Performs UTC phase and frequency adjustment.
00043  * The UTC time will be zero until first adjustment has been performed.
00044  * This function is thread safe.
00045  */
00046 void adjustUtc(uavcan::UtcDuration adjustment);
00047 
00048 /**
00049  * UTC clock synchronization parameters
00050  */
00051 struct UtcSyncParams
00052 {
00053     float offset_p;                        ///< PPM per one usec error
00054     float rate_i;                          ///< PPM per one PPM error for second
00055     float rate_error_corner_freq;
00056     float max_rate_correction_ppm;
00057     float lock_thres_rate_ppm;
00058     uavcan::UtcDuration lock_thres_offset;
00059     uavcan::UtcDuration min_jump;          ///< Min error to jump rather than change rate
00060 
00061     UtcSyncParams()
00062         : offset_p(0.01F)
00063         , rate_i(0.02F)
00064         , rate_error_corner_freq(0.01F)
00065         , max_rate_correction_ppm(300.0F)
00066         , lock_thres_rate_ppm(2.0F)
00067         , lock_thres_offset(uavcan::UtcDuration::fromMSec(4))
00068         , min_jump(uavcan::UtcDuration::fromMSec(10))
00069     { }
00070 };
00071 
00072 /**
00073  * Clock rate error.
00074  * Positive if the hardware timer is slower than reference time.
00075  * This function is thread safe.
00076  */
00077 float getUtcRateCorrectionPPM();
00078 
00079 /**
00080  * Number of non-gradual adjustments performed so far.
00081  * Ideally should be zero.
00082  * This function is thread safe.
00083  */
00084 uavcan::uint32_t getUtcJumpCount();
00085 
00086 /**
00087  * Whether UTC is synchronized and locked.
00088  * This function is thread safe.
00089  */
00090 bool isUtcLocked();
00091 
00092 /**
00093  * UTC sync params get/set.
00094  * Both functions are thread safe.
00095  */
00096 UtcSyncParams getUtcSyncParams();
00097 void setUtcSyncParams(const UtcSyncParams& params);
00098 
00099 }
00100 
00101 /**
00102  * Adapter for uavcan::ISystemClock.
00103  */
00104 class SystemClock : public uavcan::ISystemClock, uavcan::Noncopyable
00105 {
00106     SystemClock() { }
00107 
00108     virtual void adjustUtc(uavcan::UtcDuration adjustment) { clock::adjustUtc(adjustment); }
00109 
00110 public:
00111     virtual uavcan::MonotonicTime getMonotonic() const { return clock::getMonotonic(); }
00112     virtual uavcan::UtcTime getUtc()             const { return clock::getUtc(); }
00113 
00114     /**
00115      * Calls clock::init() as needed.
00116      * This function is thread safe.
00117      */
00118     static SystemClock& instance();
00119 };
00120 
00121 }