Now you can download a copy of these docs so you can use them offline! Download now
Timer.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "Timer.h" 00008 00009 #include <sysLib.h> // for sysClkRateGet 00010 #include <time.h> 00011 #include <usrLib.h> // for taskDelay 00012 00013 #include "Synchronized.h" 00014 #include "Utility.h" 00015 00025 void Wait(double seconds) 00026 { 00027 if (seconds < 0.0) return; 00028 taskDelay((INT32)((double)sysClkRateGet() * seconds)); 00029 } 00030 00031 /* 00032 * Return the FPGA system clock time in seconds. 00033 * This is deprecated and just forwards to Timer::GetFPGATimestamp(). 00034 * @returns Robot running time in seconds. 00035 */ 00036 double GetClock() 00037 { 00038 return Timer::GetFPGATimestamp(); 00039 } 00040 00045 double GetTime() 00046 { 00047 struct timespec tp; 00048 00049 clock_gettime(CLOCK_REALTIME,&tp); 00050 double realTime = (double)tp.tv_sec + (double)((double)tp.tv_nsec*1e-9); 00051 00052 return (realTime); 00053 } 00054 00061 Timer::Timer() 00062 : m_startTime (0.0) 00063 , m_accumulatedTime (0.0) 00064 , m_running (false) 00065 , m_semaphore (0) 00066 { 00067 //Creates a semaphore to control access to critical regions. 00068 //Initially 'open' 00069 m_semaphore = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE); 00070 Reset(); 00071 } 00072 00073 Timer::~Timer() 00074 { 00075 semDelete(m_semaphore); 00076 } 00077 00085 double Timer::Get() 00086 { 00087 double result; 00088 double currentTime = GetFPGATimestamp(); 00089 00090 Synchronized sync(m_semaphore); 00091 if(m_running) 00092 { 00093 // This math won't work if the timer rolled over (71 minutes after boot). 00094 // TODO: Check for it and compensate. 00095 result = (currentTime - m_startTime) + m_accumulatedTime; 00096 } 00097 else 00098 { 00099 result = m_accumulatedTime; 00100 } 00101 00102 return result; 00103 } 00104 00110 void Timer::Reset() 00111 { 00112 Synchronized sync(m_semaphore); 00113 m_accumulatedTime = 0; 00114 m_startTime = GetFPGATimestamp(); 00115 } 00116 00122 void Timer::Start() 00123 { 00124 Synchronized sync(m_semaphore); 00125 if (!m_running) 00126 { 00127 m_startTime = GetFPGATimestamp(); 00128 m_running = true; 00129 } 00130 } 00131 00138 void Timer::Stop() 00139 { 00140 double temp = Get(); 00141 00142 Synchronized sync(m_semaphore); 00143 if (m_running) 00144 { 00145 m_accumulatedTime += temp; 00146 m_running = false; 00147 } 00148 } 00149 00158 bool Timer::HasPeriodPassed(double period) 00159 { 00160 if (Get() > period) 00161 { 00162 Synchronized sync(m_semaphore); 00163 // Advance the start time by the period. 00164 // Don't set it to the current time... we want to avoid drift. 00165 m_startTime += period; 00166 return true; 00167 } 00168 return false; 00169 } 00170 00171 /* 00172 * Return the FPGA system clock time in seconds. 00173 * 00174 * Return the time from the FPGA hardware clock in seconds since the FPGA 00175 * started. 00176 * Rolls over after 71 minutes. 00177 * @returns Robot running time in seconds. 00178 */ 00179 double Timer::GetFPGATimestamp() 00180 { 00181 // FPGA returns the timestamp in microseconds 00182 // Call the helper GetFPGATime() in Utility.cpp 00183 return GetFPGATime() * 1.0e-6; 00184 } 00185 00186 // Internal function that reads the PPC timestamp counter. 00187 extern "C" 00188 { 00189 UINT32 niTimestamp32(void); 00190 UINT64 niTimestamp64(void); 00191 } 00192 00193 /* 00194 * Return the PowerPC timestamp since boot in seconds. 00195 * 00196 * This is lower overhead than GetFPGATimestamp() but not synchronized with other FPGA timestamps. 00197 * @returns Robot running time in seconds. 00198 */ 00199 double Timer::GetPPCTimestamp() 00200 { 00201 // PPC system clock is 33MHz 00202 return niTimestamp64() / 33.0e6; 00203 }
Generated on Thu Jan 12 2012 22:35:24 for WPILibC++ by
1.7.1