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.
|
|
|
|
|
|
|
|
#include <simgear/compiler.h>
|
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
|
|
|
#include <simgear/math/sg_random.h>
|
|
|
|
|
|
|
|
#include <Main/fg_props.hxx>
|
|
|
|
#include <Navaids/navlist.hxx>
|
|
|
|
|
|
|
|
#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),
|
|
|
|
_transmitter_valid(false),
|
|
|
|
_transmitter_elevation_ft(0),
|
|
|
|
_transmitter_range_nm(0),
|
|
|
|
_transmitter_bias(0.0),
|
|
|
|
name("dme"),
|
|
|
|
num(0)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for ( i = 0; i < node->nChildren(); ++i ) {
|
|
|
|
SGPropertyNode *child = node->getChild(i);
|
|
|
|
string cname = child->getName();
|
|
|
|
string cval = child->getStringValue();
|
|
|
|
if ( cname == "name" ) {
|
|
|
|
name = cval;
|
|
|
|
} else if ( cname == "number" ) {
|
|
|
|
num = child->getIntValue();
|
|
|
|
} else {
|
|
|
|
SG_LOG( SG_AUTOPILOT, SG_WARN, "Error in dme config logic" );
|
|
|
|
if ( name.length() ) {
|
|
|
|
SG_LOG( SG_AUTOPILOT, SG_WARN, "Section = " << name );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-09 03:20:34 +00:00
|
|
|
DME::DME ()
|
|
|
|
: _last_distance_nm(0),
|
|
|
|
_last_frequency_mhz(-1),
|
|
|
|
_time_before_search_sec(0),
|
|
|
|
_transmitter_valid(false),
|
|
|
|
_transmitter_elevation_ft(0),
|
2004-05-28 05:24:54 +00:00
|
|
|
_transmitter_range_nm(0),
|
|
|
|
_transmitter_bias(0.0)
|
2003-03-09 03:20:34 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DME::~DME ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DME::init ()
|
|
|
|
{
|
2004-10-16 12:37:39 +00:00
|
|
|
string branch;
|
|
|
|
branch = "/instrumentation/" + name;
|
|
|
|
|
|
|
|
SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
|
|
|
|
|
2003-03-09 03:20:34 +00:00
|
|
|
_longitude_node = fgGetNode("/position/longitude-deg", true);
|
|
|
|
_latitude_node = fgGetNode("/position/latitude-deg", true);
|
2003-03-10 14:09:26 +00:00
|
|
|
_altitude_node = fgGetNode("/position/altitude-ft", 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);
|
|
|
|
_speed_node = node->getChild("indicated-ground-speed", 0, true);
|
|
|
|
_time_node = node->getChild("indicated-time-min", 0, true);
|
2003-03-09 03:20:34 +00:00
|
|
|
|
2004-10-16 12:37:39 +00:00
|
|
|
_serviceable_node->setBoolValue(true);
|
2003-03-09 03:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DME::update (double delta_time_sec)
|
|
|
|
{
|
|
|
|
// Figure out the source
|
|
|
|
const char * source = _source_node->getStringValue();
|
|
|
|
if (source[0] == '\0') {
|
2004-10-16 12:37:39 +00:00
|
|
|
string branch;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the aircraft position
|
|
|
|
double longitude_rad =
|
|
|
|
_longitude_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
|
|
|
|
double latitude_rad =
|
|
|
|
_latitude_node->getDoubleValue() * SGD_DEGREES_TO_RADIANS;
|
|
|
|
double altitude_m =
|
2003-03-10 14:09:26 +00:00
|
|
|
_altitude_node->getDoubleValue() * SG_FEET_TO_METER;
|
2003-03-09 03:20:34 +00:00
|
|
|
|
|
|
|
// On timeout, scan again
|
|
|
|
_time_before_search_sec -= delta_time_sec;
|
|
|
|
if (_time_before_search_sec < 0)
|
|
|
|
search(frequency_mhz, longitude_rad,
|
|
|
|
latitude_rad, altitude_m);
|
|
|
|
|
|
|
|
// If it's off, don't bother.
|
|
|
|
if (!_serviceable_node->getBoolValue() ||
|
|
|
|
!_electrical_node->getBoolValue() ||
|
|
|
|
!_transmitter_valid) {
|
|
|
|
_last_distance_nm = 0;
|
|
|
|
_in_range_node->setBoolValue(false);
|
|
|
|
_distance_node->setDoubleValue(0);
|
|
|
|
_speed_node->setDoubleValue(0);
|
|
|
|
_time_node->setDoubleValue(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the distance to the transmitter
|
|
|
|
Point3D location =
|
|
|
|
sgGeodToCart(Point3D(longitude_rad, latitude_rad, altitude_m));
|
|
|
|
double distance_nm = _transmitter.distance3D(location) * SG_METER_TO_NM;
|
2004-10-18 13:29:16 +00:00
|
|
|
|
2003-03-09 03:20:34 +00:00
|
|
|
double range_nm = adjust_range(_transmitter_elevation_ft,
|
|
|
|
altitude_m * SG_METER_TO_FEET,
|
|
|
|
_transmitter_range_nm);
|
2004-10-18 13:29:16 +00:00
|
|
|
|
2003-03-09 03:20:34 +00:00
|
|
|
if (distance_nm <= range_nm) {
|
|
|
|
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);
|
2004-06-09 20:21:18 +00:00
|
|
|
double tmp_dist = distance_nm - _transmitter_bias;
|
|
|
|
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);
|
|
|
|
_time_node->setDoubleValue(distance_nm/speed_kt*60.0);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
_last_distance_nm = 0;
|
|
|
|
_in_range_node->setBoolValue(false);
|
|
|
|
_distance_node->setDoubleValue(0);
|
|
|
|
_speed_node->setDoubleValue(0);
|
|
|
|
_time_node->setDoubleValue(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DME::search (double frequency_mhz, double longitude_rad,
|
|
|
|
double latitude_rad, double altitude_m)
|
|
|
|
{
|
2004-05-28 05:24:54 +00:00
|
|
|
// reset search time
|
2003-03-09 03:20:34 +00:00
|
|
|
_time_before_search_sec = 1.0;
|
|
|
|
|
2004-05-28 05:24:54 +00:00
|
|
|
// try the ILS list first
|
|
|
|
FGNavRecord *dme
|
|
|
|
= globals->get_dmelist()->findByFreq( frequency_mhz, longitude_rad,
|
|
|
|
latitude_rad, altitude_m);
|
2003-03-09 03:20:34 +00:00
|
|
|
|
2004-05-28 05:24:54 +00:00
|
|
|
_transmitter_valid = (dme != NULL);
|
2003-03-09 03:20:34 +00:00
|
|
|
|
2004-05-28 05:24:54 +00:00
|
|
|
if ( _transmitter_valid ) {
|
|
|
|
_transmitter = Point3D(dme->get_x(), dme->get_y(), dme->get_z());
|
|
|
|
_transmitter_elevation_ft = dme->get_elev_ft();
|
|
|
|
_transmitter_range_nm = dme->get_range();
|
|
|
|
_transmitter_bias = dme->get_multiuse();
|
|
|
|
}
|
2003-03-09 03:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// end of dme.cxx
|