]> git.mxchange.org Git - flightgear.git/blob - src/Time/TimeManager.cxx
f28deeaf9680fccdcfffda986aae40c23fa5720e
[flightgear.git] / src / Time / TimeManager.cxx
1 // TimeManager.cxx -- simulation-wide time management
2 //
3 // Written by James Turner, started July 2010.
4 //
5 // Copyright (C) 2010  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "TimeManager.hxx"
26
27 #ifdef _WIN32
28 #  define WIN32_LEAN_AND_MEAN
29 #  include <windows.h> // for Sleep()
30 #else
31 #  include <unistd.h> // for usleep()
32 #endif
33
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>
38
39 #include <Main/fg_props.hxx>
40 #include <Main/globals.hxx>
41 #include <Time/sunsolver.hxx>
42
43 TimeManager::TimeManager() :
44   _inited(false),
45   _impl(NULL)
46 {
47   
48 }
49
50 void TimeManager::init()
51 {
52   if (_inited) {
53     // time manager has to be initialised early, so needs to be defensive
54     // about multiple initialisation 
55     return; 
56   }
57   
58   _firstUpdate = true;
59   _inited = true;
60   _dtRemainder = 0.0;
61   
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);
65   
66   _longitudeDeg = fgGetNode("/position/longitude-deg", true);
67   _latitudeDeg = fgGetNode("/position/latitude-deg", true);
68   
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());
74   
75   globals->set_warp_delta(0);
76   
77   globals->get_event_mgr()->addTask("updateLocalTime", this,
78                             &TimeManager::updateLocalTime, 30*60 );
79   updateLocalTime();
80   
81   _impl->update(lon, lat, _timeOverride->getLongValue(),
82                globals->get_warp());
83   globals->set_time_params(_impl);
84     
85 // frame/update-rate counters
86   _frameRate = fgGetNode("/sim/frame-rate", true);
87   _lastFrameTime = 0;
88   _frameCount = 0;
89 }
90
91 void TimeManager::postinit()
92 {
93   initTimeOffset();
94 }
95
96 void TimeManager::reinit()
97 {
98   globals->set_time_params(NULL);
99   delete _impl;
100   _impl = NULL;
101   _inited = false;
102   globals->get_event_mgr()->removeTask("updateLocalTime");
103   
104   init();
105   postinit();
106 }
107
108 void TimeManager::computeTimeDeltas(double& simDt, double& realDt)
109 {
110   // Update the elapsed time.
111   if (_firstUpdate) {
112     _lastStamp.stamp();
113     _firstUpdate = false;
114     _lastClockFreeze = _clockFreeze->getBoolValue();
115   }
116
117   bool scenery_loaded = fgGetBool("sim/sceneryloaded");
118   bool wait_for_scenery = !(scenery_loaded || fgGetBool("sim/sceneryloaded-override"));
119   
120   if (!wait_for_scenery) {
121     throttleUpdateRate();
122   }
123   else
124   {
125       // suppress framerate while initial scenery isn't loaded yet (splash screen still active) 
126       _lastFrameTime=0;
127       _frameCount = 0;
128   }
129   
130   SGTimeStamp currentStamp;
131   currentStamp.stamp();
132   double dt = (currentStamp - _lastStamp).toSecs();
133   
134 // Limit the time we need to spend in simulation loops
135 // That means, if the /sim/max-simtime-per-frame value is strictly positive
136 // you can limit the maximum amount of time you will do simulations for
137 // one frame to display. The cpu time spent in simulations code is roughly
138 // at least O(real_delta_time_sec). If this is (due to running debug
139 // builds or valgrind or something different blowing up execution times)
140 // larger than the real time you will no longer get any response
141 // from flightgear. This limits that effect. Just set to property from
142 // your .fgfsrc or commandline ...
143   double dtMax = _maxDtPerFrame->getDoubleValue();
144   if (0 < dtMax && dtMax < dt) {
145     dt = dtMax;
146   }
147   
148   int model_hz = fgGetInt("/sim/model-hz");
149   
150   SGSubsystemGroup* fdmGroup = 
151     globals->get_subsystem_mgr()->get_group(SGSubsystemMgr::FDM);
152   fdmGroup->set_fixed_update_time(1.0 / model_hz);
153   
154 // round the real time down to a multiple of 1/model-hz.
155 // this way all systems are updated the _same_ amount of dt.
156   dt += _dtRemainder;
157   int multiLoop = long(floor(dt * model_hz));
158   multiLoop = SGMisc<long>::max(0, multiLoop);
159   _dtRemainder = dt - double(multiLoop)/double(model_hz);
160   dt = double(multiLoop)/double(model_hz);
161
162   realDt = dt;
163   if (_clockFreeze->getBoolValue() || wait_for_scenery) {
164     simDt = 0;
165   } else {
166     simDt = dt;
167   }
168   
169   _lastStamp = currentStamp;
170   globals->inc_sim_time_sec(simDt);
171
172 // These are useful, especially for Nasal scripts.
173   fgSetDouble("/sim/time/delta-realtime-sec", realDt);
174   fgSetDouble("/sim/time/delta-sec", simDt);
175 }
176
177 void TimeManager::update(double dt)
178 {
179   bool freeze = _clockFreeze->getBoolValue();
180   if (freeze) {
181     // clock freeze requested
182     if (_timeOverride->getLongValue() == 0) {
183       fgSetLong( "/sim/time/cur-time-override", _impl->get_cur_time());
184       globals->set_warp(0);
185     }
186   } else {
187     // no clock freeze requested
188     if (_lastClockFreeze) {
189     // clock just unfroze, let's set warp as the difference
190     // between frozen time and current time so we don't get a
191     // time jump (and corresponding sky object and lighting
192     // jump.)
193       globals->set_warp(_timeOverride->getLongValue() - time(NULL));
194       fgSetLong( "/sim/time/cur-time-override", 0 );
195     }
196     
197     if ( globals->get_warp_delta() != 0 ) {
198       globals->inc_warp( globals->get_warp_delta() );
199     }
200   }
201
202   _lastClockFreeze = freeze;
203   double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
204   double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
205   _impl->update(lon, lat,
206                _timeOverride->getLongValue(),
207                globals->get_warp());
208
209   computeFrameRate();
210 }
211
212 void TimeManager::computeFrameRate()
213 {
214   // Calculate frame rate average
215   if ((_impl->get_cur_time() != _lastFrameTime)) {
216     _frameRate->setIntValue(_frameCount);
217     _frameCount = 0;
218   }
219   
220   _lastFrameTime = _impl->get_cur_time();
221   ++_frameCount;
222 }
223
224 void TimeManager::throttleUpdateRate()
225 {
226   double throttle_hz = fgGetDouble("/sim/frame-rate-throttle-hz", 0.0);
227   SGTimeStamp currentStamp;
228   
229   // common case, no throttle requested
230   if (throttle_hz <= 0.0) {
231     return; // no-op
232   }
233   
234   double frame_us = 1000000.0 / throttle_hz;
235 #define FG_SLEEP_BASED_TIMING 1
236 #if defined(FG_SLEEP_BASED_TIMING)
237   // sleep based timing loop.
238   //
239   // Calling sleep, even usleep() on linux is less accurate than
240   // we like, but it does free up the cpu for other tasks during
241   // the sleep so it is desirable.  Because of the way sleep()
242   // is implemented in consumer operating systems like windows
243   // and linux, you almost always sleep a little longer than the
244   // requested amount.
245   //
246   // To combat the problem of sleeping too long, we calculate the
247   // desired wait time and shorten it by 2000us (2ms) to avoid
248   // [hopefully] over-sleep'ing.  The 2ms value was arrived at
249   // via experimentation.  We follow this up at the end with a
250   // simple busy-wait loop to get the final pause timing exactly
251   // right.
252   //
253   // Assuming we don't oversleep by more than 2000us, this
254   // should be a reasonable compromise between sleep based
255   // waiting, and busy waiting.
256
257   // sleep() will always overshoot by a bit so undersleep by
258   // 2000us in the hopes of never oversleeping.
259   frame_us -= 2000.0;
260   if ( frame_us < 0.0 ) {
261       frame_us = 0.0;
262   }
263   
264   currentStamp.stamp();
265
266   double elapsed_us = (currentStamp - _lastStamp).toUSecs();
267   if ( elapsed_us < frame_us ) {
268     double requested_us = frame_us - elapsed_us;
269  
270 #ifdef _WIN32
271     Sleep ((int)(requested_us / 1000.0)) ;
272 #else
273     usleep(requested_us) ;
274 #endif
275   }
276 #endif
277
278   // busy wait timing loop.
279   //
280   // This yields the most accurate timing.  If the previous
281   // ulMilliSecondSleep() call is omitted this will peg the cpu
282   // (which is just fine if FG is the only app you care about.)
283   currentStamp.stamp();
284   SGTimeStamp next_time_stamp = _lastStamp;
285   next_time_stamp += SGTimeStamp::fromSec(1e-6*frame_us);
286   while ( currentStamp < next_time_stamp ) {
287       currentStamp.stamp();
288   }
289 }
290
291 // periodic time updater wrapper
292 void TimeManager::updateLocalTime() 
293 {
294   SGPath zone(globals->get_fg_root());
295   zone.append("Timezone");
296   
297   double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
298   double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
299   
300   SG_LOG(SG_GENERAL, SG_INFO, "updateLocal(" << lon << ", " << lat << ", " << zone.str() << ")");
301   _impl->updateLocal(lon, lat, zone.str());
302 }
303
304 void TimeManager::initTimeOffset()
305 {
306   // Handle potential user specified time offsets
307   int orig_warp = globals->get_warp();
308   time_t cur_time = _impl->get_cur_time();
309   time_t currGMT = sgTimeGetGMT( gmtime(&cur_time) );
310   time_t systemLocalTime = sgTimeGetGMT( localtime(&cur_time) );
311   time_t aircraftLocalTime = 
312       sgTimeGetGMT( fgLocaltime(&cur_time, _impl->get_zonename() ) );
313     
314   // Okay, we now have several possible scenarios
315   int offset = fgGetInt("/sim/startup/time-offset");
316   string offset_type = fgGetString("/sim/startup/time-offset-type");
317   double lon = _longitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
318   double lat = _latitudeDeg->getDoubleValue() * SG_DEGREES_TO_RADIANS;
319   int warp = 0;
320   
321   if ( offset_type == "real" ) {
322       warp = 0;
323   } else if ( offset_type == "dawn" ) {
324       warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 90.0, true ); 
325   } else if ( offset_type == "morning" ) {
326      warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 75.0, true ); 
327   } else if ( offset_type == "noon" ) {
328      warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 0.0, true ); 
329   } else if ( offset_type == "afternoon" ) {
330     warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 60.0, false );  
331   } else if ( offset_type == "dusk" ) {
332     warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 90.0, false );
333   } else if ( offset_type == "evening" ) {
334     warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 100.0, false );
335   } else if ( offset_type == "midnight" ) {
336     warp = fgTimeSecondsUntilSunAngle( cur_time, lon, lat, 180.0, false );
337   } else if ( offset_type == "system-offset" ) {
338     warp = offset;
339     orig_warp = 0;
340   } else if ( offset_type == "gmt-offset" ) {
341     warp = offset - (currGMT - systemLocalTime);
342     orig_warp = 0;
343   } else if ( offset_type == "latitude-offset" ) {
344     warp = offset - (aircraftLocalTime - systemLocalTime);
345     orig_warp = 0;
346   } else if ( offset_type == "system" ) {
347     warp = offset - (systemLocalTime - currGMT) - cur_time;
348   } else if ( offset_type == "gmt" ) {
349       warp = offset - cur_time;
350   } else if ( offset_type == "latitude" ) {
351       warp = offset - (aircraftLocalTime - currGMT)- cur_time; 
352   } else {
353     SG_LOG( SG_GENERAL, SG_ALERT,
354           "TimeManager::initTimeOffset: unsupported offset: " << offset_type );
355      warp = 0;
356   }
357   
358   globals->set_warp( orig_warp + warp );
359   _impl->update(lon, lat, _timeOverride->getLongValue(),
360                globals->get_warp() );
361
362   SG_LOG( SG_GENERAL, SG_INFO, "After fgInitTimeOffset(): warp = " 
363             << globals->get_warp() );
364 }