Orocos Real-Time Toolkit  2.8.3
TimeService.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Wed Apr 17 16:01:31 CEST 2002 TimeService.cpp
3 
4  TimeService.cpp - description
5  -------------------
6  begin : Wed April 17 2002
7  copyright : (C) 2002 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10 ***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18 
19 #include "os/fosi.h"
20 #include "TimeService.hpp"
21 
22 namespace RTT {
23  using namespace os;
24 
25  TimeService* TimeService::_instance = 0;
26 
30 
32  {
33  return nano2ticks( m );
34  }
35 
37  {
38  return ticks2nano( t );
39  }
40 
42  {
43  if ( _instance == 0 )
44  {
45  _instance = new TimeService();
46  }
47 
48  return _instance;
49  }
50 
51 
53  {
54  if ( _instance != 0 )
55  {
56  delete _instance;
57  _instance = 0;
58  return true;
59  }
60 
61  return false;
62  }
63 
64 
66  : offset(0), use_clock(true)
67  {
68  //os::cout << "HeartBeat Created\n";
69  }
70 
72  {
73  //os::cout << "HB DESTRUCTOR\n";
74  }
75 
76  void TimeService::enableSystemClock( bool yes_no )
77  {
78  // guarantee monotonous time increase :
79  if ( yes_no == use_clock )
80  return;
81  // we switched from one time domain to the other
82  use_clock=yes_no;
83  if ( use_clock == true )
84  {
85  // if offset is X, then start counting from X.
86  offset = offset - rtos_get_time_ticks();
87  }
88  else
89  {
90  // start counting from _now_ + old offset
91  offset = offset + rtos_get_time_ticks();
92  }
93  }
94 
96  {
97  return use_clock;
98  }
99 
102  {
103  return use_clock ? rtos_get_time_ticks() + offset : 0 + offset;
104  }
105 
108  {
109  if ( relativeTime == 0 )
110  {
111  relativeTime = getTicks();
112  return 0;
113  }
114  return ( getTicks() - relativeTime );
115  }
116 
119  {
120  return ( getTicks() - relativeTime );
121  }
122 
123 
126  {
127  return nsecs_to_Seconds( ticks2nsecs( getTicks( relativeTime ) ) ) ;
128  }
129 
132  {
133  return nsecs_to_Seconds( ticks2nsecs( ticksSince( relativeTime ) ) ) ;
134  }
135 
138  {
139  offset += nsecs2ticks( Seconds_to_nsecs( delta ) );
140  return nsecs_to_Seconds( ticks2nsecs( ticksSince( 0 ) ) ) ;
141  }
142 
145  {
146  offset += delta;
147  return ticksSince( 0 );
148  }
149 
152  {
153  return rtos_get_time_ns();
154  }
155 
158  {
159  if ( relativeTime == 0 )
160  {
161  relativeTime = getNSecs();
162  return 0;
163  }
164  return ( getNSecs() - relativeTime );
165  }
166 }
virtual ~TimeService()
Destructor.
Definition: TimeService.cpp:71
const double InfiniteSeconds
Definition: fosi.h:60
The TimeService is used for system-wide time keeping and conversions.
Definition: TimeService.hpp:35
const TICK_TIME InfiniteTicks
Definition: fosi.h:58
static nsecs ticks2nsecs(const ticks t)
Convert an amount of ticks to nano seconds.
Definition: TimeService.cpp:36
nsecs getNSecs() const
Get current nsecs of the System clock.
Seconds nsecs_to_Seconds(const nsecs ns)
Definition: Time.hpp:108
NANO_TIME rtos_get_time_ns(void)
Get "system" time in nanoseconds.
Definition: fosi.h:113
const NANO_TIME InfiniteNSecs
Definition: fosi.h:59
long long ticks
The type for the systems clock tick.
Definition: TimeService.hpp:66
static bool Release()
Releases the TimeService Reference counting might aid in making this call safe.
Definition: TimeService.cpp:52
Seconds secondsSince(ticks relativeTime) const
Get Seconds passed since a certain moment.
ticks ticksChange(ticks delta)
Change the time with delta ticks.
static const ticks InfiniteTicks
The largest number representable in ticks.
ticks getTicks(ticks &relativeTime) const
Get clicks passed since a certain moment.
static TimeService * Instance()
Definition: TimeService.cpp:41
Seconds getSeconds(ticks &relativeTime) const
Get the time in seconds passed since a certain moment.
bool systemClockEnabled() const
Check if the system clock is being read.
Definition: TimeService.cpp:95
static const nsecs InfiniteNSecs
The largest number representable in nsecs.
TimeService()
Constructor.
Definition: TimeService.cpp:65
static ticks nsecs2ticks(const nsecs m)
Convert an amount of nano seconds to System ticks.
Definition: TimeService.cpp:31
static const Seconds InfiniteSeconds
The largest number representable in Seconds.
void enableSystemClock(bool yes_no)
Enables or disables reading the system clock.
Definition: TimeService.cpp:76
ticks ticksSince(ticks relativeTime) const
Get clicks passed since a certain moment.
TICK_TIME rtos_get_time_ticks(void)
Get "system" time in ticks FIXME see https://proj.fmtc.be/orocos-bugzilla/show_bug.cgi?id=60
Definition: fosi.h:118
double Seconds
The type used to store SI unit time keeping.
Definition: TimeService.hpp:41
NANO_TIME ticks2nano(TICK_TIME count)
Time conversions from system ticks to nano seconds.
Definition: fosi.h:107
nsecs Seconds_to_nsecs(const Seconds s)
Definition: Time.hpp:107
TICK_TIME nano2ticks(NANO_TIME nano)
Time conversions from nano seconds to system ticks.
Definition: fosi.h:100
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
Seconds secondsChange(Seconds delta)
Change the time with delta seconds.
long long nsecs
An integer representation of nanoseconds.
Definition: TimeService.hpp:61
ticks getTicks() const
Get current tick of the System clock.