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