1 // TimeManager.cxx -- simulation-wide time management
3 // Written by James Turner, started July 2010.
5 // Copyright (C) 2010 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "TimeManager.hxx"
28 # define WIN32_LEAN_AND_MEAN
29 # include <windows.h> // for Sleep()
31 # include <unistd.h> // for usleep()
34 #include <simgear/timing/sg_time.hxx>
35 #include <simgear/structure/event_mgr.hxx>
36 #include <simgear/misc/sg_path.hxx>
37 #include <simgear/timing/lowleveltime.h>
39 #include <Main/fg_props.hxx>
40 #include <Main/globals.hxx>
41 #include <Time/sunsolver.hxx>
43 TimeManager::TimeManager() :
50 void TimeManager::init()
53 // time manager has to be initialised early, so needs to be defensive
54 // about multiple initialisation
62 _maxDtPerFrame = fgGetNode("/sim/max-simtime-per-frame", true);
63 _clockFreeze = fgGetNode("/sim/freeze/clock", true);
64 _timeOverride = fgGetNode("/sim/time/cur-time-override", true);
66 _longitudeDeg = fgGetNode("/position/longitude-deg", true);
67 _latitudeDeg = fgGetNode("/position/latitude-deg", true);
69 SGPath zone(globals->get_fg_root());
70 zone.append("Timezone");
71 double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
72 double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
73 _impl = new SGTime(lon, lat, zone.str(), _timeOverride->getLongValue());
75 globals->set_warp_delta(0);
77 globals->get_event_mgr()->addTask("updateLocalTime", this,
78 &TimeManager::updateLocalTime, 30*60 );
81 _impl->update(lon, lat, _timeOverride->getLongValue(),
83 globals->set_time_params(_impl);
87 // frame/update-rate counters
88 _frameRate = fgGetNode("/sim/frame-rate", true);
89 _lastFrameTime = _impl->get_cur_time();
93 void TimeManager::postinit()
98 void TimeManager::reinit()
100 globals->set_time_params(NULL);
103 globals->get_event_mgr()->removeTask("updateLocalTime");
109 void TimeManager::computeTimeDeltas(double& simDt, double& realDt)
111 // Update the elapsed time.
114 _firstUpdate = false;
115 _lastClockFreeze = _clockFreeze->getBoolValue();
118 bool scenery_loaded = fgGetBool("sim/sceneryloaded");
119 bool wait_for_scenery = !(scenery_loaded || fgGetBool("sim/sceneryloaded-override"));
121 if (!wait_for_scenery) {
122 throttleUpdateRate();
125 SGTimeStamp currentStamp;
126 currentStamp.stamp();
127 double dt = (currentStamp - _lastStamp).toSecs();
129 // Limit the time we need to spend in simulation loops
130 // That means, if the /sim/max-simtime-per-frame value is strictly positive
131 // you can limit the maximum amount of time you will do simulations for
132 // one frame to display. The cpu time spent in simulations code is roughly
133 // at least O(real_delta_time_sec). If this is (due to running debug
134 // builds or valgrind or something different blowing up execution times)
135 // larger than the real time you will no longer get any response
136 // from flightgear. This limits that effect. Just set to property from
137 // your .fgfsrc or commandline ...
138 double dtMax = _maxDtPerFrame->getDoubleValue();
139 if (0 < dtMax && dtMax < dt) {
143 int model_hz = fgGetInt("/sim/model-hz");
145 SGSubsystemGroup* fdmGroup =
146 globals->get_subsystem_mgr()->get_group(SGSubsystemMgr::FDM);
147 fdmGroup->set_fixed_update_time(1.0 / model_hz);
149 // round the real time down to a multiple of 1/model-hz.
150 // this way all systems are updated the _same_ amount of dt.
152 int multiLoop = long(floor(dt * model_hz));
153 multiLoop = SGMisc<long>::max(0, multiLoop);
154 _dtRemainder = dt - double(multiLoop)/double(model_hz);
155 dt = double(multiLoop)/double(model_hz);
158 if (_clockFreeze->getBoolValue() || wait_for_scenery) {
164 _lastStamp = currentStamp;
165 globals->inc_sim_time_sec(simDt);
167 // These are useful, especially for Nasal scripts.
168 fgSetDouble("/sim/time/delta-realtime-sec", realDt);
169 fgSetDouble("/sim/time/delta-sec", simDt);
172 void TimeManager::update(double dt)
174 bool freeze = _clockFreeze->getBoolValue();
176 // clock freeze requested
177 if (_timeOverride->getLongValue() == 0) {
178 fgSetLong( "/sim/time/cur-time-override", _impl->get_cur_time());
179 globals->set_warp(0);
182 // no clock freeze requested
183 if (_lastClockFreeze) {
184 // clock just unfroze, let's set warp as the difference
185 // between frozen time and current time so we don't get a
186 // time jump (and corresponding sky object and lighting
188 globals->set_warp(_timeOverride->getLongValue() - time(NULL));
189 fgSetLong( "/sim/time/cur-time-override", 0 );
192 if ( globals->get_warp_delta() != 0 ) {
193 globals->inc_warp( globals->get_warp_delta() );
197 _lastClockFreeze = freeze;
198 double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
199 double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
200 _impl->update(lon, lat,
201 _timeOverride->getLongValue(),
202 globals->get_warp());
207 void TimeManager::computeFrameRate()
209 // Calculate frame rate average
210 if ((_impl->get_cur_time() != _lastFrameTime) && (_lastFrameTime > 0)) {
211 _frameRate->setIntValue(_frameCount);
215 _lastFrameTime = _impl->get_cur_time();
219 void TimeManager::throttleUpdateRate()
221 double throttle_hz = fgGetDouble("/sim/frame-rate-throttle-hz", 0.0);
222 SGTimeStamp currentStamp;
224 // common case, no throttle requested
225 if (throttle_hz <= 0.0) {
229 double frame_us = 1000000.0 / throttle_hz;
230 #define FG_SLEEP_BASED_TIMING 1
231 #if defined(FG_SLEEP_BASED_TIMING)
232 // sleep based timing loop.
234 // Calling sleep, even usleep() on linux is less accurate than
235 // we like, but it does free up the cpu for other tasks during
236 // the sleep so it is desirable. Because of the way sleep()
237 // is implemented in consumer operating systems like windows
238 // and linux, you almost always sleep a little longer than the
241 // To combat the problem of sleeping too long, we calculate the
242 // desired wait time and shorten it by 2000us (2ms) to avoid
243 // [hopefully] over-sleep'ing. The 2ms value was arrived at
244 // via experimentation. We follow this up at the end with a
245 // simple busy-wait loop to get the final pause timing exactly
248 // Assuming we don't oversleep by more than 2000us, this
249 // should be a reasonable compromise between sleep based
250 // waiting, and busy waiting.
252 // sleep() will always overshoot by a bit so undersleep by
253 // 2000us in the hopes of never oversleeping.
255 if ( frame_us < 0.0 ) {
259 currentStamp.stamp();
261 double elapsed_us = (currentStamp - _lastStamp).toUSecs();
262 if ( elapsed_us < frame_us ) {
263 double requested_us = frame_us - elapsed_us;
266 Sleep ((int)(requested_us / 1000.0)) ;
268 usleep(requested_us) ;
273 // busy wait timing loop.
275 // This yields the most accurate timing. If the previous
276 // ulMilliSecondSleep() call is omitted this will peg the cpu
277 // (which is just fine if FG is the only app you care about.)
278 currentStamp.stamp();
279 SGTimeStamp next_time_stamp = _lastStamp;
280 next_time_stamp += SGTimeStamp::fromSec(1e-6*frame_us);
281 while ( currentStamp < next_time_stamp ) {
282 currentStamp.stamp();
286 // periodic time updater wrapper
287 void TimeManager::updateLocalTime()
289 SGPath zone(globals->get_fg_root());
290 zone.append("Timezone");
292 double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
293 double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
295 SG_LOG(SG_GENERAL, SG_INFO, "updateLocal(" << lon << ", " << lat << ", " << zone.str() << ")");
296 _impl->updateLocal(lon, lat, zone.str());
299 void TimeManager::initTimeOffset()
301 // Handle potential user specified time offsets
302 int orig_warp = globals->get_warp();
303 time_t cur_time = _impl->get_cur_time();
304 time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
305 time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
306 time_t aircraftLocalTime =
307 sgTimeGetGMT( fgLocaltime(&cur_time, _impl->get_zonename() ) );
309 // Okay, we now have several possible scenarios
310 int offset = fgGetInt("/sim/startup/time-offset");
311 string offset_type = fgGetString("/sim/startup/time-offset-type");
312 double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
313 double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
316 if ( offset_type == "real" ) {
318 } else if ( offset_type == "dawn" ) {
319 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 90.0, true );
320 } else if ( offset_type == "morning" ) {
321 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 75.0, true );
322 } else if ( offset_type == "noon" ) {
323 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 0.0, true );
324 } else if ( offset_type == "afternoon" ) {
325 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 60.0, false );
326 } else if ( offset_type == "dusk" ) {
327 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 90.0, false );
328 } else if ( offset_type == "evening" ) {
329 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 100.0, false );
330 } else if ( offset_type == "midnight" ) {
331 warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 180.0, false );
332 } else if ( offset_type == "system-offset" ) {
335 } else if ( offset_type == "gmt-offset" ) {
336 warp = offset - (currGMT - systemLocalTime);
338 } else if ( offset_type == "latitude-offset" ) {
339 warp = offset - (aircraftLocalTime - systemLocalTime);
341 } else if ( offset_type == "system" ) {
342 warp = offset - (systemLocalTime - currGMT) - cur_time;
343 } else if ( offset_type == "gmt" ) {
344 warp = offset - cur_time;
345 } else if ( offset_type == "latitude" ) {
346 warp = offset - (aircraftLocalTime - currGMT)- cur_time;
348 SG_LOG( SG_GENERAL, SG_ALERT,
349 "TimeManager::initTimeOffset: unsupported offset: " << offset_type );
353 globals->set_warp( orig_warp + warp );
354 _impl->update(lon, lat, _timeOverride->getLongValue(),
355 globals->get_warp() );
357 SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = "
358 << globals->get_warp() );