Fawkes API  Fawkes Development Version
simts.cpp
1 
2 /***************************************************************************
3  * simts.cpp - Simulator time source
4  *
5  * Created: Mon Feb 25 15:49:16 2008
6  * Copyright 2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <utils/time/simts.h>
25 #include <cstddef>
26 
27 namespace fawkes {
28 
29 /** @class SimulatorTimeSource <utils/time/simts.h>
30  * Simulation time source.
31  * This class is an utility to provide a generic time source for time in a simulated
32  * environment. It can be restarted at an arbitrary time with an arbitrary offset.
33  * It will then read the current real system time and save the initial offset. Each
34  * time you query the time source it will return a given fixed time. The time is advanced
35  * by setting a new offset (usually in every cycle).
36  *
37  * This implementation is rather primitive at the moment and could use some love.
38  *
39  * @author Tim Niemueller
40  */
41 
42 /** Constructor. */
44 {
45  clock = Clock::instance();
46  clock->get_systime(start_time);
47  start_simoffset = 0;
48  current_simtime = start_time;
49 }
50 
51 /** Destructor. */
53 {
54 }
55 
56 
57 void
59 {
60  if ( tv != NULL ) {
61  const timeval *curt = current_simtime.get_timeval();
62  tv->tv_sec = curt->tv_sec;
63  tv->tv_usec = curt->tv_usec;
64  }
65 }
66 
67 
68 timeval
69 SimulatorTimeSource::conv_to_realtime(const timeval *tv) const
70 {
71  float simdiff = current_simoffset - start_simoffset;
72  float realdiff = current_realtime - &start_time;
73 
74  float sim_to_real = realdiff / simdiff;
75 
76  Time query_simtime(tv);
77  query_simtime -= start_time;
78  float query_simtime_offset = query_simtime.in_sec() - start_simoffset;
79 
80  query_simtime_offset *= sim_to_real;
81 
82  Time final(query_simtime_offset);
83  final += start_time;
84 
85  return *(final.get_timeval());;
86 }
87 
88 
89 timeval
91 {
92  timeval rv = *tv;
93  return rv;
94 }
95 
96 /** Set start time.
97  * @param initial_offset initial offset in seconds
98  */
99 void
100 SimulatorTimeSource::set_start(float initial_offset)
101 {
102  clock->get_systime(start_time);
103  start_simoffset = initial_offset;
104  current_simtime = start_time;
105  //printf("Start time: %s Start offset: %f\n", start_time.str(), start_simoffset);
106 }
107 
108 
109 /** Set simulation offset.
110  * @param sim_offset simulation offset in seconds.
111  */
112 void
114 {
115  clock->get_systime(current_realtime);
116  current_simtime = start_time + (sim_offset - start_simoffset);
117  current_simoffset = sim_offset;
118  //printf("New current real time: %s New current simtime: %s new offset: %f\n",
119  // start_time.str(), current_simtime.str(), current_simoffset);
120 }
121 
122 } // end namespace fawkes
double in_sec() const
Convet time to seconds.
Definition: time.cpp:232
virtual timeval conv_to_realtime(const timeval *tv) const
Convert a time given w.r.t.
Definition: simts.cpp:69
const timeval * get_timeval() const
Obtain the timeval where the time is stored.
Definition: time.h:109
static Clock * instance()
Clock initializer.
Definition: clock.cpp:65
virtual void get_time(timeval *tv) const
Get the current time.
Definition: simts.cpp:58
Fawkes library namespace.
SimulatorTimeSource()
Constructor.
Definition: simts.cpp:43
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:239
void set_start(float initial_offset)
Set start time.
Definition: simts.cpp:100
A class for handling time.
Definition: time.h:91
virtual ~SimulatorTimeSource()
Destructor.
Definition: simts.cpp:52
void set_sim_offset(float sim_offset)
Set simulation offset.
Definition: simts.cpp:113
virtual timeval conv_native_to_exttime(const timeval *tv) const
Convert a native time to the external time.
Definition: simts.cpp:90