Utilities.h

Go to the documentation of this file.
00001 /***************************************************************
00002  
00003  Mini Grand Challenge 2010
00004  Pennsylvania State University - Robotics Club
00005  Learn more at www.psurobotics.org
00006  Protected by the GNU General Public License
00007  
00008  This source file is developed and maintained by:
00009  + Jeremy Bridon jgbridon@gmail.com
00010  
00011  File: Utilities.cpp/h
00012  Desc: Includes system-wide multiplatform generic functions for
00013  assertions, logging, and system performance.
00014  
00015 ***************************************************************/
00016 
00017 // Inclusion guard
00018 #ifndef __UTILITIES_H_
00019 #define __UTILITIES_H_
00020 
00021 // Window includes
00022 #ifdef _WIN32
00023         #include <windows.h>
00024         #include <gl/gl.h>     // The GL Header File
00025         #include <gl/glu.h>    // The GL Utilities (GLU) header
00026         #include "gl/glut.h"   // The GL Utility Toolkit (Glut) Header
00027         #include "ipc.h"
00028 
00029 // OSX includes
00030 #elif __APPLE__
00031         #include <unistd.h>
00032         #include <sys/time.h>
00033         #include <ipc.h>
00034 
00035 // Else, linux/unix:
00036 #else
00037         #include <unistd.h>
00038         #include <sys/time.h>
00039         #include <ipc/ipc.h>
00040         #include <opencv/cv.h>
00041         #include <opencv/highgui.h>
00042 
00043 #endif
00044 
00045 // Standard includes for all systems
00046 #include <stdlib.h>
00047 #include <iostream>
00048 #include <fstream>
00049 #include <stdio.h>
00050 #include <string.h>
00051 #include <math.h>
00052 
00053 // Disable deprecation
00054 //#define _CRT_SECURE_NO_WARNINGS
00055 
00056 using namespace std;
00057 
00060 #define Assert(Assertion, FailText) __Assert( (Assertion), (FailText), __FILE__, __LINE__)
00061 
00062 // Define PI
00063 #define PI 3.14159265
00064 
00067 void __Assert(bool Assertion, const char* FailText, const char* FileName, int LineNumber);
00068 
00071 void UtilSleep(float SleepTime);
00072 
00074 void UtilGetTime(unsigned int* seconds, unsigned int* milliseconds);
00075 
00076 // If Windows version
00077 #if _WIN32
00078 
00080 class HighresClock
00081 {
00082 public:
00083         
00084         HighresClock()
00085         {
00086                 // Setup the high-resolution timer
00087                 QueryPerformanceFrequency(&TicksPerSec);
00088                 
00089                 // High-resolution timers, iniitalized timer
00090                 QueryPerformanceCounter(&startTime);
00091                 
00092                 // Start off the timer
00093                 Start(); Stop();
00094         }
00095         
00096         void Stop()
00097         {
00098                 QueryPerformanceCounter(&endTime);
00099         }
00100         
00101         // In seconds
00102         float GetTime()
00103         {
00104                 // Measure the cycle time
00105                 cpuTime.QuadPart = endTime.QuadPart - startTime.QuadPart;
00106                 return (float)cpuTime.QuadPart / (float)TicksPerSec.QuadPart;
00107         }
00108         
00109         void Start()
00110         {
00111                 QueryPerformanceCounter(&startTime);
00112         }
00113         
00114 private:
00115         LARGE_INTEGER TicksPerSec;
00116         LARGE_INTEGER startTime, endTime, cpuTime;
00117 };
00118 
00119 // UNIX / Linux / OSX interface
00120 #else
00121 
00123 class HighresClock
00124 {
00125 public:
00126 
00127         HighresClock()
00128         {
00129                 // High-resolution timers, iniitalized timer
00130                 gettimeofday(&startTime, NULL);
00131 
00132                 // Start off the timer
00133                 Start(); Stop();
00134         }
00135 
00136         void Stop()
00137         {
00138                 gettimeofday(&endTime, NULL);
00139         }
00140 
00141         float GetTime()
00142         {
00143                 // Measure the cycle time (Convert from micro to milli to seconds)
00144                 // If the end time is one second more than the start time, modify the
00145                 float secondDif = float(endTime.tv_sec - startTime.tv_sec);
00146                 return float(float(endTime.tv_usec) / 1000.0f - float(startTime.tv_usec) / 1000.0f) / 1000.0f + secondDif;
00147         }
00148 
00149         void Start()
00150         {
00151                 gettimeofday(&startTime, NULL);
00152         }
00153 
00154 private:
00155         struct timeval startTime, endTime;
00156 };
00157         
00158 #endif // End of UNIX version
00159 
00162 void itoa(char *dest, int val);
00163 
00164 // End of inclusion guard
00165 #endif

Generated on Sun Feb 21 00:00:08 2010 for Penn State Robotics Club: Mini Grand Challenge 2010 by  doxygen 1.5.5