2003-03-09 03:20:34 +00:00
|
|
|
// dme.cxx - distance-measuring equipment.
|
|
|
|
// Written by David Megginson, started 2003.
|
|
|
|
//
|
|
|
|
// This file is in the Public Domain and comes with no warranty.
|
|
|
|
|
2006-02-22 21:04:47 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2003-03-09 03:20:34 +00:00
|
|
|
#include <simgear/compiler.h>
|
2011-09-15 21:28:30 +02:00
|
|
|
#include <simgear/sg_inlines.h>
|
2003-03-09 03:20:34 +00:00
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
|
|
|
#include <simgear/math/sg_random.h>
|
|
|
|
|
|
|
|
#include <Main/fg_props.hxx>
|
|
|
|
#include <Navaids/navlist.hxx>
|
2011-09-15 21:28:30 +02:00
|
|
|
#include <Sound/audioident.hxx>
|
2003-03-09 03:20:34 +00:00
|
|
|
|
|
|
|
#include "dme.hxx"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjust the range.
|
|
|
|
*
|
|
|
|
* Start by calculating the radar horizon based on the elevation
|
|
|
|
* difference, then clamp to the maximum, then add a fudge for
|
|
|
|
* borderline reception.
|
|
|
|
*/
|
|
|
|
static double
|
|
|
|
adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
|
|
|
|
double max_range_nm)
|
|
|
|
{
|
|
|
|
double delta_elevation_ft =
|
|
|
|
fabs(aircraft_altitude_ft - transmitter_elevation_ft);
|
|
|
|
double range_nm = 1.23 * sqrt(delta_elevation_ft);
|
|
|
|
if (range_nm > max_range_nm)
|
|
|
|
range_nm = max_range_nm;
|
2003-10-01 21:11:34 +00:00
|
|
|
else if (range_nm < 20.0)
|
|
|
|
range_nm = 20.0;
|
2003-03-09 03:20:34 +00:00
|
|
|
double rand = sg_random();
|
|
|
|
return range_nm + (range_nm * rand * rand);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-16 12:37:39 +00:00
|
|
|
DME::DME ( SGPropertyNode *node )
|
|
|
|
: _last_distance_nm(0),
|
|
|
|
_last_frequency_mhz(-1),
|
|
|
|
_time_before_search_sec(0),
|
2011-09-15 21:28:30 +02:00
|
|
|
_navrecord(NULL),
|
2006-12-06 22:11:43 +00:00
|
|
|
_name(node->getStringValue("name", "dme")),
|
2011-09-15 21:28:30 +02:00
|
|
|
_num(node->getIntValue("number", 0)),
|
|
|
|
_audioIdent(NULL)
|
2003-03-09 03:20:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DME::~DME ()
|
|
|
|
{
|
2011-09-15 21:28:30 +02:00
|
|
|
delete _audioIdent;
|
2003-03-09 03:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DME::init ()
|
|
|
|
{
|
2004-10-16 12:37:39 +00:00
|
|
|
string branch;
|
2006-12-06 22:11:43 +00:00
|
|
|
branch = "/instrumentation/" + _name;
|
2004-10-16 12:37:39 +00:00
|
|
|
|
2006-12-06 22:11:43 +00:00
|
|
|
SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
|
2004-10-16 12:37:39 +00:00
|
|
|
|
|
|
|
_serviceable_node = node->getChild("serviceable", 0, true);
|
2003-03-09 03:20:34 +00:00
|
|
|
_electrical_node = fgGetNode("/systems/electrical/outputs/dme", true);
|
2004-10-16 12:37:39 +00:00
|
|
|
SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
|
|
|
|
_source_node = fnode->getChild("source", 0, true);
|
|
|
|
_frequency_node = fnode->getChild("selected-mhz", 0, true);
|
|
|
|
_in_range_node = node->getChild("in-range", 0, true);
|
|
|
|
_distance_node = node->getChild("indicated-distance-nm", 0, true);
|
2004-11-06 16:22:17 +00:00
|
|
|
_speed_node = node->getChild("indicated-ground-speed-kt", 0, true);
|
2004-10-16 12:37:39 +00:00
|
|
|
_time_node = node->getChild("indicated-time-min", 0, true);
|
2011-09-15 21:28:30 +02:00
|
|
|
|
|
|
|
double d = node->getDoubleValue( "volume", 1.0 );
|
|
|
|
_volume_node = node->getChild("volume", 0, true);
|
|
|
|
_volume_node->setDoubleValue( d );
|
|
|
|
|
|
|
|
bool b = node->getBoolValue( "ident", false );
|
|
|
|
_ident_btn_node = node->getChild("ident", 0, true);
|
|
|
|
_ident_btn_node->setBoolValue( b );
|
|
|
|
|
|
|
|
std::ostringstream temp;
|
|
|
|
temp << _name << "-ident-" << _num;
|
|
|
|
if( NULL == _audioIdent )
|
|
|
|
_audioIdent = new DMEAudioIdent( temp.str() );
|
|
|
|
_audioIdent->init();
|
2012-09-17 00:12:29 +02:00
|
|
|
|
|
|
|
reinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DME::reinit ()
|
|
|
|
{
|
|
|
|
_time_before_search_sec = 0;
|
2003-03-09 03:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DME::update (double delta_time_sec)
|
|
|
|
{
|
2011-09-15 21:28:30 +02:00
|
|
|
if( delta_time_sec < SGLimitsd::min() )
|
|
|
|
return; //paused
|
|
|
|
|
2003-03-09 03:20:34 +00:00
|
|
|
// Figure out the source
|
|
|
|
const char * source = _source_node->getStringValue();
|
|
|
|
if (source[0] == '\0') {
|
2004-10-16 12:37:39 +00:00
|
|
|
string branch;
|
2006-12-06 22:11:43 +00:00
|
|
|
branch = "/instrumentation/" + _name + "/frequencies/selected-mhz";
|
2004-10-18 13:29:16 +00:00
|
|
|
_source_node->setStringValue(branch.c_str());
|
|
|
|
source = _source_node->getStringValue();
|
2003-03-09 03:20:34 +00:00
|
|
|
}
|
|
|
|
// Get the frequency
|
2004-10-18 13:29:16 +00:00
|
|
|
|
2003-03-09 03:20:34 +00:00
|
|
|
double frequency_mhz = fgGetDouble(source, 108.0);
|
|
|
|
if (frequency_mhz != _last_frequency_mhz) {
|
|
|
|
_time_before_search_sec = 0;
|
|
|
|
_last_frequency_mhz = frequency_mhz;
|
|
|
|
}
|
2010-03-09 11:11:07 +00:00
|
|
|
_frequency_node->setDoubleValue(frequency_mhz);
|
2003-03-09 03:20:34 +00:00
|
|
|
|
|
|
|
// Get the aircraft position
|
2011-09-15 21:28:30 +02:00
|
|
|
// On timeout, scan again
|
2003-03-09 03:20:34 +00:00
|
|
|
_time_before_search_sec -= delta_time_sec;
|
2011-09-15 21:28:30 +02:00
|
|
|
if (_time_before_search_sec < 0) {
|
|
|
|
_time_before_search_sec = 1.0;
|
2003-03-09 03:20:34 +00:00
|
|
|
|
2011-10-09 20:21:02 +02:00
|
|
|
if( fgGetBool( "/sim/realism/dme-fallback-to-loc", true ) ) {
|
|
|
|
if( NULL == (_navrecord = globals->get_loclist()->findByFreq( frequency_mhz,
|
|
|
|
globals->get_aircraft_position())) ) {
|
|
|
|
|
|
|
|
_navrecord = globals->get_dmelist()->findByFreq( frequency_mhz,
|
|
|
|
globals->get_aircraft_position());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_navrecord = globals->get_dmelist()->findByFreq( frequency_mhz,
|
|
|
|
globals->get_aircraft_position());
|
|
|
|
}
|
2011-09-15 21:28:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it's off, don't bother.
|
2003-03-09 03:20:34 +00:00
|
|
|
if (!_serviceable_node->getBoolValue() ||
|
|
|
|
!_electrical_node->getBoolValue() ||
|
2011-09-15 21:28:30 +02:00
|
|
|
NULL == _navrecord ) {
|
2003-03-09 03:20:34 +00:00
|
|
|
_last_distance_nm = 0;
|
|
|
|
_in_range_node->setBoolValue(false);
|
|
|
|
_distance_node->setDoubleValue(0);
|
|
|
|
_speed_node->setDoubleValue(0);
|
|
|
|
_time_node->setDoubleValue(0);
|
2011-09-15 21:28:30 +02:00
|
|
|
_audioIdent->setIdent("", 0.0 );
|
2003-03-09 03:20:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-15 21:28:30 +02:00
|
|
|
// Calculate the distance to the transmitter
|
|
|
|
SGVec3d location = SGVec3d::fromGeod(globals->get_aircraft_position());
|
2006-06-15 19:16:21 +00:00
|
|
|
|
2011-09-15 21:28:30 +02:00
|
|
|
double distance_nm = dist(_navrecord->cart(), location) * SG_METER_TO_NM;
|
2004-10-18 13:29:16 +00:00
|
|
|
|
2011-09-15 21:28:30 +02:00
|
|
|
double range_nm = adjust_range(_navrecord->get_elev_ft(),
|
|
|
|
globals->get_aircraft_position().getElevationFt(),
|
|
|
|
_navrecord->get_range());
|
2004-10-18 13:29:16 +00:00
|
|
|
|
2003-03-09 03:20:34 +00:00
|
|
|
if (distance_nm <= range_nm) {
|
2011-09-15 21:28:30 +02:00
|
|
|
double volume = _volume_node->getDoubleValue();
|
|
|
|
if( false == _ident_btn_node->getBoolValue() )
|
|
|
|
volume = 0.0;
|
|
|
|
|
|
|
|
_audioIdent->setIdent(_navrecord->ident(), volume );
|
|
|
|
|
2003-03-09 03:20:34 +00:00
|
|
|
double speed_kt = (fabs(distance_nm - _last_distance_nm) *
|
|
|
|
((1 / delta_time_sec) * 3600.0));
|
|
|
|
_last_distance_nm = distance_nm;
|
|
|
|
|
|
|
|
_in_range_node->setBoolValue(true);
|
2011-09-15 21:28:30 +02:00
|
|
|
double tmp_dist = distance_nm - _navrecord->get_multiuse();
|
2004-06-09 20:21:18 +00:00
|
|
|
if ( tmp_dist < 0.0 ) {
|
|
|
|
tmp_dist = 0.0;
|
|
|
|
}
|
|
|
|
_distance_node->setDoubleValue( tmp_dist );
|
2003-03-09 03:20:34 +00:00
|
|
|
_speed_node->setDoubleValue(speed_kt);
|
2009-09-04 17:01:53 +00:00
|
|
|
if (SGLimitsd::min() < fabs(speed_kt))
|
|
|
|
_time_node->setDoubleValue(distance_nm/speed_kt*60.0);
|
2003-03-09 03:20:34 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
_last_distance_nm = 0;
|
|
|
|
_in_range_node->setBoolValue(false);
|
|
|
|
_distance_node->setDoubleValue(0);
|
|
|
|
_speed_node->setDoubleValue(0);
|
|
|
|
_time_node->setDoubleValue(0);
|
2011-09-15 21:28:30 +02:00
|
|
|
_audioIdent->setIdent("", 0.0 );
|
2003-03-09 03:20:34 +00:00
|
|
|
}
|
2008-12-25 23:11:43 +00:00
|
|
|
|
2011-09-15 21:28:30 +02:00
|
|
|
_audioIdent->update( delta_time_sec );
|
2003-03-09 03:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// end of dme.cxx
|