TimeStamp.cpp
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2010
00004  *
00005  * Fraunhofer Institute for Manufacturing Engineering   
00006  * and Automation (IPA)
00007  *
00008  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00009  *
00010  * Project name: care-o-bot
00011  * ROS stack name: cob_driver
00012  * ROS package name: cob_powercube_chain
00013  * Description:
00014  *                                                              
00015  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00016  *                      
00017  * Author: Felix Geibel, email:Felix.Geibel@gmx.de
00018  * Supervised by: Alexander Bubeck, email:alexander.bubeck@ipa.fhg.de
00019  *
00020  * Date of creation: Aug 2007
00021  * ToDo:
00022  *
00023  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00024  *
00025  * Redistribution and use in source and binary forms, with or without
00026  * modification, are permitted provided that the following conditions are met:
00027  *
00028  *     * Redistributions of source code must retain the above copyright
00029  *       notice, this list of conditions and the following disclaimer.
00030  *     * Redistributions in binary form must reproduce the above copyright
00031  *       notice, this list of conditions and the following disclaimer in the
00032  *       documentation and/or other materials provided with the distribution.
00033  *     * Neither the name of the Fraunhofer Institute for Manufacturing 
00034  *       Engineering and Automation (IPA) nor the names of its
00035  *       contributors may be used to endorse or promote products derived from
00036  *       this software without specific prior written permission.
00037  *
00038  * This program is free software: you can redistribute it and/or modify
00039  * it under the terms of the GNU Lesser General Public License LGPL as 
00040  * published by the Free Software Foundation, either version 3 of the 
00041  * License, or (at your option) any later version.
00042  * 
00043  * This program is distributed in the hope that it will be useful,
00044  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00045  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00046  * GNU Lesser General Public License LGPL for more details.
00047  * 
00048  * You should have received a copy of the GNU Lesser General Public 
00049  * License LGPL along with this program. 
00050  * If not, see <http://www.gnu.org/licenses/>.
00051  *
00052  ****************************************************************/
00053 #include <cob_trajectory_controller/TimeStamp.h>
00054 
00055 //-----------------------------------------------------------------------------
00056 TimeStamp::TimeStamp()
00057 {
00058         m_TimeStamp.tv_sec = 0;
00059         m_TimeStamp.tv_nsec = 0;
00060 }
00061 
00062 void TimeStamp::SetNow()
00063 {
00064         ::clock_gettime(CLOCK_REALTIME, &m_TimeStamp);
00065 }
00066 
00067 double TimeStamp::TimespecToDouble(const ::timespec& LargeInt)
00068 {
00069         return double(LargeInt.tv_sec) + double(LargeInt.tv_nsec) / 1e9;
00070 }
00071 
00072 ::timespec TimeStamp::DoubleToTimespec(double TimeS)    
00073 {       
00074         ::timespec DeltaTime;
00075         if (! ( TimeS < 4e9 && TimeS > 0.0 ))
00076         {
00077                 DeltaTime.tv_sec = 0;
00078                 DeltaTime.tv_nsec = 0;
00079                 return DeltaTime;
00080         }
00081 
00082         DeltaTime.tv_sec = ::time_t(TimeS);
00083         DeltaTime.tv_nsec
00084                 = static_cast<long int>((TimeS - double(DeltaTime.tv_sec)) * 1e9);
00085 
00086         return DeltaTime;
00087 }
00088 
00089 double TimeStamp::operator-(const TimeStamp& EarlierTime) const
00090 {
00091         ::timespec Res;
00092 
00093         Res.tv_sec = m_TimeStamp.tv_sec - EarlierTime.m_TimeStamp.tv_sec;
00094         Res.tv_nsec = m_TimeStamp.tv_nsec - EarlierTime.m_TimeStamp.tv_nsec;
00095 
00096         if (Res.tv_nsec < 0) {
00097                 Res.tv_sec--;
00098                 Res.tv_nsec += 1000000000;
00099         }
00100 
00101         return TimespecToDouble(Res);
00102 }
00103 
00104 void TimeStamp::operator+=(double TimeS)
00105 {
00106         ::timespec Dbl = DoubleToTimespec(TimeS);
00107         m_TimeStamp.tv_sec += Dbl.tv_sec;
00108         m_TimeStamp.tv_nsec += Dbl.tv_nsec;
00109         if (m_TimeStamp.tv_nsec > 1000000000)
00110         {
00111                 m_TimeStamp.tv_sec++;
00112                 m_TimeStamp.tv_nsec -= 1000000000;
00113         }
00114 }
00115 
00116 void TimeStamp::operator-=(double TimeS)
00117 {
00118         ::timespec Dbl = DoubleToTimespec(TimeS);
00119         m_TimeStamp.tv_sec -= Dbl.tv_sec;
00120         m_TimeStamp.tv_nsec -= Dbl.tv_nsec;
00121         if (m_TimeStamp.tv_nsec < 0.0)
00122         {
00123                 m_TimeStamp.tv_sec--;
00124                 m_TimeStamp.tv_nsec += 1000000000;
00125         }
00126 }
00127 
00128 bool TimeStamp::operator>(const TimeStamp& Time)
00129 {
00130         if (m_TimeStamp.tv_sec > Time.m_TimeStamp.tv_sec) return true;
00131         if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) &&
00132                 (m_TimeStamp.tv_nsec > Time.m_TimeStamp.tv_nsec)) return true;
00133         return false;
00134 }
00135 
00136 bool TimeStamp::operator<(const TimeStamp& Time)
00137 {
00138         if (m_TimeStamp.tv_sec < Time.m_TimeStamp.tv_sec) return true;
00139         if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) &&
00140                 (m_TimeStamp.tv_nsec < Time.m_TimeStamp.tv_nsec)) return true;
00141         return false;
00142 }
00143 
00144 void TimeStamp::getTimeStamp(long& lSeconds, long& lNanoSeconds)
00145 {
00146         lSeconds = m_TimeStamp.tv_sec;
00147         lNanoSeconds = m_TimeStamp.tv_nsec;
00148 };
00149 
00150 void TimeStamp::setTimeStamp(const long& lSeconds, const long& lNanoSeconds)
00151 {
00152         m_TimeStamp.tv_sec = lSeconds;
00153         m_TimeStamp.tv_nsec = lNanoSeconds;
00154 };


cob_trajectory_controller
Author(s): Alexander Bubeck
autogenerated on Tue Mar 3 2015 15:12:41