2003-10-01 21:10:33 +00:00
|
|
|
// adf.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-10-01 21:10:33 +00:00
|
|
|
#include <simgear/compiler.h>
|
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
|
|
|
#include <simgear/math/sg_random.h>
|
2008-08-03 14:34:42 +00:00
|
|
|
#include <simgear/timing/sg_time.hxx>
|
2003-10-01 21:10:33 +00:00
|
|
|
|
|
|
|
#include <Main/fg_props.hxx>
|
|
|
|
#include <Main/util.hxx>
|
|
|
|
#include <Navaids/navlist.hxx>
|
|
|
|
|
|
|
|
#include "adf.hxx"
|
2011-09-13 18:06:31 +00:00
|
|
|
#include <Sound/morse.hxx>
|
2012-09-30 18:40:08 +00:00
|
|
|
#include <simgear/sound/sample_group.hxx>
|
2003-10-01 21:10:33 +00:00
|
|
|
|
2004-10-16 12:37:39 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
|
2012-09-26 16:02:11 +00:00
|
|
|
using std::string;
|
2003-10-01 21:10:33 +00:00
|
|
|
|
|
|
|
// Use a bigger number to be more responsive, or a smaller number
|
|
|
|
// to be more sluggish.
|
|
|
|
#define RESPONSIVENESS 0.5
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fiddle with the reception range a bit.
|
|
|
|
*
|
|
|
|
* TODO: better reception at night (??).
|
|
|
|
*/
|
|
|
|
static double
|
|
|
|
adjust_range (double transmitter_elevation_ft, double aircraft_altitude_ft,
|
|
|
|
double max_range_nm)
|
|
|
|
{
|
|
|
|
double delta_elevation_ft =
|
|
|
|
aircraft_altitude_ft - transmitter_elevation_ft;
|
|
|
|
double range_nm = max_range_nm;
|
|
|
|
|
|
|
|
// kludge slightly better reception at
|
|
|
|
// altitude
|
|
|
|
if (delta_elevation_ft < 0)
|
|
|
|
delta_elevation_ft = 200;
|
|
|
|
if (delta_elevation_ft <= 1000)
|
|
|
|
range_nm *= sqrt(delta_elevation_ft / 1000);
|
|
|
|
else if (delta_elevation_ft >= 5000)
|
|
|
|
range_nm *= sqrt(delta_elevation_ft / 5000);
|
|
|
|
if (range_nm >= max_range_nm * 3)
|
|
|
|
range_nm = max_range_nm * 3;
|
|
|
|
|
|
|
|
double rand = sg_random();
|
|
|
|
return range_nm + (range_nm * rand * rand);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-16 12:37:39 +00:00
|
|
|
ADF::ADF (SGPropertyNode *node )
|
|
|
|
:
|
2006-10-26 14:47:15 +00:00
|
|
|
_name(node->getStringValue("name", "adf")),
|
|
|
|
_num(node->getIntValue("number", 0)),
|
2004-10-16 12:37:39 +00:00
|
|
|
_time_before_search_sec(0),
|
|
|
|
_last_frequency_khz(-1),
|
|
|
|
_transmitter_valid(false),
|
2006-06-15 19:16:21 +00:00
|
|
|
_transmitter_pos(SGGeod::fromDeg(0, 0)),
|
|
|
|
_transmitter_cart(0, 0, 0),
|
2004-10-16 12:37:39 +00:00
|
|
|
_transmitter_range_nm(0),
|
|
|
|
_ident_count(0),
|
|
|
|
_last_ident_time(0),
|
2009-10-04 13:52:53 +00:00
|
|
|
_last_volume(-1),
|
|
|
|
_sgr(0)
|
2003-10-01 21:10:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ADF::~ADF ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ADF::init ()
|
|
|
|
{
|
2004-10-16 12:37:39 +00:00
|
|
|
string branch;
|
2006-10-26 14:47:15 +00:00
|
|
|
branch = "/instrumentation/" + _name;
|
|
|
|
SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
|
2012-07-01 14:31:20 +00:00
|
|
|
|
|
|
|
// instrument properties
|
|
|
|
_error_node = node->getChild("error-deg", 0, true);
|
|
|
|
_mode_node = node->getChild("mode", 0, true);
|
|
|
|
_volume_node = node->getChild("volume-norm", 0, true);
|
|
|
|
_in_range_node = node->getChild("in-range", 0, true);
|
|
|
|
_bearing_node = node->getChild("indicated-bearing-deg", 0, true);
|
|
|
|
_ident_node = node->getChild("ident", 0, true);
|
2004-10-16 12:37:39 +00:00
|
|
|
_ident_audible_node = node->getChild("ident-audible", 0, true);
|
2012-07-01 14:31:20 +00:00
|
|
|
_serviceable_node = node->getChild("serviceable", 0, true);
|
|
|
|
_power_btn_node = node->getChild("power-btn", 0, true);
|
2012-07-01 14:30:53 +00:00
|
|
|
_operable_node = node->getChild("operable", 0, true);
|
2009-11-30 10:19:17 +00:00
|
|
|
|
2012-07-01 14:31:20 +00:00
|
|
|
// frequency properties
|
|
|
|
SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
|
|
|
|
_frequency_node = fnode->getChild("selected-khz", 0, true);
|
|
|
|
|
|
|
|
// foreign simulator properties
|
|
|
|
_electrical_node = fgGetNode("/systems/electrical/outputs/adf", true);
|
|
|
|
_heading_node = fgGetNode("/orientation/heading-deg", true);
|
|
|
|
|
|
|
|
// backward compatibility check
|
2011-01-16 20:20:55 +00:00
|
|
|
if (_power_btn_node->getType() == simgear::props::NONE)
|
|
|
|
_power_btn_node->setBoolValue(true); // front end didn't implement a power button
|
|
|
|
|
2012-07-01 14:31:20 +00:00
|
|
|
// sound support (audible ident code)
|
2015-12-31 05:11:54 +00:00
|
|
|
SGSoundMgr *smgr = globals->get_subsystem<SGSoundMgr>();
|
2009-10-04 13:52:53 +00:00
|
|
|
_sgr = smgr->find("avionics", true);
|
2009-10-07 12:55:08 +00:00
|
|
|
_sgr->tie_to_listener();
|
2009-10-04 13:52:53 +00:00
|
|
|
|
2004-10-16 12:37:39 +00:00
|
|
|
std::ostringstream temp;
|
2006-10-26 14:47:15 +00:00
|
|
|
temp << _name << _num;
|
|
|
|
_adf_ident = temp.str();
|
2003-10-01 21:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ADF::update (double delta_time_sec)
|
|
|
|
{
|
|
|
|
// If it's off, don't waste any time.
|
2009-11-30 10:19:17 +00:00
|
|
|
if (_electrical_node->getDoubleValue() < 8.0
|
|
|
|
|| !_serviceable_node->getBoolValue()
|
|
|
|
|| !_power_btn_node->getBoolValue() ) {
|
2011-10-02 11:24:12 +00:00
|
|
|
_in_range_node->setBoolValue(false);
|
2012-07-01 14:30:53 +00:00
|
|
|
_operable_node->setBoolValue(false);
|
2003-10-01 21:10:33 +00:00
|
|
|
_ident_node->setStringValue("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-01 14:30:53 +00:00
|
|
|
_operable_node->setBoolValue(true);
|
2012-07-01 14:31:20 +00:00
|
|
|
|
2009-11-30 10:19:17 +00:00
|
|
|
string mode = _mode_node->getStringValue();
|
|
|
|
if (mode == "ant" || mode == "test") set_bearing(delta_time_sec, 90);
|
|
|
|
if (mode != "bfo" && mode != "adf") {
|
2011-10-02 11:24:12 +00:00
|
|
|
_in_range_node->setBoolValue(false);
|
2009-11-30 10:19:17 +00:00
|
|
|
_ident_node->setStringValue("");
|
|
|
|
return;
|
|
|
|
}
|
2003-10-01 21:10:33 +00:00
|
|
|
// Get the frequency
|
|
|
|
int frequency_khz = _frequency_node->getIntValue();
|
|
|
|
if (frequency_khz != _last_frequency_khz) {
|
|
|
|
_time_before_search_sec = 0;
|
|
|
|
_last_frequency_khz = frequency_khz;
|
|
|
|
}
|
|
|
|
|
2012-08-27 23:26:36 +00:00
|
|
|
SGGeod acPos(globals->get_aircraft_position());
|
|
|
|
|
2003-10-01 21:10:33 +00:00
|
|
|
// On timeout, scan again
|
|
|
|
_time_before_search_sec -= delta_time_sec;
|
|
|
|
if (_time_before_search_sec < 0)
|
2012-08-27 23:26:36 +00:00
|
|
|
search(frequency_khz, acPos);
|
2003-10-01 21:10:33 +00:00
|
|
|
|
2009-11-30 10:19:17 +00:00
|
|
|
if (!_transmitter_valid) {
|
2011-10-02 11:24:12 +00:00
|
|
|
_in_range_node->setBoolValue(false);
|
2003-10-01 21:10:33 +00:00
|
|
|
_ident_node->setStringValue("");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the bearing to the transmitter
|
2012-09-25 09:06:30 +00:00
|
|
|
SGVec3d location = globals->get_aircraft_position_cart();
|
2006-06-15 19:16:21 +00:00
|
|
|
|
|
|
|
double distance_nm = dist(_transmitter_cart, location) * SG_METER_TO_NM;
|
|
|
|
double range_nm = adjust_range(_transmitter_pos.getElevationFt(),
|
2012-08-27 23:26:36 +00:00
|
|
|
acPos.getElevationFt(),
|
2003-10-01 21:10:33 +00:00
|
|
|
_transmitter_range_nm);
|
2009-10-04 13:52:53 +00:00
|
|
|
|
2003-10-01 21:10:33 +00:00
|
|
|
if (distance_nm <= range_nm) {
|
|
|
|
|
|
|
|
double bearing, az2, s;
|
|
|
|
double heading = _heading_node->getDoubleValue();
|
|
|
|
|
2012-08-27 23:26:36 +00:00
|
|
|
geo_inverse_wgs_84(acPos, _transmitter_pos,
|
2003-10-01 21:10:33 +00:00
|
|
|
&bearing, &az2, &s);
|
|
|
|
_in_range_node->setBoolValue(true);
|
2014-10-03 10:40:07 +00:00
|
|
|
_ident_node->setStringValue(_last_ident);
|
2003-10-01 21:10:33 +00:00
|
|
|
|
|
|
|
bearing -= heading;
|
2004-11-10 20:01:28 +00:00
|
|
|
if (bearing < 0)
|
|
|
|
bearing += 360;
|
2003-10-01 21:10:33 +00:00
|
|
|
set_bearing(delta_time_sec, bearing);
|
2004-08-19 11:54:57 +00:00
|
|
|
|
|
|
|
// adf ident sound
|
2009-10-04 13:52:53 +00:00
|
|
|
float volume;
|
2004-10-06 09:30:36 +00:00
|
|
|
if ( _ident_audible_node->getBoolValue() )
|
2009-10-04 13:52:53 +00:00
|
|
|
volume = _volume_node->getFloatValue();
|
2004-08-19 11:54:57 +00:00
|
|
|
else
|
|
|
|
volume = 0.0;
|
|
|
|
|
|
|
|
if ( volume != _last_volume ) {
|
|
|
|
_last_volume = volume;
|
|
|
|
|
|
|
|
SGSoundSample *sound;
|
2009-10-04 13:52:53 +00:00
|
|
|
sound = _sgr->find( _adf_ident );
|
2004-08-19 11:54:57 +00:00
|
|
|
if ( sound != NULL )
|
|
|
|
sound->set_volume( volume );
|
|
|
|
else
|
2011-12-11 12:55:56 +00:00
|
|
|
SG_LOG( SG_INSTR, SG_ALERT, "Can't find adf-ident sound" );
|
2004-08-19 11:54:57 +00:00
|
|
|
}
|
|
|
|
|
2004-10-06 09:30:36 +00:00
|
|
|
time_t cur_time = globals->get_time_params()->get_cur_time();
|
2004-08-19 11:54:57 +00:00
|
|
|
if ( _last_ident_time < cur_time - 30 ) {
|
|
|
|
_last_ident_time = cur_time;
|
|
|
|
_ident_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( _ident_count < 4 ) {
|
2009-10-04 13:52:53 +00:00
|
|
|
if ( !_sgr->is_playing(_adf_ident) && (volume > 0.05) ) {
|
|
|
|
_sgr->play_once( _adf_ident );
|
2004-08-19 11:54:57 +00:00
|
|
|
++_ident_count;
|
|
|
|
}
|
|
|
|
}
|
2003-10-01 21:10:33 +00:00
|
|
|
} else {
|
|
|
|
_in_range_node->setBoolValue(false);
|
|
|
|
_ident_node->setStringValue("");
|
2009-10-04 13:52:53 +00:00
|
|
|
_sgr->stop( _adf_ident );
|
2003-10-01 21:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-27 23:26:36 +00:00
|
|
|
ADF::search (double frequency_khz, const SGGeod& pos)
|
2003-10-01 21:10:33 +00:00
|
|
|
{
|
|
|
|
string ident = "";
|
|
|
|
// reset search time
|
|
|
|
_time_before_search_sec = 1.0;
|
|
|
|
|
2012-08-27 23:26:36 +00:00
|
|
|
FGNavList::TypeFilter filter(FGPositioned::NDB);
|
|
|
|
FGNavRecord *nav = FGNavList::findByFreq(frequency_khz, pos, &filter);
|
2003-10-01 21:10:33 +00:00
|
|
|
|
2004-05-28 05:24:54 +00:00
|
|
|
_transmitter_valid = (nav != NULL);
|
|
|
|
if ( _transmitter_valid ) {
|
2003-10-01 21:10:33 +00:00
|
|
|
ident = nav->get_trans_ident();
|
2004-05-28 05:24:54 +00:00
|
|
|
if ( ident != _last_ident ) {
|
2008-12-25 23:11:43 +00:00
|
|
|
_transmitter_pos = nav->geod();
|
|
|
|
_transmitter_cart = nav->cart();
|
2003-10-01 21:10:33 +00:00
|
|
|
_transmitter_range_nm = nav->get_range();
|
|
|
|
}
|
|
|
|
}
|
2004-08-19 11:54:57 +00:00
|
|
|
|
|
|
|
if ( _last_ident != ident ) {
|
|
|
|
_last_ident = ident;
|
|
|
|
_ident_node->setStringValue(ident.c_str());
|
|
|
|
|
2009-10-04 13:52:53 +00:00
|
|
|
if ( _sgr->exists( _adf_ident ) ) {
|
2004-10-16 12:37:39 +00:00
|
|
|
// stop is required! -- remove alone wouldn't stop immediately
|
2009-10-04 13:52:53 +00:00
|
|
|
_sgr->stop( _adf_ident );
|
|
|
|
_sgr->remove( _adf_ident );
|
2004-08-19 11:54:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SGSoundSample *sound;
|
2011-09-15 19:28:30 +00:00
|
|
|
sound = FGMorse::instance()->make_ident( ident, FGMorse::LO_FREQUENCY );
|
2004-10-06 09:30:36 +00:00
|
|
|
sound->set_volume(_last_volume = 0);
|
2009-10-04 13:52:53 +00:00
|
|
|
_sgr->add( sound, _adf_ident );
|
2004-08-19 11:54:57 +00:00
|
|
|
|
|
|
|
int offset = (int)(sg_random() * 30.0);
|
|
|
|
_ident_count = offset / 4;
|
|
|
|
_last_ident_time = globals->get_time_params()->get_cur_time() -
|
|
|
|
offset;
|
|
|
|
}
|
2003-10-01 21:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ADF::set_bearing (double dt, double bearing_deg)
|
|
|
|
{
|
|
|
|
double old_bearing_deg = _bearing_node->getDoubleValue();
|
|
|
|
|
2004-11-10 20:01:28 +00:00
|
|
|
while ((bearing_deg - old_bearing_deg) >= 180)
|
|
|
|
old_bearing_deg += 360;
|
|
|
|
while ((bearing_deg - old_bearing_deg) <= -180)
|
|
|
|
old_bearing_deg -= 360;
|
2003-10-01 21:10:33 +00:00
|
|
|
bearing_deg += _error_node->getDoubleValue();
|
|
|
|
bearing_deg =
|
|
|
|
fgGetLowPass(old_bearing_deg, bearing_deg, dt * RESPONSIVENESS);
|
|
|
|
|
|
|
|
_bearing_node->setDoubleValue(bearing_deg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// end of adf.cxx
|