10#include <simgear/compiler.h>
11#include <simgear/math/sg_geodesy.hxx>
12#include <simgear/math/sg_random.hxx>
13#include <simgear/timing/sg_time.hxx>
21#include <simgear/sound/sample_group.hxx>
31#define RESPONSIVENESS 0.5
40adjust_range (
double transmitter_elevation_ft,
double aircraft_altitude_ft,
43 double delta_elevation_ft =
44 aircraft_altitude_ft - transmitter_elevation_ft;
45 double range_nm = max_range_nm;
49 if (delta_elevation_ft < 0)
50 delta_elevation_ft = 200;
51 if (delta_elevation_ft <= 1000)
52 range_nm *= sqrt(delta_elevation_ft / 1000);
53 else if (delta_elevation_ft >= 5000)
54 range_nm *= sqrt(delta_elevation_ft / 5000);
55 if (range_nm >= max_range_nm * 3)
56 range_nm = max_range_nm * 3;
58 double rand = sg_random();
59 return range_nm + (range_nm * rand * rand);
65 _time_before_search_sec(0),
66 _last_frequency_khz(-1),
67 _transmitter_valid(false),
68 _transmitter_pos(SGGeod::fromDeg(0, 0)),
69 _transmitter_cart(0, 0, 0),
70 _transmitter_range_nm(0),
87 SGPropertyNode *node =
fgGetNode(branch,
true );
92 _error_node = node->getChild(
"error-deg", 0,
true);
93 _mode_node = node->getChild(
"mode", 0,
true);
94 _volume_node = node->getChild(
"volume-norm", 0,
true);
95 _in_range_node = node->getChild(
"in-range", 0,
true);
96 _bearing_node = node->getChild(
"indicated-bearing-deg", 0,
true);
97 _ident_node = node->getChild(
"ident", 0,
true);
98 _ident_audible_node = node->getChild(
"ident-audible", 0,
true);
101 SGPropertyNode *fnode = node->getChild(
"frequencies", 0,
true);
102 _frequency_node = fnode->getChild(
"selected-khz", 0,
true);
105 _heading_node =
fgGetNode(
"/orientation/heading-deg",
true);
108 auto smgr =
globals->get_subsystem<SGSoundMgr>();
109 _sgr = smgr->find(
"avionics",
true);
110 _sgr->tie_to_listener();
112 std::ostringstream temp;
114 _adf_ident = temp.str();
122 _in_range_node->setBoolValue(
false);
123 _ident_node->setStringValue(
"");
127 string mode = _mode_node->getStringValue();
128 if (mode ==
"ant" || mode ==
"test") set_bearing(delta_time_sec, 90);
129 if (mode !=
"bfo" && mode !=
"adf") {
130 _in_range_node->setBoolValue(
false);
131 _ident_node->setStringValue(
"");
135 int frequency_khz = _frequency_node->getIntValue();
136 if (frequency_khz != _last_frequency_khz) {
137 _time_before_search_sec = 0;
138 _last_frequency_khz = frequency_khz;
141 SGGeod acPos(
globals->get_aircraft_position());
144 _time_before_search_sec -= delta_time_sec;
145 if (_time_before_search_sec < 0)
146 search(frequency_khz, acPos);
148 if (!_transmitter_valid) {
149 _in_range_node->setBoolValue(
false);
150 _ident_node->setStringValue(
"");
155 SGVec3d location =
globals->get_aircraft_position_cart();
157 double distance_nm = dist(_transmitter_cart, location) * SG_METER_TO_NM;
158 double range_nm =
adjust_range(_transmitter_pos.getElevationFt(),
159 acPos.getElevationFt(),
160 _transmitter_range_nm);
162 if (distance_nm <= range_nm) {
164 double bearing, az2, s;
165 double heading = _heading_node->getDoubleValue();
167 geo_inverse_wgs_84(acPos, _transmitter_pos,
169 _in_range_node->setBoolValue(
true);
170 _ident_node->setStringValue(_last_ident);
175 set_bearing(delta_time_sec, bearing);
179 if ( _ident_audible_node->getBoolValue() )
180 volume = _volume_node->getFloatValue();
184 if ( volume != _last_volume ) {
185 _last_volume = volume;
187 SGSoundSample *sound;
188 sound = _sgr->find( _adf_ident );
190 sound->set_volume( volume );
192 SG_LOG( SG_INSTR, SG_ALERT,
"Can't find adf-ident sound" );
195 time_t cur_time =
globals->get_time_params()->get_cur_time();
196 if ( _last_ident_time < cur_time - 30 ) {
197 _last_ident_time = cur_time;
201 if ( _ident_count < 4 ) {
202 if ( !_sgr->is_playing(_adf_ident) && (volume > 0.05) ) {
203 _sgr->play_once( _adf_ident );
208 _in_range_node->setBoolValue(
false);
209 _ident_node->setStringValue(
"");
210 _sgr->stop( _adf_ident );
215ADF::search (
double frequency_khz,
const SGGeod& pos)
219 _time_before_search_sec = 1.0;
224 _transmitter_valid = (nav != NULL);
225 if ( _transmitter_valid ) {
227 if ( ident != _last_ident ) {
228 _transmitter_pos = nav->
geod();
229 _transmitter_cart = nav->
cart();
230 _transmitter_range_nm = nav->
get_range();
234 if ( _last_ident != ident ) {
236 _ident_node->setStringValue(ident.c_str());
238 if ( _sgr->exists( _adf_ident ) ) {
240 _sgr->stop( _adf_ident );
241 _sgr->remove( _adf_ident );
244 SGSoundSample *sound;
246 sound->set_volume(_last_volume = 0);
247 _sgr->add( sound, _adf_ident );
249 int offset = (int)(sg_random() * 30.0);
250 _ident_count = offset / 4;
257ADF::set_bearing (
double dt,
double bearing_deg)
259 double old_bearing_deg = _bearing_node->getDoubleValue();
261 while ((bearing_deg - old_bearing_deg) >= 180)
262 old_bearing_deg += 360;
263 while ((bearing_deg - old_bearing_deg) <= -180)
264 old_bearing_deg -= 360;
265 bearing_deg += _error_node->getDoubleValue();
269 _bearing_node->setDoubleValue(bearing_deg);
275SGSubsystemMgr::InstancedRegistrant<ADF> registrantADF(
277 {{
"instrumentation", SGSubsystemMgr::Dependency::HARD}},
static double adjust_range(double transmitter_elevation_ft, double aircraft_altitude_ft, double max_range_nm)
Fiddle with the reception range a bit.
ADF(SGPropertyNode *node)
void update(double delta_time_sec) override
void initServicePowerProperties(SGPropertyNode *node)
void readConfig(SGPropertyNode *config, std::string defaultName)
std::string nodePath() const
bool isServiceableAndPowered() const
SGTime * get_time_params() const
static const int LO_FREQUENCY
SGSoundSample * make_ident(const std::string &id, const int freq=LO_FREQUENCY)
static FGMorse * instance()
static FGNavRecordRef findByFreq(double freq, const SGGeod &position, TypeFilter *filter=nullptr)
Query the database for the specified station.
const char * get_trans_ident() const
virtual const SGGeod & geod() const
virtual const SGVec3d & cart() const
The cartesian position associated with this object.
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
double fgGetLowPass(double current, double target, double timeratio)
Move a value towards a target.