TimeStamp.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
19 
20 //-----------------------------------------------------------------------------
22 {
23  m_TimeStamp.tv_sec = 0;
24  m_TimeStamp.tv_nsec = 0;
25 }
26 
28 {
29  ::clock_gettime(CLOCK_REALTIME, &m_TimeStamp);
30 }
31 
32 double TimeStamp::TimespecToDouble(const ::timespec& LargeInt)
33 {
34  return double(LargeInt.tv_sec) + double(LargeInt.tv_nsec) / 1e9;
35 }
36 
37 ::timespec TimeStamp::DoubleToTimespec(double TimeS)
38 {
39  ::timespec DeltaTime;
40  if (! ( TimeS < 4e9 && TimeS > 0.0 ))
41  {
42  DeltaTime.tv_sec = 0;
43  DeltaTime.tv_nsec = 0;
44  return DeltaTime;
45  }
46 
47  DeltaTime.tv_sec = ::time_t(TimeS);
48  DeltaTime.tv_nsec
49  = static_cast<long int>((TimeS - double(DeltaTime.tv_sec)) * 1e9);
50 
51  return DeltaTime;
52 }
53 
54 double TimeStamp::operator-(const TimeStamp& EarlierTime) const
55 {
56  ::timespec Res;
57 
58  Res.tv_sec = m_TimeStamp.tv_sec - EarlierTime.m_TimeStamp.tv_sec;
59  Res.tv_nsec = m_TimeStamp.tv_nsec - EarlierTime.m_TimeStamp.tv_nsec;
60 
61  if (Res.tv_nsec < 0) {
62  Res.tv_sec--;
63  Res.tv_nsec += 1000000000;
64  }
65 
66  return TimespecToDouble(Res);
67 }
68 
69 void TimeStamp::operator+=(double TimeS)
70 {
71  ::timespec Dbl = DoubleToTimespec(TimeS);
72  m_TimeStamp.tv_sec += Dbl.tv_sec;
73  m_TimeStamp.tv_nsec += Dbl.tv_nsec;
74  if (m_TimeStamp.tv_nsec > 1000000000)
75  {
76  m_TimeStamp.tv_sec++;
77  m_TimeStamp.tv_nsec -= 1000000000;
78  }
79 }
80 
81 void TimeStamp::operator-=(double TimeS)
82 {
83  ::timespec Dbl = DoubleToTimespec(TimeS);
84  m_TimeStamp.tv_sec -= Dbl.tv_sec;
85  m_TimeStamp.tv_nsec -= Dbl.tv_nsec;
86  if (m_TimeStamp.tv_nsec < 0.0)
87  {
88  m_TimeStamp.tv_sec--;
89  m_TimeStamp.tv_nsec += 1000000000;
90  }
91 }
92 
94 {
95  if (m_TimeStamp.tv_sec > Time.m_TimeStamp.tv_sec) return true;
96  if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) &&
97  (m_TimeStamp.tv_nsec > Time.m_TimeStamp.tv_nsec)) return true;
98  return false;
99 }
100 
102 {
103  if (m_TimeStamp.tv_sec < Time.m_TimeStamp.tv_sec) return true;
104  if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) &&
105  (m_TimeStamp.tv_nsec < Time.m_TimeStamp.tv_nsec)) return true;
106  return false;
107 }
108 
109 void TimeStamp::getTimeStamp(long& lSeconds, long& lNanoSeconds)
110 {
111  lSeconds = m_TimeStamp.tv_sec;
112  lNanoSeconds = m_TimeStamp.tv_nsec;
113 };
114 
115 void TimeStamp::setTimeStamp(const long& lSeconds, const long& lNanoSeconds)
116 {
117  m_TimeStamp.tv_sec = lSeconds;
118  m_TimeStamp.tv_nsec = lNanoSeconds;
119 };
void setTimeStamp(const long &lSeconds, const long &lNanoSeconds)
Definition: TimeStamp.cpp:115
timespec m_TimeStamp
Internal time stamp data.
Definition: TimeStamp.h:76
void operator-=(double TimeS)
Reduces the timestamp by TimeS seconds.
Definition: TimeStamp.cpp:81
void getTimeStamp(long &lSeconds, long &lNanoSeconds)
Definition: TimeStamp.cpp:109
void SetNow()
Makes time measurement.
Definition: TimeStamp.cpp:27
TimeStamp()
Constructor.
Definition: TimeStamp.cpp:21
void operator+=(double TimeS)
Increase the timestamp by TimeS seconds.
Definition: TimeStamp.cpp:69
bool operator>(const TimeStamp &Time)
Checks if this time is after time "Time".
Definition: TimeStamp.cpp:93
bool operator<(const TimeStamp &Time)
Checks if this time is before time "Time".
Definition: TimeStamp.cpp:101
double operator-(const TimeStamp &EarlierTime) const
Retrieves time difference in seconds.
Definition: TimeStamp.cpp:54
static double TimespecToDouble(const ::timespec &LargeInt)
Conversion timespec -> double.
Definition: TimeStamp.cpp:32
::timespec DoubleToTimespec(double TimeS)
Conversion double -> timespec.
Definition: TimeStamp.cpp:37


cob_trajectory_controller
Author(s): Alexander Bubeck
autogenerated on Thu Apr 8 2021 02:39:55