| 1 | // tacan.cxx - Tactical Navigation Beacon. |
| 2 | // Written by Vivian Meazaa, started 2005. |
| 3 | // |
| 4 | // This file is in the Public Domain and comes with no warranty. |
| 5 | |
| 6 | #ifdef HAVE_CONFIG_H |
| 7 | # include <config.h> |
| 8 | #endif |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <simgear/compiler.h> |
| 14 | #include <simgear/math/sg_geodesy.hxx> |
| 15 | #include <simgear/math/sg_random.hxx> |
| 16 | |
| 17 | #include <Main/fg_props.hxx> |
| 18 | #include <Navaids/navlist.hxx> |
| 19 | |
| 20 | #include "tacan.hxx" |
| 21 | |
| 22 | /** |
| 23 | * Adjust the range. |
| 24 | * |
| 25 | * Start by calculating the radar horizon based on the elevation |
| 26 | * difference, then clamp to the maximum, then add a fudge for |
| 27 | * borderline reception. |
| 28 | */ |
| 29 | static double |
| 30 | adjust_range( double station_elevation_ft, |
| 31 | double aircraft_altitude_ft, |
| 32 | double max_range_nm ) |
| 33 | { |
| 34 | // See http://en.wikipedia.org/wiki/Line-of-sight_propagation for approximate |
| 35 | // line-of-sight distance to the horizon |
| 36 | |
| 37 | double range_nm = 0; |
| 38 | if( station_elevation_ft > 5 ) |
| 39 | range_nm += 1.23 * sqrt(station_elevation_ft); |
| 40 | |
| 41 | if( aircraft_altitude_ft > 5 ) |
| 42 | range_nm += 1.23 * sqrt(aircraft_altitude_ft); |
| 43 | |
| 44 | return std::max(20., std::min(range_nm, max_range_nm)) |
| 45 | * (1 + SGMiscd::pow<2>(0.3 * sg_random())); |
| 46 | } |
| 47 | |
| 48 | //------------------------------------------------------------------------------ |
| 49 | TACAN::TACAN( SGPropertyNode *node ): |
| 50 | _name(node->getStringValue("name", "tacan")), |
| 51 | _num(node->getIntValue("number", 0)), |
| 52 | _was_disabled(true), |
| 53 | _new_frequency(false), |
| 54 | _channel("0000"), |
| 55 | _last_distance_nm(0), |
| 56 | _frequency_mhz(-1), |
| 57 | _time_before_search_sec(0), |
| 58 | _listener_active(0) |
| 59 | { |
| 60 | |
| 61 | } |
| 62 | |
| 63 | //------------------------------------------------------------------------------ |
| 64 | TACAN::~TACAN() |
| 65 | { |
| 66 | |
| 67 | } |
| 68 | |
| 69 | //------------------------------------------------------------------------------ |
| 70 | void TACAN::init() |
| 71 | { |
| 72 | std::string branch = "/instrumentation/" + _name; |
| 73 | |
| 74 | SGPropertyNode *node = fgGetNode(branch, _num, true ); |
| 75 | |
| 76 | _serviceable_node = node->getChild("serviceable", 0, true); |
| 77 | _ident_node = node->getChild("ident", 0, true); |
| 78 | |
| 79 | SGPropertyNode *fnode = node->getChild("frequencies", 0, true); |
| 80 | _frequency_node = fnode->getChild("selected-mhz", 0, true); |
| 81 | |
| 82 | _channel_in0_node = fnode->getChild("selected-channel", 0, true); |
| 83 | _channel_in1_node = fnode->getChild("selected-channel", 1, true); |
| 84 | _channel_in2_node = fnode->getChild("selected-channel", 2, true); |
| 85 | _channel_in3_node = fnode->getChild("selected-channel", 3, true); |
| 86 | _channel_in4_node = fnode->getChild("selected-channel", 4, true); |
| 87 | |
| 88 | _in_range_node = node->getChild("in-range", 0, true); |
| 89 | _distance_node = node->getChild("indicated-distance-nm", 0, true); |
| 90 | _speed_node = node->getChild("indicated-ground-speed-kt", 0, true); |
| 91 | _time_node = node->getChild("indicated-time-min", 0, true); |
| 92 | _name_node = node->getChild("name", 0, true); |
| 93 | _bearing_node = node->getChild("indicated-bearing-true-deg", 0, true); |
| 94 | |
| 95 | SGPropertyNode *dnode = node->getChild("display", 0, true); |
| 96 | _x_shift_node = dnode->getChild("x-shift", 0, true); |
| 97 | _y_shift_node = dnode->getChild("y-shift", 0, true); |
| 98 | _channel_node = dnode->getChild("channel", 0, true); |
| 99 | |
| 100 | _heading_node = fgGetNode("/orientation/heading-deg", true); |
| 101 | _electrical_node = fgGetNode("/systems/electrical/outputs/tacan", true); |
| 102 | |
| 103 | // Add/trigger change listener after creating all nodes |
| 104 | _frequency_node->addChangeListener(this); |
| 105 | |
| 106 | _channel_in0_node->addChangeListener(this); |
| 107 | _channel_in1_node->addChangeListener(this); |
| 108 | _channel_in2_node->addChangeListener(this); |
| 109 | _channel_in3_node->addChangeListener(this); |
| 110 | _channel_in4_node->addChangeListener(this, true); |
| 111 | |
| 112 | disabled(true); |
| 113 | } |
| 114 | |
| 115 | //------------------------------------------------------------------------------ |
| 116 | void TACAN::reinit() |
| 117 | { |
| 118 | _time_before_search_sec = 0; |
| 119 | } |
| 120 | |
| 121 | //------------------------------------------------------------------------------ |
| 122 | void TACAN::update(double delta_time_sec) |
| 123 | { |
| 124 | // don't do anything when paused |
| 125 | if( delta_time_sec == 0 ) |
| 126 | return; |
| 127 | |
| 128 | if( !_serviceable_node->getBoolValue() |
| 129 | || !_electrical_node->getBoolValue() ) |
| 130 | return disabled(); |
| 131 | |
| 132 | SGGeod pos(globals->get_aircraft_position()); |
| 133 | |
| 134 | // On timeout, scan again |
| 135 | _time_before_search_sec -= delta_time_sec; |
| 136 | if ((_time_before_search_sec < 0 || _new_frequency) && _frequency_mhz >= 0) |
| 137 | search(_frequency_mhz, pos); |
| 138 | |
| 139 | if( !_active_station || !_active_station->get_serviceable() ) |
| 140 | return disabled(); |
| 141 | |
| 142 | const SGGeod& nav_pos = _active_station->geod(); |
| 143 | |
| 144 | // Calculate the bearing and range of the station from the aircraft |
| 145 | double az = 0; |
| 146 | double bearing = 0; |
| 147 | double distance = SGLimitsd::max(); |
| 148 | if( !SGGeodesy::inverse(pos, nav_pos, bearing, az, distance) ) |
| 149 | return disabled(); |
| 150 | |
| 151 | // Increase distance due to difference in altitude |
| 152 | distance = |
| 153 | sqrt( SGMiscd::pow<2>(distance) |
| 154 | + SGMiscd::pow<2>(pos.getElevationM() - nav_pos.getElevationM()) ); |
| 155 | double distance_nm = distance * SG_METER_TO_NM; |
| 156 | |
| 157 | double range_nm = adjust_range( nav_pos.getElevationFt(), |
| 158 | pos.getElevationFt(), |
| 159 | _active_station->get_range() ); |
| 160 | |
| 161 | if( distance_nm > range_nm ) |
| 162 | return disabled(); |
| 163 | |
| 164 | //// calculate look left/right to target, without yaw correction |
| 165 | // double horiz_offset = bearing - heading; |
| 166 | // |
| 167 | // if (horiz_offset > 180.0) horiz_offset -= 360.0; |
| 168 | // if (horiz_offset < -180.0) horiz_offset += 360.0; |
| 169 | |
| 170 | //// now correct look left/right for yaw |
| 171 | // horiz_offset += yaw; |
| 172 | |
| 173 | // use the bearing for a plan position indicator display |
| 174 | double horiz_offset = bearing; |
| 175 | |
| 176 | // calculate values for radar display |
| 177 | double y_shift = distance_nm * cos(horiz_offset * SG_DEGREES_TO_RADIANS); |
| 178 | double x_shift = distance_nm * sin(horiz_offset * SG_DEGREES_TO_RADIANS); |
| 179 | |
| 180 | // TODO probably not the best way to do this (especially with mobile stations) |
| 181 | double speed_kt = fabs(distance_nm - _last_distance_nm) |
| 182 | * (3600 / delta_time_sec); |
| 183 | _speed_node->setDoubleValue(speed_kt); |
| 184 | _last_distance_nm = distance_nm; |
| 185 | |
| 186 | double bias = _active_station->get_multiuse(); |
| 187 | _distance_node->setDoubleValue( SGMiscd::max(0.0, distance_nm - bias) ); |
| 188 | |
| 189 | _time_node->setDoubleValue(speed_kt > 0 ? (distance_nm/speed_kt*60.0) : 0); |
| 190 | _bearing_node->setDoubleValue(bearing); |
| 191 | _x_shift_node->setDoubleValue(x_shift); |
| 192 | _y_shift_node->setDoubleValue(y_shift); |
| 193 | |
| 194 | _name_node->setStringValue(_active_station->name()); |
| 195 | _ident_node->setStringValue(_active_station->ident()); |
| 196 | _in_range_node->setBoolValue(true); |
| 197 | |
| 198 | _was_disabled = false; |
| 199 | } |
| 200 | |
| 201 | //------------------------------------------------------------------------------ |
| 202 | void TACAN::disabled(bool force) |
| 203 | { |
| 204 | if( _was_disabled && !force ) |
| 205 | return; |
| 206 | |
| 207 | _last_distance_nm = 0; |
| 208 | |
| 209 | _in_range_node->setBoolValue(false); |
| 210 | _distance_node->setDoubleValue(0); |
| 211 | _speed_node->setDoubleValue(0); |
| 212 | _time_node->setDoubleValue(0); |
| 213 | _bearing_node->setDoubleValue(0); |
| 214 | _x_shift_node->setDoubleValue(0); |
| 215 | _y_shift_node->setDoubleValue(0); |
| 216 | _name_node->setStringValue(""); |
| 217 | _ident_node->setStringValue(""); |
| 218 | |
| 219 | _was_disabled = true; |
| 220 | } |
| 221 | |
| 222 | //------------------------------------------------------------------------------ |
| 223 | void TACAN::search (double frequency_mhz,const SGGeod& pos) |
| 224 | { |
| 225 | // reset search time |
| 226 | _time_before_search_sec = 5; |
| 227 | |
| 228 | // Get first matching mobile station (carriers/tankers/etc.) |
| 229 | // TODO do we need to check for mobile stations with same frequency? Currently |
| 230 | // the distance is not taken into account. |
| 231 | FGNavRecordRef mobile_tacan = |
| 232 | FGNavList::findByFreq(frequency_mhz, FGNavList::mobileTacanFilter()); |
| 233 | |
| 234 | double mobile_dist = (mobile_tacan && mobile_tacan->get_serviceable()) |
| 235 | ? SGGeodesy::distanceM(pos, mobile_tacan->geod()) |
| 236 | : SGLimitsd::max(); |
| 237 | |
| 238 | // No get nearest TACAN/VORTAC |
| 239 | FGNavRecordRef tacan = |
| 240 | FGNavList::findByFreq(frequency_mhz, pos, FGNavList::tacanFilter()); |
| 241 | |
| 242 | double tacan_dist = tacan |
| 243 | ? SGGeodesy::distanceM(pos, tacan->geod()) |
| 244 | : SGLimitsd::max(); |
| 245 | |
| 246 | // Select nearer station as active |
| 247 | // TODO do we need to take more care of stations with same frequency? |
| 248 | _active_station = mobile_dist < tacan_dist |
| 249 | ? mobile_tacan |
| 250 | : tacan; |
| 251 | } |
| 252 | |
| 253 | //------------------------------------------------------------------------------ |
| 254 | double TACAN::searchChannel(const std::string& channel) |
| 255 | { |
| 256 | FGTACANRecord *freq = globals->get_channellist()->findByChannel(channel); |
| 257 | if( !freq ) |
| 258 | { |
| 259 | SG_LOG(SG_INSTR, SG_DEBUG, "Invalid TACAN channel: " << channel); |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | double frequency_khz = freq->get_freq(); |
| 264 | if(frequency_khz < 9620 || frequency_khz > 121300) |
| 265 | { |
| 266 | SG_LOG |
| 267 | ( |
| 268 | SG_INSTR, |
| 269 | SG_DEBUG, |
| 270 | "TACAN frequency out of range: " << channel |
| 271 | << ": " << frequency_khz << "kHz" |
| 272 | ); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | return frequency_khz/100; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Listener callback. Maintains channel input properties, |
| 281 | * searches new channel frequency, updates _channel and |
| 282 | * _frequency and sets boolean _new_frequency appropriately. |
| 283 | */ |
| 284 | void |
| 285 | TACAN::valueChanged(SGPropertyNode *prop) |
| 286 | { |
| 287 | if (_listener_active) |
| 288 | return; |
| 289 | _listener_active++; |
| 290 | |
| 291 | int index = prop->getIndex(); |
| 292 | std::string channel = _channel; |
| 293 | |
| 294 | if (std::string("selected-mhz") == prop->getNameString()) |
| 295 | { |
| 296 | FGTACANRecord *rec= globals->get_channellist()->findByFrequency(prop->getDoubleValue()*1000); |
| 297 | if (rec != nullptr) { |
| 298 | channel = rec->get_channel(); |
| 299 | _channel_in1_node->setStringValue(channel.substr(0, 1)); |
| 300 | _channel_in2_node->setStringValue(channel.substr(1, 1)); |
| 301 | _channel_in3_node->setStringValue(channel.substr(2, 1)); |
| 302 | _channel_in4_node->setStringValue(channel.substr(3, 1)); |
| 303 | index = 1; |
| 304 | } |
| 305 | else { |
| 306 | SG_LOG(SG_INSTR, SG_WARN, "Entered TACAN Frequency " << prop->getDoubleValue() << " does not map to a known TACAN channel"); |
| 307 | } |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | if (index) { // channel digit or X/Y input |
| 312 | int c; |
| 313 | if (isdigit(c = _channel_in1_node->getStringValue()[0])) |
| 314 | channel[0] = c; |
| 315 | if (isdigit(c = _channel_in2_node->getStringValue()[0])) |
| 316 | channel[1] = c; |
| 317 | if (isdigit(c = _channel_in3_node->getStringValue()[0])) |
| 318 | channel[2] = c; |
| 319 | c = _channel_in4_node->getStringValue()[0]; |
| 320 | if (c == 'X' || c == 'Y') |
| 321 | channel[3] = c; |
| 322 | |
| 323 | } |
| 324 | else { // channel number input |
| 325 | unsigned int f = prop->getIntValue(); |
| 326 | if (f >= 1 && f <= 126) { |
| 327 | channel[0] = '0' + (f / 100) % 10; |
| 328 | channel[1] = '0' + (f / 10) % 10; |
| 329 | channel[2] = '0' + f % 10; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if (channel != _channel) { |
| 335 | SG_LOG(SG_INSTR, SG_DEBUG, "new channel " << channel); |
| 336 | |
| 337 | // write back result |
| 338 | _channel_in0_node->setIntValue((channel[0] - '0') * 100 |
| 339 | + (channel[1] - '0') * 10 + (channel[2] - '0')); |
| 340 | char s[2] = "0"; |
| 341 | s[0] = channel[0], _channel_in1_node->setStringValue(s); |
| 342 | s[0] = channel[1], _channel_in2_node->setStringValue(s); |
| 343 | s[0] = channel[2], _channel_in3_node->setStringValue(s); |
| 344 | s[0] = channel[3], _channel_in4_node->setStringValue(s); |
| 345 | |
| 346 | // search channel frequency |
| 347 | double freq = searchChannel(channel); |
| 348 | if (freq != _frequency_mhz) { |
| 349 | SG_LOG(SG_INSTR, SG_DEBUG, "new frequency " << freq); |
| 350 | _frequency_node->setDoubleValue(freq); |
| 351 | _frequency_mhz = freq; |
| 352 | _new_frequency = true; |
| 353 | } |
| 354 | |
| 355 | _channel = channel; |
| 356 | _channel_node->setStringValue(_channel); |
| 357 | _time_before_search_sec = 0; |
| 358 | } |
| 359 | |
| 360 | _listener_active--; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | // Register the subsystem. |
| 365 | #if 0 |
| 366 | SGSubsystemMgr::InstancedRegistrant<TACAN> registrantTACAN( |
| 367 | SGSubsystemMgr::FDM, |
| 368 | {{"instrumentation", SGSubsystemMgr::Dependency::HARD}}, |
| 369 | 0.2); |
| 370 | #endif |
| 371 | |
| 372 | // end of TACAN.cxx |