1
0
Fork 0

Replay upgrade, part II: Integrate flight recorder, update replay system.

Drop hard-coded replay property logic, cut ties to net_ctrls, net_fdm.
Improve replay system controls and status information.
This commit is contained in:
ThorstenB 2011-10-01 22:58:40 +02:00
parent d64bcf0175
commit ab84ff5904
5 changed files with 282 additions and 373 deletions

View file

@ -1,6 +1,7 @@
// replay.cxx - a system to record and replay FlightGear flights
//
// Written by Curtis Olson, started Juley 2003.
// Written by Curtis Olson, started July 2003.
// Updated by Thorsten Brehm, September 2011.
//
// Copyright (C) 2003 Curtis L. Olson - http://www.flightgear.org/~curt
//
@ -25,36 +26,31 @@
#endif
#include <float.h>
#include <simgear/constants.h>
#include <simgear/structure/exception.hxx>
#include <Main/fg_props.hxx>
#include <Network/native_ctrls.hxx>
#include <Network/native_fdm.hxx>
#include <Network/net_ctrls.hxx>
#include <Network/net_fdm.hxx>
#include <FDM/fdm_shell.hxx>
#include "replay.hxx"
const double FGReplay::st_list_time = 60.0; // 60 secs of high res data
const double FGReplay::mt_list_time = 600.0; // 10 mins of 1 fps data
const double FGReplay::lt_list_time = 3600.0; // 1 hr of 10 spf data
// short term sample rate is as every frame
const double FGReplay::mt_dt = 0.5; // medium term sample rate (sec)
const double FGReplay::lt_dt = 5.0; // long term sample rate (sec)
#include "flightrecorder.hxx"
/**
* Constructor
*/
FGReplay::FGReplay() :
last_replay_state(0)
last_replay_state(0),
m_high_res_time(60.0),
m_medium_res_time(600.0),
m_low_res_time(3600.0),
m_medium_sample_rate(0.5), // medium term sample rate (sec)
m_long_sample_rate(5.0), // long term sample rate (sec)
m_pRecorder(new FGFlightRecorder("replay-config"))
{
}
/**
* Destructor
*/
@ -62,31 +58,35 @@ FGReplay::FGReplay() :
FGReplay::~FGReplay()
{
clear();
delete m_pRecorder;
m_pRecorder = NULL;
}
/**
* Clear all internal buffers.
*/
void FGReplay::clear()
void
FGReplay::clear()
{
while ( !short_term.empty() )
{
delete short_term.front();
m_pRecorder->deleteRecord(short_term.front());
short_term.pop_front();
}
while ( !medium_term.empty() )
{
delete medium_term.front();
m_pRecorder->deleteRecord(medium_term.front());
medium_term.pop_front();
}
while ( !long_term.empty() )
{
delete long_term.front();
m_pRecorder->deleteRecord(long_term.front());
long_term.pop_front();
}
while ( !recycler.empty() )
{
delete recycler.front();
m_pRecorder->deleteRecord(recycler.front());
recycler.pop_front();
}
}
@ -95,12 +95,15 @@ void FGReplay::clear()
* Initialize the data structures
*/
void FGReplay::init()
void
FGReplay::init()
{
disable_replay = fgGetNode( "/sim/replay/disable", true );
replay_master = fgGetNode( "/sim/freeze/replay-state", true );
replay_time = fgGetNode( "/sim/replay/time", true);
replay_looped = fgGetNode( "/sim/replay/looped", true);
disable_replay = fgGetNode("/sim/replay/disable", true);
replay_master = fgGetNode("/sim/freeze/replay-state", true);
replay_time = fgGetNode("/sim/replay/time", true);
replay_time_str = fgGetNode("/sim/replay/time-str", true);
replay_looped = fgGetNode("/sim/replay/looped", true);
speed_up = fgGetNode("/sim/speed-up", true);
reinit();
}
@ -108,33 +111,50 @@ void FGReplay::init()
* Reset replay queues.
*/
void FGReplay::reinit()
void
FGReplay::reinit()
{
sim_time = 0.0;
last_mt_time = 0.0;
last_lt_time = 0.0;
// Make sure all queues are flushed
// Flush queues
clear();
m_pRecorder->reinit();
m_high_res_time = fgGetDouble("/sim/replay/buffer/high-res-time", 60.0);
m_medium_res_time = fgGetDouble("/sim/replay/buffer/medium-res-time", 600.0); // 10 mins
m_low_res_time = fgGetDouble("/sim/replay/buffer/low-res-time", 3600.0); // 1 h
// short term sample rate is as every frame
m_medium_sample_rate = fgGetDouble("/sim/replay/buffer/medium-res-sample-dt", 0.5); // medium term sample rate (sec)
m_long_sample_rate = fgGetDouble("/sim/replay/buffer/low-res-sample-dt", 5.0); // long term sample rate (sec)
// Create an estimated nr of required ReplayData objects
// 120 is an estimated maximum frame rate.
int estNrObjects = (int) ((st_list_time*120) + (mt_list_time*mt_dt) +
(lt_list_time*lt_dt));
// 120 is an estimated maximum frame rate.
int estNrObjects = (int) ((m_high_res_time*120) + (m_medium_res_time*m_medium_sample_rate) +
(m_low_res_time*m_long_sample_rate));
for (int i = 0; i < estNrObjects; i++)
{
recycler.push_back(new FGReplayData);
FGReplayData* r = m_pRecorder->createEmptyRecord();
if (r)
recycler.push_back(r);
else
{
SG_LOG(SG_SYSTEMS, SG_ALERT, "ReplaySystem: Out of memory!");
}
}
replay_master->setIntValue(0);
disable_replay->setBoolValue(0);
replay_time->setDoubleValue(0);
replay_time_str->setStringValue("");
}
/**
* Bind to the property tree
*/
void FGReplay::bind()
void
FGReplay::bind()
{
}
@ -143,26 +163,85 @@ void FGReplay::bind()
* Unbind from the property tree
*/
void FGReplay::unbind()
void
FGReplay::unbind()
{
// nothing to unbind
}
static void
printTimeStr(char* pStrBuffer,double _Time, bool ShowDecimal=true)
{
if (_Time<0)
_Time = 0;
unsigned int Time = (unsigned int) (_Time*10);
int h = Time/36000;
int m = (Time % 36000)/600;
int s = (Time % 600)/10;
int d = Time % 10;
if (h>0)
sprintf(pStrBuffer,"%u:%02u",h,m);
else
sprintf(pStrBuffer,"%u",m);
if (ShowDecimal)
sprintf(pStrBuffer,"%s:%02u.%u",pStrBuffer,s,d);
else
sprintf(pStrBuffer,"%s:%02u",pStrBuffer,s);
}
/** Start replay session
*/
bool
FGReplay::start()
{
// freeze the fdm, resume from sim pause
double StartTime = get_start_time();
double EndTime = get_end_time();
fgSetDouble("/sim/replay/start-time", StartTime);
fgSetDouble("/sim/replay/end-time", EndTime);
char StrBuffer[30];
printTimeStr(StrBuffer,StartTime,false);
fgSetString("/sim/replay/start-time-str", StrBuffer);
printTimeStr(StrBuffer,EndTime,false);
fgSetString("/sim/replay/end-time-str", StrBuffer);
unsigned long buffer_elements = short_term.size()+medium_term.size()+long_term.size();
fgSetDouble("/sim/replay/buffer-size-mbyte",
buffer_elements*m_pRecorder->getRecordSize() / (1024*1024.0));
if ((fgGetBool("/sim/freeze/master"))||
(0 == replay_master->getIntValue()))
fgSetString("/sim/messages/copilot", "Replay active. 'Esc' to stop.");
fgSetBool ("/sim/freeze/master", 0);
fgSetBool ("/sim/freeze/clock", 0);
if (0 == replay_master->getIntValue())
{
replay_master->setIntValue(1);
replay_time->setDoubleValue(-1);
replay_time_str->setStringValue("");
}
return true;
}
/**
* Update the saved data
*/
void FGReplay::update( double dt )
void
FGReplay::update( double dt )
{
int current_replay_state = last_replay_state;
timingInfo.clear();
stamp("begin");
if ( disable_replay->getBoolValue() )
{
current_replay_state = replay_master->getIntValue();
replay_master->setIntValue(0);
replay_time->setDoubleValue(0);
replay_time_str->setStringValue("");
disable_replay->setBoolValue(0);
speed_up->setDoubleValue(1.0);
fgSetString("/sim/messages/copilot", "Replay stopped");
}
int replay_state = replay_master->getIntValue();
@ -180,8 +259,17 @@ void FGReplay::update( double dt )
if ((replay_state == 0)&&
(last_replay_state > 0))
{
// replay was active, restore most recent frame
replay(DBL_MAX);
if (current_replay_state == 3)
{
// "my controls!" requested: pilot takes control at current replay position...
// May need to uncrash the aircraft here :)
fgSetBool("/sim/crashed", false);
}
else
{
// replay was active, restore most recent frame
replay(DBL_MAX);
}
// replay is finished, resume FDM
((FDMShell*) globals->get_subsystem("flight"))->getFDM()->resume();
}
@ -195,30 +283,36 @@ void FGReplay::update( double dt )
// replay inactive, keep recording
break;
case 1:
{
// replay active
double current_time = replay_time->getDoubleValue();
if (current_time<=0.0)
{
// replay active
double current_time = replay_time->getDoubleValue();
if (current_time<0.0)
{
// initialize start time
fgSetDouble( "/sim/replay/start-time", get_start_time() );
fgSetDouble( "/sim/replay/end-time", get_end_time() );
double duration = fgGetDouble( "/sim/replay/duration" );
if( duration && duration < (get_end_time() - get_start_time()) ) {
current_time = get_end_time() - duration;
} else {
current_time = get_start_time();
}
// initialize start time
double startTime = get_start_time();
double endTime = get_end_time();
fgSetDouble( "/sim/replay/start-time", startTime );
fgSetDouble( "/sim/replay/end-time", endTime );
double duration = fgGetDouble( "/sim/replay/duration" );
if( duration && (duration < (endTime - startTime)) ) {
current_time = endTime - duration;
} else {
current_time = startTime;
}
bool IsFinished = replay( replay_time->getDoubleValue() );
if ((IsFinished)&&(replay_looped->getBoolValue()))
current_time = -1;
else
current_time += dt * fgGetInt("/sim/speed-up");
replay_time->setDoubleValue(current_time);
}
bool IsFinished = replay( replay_time->getDoubleValue() );
if (IsFinished)
current_time = (replay_looped->getBoolValue()) ? -1 : get_end_time()+0.01;
else
current_time += dt * speed_up->getDoubleValue();
replay_time->setDoubleValue(current_time);
char StrBuffer[30];
printTimeStr(StrBuffer,current_time);
replay_time_str->setStringValue((const char*)StrBuffer);
return; // don't record the replay session
case 2:
}
case 2: // normal replay operation
case 3: // replay operation, prepare to resume normal flight at current replay position
// replay paused, no-op
return; // don't record the replay session
default:
@ -228,78 +322,64 @@ void FGReplay::update( double dt )
// flight recording
//cerr << "Recording replay" << endl;
sim_time += dt;
// build the replay record
//FGNetFDM f;
//FGProps2NetFDM( &f, false );
sim_time += dt * speed_up->getDoubleValue();
// sanity check, don't collect data if FDM data isn't good
if (!fgGetBool("/sim/fdm-initialized", false)) {
return;
}
//FGNetCtrls c;
//FGProps2NetCtrls( &c, false, false );
//stamp("point_04ba");
FGReplayData *r;
//stamp("point_04bb");
if (!recycler.size()) {
stamp("Replay_01");
r = new FGReplayData;
stamp("Replay_02");
} else {
r = recycler.front();
recycler.pop_front();
//stamp("point_04be");
}
r->sim_time = sim_time;
//r->ctrls = c;
//stamp("point_04e");
FGProps2NetFDM( &(r->fdm), false );
FGProps2NetCtrls( &(r->ctrls), false, false );
//r->fdm = f;
//stamp("point_05");
FGReplayData* r = record(sim_time);
if (!r)
{
SG_LOG(SG_SYSTEMS, SG_ALERT, "ReplaySystem: Out of memory!");
return;
}
// update the short term list
//stamp("point_06");
short_term.push_back( r );
//stamp("point_07");
FGReplayData *st_front = short_term.front();
if ( sim_time - st_front->sim_time > st_list_time ) {
while ( sim_time - st_front->sim_time > st_list_time ) {
if (!st_front)
{
SG_LOG(SG_SYSTEMS, SG_ALERT, "ReplaySystem: Inconsistent data!");
}
if ( sim_time - st_front->sim_time > m_high_res_time ) {
while ( sim_time - st_front->sim_time > m_high_res_time ) {
st_front = short_term.front();
recycler.push_back(st_front);
short_term.pop_front();
}
//stamp("point_08");
// update the medium term list
if ( sim_time - last_mt_time > mt_dt ) {
if ( sim_time - last_mt_time > m_medium_sample_rate ) {
last_mt_time = sim_time;
st_front = short_term.front();
medium_term.push_back( st_front );
short_term.pop_front();
FGReplayData *mt_front = medium_term.front();
if ( sim_time - mt_front->sim_time > mt_list_time ) {
if ( sim_time - mt_front->sim_time > m_medium_res_time ) {
//stamp("point_09");
while ( sim_time - mt_front->sim_time > mt_list_time ) {
while ( sim_time - mt_front->sim_time > m_medium_res_time ) {
mt_front = medium_term.front();
recycler.push_back(mt_front);
medium_term.pop_front();
}
// update the long term list
if ( sim_time - last_lt_time > lt_dt ) {
if ( sim_time - last_lt_time > m_long_sample_rate ) {
last_lt_time = sim_time;
mt_front = medium_term.front();
long_term.push_back( mt_front );
medium_term.pop_front();
FGReplayData *lt_front = long_term.front();
if ( sim_time - lt_front->sim_time > lt_list_time ) {
if ( sim_time - lt_front->sim_time > m_low_res_time ) {
//stamp("point_10");
while ( sim_time - lt_front->sim_time > lt_list_time ) {
while ( sim_time - lt_front->sim_time > m_low_res_time ) {
lt_front = long_term.front();
recycler.push_back(lt_front);
long_term.pop_front();
@ -324,224 +404,37 @@ void FGReplay::update( double dt )
//stamp("point_finished");
}
static double weight( double data1, double data2, double ratio,
bool rotational = false ) {
if ( rotational ) {
// special handling of rotational data
double tmp = data2 - data1;
if ( tmp > SGD_PI ) {
tmp -= SGD_2PI;
} else if ( tmp < -SGD_PI ) {
tmp += SGD_2PI;
}
return data1 + tmp * ratio;
} else {
// normal "linear" data
return data1 + ( data2 - data1 ) * ratio;
}
}
/**
* given two FGReplayData elements and a time, interpolate between them
*/
static void update_fdm( FGReplayData frame ) {
FGNetFDM2Props( &frame.fdm, false );
FGNetCtrls2Props( &frame.ctrls, false, false );
}
/**
* given two FGReplayData elements and a time, interpolate between them
*/
static FGReplayData interpolate( double time, FGReplayData f1, FGReplayData f2 )
FGReplayData*
FGReplay::record(double time)
{
FGReplayData result = f1;
FGReplayData* r = NULL;
FGNetFDM fdm1 = f1.fdm;
FGNetFDM fdm2 = f2.fdm;
FGNetCtrls ctrls1 = f1.ctrls;
FGNetCtrls ctrls2 = f2.ctrls;
double ratio = (time - f1.sim_time) / (f2.sim_time - f1.sim_time);
// Interpolate FDM data
// Positions
result.fdm.longitude = weight( fdm1.longitude, fdm2.longitude, ratio );
result.fdm.latitude = weight( fdm1.latitude, fdm2.latitude, ratio );
result.fdm.altitude = weight( fdm1.altitude, fdm2.altitude, ratio );
result.fdm.agl = weight( fdm1.agl, fdm2.agl, ratio );
result.fdm.phi = weight( fdm1.phi, fdm2.phi, ratio, true );
result.fdm.theta = weight( fdm1.theta, fdm2.theta, ratio, true );
result.fdm.psi = weight( fdm1.psi, fdm2.psi, ratio, true );
// Velocities
result.fdm.phidot = weight( fdm1.phidot, fdm2.phidot, ratio, true );
result.fdm.thetadot = weight( fdm1.thetadot, fdm2.thetadot, ratio, true );
result.fdm.psidot = weight( fdm1.psidot, fdm2.psidot, ratio, true );
result.fdm.vcas = weight( fdm1.vcas, fdm2.vcas, ratio );
result.fdm.climb_rate = weight( fdm1.climb_rate, fdm2.climb_rate, ratio );
result.fdm.v_north = weight( fdm1.v_north, fdm2.v_north, ratio );
result.fdm.v_east = weight( fdm1.v_east, fdm2.v_east, ratio );
result.fdm.v_down = weight( fdm1.v_down, fdm2.v_down, ratio );
result.fdm.v_wind_body_north
= weight( fdm1.v_wind_body_north, fdm2.v_wind_body_north, ratio );
result.fdm.v_wind_body_east
= weight( fdm1.v_wind_body_east, fdm2.v_wind_body_east, ratio );
result.fdm.v_wind_body_down
= weight( fdm1.v_wind_body_down, fdm2.v_wind_body_down, ratio );
// Stall
result.fdm.stall_warning
= weight( fdm1.stall_warning, fdm2.stall_warning, ratio );
// Accelerations
result.fdm.A_X_pilot = weight( fdm1.A_X_pilot, fdm2.A_X_pilot, ratio );
result.fdm.A_Y_pilot = weight( fdm1.A_Y_pilot, fdm2.A_Y_pilot, ratio );
result.fdm.A_Z_pilot = weight( fdm1.A_Z_pilot, fdm2.A_Z_pilot, ratio );
unsigned int i;
// Engine status
for ( i = 0; i < fdm1.num_engines; ++i ) {
result.fdm.eng_state[i] = fdm1.eng_state[i];
result.fdm.rpm[i] = weight( fdm1.rpm[i], fdm2.rpm[i], ratio );
result.fdm.fuel_flow[i]
= weight( fdm1.fuel_flow[i], fdm2.fuel_flow[i], ratio );
result.fdm.fuel_px[i]
= weight( fdm1.fuel_px[i], fdm2.fuel_px[i], ratio );
result.fdm.egt[i] = weight( fdm1.egt[i], fdm2.egt[i], ratio );
result.fdm.cht[i] = weight( fdm1.cht[i], fdm2.cht[i], ratio );
result.fdm.mp_osi[i] = weight( fdm1.mp_osi[i], fdm2.mp_osi[i], ratio );
result.fdm.tit[i] = weight( fdm1.tit[i], fdm2.tit[i], ratio );
result.fdm.oil_temp[i]
= weight( fdm1.oil_temp[i], fdm2.oil_temp[i], ratio );
result.fdm.oil_px[i] = weight( fdm1.oil_px[i], fdm2.oil_px[i], ratio );
if (recycler.size())
{
r = recycler.front();
recycler.pop_front();
}
// Consumables
for ( i = 0; i < fdm1.num_tanks; ++i ) {
result.fdm.fuel_quantity[i]
= weight( fdm1.fuel_quantity[i], fdm2.fuel_quantity[i], ratio );
}
r = m_pRecorder->capture(time, r);
// Gear status
for ( i = 0; i < fdm1.num_wheels; ++i ) {
result.fdm.wow[i] = (int)(weight( fdm1.wow[i], fdm2.wow[i], ratio ));
result.fdm.gear_pos[i]
= weight( fdm1.gear_pos[i], fdm2.gear_pos[i], ratio );
result.fdm.gear_steer[i]
= weight( fdm1.gear_steer[i], fdm2.gear_steer[i], ratio );
result.fdm.gear_compression[i]
= weight( fdm1.gear_compression[i], fdm2.gear_compression[i],
ratio );
}
// Environment
result.fdm.cur_time = fdm1.cur_time;
result.fdm.warp = fdm1.warp;
result.fdm.visibility = weight( fdm1.visibility, fdm2.visibility, ratio );
// Control surface positions (normalized values)
result.fdm.elevator = weight( fdm1.elevator, fdm2.elevator, ratio );
result.fdm.left_flap = weight( fdm1.left_flap, fdm2.left_flap, ratio );
result.fdm.right_flap = weight( fdm1.right_flap, fdm2.right_flap, ratio );
result.fdm.left_aileron
= weight( fdm1.left_aileron, fdm2.left_aileron, ratio );
result.fdm.right_aileron
= weight( fdm1.right_aileron, fdm2.right_aileron, ratio );
result.fdm.rudder = weight( fdm1.rudder, fdm2.rudder, ratio );
result.fdm.speedbrake = weight( fdm1.speedbrake, fdm2.speedbrake, ratio );
result.fdm.spoilers = weight( fdm1.spoilers, fdm2.spoilers, ratio );
// Interpolate Control input data
// Aero controls
result.ctrls.aileron = weight( ctrls1.aileron, ctrls2.aileron, ratio );
result.ctrls.elevator = weight( ctrls1.elevator, ctrls2.elevator, ratio );
result.ctrls.rudder = weight( ctrls1.rudder, ctrls2.rudder, ratio );
result.ctrls.aileron_trim
= weight( ctrls1.aileron_trim, ctrls2.aileron_trim, ratio );
result.ctrls.elevator_trim
= weight( ctrls1.elevator_trim, ctrls2.elevator_trim, ratio );
result.ctrls.rudder_trim
= weight( ctrls1.rudder_trim, ctrls2.rudder_trim, ratio );
result.ctrls.flaps = weight( ctrls1.flaps, ctrls2.flaps, ratio );
result.ctrls.flaps_power = ctrls1.flaps_power;
result.ctrls.flap_motor_ok = ctrls1.flap_motor_ok;
// Engine controls
for ( i = 0; i < ctrls1.num_engines; ++i ) {
result.ctrls.master_bat[i] = ctrls1.master_bat[i];
result.ctrls.master_alt[i] = ctrls1.master_alt[i];
result.ctrls.magnetos[i] = ctrls1.magnetos[i];
result.ctrls.starter_power[i] = ctrls1.starter_power[i];
result.ctrls.throttle[i]
= weight( ctrls1.throttle[i], ctrls2.throttle[i], ratio );
result.ctrls.mixture[i]
= weight( ctrls1.mixture[i], ctrls2.mixture[i], ratio );
result.ctrls.fuel_pump_power[i] = ctrls1.fuel_pump_power[i];
result.ctrls.prop_advance[i]
= weight( ctrls1.prop_advance[i], ctrls2.prop_advance[i], ratio );
result.ctrls.engine_ok[i] = ctrls1.engine_ok[i];
result.ctrls.mag_left_ok[i] = ctrls1.mag_left_ok[i];
result.ctrls.mag_right_ok[i] = ctrls1.mag_right_ok[i];
result.ctrls.spark_plugs_ok[i] = ctrls1.spark_plugs_ok[i];
result.ctrls.oil_press_status[i] = ctrls1.oil_press_status[i];
result.ctrls.fuel_pump_ok[i] = ctrls1.fuel_pump_ok[i];
}
// Fuel management
for ( i = 0; i < ctrls1.num_tanks; ++i ) {
result.ctrls.fuel_selector[i] = ctrls1.fuel_selector[i];
}
// Brake controls
result.ctrls.brake_left
= weight( ctrls1.brake_left, ctrls2.brake_left, ratio );
result.ctrls.brake_right
= weight( ctrls1.brake_right, ctrls2.brake_right, ratio );
result.ctrls.brake_parking
= weight( ctrls1.brake_parking, ctrls2.brake_parking, ratio );
// Landing Gear
result.ctrls.gear_handle = ctrls1.gear_handle;
// Switches
result.ctrls.turbulence_norm = ctrls1.turbulence_norm;
// wind and turbulance
result.ctrls.wind_speed_kt
= weight( ctrls1.wind_speed_kt, ctrls2.wind_speed_kt, ratio );
result.ctrls.wind_dir_deg
= weight( ctrls1.wind_dir_deg, ctrls2.wind_dir_deg, ratio );
result.ctrls.turbulence_norm
= weight( ctrls1.turbulence_norm, ctrls2.turbulence_norm, ratio );
// other information about environment
result.ctrls.hground = weight( ctrls1.hground, ctrls2.hground, ratio );
result.ctrls.magvar = weight( ctrls1.magvar, ctrls2.magvar, ratio );
// simulation control
result.ctrls.speedup = ctrls1.speedup;
result.ctrls.freeze = ctrls1.freeze;
return result;
return r;
}
/**
* interpolate a specific time from a specific list
*/
static void interpolate( double time, const replay_list_type &list ) {
void
FGReplay::interpolate( double time, const replay_list_type &list)
{
// sanity checking
if ( list.size() == 0 ) {
if ( list.size() == 0 )
{
// handle empty list
return;
} else if ( list.size() == 1 ) {
} else if ( list.size() == 1 )
{
// handle list size == 1
update_fdm( (*list[0]) );
replay(time, list[0]);
return;
}
@ -549,9 +442,9 @@ static void interpolate( double time, const replay_list_type &list ) {
unsigned int first = 0;
unsigned int mid = ( last + first ) / 2;
bool done = false;
while ( !done ) {
while ( !done )
{
// cout << " " << first << " <=> " << last << endl;
if ( last == first ) {
done = true;
@ -568,19 +461,17 @@ static void interpolate( double time, const replay_list_type &list ) {
}
}
FGReplayData result = interpolate( time, (*list[mid]), (*list[mid+1]) );
update_fdm( result );
replay(time, list[mid+1], list[mid]);
}
/**
* Replay a saved frame based on time, interpolate from the two
* nearest saved frames.
* Returns true when replay sequence has finished, false otherwise.
*/
bool FGReplay::replay( double time ) {
bool
FGReplay::replay( double time ) {
// cout << "replay: " << time << " ";
// find the two frames to interpolate between
double t1, t2;
@ -590,7 +481,7 @@ bool FGReplay::replay( double time ) {
t2 = short_term.front()->sim_time;
if ( time > t1 ) {
// replay the most recent frame
update_fdm( (*short_term.back()) );
replay( time, short_term.back() );
// replay is finished now
return true;
// cout << "first frame" << endl;
@ -600,11 +491,9 @@ bool FGReplay::replay( double time ) {
} else if ( medium_term.size() > 0 ) {
t1 = short_term.front()->sim_time;
t2 = medium_term.back()->sim_time;
if ( time <= t1 && time >= t2 ) {
FGReplayData result = interpolate( time,
(*medium_term.back()),
(*short_term.front()) );
update_fdm( result );
if ( time <= t1 && time >= t2 )
{
replay(time, medium_term.back(), short_term.front());
// cout << "from short/medium term" << endl;
} else {
t1 = medium_term.back()->sim_time;
@ -615,11 +504,9 @@ bool FGReplay::replay( double time ) {
} else if ( long_term.size() > 0 ) {
t1 = medium_term.front()->sim_time;
t2 = long_term.back()->sim_time;
if ( time <= t1 && time >= t2 ) {
FGReplayData result = interpolate( time,
(*long_term.back()),
(*medium_term.front()));
update_fdm( result );
if ( time <= t1 && time >= t2 )
{
replay(time, long_term.back(), medium_term.front());
// cout << "from medium/long term" << endl;
} else {
t1 = long_term.back()->sim_time;
@ -629,19 +516,19 @@ bool FGReplay::replay( double time ) {
// cout << "from long term" << endl;
} else {
// replay the oldest long term frame
update_fdm( (*long_term.front()) );
replay(time, long_term.front());
// cout << "oldest long term frame" << endl;
}
}
} else {
// replay the oldest medium term frame
update_fdm( (*medium_term.front()) );
replay(time, medium_term.front());
// cout << "oldest medium term frame" << endl;
}
}
} else {
// replay the oldest short term frame
update_fdm( (*short_term.front()) );
replay(time, short_term.front());
// cout << "oldest short term frame" << endl;
}
} else {
@ -651,23 +538,41 @@ bool FGReplay::replay( double time ) {
return false;
}
/**
* given two FGReplayData elements and a time, interpolate between them
*/
void
FGReplay::replay(double time, FGReplayData* pCurrentFrame, FGReplayData* pOldFrame)
{
m_pRecorder->replay(time,pCurrentFrame,pOldFrame);
}
double FGReplay::get_start_time() {
if ( long_term.size() > 0 ) {
return (*long_term.front()).sim_time;
} else if ( medium_term.size() > 0 ) {
return (*medium_term.front()).sim_time;
} else if ( short_term.size() ) {
return (*short_term.front()).sim_time;
} else {
double
FGReplay::get_start_time()
{
if ( long_term.size() > 0 )
{
return long_term.front()->sim_time;
} else if ( medium_term.size() > 0 )
{
return medium_term.front()->sim_time;
} else if ( short_term.size() )
{
return short_term.front()->sim_time;
} else
{
return 0.0;
}
}
double FGReplay::get_end_time() {
if ( short_term.size() ) {
return (*short_term.back()).sim_time;
} else {
double
FGReplay::get_end_time()
{
if ( short_term.size() )
{
return short_term.back()->sim_time;
} else
{
return 0.0;
}
}

View file

@ -1,6 +1,6 @@
// replay.hxx - a system to record and replay FlightGear flights
//
// Written by Curtis Olson, started Juley 2003.
// Written by Curtis Olson, started July 2003.
//
// Copyright (C) 2003 Curtis L. Olson - http://www.flightgear.org/~curt
//
@ -36,20 +36,15 @@
#include <simgear/props/props.hxx>
#include <simgear/structure/subsystem_mgr.hxx>
#include <Network/net_ctrls.hxx>
#include <Network/net_fdm.hxx>
using std::deque;
class FGFlightRecorder;
class FGReplayData {
public:
typedef struct {
double sim_time;
FGNetFDM fdm;
FGNetCtrls ctrls;
};
char raw_data;
/* more data here, hidden to the outside world */
} FGReplayData;
typedef deque < FGReplayData *> replay_list_type;
@ -73,21 +68,17 @@ public:
virtual void bind();
virtual void unbind();
virtual void update( double dt );
bool start();
private:
void clear();
FGReplayData* record(double time);
void interpolate(double time, const replay_list_type &list);
void replay(double time, FGReplayData* pCurrentFrame, FGReplayData* pOldFrame=NULL);
bool replay( double time );
double get_start_time();
double get_end_time();
private:
void clear();
static const double st_list_time; // 60 secs of high res data
static const double mt_list_time; // 10 mins of 1 fps data
static const double lt_list_time; // 1 hr of 10 spf data
// short term sample rate is as every frame
static const double mt_dt; // medium term sample rate (sec)
static const double lt_dt; // long term sample rate (sec)
double sim_time;
double last_mt_time;
@ -101,7 +92,18 @@ private:
SGPropertyNode_ptr disable_replay;
SGPropertyNode_ptr replay_master;
SGPropertyNode_ptr replay_time;
SGPropertyNode_ptr replay_time_str;
SGPropertyNode_ptr replay_looped;
SGPropertyNode_ptr speed_up;
double m_high_res_time; // default: 60 secs of high res data
double m_medium_res_time; // default: 10 mins of 1 fps data
double m_low_res_time; // default: 1 hr of 10 spf data
// short term sample rate is as every frame
double m_medium_sample_rate; // medium term sample rate (sec)
double m_long_sample_rate; // long term sample rate (sec)
FGFlightRecorder* m_pRecorder;
};

View file

@ -305,7 +305,8 @@ FGInterface::bind ()
// Ground speed knots
fgTie("/velocities/groundspeed-kt", this,
&FGInterface::get_V_ground_speed_kt); // read-only
&FGInterface::get_V_ground_speed_kt,
&FGInterface::set_V_ground_speed_kt); // read-only
// Calibrated airspeed
fgTie("/velocities/airspeed-kt", this,

View file

@ -552,6 +552,7 @@ public:
inline double get_V_ground_speed() const { return v_ground_speed; }
inline double get_V_ground_speed_kt() const { return v_ground_speed * SG_FEET_TO_METER * 3600 * SG_METER_TO_NM; }
inline void set_V_ground_speed_kt(double ground_speed) { v_ground_speed = ground_speed / ( SG_FEET_TO_METER * 3600 * SG_METER_TO_NM); }
inline double get_V_equiv_kts() const { return v_equiv_kts; }

View file

@ -318,14 +318,32 @@ do_resume (const SGPropertyNode * arg)
#endif
/**
* Built-in command: replay the FDR buffer
*/
static bool
do_replay (const SGPropertyNode * arg)
{
FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
return r->start();
}
/**
* Built-in command: pause/unpause the sim
*/
static bool
do_pause (const SGPropertyNode * arg)
{
bool paused = fgGetBool("/sim/freeze/master",true) || fgGetBool("/sim/freeze/clock",true);
fgSetBool("/sim/freeze/master",!paused);
fgSetBool("/sim/freeze/clock",!paused);
if (fgGetBool("/sim/freeze/replay-state",false))
fgSetBool("/sim/replay/disable",true);
if (paused && (fgGetInt("/sim/freeze/replay-state",0)>0))
{
do_replay(NULL);
}
else
{
fgSetBool("/sim/freeze/master",!paused);
fgSetBool("/sim/freeze/clock",!paused);
}
return true;
}
@ -1170,24 +1188,6 @@ do_log_level (const SGPropertyNode * arg)
return true;
}
/**
* Built-in command: replay the FDR buffer
*/
static bool
do_replay (const SGPropertyNode * arg)
{
// freeze the fdm, resume from sim pause
fgSetInt( "/sim/freeze/replay-state", 1 );
fgSetBool("/sim/freeze/master", 0 );
fgSetBool("/sim/freeze/clock", 0 );
fgSetDouble( "/sim/replay/time", -1 );
// cout << "start = " << r->get_start_time()
// << " end = " << r->get_end_time() << endl;
return true;
}
/*
static bool
do_decrease_visibility (const SGPropertyNode * arg)