Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * simts.cpp - Simulator time source 00004 * 00005 * Created: Mon Feb 25 15:49:16 2008 00006 * Copyright 2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <utils/time/simts.h> 00025 #include <cstddef> 00026 00027 namespace fawkes { 00028 00029 /** @class SimulatorTimeSource <utils/time/simts.h> 00030 * Simulation time source. 00031 * This class is an utility to provide a generic time source for time in a simulated 00032 * environment. It can be restarted at an arbitrary time with an arbitrary offset. 00033 * It will then read the current real system time and save the initial offset. Each 00034 * time you query the time source it will return a given fixed time. The time is advanced 00035 * by setting a new offset (usually in every cycle). 00036 * 00037 * This implementation is rather primitive at the moment and could use some love. 00038 * 00039 * @author Tim Niemueller 00040 */ 00041 00042 /** Constructor. */ 00043 SimulatorTimeSource::SimulatorTimeSource() 00044 { 00045 clock = Clock::instance(); 00046 clock->get_systime(start_time); 00047 start_simoffset = 0; 00048 current_simtime = start_time; 00049 } 00050 00051 /** Destructor. */ 00052 SimulatorTimeSource::~SimulatorTimeSource() 00053 { 00054 } 00055 00056 00057 void 00058 SimulatorTimeSource::get_time(timeval *tv) const 00059 { 00060 if ( tv != NULL ) { 00061 const timeval *curt = current_simtime.get_timeval(); 00062 tv->tv_sec = curt->tv_sec; 00063 tv->tv_usec = curt->tv_usec; 00064 } 00065 } 00066 00067 00068 timeval 00069 SimulatorTimeSource::conv_to_realtime(const timeval *tv) const 00070 { 00071 float simdiff = current_simoffset - start_simoffset; 00072 float realdiff = current_realtime - &start_time; 00073 00074 float sim_to_real = realdiff / simdiff; 00075 00076 Time query_simtime(tv); 00077 query_simtime -= start_time; 00078 float query_simtime_offset = query_simtime.in_sec() - start_simoffset; 00079 00080 query_simtime_offset *= sim_to_real; 00081 00082 Time final(query_simtime_offset); 00083 final += start_time; 00084 00085 return *(final.get_timeval());; 00086 } 00087 00088 00089 /** Set start time. 00090 * @param initial_offset initial offset in seconds 00091 */ 00092 void 00093 SimulatorTimeSource::set_start(float initial_offset) 00094 { 00095 clock->get_systime(start_time); 00096 start_simoffset = initial_offset; 00097 current_simtime = start_time; 00098 //printf("Start time: %s Start offset: %f\n", start_time.str(), start_simoffset); 00099 } 00100 00101 00102 /** Set simulation offset. 00103 * @param sim_offset simulation offset in seconds. 00104 */ 00105 void 00106 SimulatorTimeSource::set_sim_offset(float sim_offset) 00107 { 00108 clock->get_systime(current_realtime); 00109 current_simtime = start_time + (sim_offset - start_simoffset); 00110 current_simoffset = sim_offset; 00111 //printf("New current real time: %s New current simtime: %s new offset: %f\n", 00112 // start_time.str(), current_simtime.str(), current_simoffset); 00113 } 00114 00115 } // end namespace fawkes