1 // replay.cxx - a system to record and replay FlightGear flights
3 // Written by Curtis Olson, started Juley 2003.
5 // Copyright (C) 2003 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/constants.h>
26 #include <FDM/flight.hxx>
27 #include <Network/native_ctrls.hxx>
28 #include <Network/native_fdm.hxx>
29 #include <Network/net_ctrls.hxx>
30 #include <Network/net_fdm.hxx>
34 const double FGReplay::st_list_time = 60.0; // 60 secs of high res data
35 const double FGReplay::mt_list_time = 600.0; // 10 mins of 1 fps data
36 const double FGReplay::lt_list_time = 3600.0; // 1 hr of 10 spf data
38 // short term sample rate is as every frame
39 const double FGReplay::mt_dt = 0.5; // medium term sample rate (sec)
40 const double FGReplay::lt_dt = 5.0; // long term sample rate (sec)
46 FGReplay::FGReplay() {
54 FGReplay::~FGReplay() {
55 // no dynamically allocated memory to free
60 * Initialize the data structures
63 void FGReplay::init() {
68 // Make sure all queues are flushed
69 while ( !short_term.empty() ) {
70 short_term.pop_front();
72 while ( !medium_term.empty() ) {
73 medium_term.pop_front();
75 while ( !medium_term.empty() ) {
76 medium_term.pop_front();
82 * Bind to the property tree
85 void FGReplay::bind() {
91 * Unbind from the property tree
94 void FGReplay::unbind() {
100 * Update the saved data
103 void FGReplay::update( double dt ) {
106 // don't save data if nothing is going on ...
113 // build the replay record
115 FGProps2NetFDM( &f, false );
117 // sanity check, don't collect data if FDM data isn't good
118 if ( !cur_fdm_state->get_inited() ) {
123 FGProps2NetCtrls( &c, false, false );
126 r.sim_time = sim_time;
130 // update the short term list
131 short_term.push_back( r );
133 FGReplayData st_front = short_term.front();
134 if ( sim_time - st_front.sim_time > st_list_time ) {
135 while ( sim_time - st_front.sim_time > st_list_time ) {
136 st_front = short_term.front();
137 short_term.pop_front();
140 // update the medium term list
141 if ( sim_time - last_mt_time > mt_dt ) {
142 last_mt_time = sim_time;
143 medium_term.push_back( st_front );
145 FGReplayData mt_front = medium_term.front();
146 if ( sim_time - mt_front.sim_time > mt_list_time ) {
147 while ( sim_time - mt_front.sim_time > mt_list_time ) {
148 mt_front = medium_term.front();
149 medium_term.pop_front();
152 // update the long term list
153 if ( sim_time - last_lt_time > lt_dt ) {
154 last_lt_time = sim_time;
155 long_term.push_back( mt_front );
157 FGReplayData lt_front = long_term.front();
158 if ( sim_time - lt_front.sim_time > lt_list_time ) {
159 while ( sim_time - lt_front.sim_time > lt_list_time ) {
160 lt_front = long_term.front();
161 long_term.pop_front();
170 cout << "short term size = " << short_term.size()
171 << " time = " << sim_time - short_term.front().sim_time
173 cout << "medium term size = " << medium_term.size()
174 << " time = " << sim_time - medium_term.front().sim_time
176 cout << "long term size = " << long_term.size()
177 << " time = " << sim_time - long_term.front().sim_time
183 static double weight( double data1, double data2, double ratio,
184 bool rotational = false ) {
186 // special handling of rotational data
187 double tmp = data2 - data1;
188 if ( tmp > SGD_PI ) {
190 } else if ( tmp < -SGD_PI ) {
193 return data1 + tmp * ratio;
195 // normal "linear" data
196 return data1 + ( data2 - data1 ) * ratio;
201 * given two FGReplayData elements and a time, interpolate between them
203 static void update_fdm( FGReplayData frame ) {
204 FGNetFDM2Props( &frame.fdm, false );
205 FGNetCtrls2Props( &frame.ctrls, false, false );
209 * given two FGReplayData elements and a time, interpolate between them
211 static FGReplayData interpolate( double time, FGReplayData f1, FGReplayData f2 )
213 FGReplayData result = f1;
215 FGNetFDM fdm1 = f1.fdm;
216 FGNetFDM fdm2 = f2.fdm;
218 double ratio = (time - f1.sim_time) / (f2.sim_time - f1.sim_time);
220 cout << fdm1.longitude << " " << fdm2.longitude << endl;
221 result.fdm.longitude = weight( fdm1.longitude, fdm2.longitude, ratio );
222 result.fdm.latitude = weight( fdm1.latitude, fdm2.latitude, ratio );
223 result.fdm.altitude = weight( fdm1.altitude, fdm2.altitude, ratio );
224 result.fdm.agl = weight( fdm1.agl, fdm2.agl, ratio );
225 result.fdm.phi = weight( fdm1.phi, fdm2.phi, ratio, true );
226 result.fdm.theta = weight( fdm1.theta, fdm2.theta, ratio, true );
227 result.fdm.psi = weight( fdm1.psi, fdm2.psi, ratio, true );
229 result.fdm.phidot = weight( fdm1.phidot, fdm2.phidot, ratio, true );
230 result.fdm.thetadot = weight( fdm1.thetadot, fdm2.thetadot, ratio, true );
231 result.fdm.psidot = weight( fdm1.psidot, fdm2.psidot, ratio, true );
232 result.fdm.vcas = weight( fdm1.vcas, fdm2.vcas, ratio );
233 result.fdm.climb_rate = weight( fdm1.climb_rate, fdm2.climb_rate, ratio );
234 result.fdm.v_north = weight( fdm1.v_north, fdm2.v_north, ratio );
235 result.fdm.v_east = weight( fdm1.v_east, fdm2.v_east, ratio );
236 result.fdm.v_down = weight( fdm1.v_down, fdm2.v_down, ratio );
238 result.fdm.v_wind_body_north
239 = weight( fdm1.v_wind_body_north, fdm2.v_wind_body_north, ratio );
240 result.fdm.v_wind_body_east
241 = weight( fdm1.v_wind_body_east, fdm2.v_wind_body_east, ratio );
242 result.fdm.v_wind_body_down
243 = weight( fdm1.v_wind_body_down, fdm2.v_wind_body_down, ratio );
245 result.fdm.stall_warning
246 = weight( fdm1.stall_warning, fdm2.stall_warning, ratio );
248 result.fdm.A_X_pilot = weight( fdm1.A_X_pilot, fdm2.A_X_pilot, ratio );
249 result.fdm.A_Y_pilot = weight( fdm1.A_Y_pilot, fdm2.A_Y_pilot, ratio );
250 result.fdm.A_Z_pilot = weight( fdm1.A_Z_pilot, fdm2.A_Z_pilot, ratio );
256 * interpolate a specific time from a specific list
258 static void interpolate( double time, replay_list_type list ) {
260 if ( list.size() == 0 ) {
263 } else if ( list.size() == 1 ) {
264 // handle list size == 1
265 update_fdm( list[0] );
269 unsigned int last = list.size() - 1;
270 unsigned int first = 0;
271 unsigned int mid = ( last + first ) / 2;
276 // cout << " " << first << " <=> " << last << endl;
277 if ( last == first ) {
279 } else if ( list[mid].sim_time < time && list[mid+1].sim_time < time ) {
282 mid = ( last + first ) / 2;
283 } else if ( list[mid].sim_time > time && list[mid+1].sim_time > time ) {
286 mid = ( last + first ) / 2;
292 FGReplayData result = interpolate( time, list[mid], list[mid+1] );
294 update_fdm( result );
299 * Replay a saved frame based on time, interpolate from the two
300 * nearest saved frames.
303 void FGReplay::replay( double time ) {
304 cout << "replay: " << time << " ";
305 // find the two frames to interpolate between
308 if ( short_term.size() > 0 ) {
309 t1 = short_term.back().sim_time;
310 t2 = short_term.front().sim_time;
312 // replay the most recent frame
313 update_fdm( short_term.back() );
314 cout << "first frame" << endl;
315 } else if ( time <= t1 && time >= t2 ) {
316 interpolate( time, short_term );
317 cout << "from short term" << endl;
318 } else if ( medium_term.size() > 0 ) {
319 t1 = short_term.front().sim_time;
320 t2 = medium_term.back().sim_time;
321 if ( time <= t1 && time >= t2 ) {
322 FGReplayData result = interpolate( time,
324 short_term.front() );
325 update_fdm( result );
326 cout << "from short/medium term" << endl;
328 t1 = medium_term.back().sim_time;
329 t2 = medium_term.front().sim_time;
330 if ( time <= t1 && time >= t2 ) {
331 interpolate( time, medium_term );
332 cout << "from medium term" << endl;
333 } else if ( long_term.size() > 0 ) {
334 t1 = medium_term.front().sim_time;
335 t2 = long_term.back().sim_time;
336 if ( time <= t1 && time >= t2 ) {
337 FGReplayData result = interpolate( time,
339 medium_term.front());
340 update_fdm( result );
341 cout << "from medium/long term" << endl;
343 t1 = long_term.back().sim_time;
344 t2 = long_term.front().sim_time;
345 if ( time <= t1 && time >= t2 ) {
346 interpolate( time, long_term );
347 cout << "from long term" << endl;
349 // replay the oldest long term frame
350 update_fdm( long_term.front() );
351 cout << "oldest long term frame" << endl;
355 // replay the oldest medium term frame
356 update_fdm( medium_term.front() );
357 cout << "oldest medium term frame" << endl;
361 // replay the oldest short term frame
362 update_fdm( short_term.front() );
363 cout << "oldest short term frame" << endl;
371 double FGReplay::get_start_time() {
372 if ( long_term.size() > 0 ) {
373 return long_term.front().sim_time;
374 } else if ( medium_term.size() > 0 ) {
375 return medium_term.front().sim_time;
376 } else if ( short_term.size() ) {
377 return short_term.front().sim_time;
383 double FGReplay::get_end_time() {
384 if ( short_term.size() ) {
385 return short_term.back().sim_time;