]> git.mxchange.org Git - flightgear.git/blob - src/Aircraft/replay.cxx
Add a lower-bound type navaid lookup, and the ability to specify navaid type in the...
[flightgear.git] / src / Aircraft / replay.cxx
1 // replay.cxx - a system to record and replay FlightGear flights
2 //
3 // Written by Curtis Olson, started Juley 2003.
4 //
5 // Copyright (C) 2003  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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <simgear/constants.h>
25
26 #include <FDM/flight.hxx>
27 #include <Main/fg_props.hxx>
28 #include <Network/native_ctrls.hxx>
29 #include <Network/native_fdm.hxx>
30 #include <Network/net_ctrls.hxx>
31 #include <Network/net_fdm.hxx>
32
33 #include "replay.hxx"
34
35 const double FGReplay::st_list_time = 60.0;   // 60 secs of high res data
36 const double FGReplay::mt_list_time = 600.0;  // 10 mins of 1 fps data
37 const double FGReplay::lt_list_time = 3600.0; // 1 hr of 10 spf data
38
39 // short term sample rate is as every frame
40 const double FGReplay::mt_dt = 0.5; // medium term sample rate (sec)
41 const double FGReplay::lt_dt = 5.0; // long term sample rate (sec)
42
43 /**
44  * Constructor
45  */
46
47 FGReplay::FGReplay() {
48 }
49
50
51 /**
52  * Destructor
53  */
54
55 FGReplay::~FGReplay() {
56     // no dynamically allocated memory to free
57 }
58
59
60 /** 
61  * Initialize the data structures
62  */
63
64 void FGReplay::init() {
65     sim_time = 0.0;
66     last_mt_time = 0.0;
67     last_lt_time = 0.0;
68
69     // Make sure all queues are flushed
70     while ( !short_term.empty() ) {
71         short_term.pop_front();
72     }
73     while ( !medium_term.empty() ) {
74         medium_term.pop_front();
75     }
76     while ( !medium_term.empty() ) {
77         medium_term.pop_front();
78     }
79 }
80
81
82 /** 
83  * Bind to the property tree
84  */
85
86 void FGReplay::bind() {
87     disable_replay = fgGetNode( "/sim/replay/disable", true );
88 }
89
90
91 /** 
92  *  Unbind from the property tree
93  */
94
95 void FGReplay::unbind() {
96     // nothing to unbind
97 }
98
99
100 /** 
101  *  Update the saved data
102  */
103
104 void FGReplay::update( double dt ) {
105     static SGPropertyNode *replay_master
106         = fgGetNode( "/sim/freeze/replay", true );
107
108     if( disable_replay->getBoolValue() ) {
109         if( sim_time != 0.0 ) {
110             // we were recording data
111             init();
112         }
113         return;
114     }
115
116     if ( replay_master->getBoolValue() ) {
117         // don't record the replay session
118         return;
119     }
120
121     sim_time += dt;
122
123     // build the replay record
124     FGNetFDM f;
125     FGProps2NetFDM( &f, false );
126
127     // sanity check, don't collect data if FDM data isn't good
128     if ( !cur_fdm_state->get_inited() ) {
129         return;
130     }
131
132     FGNetCtrls c;
133     FGProps2NetCtrls( &c, false, false );
134
135     FGReplayData r;
136     r.sim_time = sim_time;
137     r.ctrls = c;
138     r.fdm = f;
139
140     // update the short term list
141     short_term.push_back( r );
142
143     FGReplayData st_front = short_term.front();
144     if ( sim_time - st_front.sim_time > st_list_time ) {
145         while ( sim_time - st_front.sim_time > st_list_time ) {
146             st_front = short_term.front();
147             short_term.pop_front();
148         }
149
150         // update the medium term list
151         if ( sim_time - last_mt_time > mt_dt ) {
152             last_mt_time = sim_time;
153             medium_term.push_back( st_front );
154
155             FGReplayData mt_front = medium_term.front();
156             if ( sim_time - mt_front.sim_time > mt_list_time ) {
157                 while ( sim_time - mt_front.sim_time > mt_list_time ) {
158                     mt_front = medium_term.front();
159                     medium_term.pop_front();
160                 }
161
162                 // update the long term list
163                 if ( sim_time - last_lt_time > lt_dt ) {
164                     last_lt_time = sim_time;
165                     long_term.push_back( mt_front );
166
167                     FGReplayData lt_front = long_term.front();
168                     if ( sim_time - lt_front.sim_time > lt_list_time ) {
169                         while ( sim_time - lt_front.sim_time > lt_list_time ) {
170                             lt_front = long_term.front();
171                             long_term.pop_front();
172                         }
173                     }
174                 }
175             }
176         }
177     }
178
179 #if 0
180     cout << "short term size = " << short_term.size()
181          << "  time = " << sim_time - short_term.front().sim_time
182          << endl;
183     cout << "medium term size = " << medium_term.size()
184          << "  time = " << sim_time - medium_term.front().sim_time
185          << endl;
186     cout << "long term size = " << long_term.size()
187          << "  time = " << sim_time - long_term.front().sim_time
188          << endl;
189 #endif
190 }
191
192
193 static double weight( double data1, double data2, double ratio,
194                       bool rotational = false ) {
195     if ( rotational ) {
196         // special handling of rotational data
197         double tmp = data2 - data1;
198         if ( tmp > SGD_PI ) {
199             tmp -= SGD_2PI;
200         } else if ( tmp < -SGD_PI ) {
201             tmp += SGD_2PI;
202         }
203         return data1 + tmp * ratio;
204     } else {
205         // normal "linear" data
206         return data1 + ( data2 - data1 ) * ratio;
207     }
208 }
209
210 /** 
211  * given two FGReplayData elements and a time, interpolate between them
212  */
213 static void update_fdm( FGReplayData frame ) {
214     FGNetFDM2Props( &frame.fdm, false );
215     FGNetCtrls2Props( &frame.ctrls, false, false );
216 }
217
218 /** 
219  * given two FGReplayData elements and a time, interpolate between them
220  */
221 static FGReplayData interpolate( double time, FGReplayData f1, FGReplayData f2 )
222 {
223     FGReplayData result = f1;
224
225     FGNetFDM fdm1 = f1.fdm;
226     FGNetFDM fdm2 = f2.fdm;
227
228     FGNetCtrls ctrls1 = f1.ctrls;
229     FGNetCtrls ctrls2 = f2.ctrls;
230
231     double ratio = (time - f1.sim_time) / (f2.sim_time - f1.sim_time);
232
233     // Interpolate FDM data
234
235     // Positions
236     result.fdm.longitude = weight( fdm1.longitude, fdm2.longitude, ratio );
237     result.fdm.latitude = weight( fdm1.latitude, fdm2.latitude, ratio );
238     result.fdm.altitude = weight( fdm1.altitude, fdm2.altitude, ratio );
239     result.fdm.agl = weight( fdm1.agl, fdm2.agl, ratio );
240     result.fdm.phi = weight( fdm1.phi, fdm2.phi, ratio, true );
241     result.fdm.theta = weight( fdm1.theta, fdm2.theta, ratio, true );
242     result.fdm.psi = weight( fdm1.psi, fdm2.psi, ratio, true );
243
244     // Velocities
245     result.fdm.phidot = weight( fdm1.phidot, fdm2.phidot, ratio, true );
246     result.fdm.thetadot = weight( fdm1.thetadot, fdm2.thetadot, ratio, true );
247     result.fdm.psidot = weight( fdm1.psidot, fdm2.psidot, ratio, true );
248     result.fdm.vcas = weight( fdm1.vcas, fdm2.vcas, ratio );
249     result.fdm.climb_rate = weight( fdm1.climb_rate, fdm2.climb_rate, ratio );
250     result.fdm.v_north = weight( fdm1.v_north, fdm2.v_north, ratio );
251     result.fdm.v_east = weight( fdm1.v_east, fdm2.v_east, ratio );
252     result.fdm.v_down = weight( fdm1.v_down, fdm2.v_down, ratio );
253
254     result.fdm.v_wind_body_north
255         = weight( fdm1.v_wind_body_north, fdm2.v_wind_body_north, ratio );
256     result.fdm.v_wind_body_east
257         = weight( fdm1.v_wind_body_east, fdm2.v_wind_body_east, ratio );
258     result.fdm.v_wind_body_down
259         = weight( fdm1.v_wind_body_down, fdm2.v_wind_body_down, ratio );
260
261     // Stall
262     result.fdm.stall_warning
263         = weight( fdm1.stall_warning, fdm2.stall_warning, ratio );
264
265     // Accelerations
266     result.fdm.A_X_pilot = weight( fdm1.A_X_pilot, fdm2.A_X_pilot, ratio );
267     result.fdm.A_Y_pilot = weight( fdm1.A_Y_pilot, fdm2.A_Y_pilot, ratio );
268     result.fdm.A_Z_pilot = weight( fdm1.A_Z_pilot, fdm2.A_Z_pilot, ratio );
269
270     unsigned int i;
271
272     // Engine status
273     for ( i = 0; i < fdm1.num_engines; ++i ) {
274         result.fdm.eng_state[i] = fdm1.eng_state[i];
275         result.fdm.rpm[i] = weight( fdm1.rpm[i], fdm2.rpm[i], ratio );
276         result.fdm.fuel_flow[i]
277             = weight( fdm1.fuel_flow[i], fdm2.fuel_flow[i], ratio );
278         result.fdm.egt[i] = weight( fdm1.egt[i], fdm2.egt[i], ratio );
279         result.fdm.cht[i] = weight( fdm1.cht[i], fdm2.cht[i], ratio );
280         result.fdm.mp_osi[i] = weight( fdm1.mp_osi[i], fdm2.mp_osi[i], ratio );
281         result.fdm.tit[i] = weight( fdm1.tit[i], fdm2.tit[i], ratio );
282         result.fdm.oil_temp[i]
283             = weight( fdm1.oil_temp[i], fdm2.oil_temp[i], ratio );
284         result.fdm.oil_px[i] = weight( fdm1.oil_px[i], fdm2.oil_px[i], ratio );
285     }
286
287     // Consumables
288     for ( i = 0; i < fdm1.num_tanks; ++i ) {
289         result.fdm.fuel_quantity[i]
290             = weight( fdm1.fuel_quantity[i], fdm2.fuel_quantity[i], ratio );
291     }
292
293     // Gear status
294     for ( i = 0; i < fdm1.num_wheels; ++i ) {
295         result.fdm.wow[i] = (int)(weight( fdm1.wow[i], fdm2.wow[i], ratio ));
296         result.fdm.gear_pos[i]
297             = weight( fdm1.gear_pos[i], fdm2.gear_pos[i], ratio );
298         result.fdm.gear_steer[i]
299             = weight( fdm1.gear_steer[i], fdm2.gear_steer[i], ratio );
300         result.fdm.gear_compression[i]
301             = weight( fdm1.gear_compression[i], fdm2.gear_compression[i],
302                       ratio );
303     }
304
305     // Environment
306     result.fdm.cur_time = fdm1.cur_time;
307     result.fdm.warp = fdm1.warp;
308     result.fdm.visibility = weight( fdm1.visibility, fdm2.visibility, ratio );
309
310     // Control surface positions (normalized values)
311     result.fdm.elevator = weight( fdm1.elevator, fdm2.elevator, ratio );
312     result.fdm.left_flap = weight( fdm1.left_flap, fdm2.left_flap, ratio );
313     result.fdm.right_flap = weight( fdm1.right_flap, fdm2.right_flap, ratio );
314     result.fdm.left_aileron
315         = weight( fdm1.left_aileron, fdm2.left_aileron, ratio );
316     result.fdm.right_aileron
317         = weight( fdm1.right_aileron, fdm2.right_aileron, ratio );
318     result.fdm.rudder = weight( fdm1.rudder, fdm2.rudder, ratio );
319     result.fdm.speedbrake = weight( fdm1.speedbrake, fdm2.speedbrake, ratio );
320     result.fdm.spoilers = weight( fdm1.spoilers, fdm2.spoilers, ratio );
321      
322     // Interpolate Control input data
323
324     // Aero controls
325     result.ctrls.aileron = weight( ctrls1.aileron, ctrls2.aileron, ratio );
326     result.ctrls.elevator = weight( ctrls1.elevator, ctrls2.elevator, ratio );
327     result.ctrls.rudder = weight( ctrls1.rudder, ctrls2.rudder, ratio );
328     result.ctrls.aileron_trim
329         = weight( ctrls1.aileron_trim, ctrls2.aileron_trim, ratio );
330     result.ctrls.elevator_trim
331         = weight( ctrls1.elevator_trim, ctrls2.elevator_trim, ratio );
332     result.ctrls.rudder_trim
333         = weight( ctrls1.rudder_trim, ctrls2.rudder_trim, ratio );
334     result.ctrls.flaps = weight( ctrls1.flaps, ctrls2.flaps, ratio );
335     result.ctrls.flaps_power = ctrls1.flaps_power;
336     result.ctrls.flap_motor_ok = ctrls1.flap_motor_ok;
337
338     // Engine controls
339     for ( i = 0; i < ctrls1.num_engines; ++i ) {
340         result.ctrls.master_bat[i] = ctrls1.master_bat[i];
341         result.ctrls.master_alt[i] = ctrls1.master_alt[i];
342         result.ctrls.magnetos[i] = ctrls1.magnetos[i];
343         result.ctrls.starter_power[i] = ctrls1.starter_power[i];
344         result.ctrls.throttle[i]
345             = weight( ctrls1.throttle[i], ctrls2.throttle[i], ratio );
346         result.ctrls.mixture[i]
347             = weight( ctrls1.mixture[i], ctrls2.mixture[i], ratio );
348         result.ctrls.fuel_pump_power[i] = ctrls1.fuel_pump_power[i];
349         result.ctrls.prop_advance[i]
350             = weight( ctrls1.prop_advance[i], ctrls2.prop_advance[i], ratio );
351         result.ctrls.engine_ok[i] = ctrls1.engine_ok[i];
352         result.ctrls.mag_left_ok[i] = ctrls1.mag_left_ok[i];
353         result.ctrls.mag_right_ok[i] = ctrls1.mag_right_ok[i];
354         result.ctrls.spark_plugs_ok[i] = ctrls1.spark_plugs_ok[i];
355         result.ctrls.oil_press_status[i] = ctrls1.oil_press_status[i];
356         result.ctrls.fuel_pump_ok[i] = ctrls1.fuel_pump_ok[i];
357     }
358
359     // Fuel management
360     for ( i = 0; i < ctrls1.num_tanks; ++i ) {
361         result.ctrls.fuel_selector[i] = ctrls1.fuel_selector[i];
362     }
363
364     // Brake controls
365     result.ctrls.brake_left
366             = weight( ctrls1.brake_left, ctrls2.brake_left, ratio );
367     result.ctrls.brake_right
368             = weight( ctrls1.brake_right, ctrls2.brake_right, ratio );
369     result.ctrls.brake_parking
370             = weight( ctrls1.brake_parking, ctrls2.brake_parking, ratio );
371
372     // Landing Gear
373     result.ctrls.gear_handle = ctrls1.gear_handle;
374
375     // Switches
376     result.ctrls.turbulence_norm = ctrls1.turbulence_norm;
377
378     // wind and turbulance
379     result.ctrls.wind_speed_kt
380         = weight( ctrls1.wind_speed_kt, ctrls2.wind_speed_kt, ratio );
381     result.ctrls.wind_dir_deg
382         = weight( ctrls1.wind_dir_deg, ctrls2.wind_dir_deg, ratio );
383     result.ctrls.turbulence_norm
384         = weight( ctrls1.turbulence_norm, ctrls2.turbulence_norm, ratio );
385
386     // other information about environment
387     result.ctrls.hground = weight( ctrls1.hground, ctrls2.hground, ratio );
388     result.ctrls.magvar = weight( ctrls1.magvar, ctrls2.magvar, ratio );
389
390     // simulation control
391     result.ctrls.speedup = ctrls1.speedup;
392     result.ctrls.freeze = ctrls1.freeze;
393
394     return result;
395 }
396
397 /** 
398  * interpolate a specific time from a specific list
399  */
400 static void interpolate( double time, const replay_list_type &list ) {
401     // sanity checking
402     if ( list.size() == 0 ) {
403         // handle empty list
404         return;
405     } else if ( list.size() == 1 ) {
406         // handle list size == 1
407         update_fdm( list[0] );
408         return;
409     }
410
411     unsigned int last = list.size() - 1;
412     unsigned int first = 0;
413     unsigned int mid = ( last + first ) / 2;
414
415
416     bool done = false;
417     while ( !done ) {
418         // cout << "  " << first << " <=> " << last << endl;
419         if ( last == first ) {
420             done = true;
421         } else if ( list[mid].sim_time < time && list[mid+1].sim_time < time ) {
422             // too low
423             first = mid;
424             mid = ( last + first ) / 2;
425         } else if ( list[mid].sim_time > time && list[mid+1].sim_time > time ) {
426             // too high
427             last = mid;
428             mid = ( last + first ) / 2;
429         } else {
430             done = true;
431         }
432     }
433
434     FGReplayData result = interpolate( time, list[mid], list[mid+1] );
435
436     update_fdm( result );
437 }
438
439
440 /** 
441  *  Replay a saved frame based on time, interpolate from the two
442  *  nearest saved frames.
443  */
444
445 void FGReplay::replay( double time ) {
446     // cout << "replay: " << time << " ";
447     // find the two frames to interpolate between
448     double t1, t2;
449
450     if ( short_term.size() > 0 ) {
451         t1 = short_term.back().sim_time;
452         t2 = short_term.front().sim_time;
453         if ( time > t1 ) {
454             // replay the most recent frame
455             update_fdm( short_term.back() );
456             // cout << "first frame" << endl;
457         } else if ( time <= t1 && time >= t2 ) {
458             interpolate( time, short_term );
459             // cout << "from short term" << endl;
460         } else if ( medium_term.size() > 0 ) {
461             t1 = short_term.front().sim_time;
462             t2 = medium_term.back().sim_time;
463             if ( time <= t1 && time >= t2 ) {
464                 FGReplayData result = interpolate( time,
465                                                    medium_term.back(),
466                                                    short_term.front() );
467                 update_fdm( result );
468                 // cout << "from short/medium term" << endl;
469             } else {
470                 t1 = medium_term.back().sim_time;
471                 t2 = medium_term.front().sim_time;
472                 if ( time <= t1 && time >= t2 ) {
473                     interpolate( time, medium_term );
474                     // cout << "from medium term" << endl;
475                 } else if ( long_term.size() > 0 ) {
476                     t1 = medium_term.front().sim_time;
477                     t2 = long_term.back().sim_time;
478                     if ( time <= t1 && time >= t2 ) {
479                         FGReplayData result = interpolate( time,
480                                                            long_term.back(),
481                                                            medium_term.front());
482                         update_fdm( result );
483                         // cout << "from medium/long term" << endl;
484                     } else {
485                         t1 = long_term.back().sim_time;
486                         t2 = long_term.front().sim_time;
487                         if ( time <= t1 && time >= t2 ) {
488                             interpolate( time, long_term );
489                             // cout << "from long term" << endl;
490                         } else {
491                             // replay the oldest long term frame
492                             update_fdm( long_term.front() );
493                             // cout << "oldest long term frame" << endl;
494                         }
495                     }
496                 } else {
497                     // replay the oldest medium term frame
498                     update_fdm( medium_term.front() );
499                     // cout << "oldest medium term frame" << endl;
500                 }
501             }
502         } else {
503             // replay the oldest short term frame
504             update_fdm( short_term.front() );
505             // cout << "oldest short term frame" << endl;
506         }
507     } else {
508         // nothing to replay
509     }
510 }
511
512
513 double FGReplay::get_start_time() {
514     if ( long_term.size() > 0 ) {
515         return long_term.front().sim_time;
516     } else if ( medium_term.size() > 0 ) {
517         return medium_term.front().sim_time;
518     } else if ( short_term.size() ) {
519         return short_term.front().sim_time;
520     } else {
521         return 0.0;
522     }
523 }
524
525 double FGReplay::get_end_time() {
526     if ( short_term.size() ) {
527         return short_term.back().sim_time;
528     } else {
529         return 0.0;
530     } 
531 }