]> git.mxchange.org Git - flightgear.git/blob - src/Replay/replay.cxx
Finish the list of values that need to be interpolated every frame.
[flightgear.git] / src / Replay / 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  - curt@flightgear.org
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     // nothing to bind
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 = fgGetNode( "/sim/freeze/replay" );
106
107     if ( replay_master->getBoolValue() ) {
108         // don't record the replay session
109         return;
110     }
111
112     sim_time += dt;
113
114     // build the replay record
115     FGNetFDM f;
116     FGProps2NetFDM( &f, false );
117
118     // sanity check, don't collect data if FDM data isn't good
119     if ( !cur_fdm_state->get_inited() ) {
120         return;
121     }
122
123     FGNetCtrls c;
124     FGProps2NetCtrls( &c, false, false );
125
126     FGReplayData r;
127     r.sim_time = sim_time;
128     r.ctrls = c;
129     r.fdm = f;
130
131     // update the short term list
132     short_term.push_back( r );
133
134     FGReplayData st_front = short_term.front();
135     if ( sim_time - st_front.sim_time > st_list_time ) {
136         while ( sim_time - st_front.sim_time > st_list_time ) {
137             st_front = short_term.front();
138             short_term.pop_front();
139         }
140
141         // update the medium term list
142         if ( sim_time - last_mt_time > mt_dt ) {
143             last_mt_time = sim_time;
144             medium_term.push_back( st_front );
145
146             FGReplayData mt_front = medium_term.front();
147             if ( sim_time - mt_front.sim_time > mt_list_time ) {
148                 while ( sim_time - mt_front.sim_time > mt_list_time ) {
149                     mt_front = medium_term.front();
150                     medium_term.pop_front();
151                 }
152
153                 // update the long term list
154                 if ( sim_time - last_lt_time > lt_dt ) {
155                     last_lt_time = sim_time;
156                     long_term.push_back( mt_front );
157
158                     FGReplayData lt_front = long_term.front();
159                     if ( sim_time - lt_front.sim_time > lt_list_time ) {
160                         while ( sim_time - lt_front.sim_time > lt_list_time ) {
161                             lt_front = long_term.front();
162                             long_term.pop_front();
163                         }
164                     }
165                 }
166             }
167         }
168     }
169
170 #if 0
171     cout << "short term size = " << short_term.size()
172          << "  time = " << sim_time - short_term.front().sim_time
173          << endl;
174     cout << "medium term size = " << medium_term.size()
175          << "  time = " << sim_time - medium_term.front().sim_time
176          << endl;
177     cout << "long term size = " << long_term.size()
178          << "  time = " << sim_time - long_term.front().sim_time
179          << endl;
180 #endif
181 }
182
183
184 static double weight( double data1, double data2, double ratio,
185                       bool rotational = false ) {
186     if ( rotational ) {
187         // special handling of rotational data
188         double tmp = data2 - data1;
189         if ( tmp > SGD_PI ) {
190             tmp -= SGD_2PI;
191         } else if ( tmp < -SGD_PI ) {
192             tmp += SGD_2PI;
193         }
194         return data1 + tmp * ratio;
195     } else {
196         // normal "linear" data
197         return data1 + ( data2 - data1 ) * ratio;
198     }
199 }
200
201 /** 
202  * given two FGReplayData elements and a time, interpolate between them
203  */
204 static void update_fdm( FGReplayData frame ) {
205     FGNetFDM2Props( &frame.fdm, false );
206     FGNetCtrls2Props( &frame.ctrls, false, false );
207 }
208
209 /** 
210  * given two FGReplayData elements and a time, interpolate between them
211  */
212 static FGReplayData interpolate( double time, FGReplayData f1, FGReplayData f2 )
213 {
214     FGReplayData result = f1;
215
216     FGNetFDM fdm1 = f1.fdm;
217     FGNetFDM fdm2 = f2.fdm;
218
219     FGNetCtrls ctrls1 = f1.ctrls;
220     FGNetCtrls ctrls2 = f2.ctrls;
221
222     double ratio = (time - f1.sim_time) / (f2.sim_time - f1.sim_time);
223
224     // Interpolate FDM data
225
226     // Positions
227     result.fdm.longitude = weight( fdm1.longitude, fdm2.longitude, ratio );
228     result.fdm.latitude = weight( fdm1.latitude, fdm2.latitude, ratio );
229     result.fdm.altitude = weight( fdm1.altitude, fdm2.altitude, ratio );
230     result.fdm.agl = weight( fdm1.agl, fdm2.agl, ratio );
231     result.fdm.phi = weight( fdm1.phi, fdm2.phi, ratio, true );
232     result.fdm.theta = weight( fdm1.theta, fdm2.theta, ratio, true );
233     result.fdm.psi = weight( fdm1.psi, fdm2.psi, ratio, true );
234
235     // Velocities
236     result.fdm.phidot = weight( fdm1.phidot, fdm2.phidot, ratio, true );
237     result.fdm.thetadot = weight( fdm1.thetadot, fdm2.thetadot, ratio, true );
238     result.fdm.psidot = weight( fdm1.psidot, fdm2.psidot, ratio, true );
239     result.fdm.vcas = weight( fdm1.vcas, fdm2.vcas, ratio );
240     result.fdm.climb_rate = weight( fdm1.climb_rate, fdm2.climb_rate, ratio );
241     result.fdm.v_north = weight( fdm1.v_north, fdm2.v_north, ratio );
242     result.fdm.v_east = weight( fdm1.v_east, fdm2.v_east, ratio );
243     result.fdm.v_down = weight( fdm1.v_down, fdm2.v_down, ratio );
244
245     result.fdm.v_wind_body_north
246         = weight( fdm1.v_wind_body_north, fdm2.v_wind_body_north, ratio );
247     result.fdm.v_wind_body_east
248         = weight( fdm1.v_wind_body_east, fdm2.v_wind_body_east, ratio );
249     result.fdm.v_wind_body_down
250         = weight( fdm1.v_wind_body_down, fdm2.v_wind_body_down, ratio );
251
252     // Stall
253     result.fdm.stall_warning
254         = weight( fdm1.stall_warning, fdm2.stall_warning, ratio );
255
256     // Accelerations
257     result.fdm.A_X_pilot = weight( fdm1.A_X_pilot, fdm2.A_X_pilot, ratio );
258     result.fdm.A_Y_pilot = weight( fdm1.A_Y_pilot, fdm2.A_Y_pilot, ratio );
259     result.fdm.A_Z_pilot = weight( fdm1.A_Z_pilot, fdm2.A_Z_pilot, ratio );
260
261     int i;
262
263     // Engine status
264     for ( i = 0; i < fdm1.num_engines; ++i ) {
265         result.fdm.eng_state[i] = fdm1.eng_state[i];
266         result.fdm.rpm[i] = weight( fdm1.rpm[i], fdm2.rpm[i], ratio );
267         result.fdm.fuel_flow[i]
268             = weight( fdm1.fuel_flow[i], fdm2.fuel_flow[i], ratio );
269         result.fdm.EGT[i] = weight( fdm1.EGT[i], fdm2.EGT[i], ratio );
270         result.fdm.oil_temp[i]
271             = weight( fdm1.oil_temp[i], fdm2.oil_temp[i], ratio );
272         result.fdm.oil_px[i] = weight( fdm1.oil_px[i], fdm2.oil_px[i], ratio );
273     }
274
275     // Consumables
276     for ( i = 0; i < fdm1.num_tanks; ++i ) {
277         result.fdm.fuel_quantity[i]
278             = weight( fdm1.fuel_quantity[i], fdm2.fuel_quantity[i], ratio );
279     }
280
281     // Gear status
282     for ( i = 0; i < fdm1.num_wheels; ++i ) {
283         result.fdm.wow[i]
284             = weight( fdm1.wow[i], fdm2.wow[i], ratio );
285     }
286
287     // Environment
288     result.fdm.cur_time = fdm1.cur_time;
289     result.fdm.warp = fdm1.warp;
290     result.fdm.visibility = weight( fdm1.visibility, fdm2.visibility, ratio );
291
292     // Control surface positions (normalized values)
293     result.fdm.elevator = weight( fdm1.elevator, fdm2.elevator, ratio );
294     result.fdm.flaps = weight( fdm1.flaps, fdm2.flaps, ratio );
295     result.fdm.left_aileron
296         = weight( fdm1.left_aileron, fdm2.left_aileron, ratio );
297     result.fdm.right_aileron
298         = weight( fdm1.right_aileron, fdm2.right_aileron, ratio );
299     result.fdm.rudder = weight( fdm1.rudder, fdm2.rudder, ratio );
300     result.fdm.speedbrake = weight( fdm1.speedbrake, fdm2.speedbrake, ratio );
301     result.fdm.spoilers = weight( fdm1.spoilers, fdm2.spoilers, ratio );
302      
303     // Interpolate Control input data
304
305     // Aero controls
306     result.ctrls.aileron = weight( ctrls1.aileron, ctrls2.aileron, ratio );
307     result.ctrls.elevator = weight( ctrls1.elevator, ctrls2.elevator, ratio );
308     result.ctrls.elevator_trim
309         = weight( ctrls1.elevator_trim, ctrls2.elevator_trim, ratio );
310     result.ctrls.rudder = weight( ctrls1.rudder, ctrls2.rudder, ratio );
311     result.ctrls.flaps = weight( ctrls1.flaps, ctrls2.flaps, ratio );
312     result.ctrls.flaps_power
313         = weight( ctrls1.flaps_power, ctrls2.flaps_power, ratio );
314
315     // Engine controls
316     for ( i = 0; i < ctrls1.num_engines; ++i ) {
317         result.ctrls.magnetos[i] = ctrls1.magnetos[i];
318         result.ctrls.starter_power[i] = ctrls1.starter_power[i];
319         result.ctrls.throttle[i]
320             = weight( ctrls1.throttle[i], ctrls2.throttle[i], ratio );
321         result.ctrls.mixture[i]
322             = weight( ctrls1.mixture[i], ctrls2.mixture[i], ratio );
323         result.ctrls.fuel_pump_power[i] = ctrls1.fuel_pump_power[i];
324         result.ctrls.prop_advance[i]
325             = weight( ctrls1.prop_advance[i], ctrls2.prop_advance[i], ratio );
326     }
327
328     // Fuel management
329     for ( i = 0; i < ctrls1.num_tanks; ++i ) {
330         result.ctrls.fuel_selector[i] = ctrls1.fuel_selector[i];
331     }
332
333     // Brake controls
334     for ( i = 0; i < ctrls1.num_wheels; ++i ) {
335         result.ctrls.brake[i]
336             = weight( ctrls1.brake[i], ctrls2.brake[i], ratio );
337     }
338
339     // Landing Gear
340     result.ctrls.gear_handle = ctrls1.gear_handle;
341
342     // Switches
343     result.ctrls.master_bat = ctrls1.master_bat;
344     result.ctrls.master_alt = ctrls1.master_alt;
345     result.ctrls.turbulence_norm = ctrls1.turbulence_norm;
346
347     // wind and turbulance
348     result.ctrls.wind_speed_kt
349         = weight( ctrls1.wind_speed_kt, ctrls2.wind_speed_kt, ratio );
350     result.ctrls.wind_dir_deg
351         = weight( ctrls1.wind_dir_deg, ctrls2.wind_dir_deg, ratio );
352     result.ctrls.turbulence_norm
353         = weight( ctrls1.turbulence_norm, ctrls2.turbulence_norm, ratio );
354
355     // other information about environment
356     result.ctrls.hground = weight( ctrls1.hground, ctrls2.hground, ratio );
357     result.ctrls.magvar = weight( ctrls1.magvar, ctrls2.magvar, ratio );
358
359     // simulation control
360     result.ctrls.speedup = ctrls1.speedup;
361     result.ctrls.freeze = ctrls1.freeze;
362
363     return result;
364 }
365
366 /** 
367  * interpolate a specific time from a specific list
368  */
369 static void interpolate( double time, replay_list_type list ) {
370     // sanity checking
371     if ( list.size() == 0 ) {
372         // handle empty list
373         return;
374     } else if ( list.size() == 1 ) {
375         // handle list size == 1
376         update_fdm( list[0] );
377         return;
378     }
379
380     unsigned int last = list.size() - 1;
381     unsigned int first = 0;
382     unsigned int mid = ( last + first ) / 2;
383
384
385     bool done = false;
386     while ( !done ) {
387         // cout << "  " << first << " <=> " << last << endl;
388         if ( last == first ) {
389             done = true;
390         } else if ( list[mid].sim_time < time && list[mid+1].sim_time < time ) {
391             // too low
392             first = mid;
393             mid = ( last + first ) / 2;
394         } else if ( list[mid].sim_time > time && list[mid+1].sim_time > time ) {
395             // too high
396             last = mid;
397             mid = ( last + first ) / 2;
398         } else {
399             done = true;
400         }
401     }
402
403     FGReplayData result = interpolate( time, list[mid], list[mid+1] );
404
405     update_fdm( result );
406 }
407
408
409 /** 
410  *  Replay a saved frame based on time, interpolate from the two
411  *  nearest saved frames.
412  */
413
414 void FGReplay::replay( double time ) {
415     cout << "replay: " << time << " ";
416     // find the two frames to interpolate between
417     double t1, t2;
418
419     if ( short_term.size() > 0 ) {
420         t1 = short_term.back().sim_time;
421         t2 = short_term.front().sim_time;
422         if ( time > t1 ) {
423             // replay the most recent frame
424             update_fdm( short_term.back() );
425             cout << "first frame" << endl;
426         } else if ( time <= t1 && time >= t2 ) {
427             interpolate( time, short_term );
428             cout << "from short term" << endl;
429         } else if ( medium_term.size() > 0 ) {
430             t1 = short_term.front().sim_time;
431             t2 = medium_term.back().sim_time;
432             if ( time <= t1 && time >= t2 ) {
433                 FGReplayData result = interpolate( time,
434                                                    medium_term.back(),
435                                                    short_term.front() );
436                 update_fdm( result );
437                 cout << "from short/medium term" << endl;
438             } else {
439                 t1 = medium_term.back().sim_time;
440                 t2 = medium_term.front().sim_time;
441                 if ( time <= t1 && time >= t2 ) {
442                     interpolate( time, medium_term );
443                     cout << "from medium term" << endl;
444                 } else if ( long_term.size() > 0 ) {
445                     t1 = medium_term.front().sim_time;
446                     t2 = long_term.back().sim_time;
447                     if ( time <= t1 && time >= t2 ) {
448                         FGReplayData result = interpolate( time,
449                                                            long_term.back(),
450                                                            medium_term.front());
451                         update_fdm( result );
452                        cout << "from medium/long term" << endl;
453                     } else {
454                         t1 = long_term.back().sim_time;
455                         t2 = long_term.front().sim_time;
456                         if ( time <= t1 && time >= t2 ) {
457                             interpolate( time, long_term );
458                             cout << "from long term" << endl;
459                         } else {
460                             // replay the oldest long term frame
461                             update_fdm( long_term.front() );
462                             cout << "oldest long term frame" << endl;
463                         }
464                     }
465                 } else {
466                     // replay the oldest medium term frame
467                     update_fdm( medium_term.front() );
468                     cout << "oldest medium term frame" << endl;
469                 }
470             }
471         } else {
472             // replay the oldest short term frame
473             update_fdm( short_term.front() );
474             cout << "oldest short term frame" << endl;
475         }
476     } else {
477         // nothing to replay
478     }
479 }
480
481
482 double FGReplay::get_start_time() {
483     if ( long_term.size() > 0 ) {
484         return long_term.front().sim_time;
485     } else if ( medium_term.size() > 0 ) {
486         return medium_term.front().sim_time;
487     } else if ( short_term.size() ) {
488         return short_term.front().sim_time;
489     } else {
490         return 0.0;
491     }
492 }
493
494 double FGReplay::get_end_time() {
495     if ( short_term.size() ) {
496         return short_term.back().sim_time;
497     } else {
498         return 0.0;
499     } 
500 }