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 <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>
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
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)
47 FGReplay::FGReplay() {
55 FGReplay::~FGReplay() {
56 // no dynamically allocated memory to free
61 * Initialize the data structures
64 void FGReplay::init() {
69 // Make sure all queues are flushed
70 while ( !short_term.empty() ) {
71 short_term.pop_front();
73 while ( !medium_term.empty() ) {
74 medium_term.pop_front();
76 while ( !medium_term.empty() ) {
77 medium_term.pop_front();
83 * Bind to the property tree
86 void FGReplay::bind() {
92 * Unbind from the property tree
95 void FGReplay::unbind() {
101 * Update the saved data
104 void FGReplay::update( double dt ) {
105 static SGPropertyNode *replay_master = fgGetNode( "/sim/freeze/replay" );
107 if ( replay_master->getBoolValue() ) {
108 // don't record the replay session
114 // build the replay record
116 FGProps2NetFDM( &f, false );
118 // sanity check, don't collect data if FDM data isn't good
119 if ( !cur_fdm_state->get_inited() ) {
124 FGProps2NetCtrls( &c, false, false );
127 r.sim_time = sim_time;
131 // update the short term list
132 short_term.push_back( r );
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();
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 );
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();
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 );
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();
171 cout << "short term size = " << short_term.size()
172 << " time = " << sim_time - short_term.front().sim_time
174 cout << "medium term size = " << medium_term.size()
175 << " time = " << sim_time - medium_term.front().sim_time
177 cout << "long term size = " << long_term.size()
178 << " time = " << sim_time - long_term.front().sim_time
184 static double weight( double data1, double data2, double ratio,
185 bool rotational = false ) {
187 // special handling of rotational data
188 double tmp = data2 - data1;
189 if ( tmp > SGD_PI ) {
191 } else if ( tmp < -SGD_PI ) {
194 return data1 + tmp * ratio;
196 // normal "linear" data
197 return data1 + ( data2 - data1 ) * ratio;
202 * given two FGReplayData elements and a time, interpolate between them
204 static void update_fdm( FGReplayData frame ) {
205 FGNetFDM2Props( &frame.fdm, false );
206 FGNetCtrls2Props( &frame.ctrls, false, false );
210 * given two FGReplayData elements and a time, interpolate between them
212 static FGReplayData interpolate( double time, FGReplayData f1, FGReplayData f2 )
214 FGReplayData result = f1;
216 FGNetFDM fdm1 = f1.fdm;
217 FGNetFDM fdm2 = f2.fdm;
219 double ratio = (time - f1.sim_time) / (f2.sim_time - f1.sim_time);
221 cout << fdm1.longitude << " " << fdm2.longitude << endl;
222 result.fdm.longitude = weight( fdm1.longitude, fdm2.longitude, ratio );
223 result.fdm.latitude = weight( fdm1.latitude, fdm2.latitude, ratio );
224 result.fdm.altitude = weight( fdm1.altitude, fdm2.altitude, ratio );
225 result.fdm.agl = weight( fdm1.agl, fdm2.agl, ratio );
226 result.fdm.phi = weight( fdm1.phi, fdm2.phi, ratio, true );
227 result.fdm.theta = weight( fdm1.theta, fdm2.theta, ratio, true );
228 result.fdm.psi = weight( fdm1.psi, fdm2.psi, ratio, true );
230 result.fdm.phidot = weight( fdm1.phidot, fdm2.phidot, ratio, true );
231 result.fdm.thetadot = weight( fdm1.thetadot, fdm2.thetadot, ratio, true );
232 result.fdm.psidot = weight( fdm1.psidot, fdm2.psidot, ratio, true );
233 result.fdm.vcas = weight( fdm1.vcas, fdm2.vcas, ratio );
234 result.fdm.climb_rate = weight( fdm1.climb_rate, fdm2.climb_rate, ratio );
235 result.fdm.v_north = weight( fdm1.v_north, fdm2.v_north, ratio );
236 result.fdm.v_east = weight( fdm1.v_east, fdm2.v_east, ratio );
237 result.fdm.v_down = weight( fdm1.v_down, fdm2.v_down, ratio );
239 result.fdm.v_wind_body_north
240 = weight( fdm1.v_wind_body_north, fdm2.v_wind_body_north, ratio );
241 result.fdm.v_wind_body_east
242 = weight( fdm1.v_wind_body_east, fdm2.v_wind_body_east, ratio );
243 result.fdm.v_wind_body_down
244 = weight( fdm1.v_wind_body_down, fdm2.v_wind_body_down, ratio );
246 result.fdm.stall_warning
247 = weight( fdm1.stall_warning, fdm2.stall_warning, ratio );
249 result.fdm.A_X_pilot = weight( fdm1.A_X_pilot, fdm2.A_X_pilot, ratio );
250 result.fdm.A_Y_pilot = weight( fdm1.A_Y_pilot, fdm2.A_Y_pilot, ratio );
251 result.fdm.A_Z_pilot = weight( fdm1.A_Z_pilot, fdm2.A_Z_pilot, ratio );
257 * interpolate a specific time from a specific list
259 static void interpolate( double time, replay_list_type list ) {
261 if ( list.size() == 0 ) {
264 } else if ( list.size() == 1 ) {
265 // handle list size == 1
266 update_fdm( list[0] );
270 unsigned int last = list.size() - 1;
271 unsigned int first = 0;
272 unsigned int mid = ( last + first ) / 2;
277 // cout << " " << first << " <=> " << last << endl;
278 if ( last == first ) {
280 } else if ( list[mid].sim_time < time && list[mid+1].sim_time < time ) {
283 mid = ( last + first ) / 2;
284 } else if ( list[mid].sim_time > time && list[mid+1].sim_time > time ) {
287 mid = ( last + first ) / 2;
293 FGReplayData result = interpolate( time, list[mid], list[mid+1] );
295 update_fdm( result );
300 * Replay a saved frame based on time, interpolate from the two
301 * nearest saved frames.
304 void FGReplay::replay( double time ) {
305 cout << "replay: " << time << " ";
306 // find the two frames to interpolate between
309 if ( short_term.size() > 0 ) {
310 t1 = short_term.back().sim_time;
311 t2 = short_term.front().sim_time;
313 // replay the most recent frame
314 update_fdm( short_term.back() );
315 cout << "first frame" << endl;
316 } else if ( time <= t1 && time >= t2 ) {
317 interpolate( time, short_term );
318 cout << "from short term" << endl;
319 } else if ( medium_term.size() > 0 ) {
320 t1 = short_term.front().sim_time;
321 t2 = medium_term.back().sim_time;
322 if ( time <= t1 && time >= t2 ) {
323 FGReplayData result = interpolate( time,
325 short_term.front() );
326 update_fdm( result );
327 cout << "from short/medium term" << endl;
329 t1 = medium_term.back().sim_time;
330 t2 = medium_term.front().sim_time;
331 if ( time <= t1 && time >= t2 ) {
332 interpolate( time, medium_term );
333 cout << "from medium term" << endl;
334 } else if ( long_term.size() > 0 ) {
335 t1 = medium_term.front().sim_time;
336 t2 = long_term.back().sim_time;
337 if ( time <= t1 && time >= t2 ) {
338 FGReplayData result = interpolate( time,
340 medium_term.front());
341 update_fdm( result );
342 cout << "from medium/long term" << endl;
344 t1 = long_term.back().sim_time;
345 t2 = long_term.front().sim_time;
346 if ( time <= t1 && time >= t2 ) {
347 interpolate( time, long_term );
348 cout << "from long term" << endl;
350 // replay the oldest long term frame
351 update_fdm( long_term.front() );
352 cout << "oldest long term frame" << endl;
356 // replay the oldest medium term frame
357 update_fdm( medium_term.front() );
358 cout << "oldest medium term frame" << endl;
362 // replay the oldest short term frame
363 update_fdm( short_term.front() );
364 cout << "oldest short term frame" << endl;
372 double FGReplay::get_start_time() {
373 if ( long_term.size() > 0 ) {
374 return long_term.front().sim_time;
375 } else if ( medium_term.size() > 0 ) {
376 return medium_term.front().sim_time;
377 } else if ( short_term.size() ) {
378 return short_term.front().sim_time;
384 double FGReplay::get_end_time() {
385 if ( short_term.size() ) {
386 return short_term.back().sim_time;