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
26#include <cstddef>
27
28namespace fawkes {
29
30/** @class SimulatorTimeSource <utils/time/simts.h>
31 * Simulation time source.
32 * This class is an utility to provide a generic time source for time in a simulated
33 * environment. It can be restarted at an arbitrary time with an arbitrary offset.
34 * It will then read the current real system time and save the initial offset. Each
35 * time you query the time source it will return a given fixed time. The time is advanced
36 * by setting a new offset (usually in every cycle).
37 *
38 * This implementation is rather primitive at the moment and could use some love.
39 *
40 * @author Tim Niemueller
41 */
42
43/** Constructor. */
45{
46 clock = Clock::instance();
47 clock->get_systime(start_time);
48 start_simoffset = 0;
49 current_simtime = start_time;
50}
51
52/** Destructor. */
54{
55}
56
57void
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
67timeval
69{
70 float simdiff = current_simoffset - start_simoffset;
71 float realdiff = current_realtime - &start_time;
72
73 float sim_to_real = realdiff / simdiff;
74
75 Time query_simtime(tv);
76 query_simtime -= start_time;
77 float query_simtime_offset = query_simtime.in_sec() - start_simoffset;
78
79 query_simtime_offset *= sim_to_real;
80
81 Time final(query_simtime_offset);
82 final += start_time;
83
84 return *(final.get_timeval());
85 ;
86}
87
88timeval
90{
91 timeval rv = *tv;
92 return rv;
93}
94
95/** Set start time.
96 * @param initial_offset initial offset in seconds
97 */
98void
99SimulatorTimeSource::set_start(float initial_offset)
100{
101 clock->get_systime(start_time);
102 start_simoffset = initial_offset;
103 current_simtime = start_time;
104 //printf("Start time: %s Start offset: %f\n", start_time.str(), start_simoffset);
105}
106
107/** Set simulation offset.
108 * @param sim_offset simulation offset in seconds.
109 */
110void
112{
113 clock->get_systime(current_realtime);
114 current_simtime = start_time + (sim_offset - start_simoffset);
115 current_simoffset = sim_offset;
116 //printf("New current real time: %s New current simtime: %s new offset: %f\n",
117 // start_time.str(), current_simtime.str(), current_simoffset);
118}
119
120} // end namespace fawkes
static Clock * instance()
Clock initializer.
Definition: clock.cpp:63
void get_systime(struct timeval *tv) const
Returns the system time.
Definition: clock.cpp:215
void set_start(float initial_offset)
Set start time.
Definition: simts.cpp:99
virtual timeval conv_to_realtime(const timeval *tv) const
Convert a time given w.r.t.
Definition: simts.cpp:68
virtual timeval conv_native_to_exttime(const timeval *tv) const
Convert a native time to the external time.
Definition: simts.cpp:89
void set_sim_offset(float sim_offset)
Set simulation offset.
Definition: simts.cpp:111
SimulatorTimeSource()
Constructor.
Definition: simts.cpp:44
virtual void get_time(timeval *tv) const
Get the current time.
Definition: simts.cpp:58
virtual ~SimulatorTimeSource()
Destructor.
Definition: simts.cpp:53
A class for handling time.
Definition: time.h:93
double in_sec() const
Convet time to seconds.
Definition: time.cpp:219
const timeval * get_timeval() const
Obtain the timeval where the time is stored.
Definition: time.h:112
Fawkes library namespace.