From 10e933dc5380a59529189f5bc589a6b9c3b1a8f3 Mon Sep 17 00:00:00 2001 From: adrian Date: Tue, 6 Dec 2011 23:00:50 +0200 Subject: [PATCH] Send geod from Nasal, properly document the code, take some parameters from properties --- src/Radio/radio.cxx | 135 ++++++++++++++++--------------------- src/Radio/radio.hxx | 89 ++++++++++++++++++------ src/Scripting/NasalSys.cxx | 3 +- 3 files changed, 126 insertions(+), 101 deletions(-) diff --git a/src/Radio/radio.cxx b/src/Radio/radio.cxx index 3412a151b..2c983285f 100644 --- a/src/Radio/radio.cxx +++ b/src/Radio/radio.cxx @@ -37,7 +37,7 @@ FGRadioTransmission::FGRadioTransmission() { - _receiver_sensitivity = -105.0; // typical AM receiver sensitivity seems to be 0.8 microVolt at 12dB SINAD + _receiver_sensitivity = -105.0; // typical AM receiver sensitivity seems to be 0.8 microVolt at 12dB SINAD or less /** AM transmitter power in dBm. * Typical output powers for ATC ground equipment, VHF-UHF: @@ -91,17 +91,15 @@ double FGRadioTransmission::getFrequency(int radio) { return freq; } -/*** TODO: receive multiplayer chat message and voice -***/ + void FGRadioTransmission::receiveChat(SGGeod tx_pos, double freq, string text, int ground_to_air) { } -/*** TODO: receive navaid -***/ + double FGRadioTransmission::receiveNav(SGGeod tx_pos, double freq, int transmission_type) { - // typical VOR/LOC transmitter power appears to be 200 Watt ~ 53 dBm + // typical VOR/LOC transmitter power appears to be 100 - 200 Watt i.e 50 - 53 dBm // vor/loc typical sensitivity between -107 and -101 dBm // glideslope sensitivity between -85 and -81 dBm if ( _propagation_model == 1) { @@ -115,40 +113,42 @@ double FGRadioTransmission::receiveNav(SGGeod tx_pos, double freq, int transmiss } -double FGRadioTransmission::receiveBeacon(double lat, double lon, double elev, double heading, double pitch) { + +double FGRadioTransmission::receiveBeacon(SGGeod &tx_pos, double heading, double pitch) { + // these properties should be set by an instrument + _receiver_sensitivity = _root_node->getDoubleValue("station[0]/rx-sensitivity", _receiver_sensitivity); + _transmitter_power = watt_to_dbm(_root_node->getDoubleValue("station[0]/tx-power-watt", _transmitter_power)); + _polarization = _root_node->getIntValue("station[0]/polarization", 1); + _tx_antenna_height += _root_node->getDoubleValue("station[0]/tx-antenna-height", 0); + _rx_antenna_height += _root_node->getDoubleValue("station[0]/rx-antenna-height", 0); + _tx_antenna_gain += _root_node->getDoubleValue("station[0]/tx-antenna-gain", 0); + _rx_antenna_gain += _root_node->getDoubleValue("station[0]/rx-antenna-gain", 0); + + double freq = _root_node->getDoubleValue("station[0]/frequency", 144.8); // by default stay in the ham 2 meter band - _transmitter_power = 36; - _tx_antenna_height += 0.0; - _tx_antenna_gain += 0.5; - elev = elev * SG_FEET_TO_METER; - double freq = _root_node->getDoubleValue("station[0]/frequency", 118.0); - int ground_to_air = 1; - string text = "Beacon1"; double comm1 = getFrequency(1); double comm2 = getFrequency(2); if ( !(fabs(freq - comm1) <= 0.0001) && !(fabs(freq - comm2) <= 0.0001) ) { return -1; } - SGGeod tx_pos = SGGeod::fromDegM( lon, lat, elev ); - double signal = ITM_calculate_attenuation(tx_pos, freq, ground_to_air); + + double signal = ITM_calculate_attenuation(tx_pos, freq, 1); return signal; } -/*** Receive ATC radio communication as text -***/ + void FGRadioTransmission::receiveATC(SGGeod tx_pos, double freq, string text, int ground_to_air) { - + // adjust some default parameters in case the ATC code does not set them if(ground_to_air == 1) { _transmitter_power += 4.0; _tx_antenna_height += 30.0; _tx_antenna_gain += 2.0; } - double comm1 = getFrequency(1); double comm2 = getFrequency(2); if ( !(fabs(freq - comm1) <= 0.0001) && !(fabs(freq - comm2) <= 0.0001) ) { @@ -156,30 +156,27 @@ void FGRadioTransmission::receiveATC(SGGeod tx_pos, double freq, string text, in } else { - if ( _propagation_model == 0) { - // skip propagation routines entirely + if ( _propagation_model == 0) { // skip propagation routines entirely fgSetString("/sim/messages/atc", text.c_str()); } - else if ( _propagation_model == 1 ) { - // Use free-space, round earth + else if ( _propagation_model == 1 ) { // Use free-space, round earth + double signal = LOS_calculate_attenuation(tx_pos, freq, ground_to_air); if (signal <= 0.0) { return; } else { - fgSetString("/sim/messages/atc", text.c_str()); - } } - else if ( _propagation_model == 2 ) { - // Use ITM propagation model + else if ( _propagation_model == 2 ) { // Use ITM propagation model + double signal = ITM_calculate_attenuation(tx_pos, freq, ground_to_air); if (signal <= 0.0) { return; } if ((signal > 0.0) && (signal < 12.0)) { - /** for low SNR values implement a way to make the conversation + /** for low SNR values need a way to make the conversation * hard to understand but audible * in the real world, the receiver AGC fails to capture the slope * and the signal, due to being amplitude modulated, decreases volume after demodulation @@ -195,27 +192,21 @@ void FGRadioTransmission::receiveATC(SGGeod tx_pos, double freq, string text, in text.replace(pos,1, hash_noise); } */ - double volume = (fabs(signal - 12.0) / 12); - double old_volume = fgGetDouble("/sim/sound/voices/voice/volume"); - SG_LOG(SG_GENERAL, SG_BULK, "Usable signal at limit: " << signal); - //cerr << "Usable signal at limit: " << signal << endl; - fgSetDouble("/sim/sound/voices/voice/volume", volume); + //double volume = (fabs(signal - 12.0) / 12); + //double old_volume = fgGetDouble("/sim/sound/voices/voice/volume"); + + //fgSetDouble("/sim/sound/voices/voice/volume", volume); fgSetString("/sim/messages/atc", text.c_str()); - fgSetDouble("/sim/sound/voices/voice/volume", old_volume); + //fgSetDouble("/sim/sound/voices/voice/volume", old_volume); } else { fgSetString("/sim/messages/atc", text.c_str()); } - } - } - } -/*** Implement radio attenuation - based on the Longley-Rice propagation model -***/ + double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, int transmission_type) { @@ -282,10 +273,10 @@ double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, i double reverse_course = SGGeodesy::courseRad(sender_pos_c, own_pos_c); double distance_m = SGGeodesy::distanceM(own_pos, sender_pos); double probe_distance = 0.0; - /** If distance larger than this value (300 km), assume reception imposssible */ + /** If distance larger than this value (300 km), assume reception imposssible to spare CPU cycles */ if (distance_m > 300000) return -1.0; - /** If above 8000 meters, consider LOS mode and calculate free-space att */ + /** If above 8000 meters, consider LOS mode and calculate free-space att to spare CPU cycles */ if (own_alt > 8000) { dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55; SG_LOG(SG_GENERAL, SG_BULK, @@ -320,9 +311,6 @@ double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, i transmitter_height += _tx_antenna_height; receiver_height += _rx_antenna_height; - - SG_LOG(SG_GENERAL, SG_BULK, - "ITM:: RX-height: " << receiver_height << " meters, TX-height: " << transmitter_height << " meters, Distance: " << distance_m << " meters"); //cerr << "ITM:: RX-height: " << receiver_height << " meters, TX-height: " << transmitter_height << " meters, Distance: " << distance_m << " meters" << endl; _root_node->setDoubleValue("station[0]/rx-height", receiver_height); _root_node->setDoubleValue("station[0]/tx-height", transmitter_height); @@ -393,8 +381,6 @@ double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, i for(int i=0;isetDoubleValue("station[0]/link-budget", link_budget); _root_node->setDoubleValue("station[0]/terrain-attenuation", dbloss); _root_node->setStringValue("station[0]/prop-mode", strmode); _root_node->setDoubleValue("station[0]/clutter-attenuation", clutter_loss); _root_node->setDoubleValue("station[0]/polarization-attenuation", pol_loss); - //if (errnum == 4) // if parameters are outside sane values for lrprop, the alternative method is used + //if (errnum == 4) // if parameters are outside sane values for lrprop, bail out fast // return -1; + + // temporary, keep this antenna radiation pattern code here double tx_pattern_gain = 0.0; double rx_pattern_gain = 0.0; - if (_root_node->getBoolValue("use-antenna-pattern", false)) { - double sender_heading = 270.0; // due West - double tx_antenna_bearing = sender_heading - reverse_course * SGD_RADIANS_TO_DEGREES; - double rx_antenna_bearing = own_heading - course * SGD_RADIANS_TO_DEGREES; - double rx_elev_angle = atan((itm_elev[2] + transmitter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m) * SGD_RADIANS_TO_DEGREES; - double tx_elev_angle = 0.0 - rx_elev_angle; + double sender_heading = 270.0; // due West + double tx_antenna_bearing = sender_heading - reverse_course * SGD_RADIANS_TO_DEGREES; + double rx_antenna_bearing = own_heading - course * SGD_RADIANS_TO_DEGREES; + double rx_elev_angle = atan((itm_elev[2] + transmitter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m) * SGD_RADIANS_TO_DEGREES; + double tx_elev_angle = 0.0 - rx_elev_angle; + if (_root_node->getBoolValue("use-tx-antenna-pattern", false)) { FGRadioAntenna* TX_antenna; - FGRadioAntenna* RX_antenna; TX_antenna = new FGRadioAntenna("Plot2"); TX_antenna->set_heading(sender_heading); TX_antenna->set_elevation_angle(0); tx_pattern_gain = TX_antenna->calculate_gain(tx_antenna_bearing, tx_elev_angle); + delete TX_antenna; + } + if (_root_node->getBoolValue("use-rx-antenna-pattern", false)) { + FGRadioAntenna* RX_antenna; RX_antenna = new FGRadioAntenna("Plot2"); RX_antenna->set_heading(own_heading); RX_antenna->set_elevation_angle(fgGetDouble("/orientation/pitch-deg")); rx_pattern_gain = RX_antenna->calculate_gain(rx_antenna_bearing, rx_elev_angle); - - delete TX_antenna; delete RX_antenna; } @@ -467,10 +457,7 @@ double FGRadioTransmission::ITM_calculate_attenuation(SGGeod pos, double freq, i } -/*** Calculate losses due to vegetation and urban clutter (WIP) -* We are only worried about clutter loss, terrain influence -* on the first Fresnel zone is calculated in the ITM functions -***/ + void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[], deque &materials, double transmitter_height, double receiver_height, int p_mode, double horizons[], double &clutter_loss) { @@ -757,16 +744,13 @@ void FGRadioTransmission::calculate_clutter_loss(double freq, double itm_elev[], } } - else if (p_mode == 2) { // troposcatter: ignore ground clutter for now... + else if (p_mode == 2) { // troposcatter: ignore ground clutter for now... maybe do something with weather clutter_loss = 0.0; } } -/*** Temporary material properties database -* height: median clutter height -* density: radiowave attenuation factor -***/ + void FGRadioTransmission::get_material_properties(string mat_name, double &height, double &density) { if(mat_name == "Landmass") { @@ -878,14 +862,10 @@ void FGRadioTransmission::get_material_properties(string mat_name, double &heigh } -/*** implement simple LOS propagation model (WIP) -***/ + double FGRadioTransmission::LOS_calculate_attenuation(SGGeod pos, double freq, int transmission_type) { - double frq_mhz; - if( (freq < 118.0) || (freq > 137.0) ) - frq_mhz = 125.0; // sane value, middle of bandplan - else - frq_mhz = freq; + + double frq_mhz = freq; double dbloss; double tx_pow = _transmitter_power; double ant_gain = _rx_antenna_gain + _tx_antenna_gain; @@ -936,8 +916,7 @@ double FGRadioTransmission::LOS_calculate_attenuation(SGGeod pos, double freq, i // free-space loss (distance calculation should be changed) dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55; signal = link_budget - dbloss + pol_loss; - SG_LOG(SG_GENERAL, SG_BULK, - "LOS:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm "); + //cerr << "LOS:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm " << endl; return signal; diff --git a/src/Radio/radio.hxx b/src/Radio/radio.hxx index 06e05e970..06ab98894 100644 --- a/src/Radio/radio.hxx +++ b/src/Radio/radio.hxx @@ -36,9 +36,6 @@ using std::string; class FGRadioTransmission { private: - bool isOperable() const - { return _operable; } - bool _operable; ///< is the unit serviceable, on, powered, etc double _receiver_sensitivity; double _transmitter_power; @@ -55,44 +52,92 @@ private: SGPropertyNode *_root_node; int _propagation_model; /// 0 none, 1 round Earth, 2 ITM double polarization_loss(); + + +/*** Implement radio attenuation +* based on the Longley-Rice propagation model +* ground_to_air: 0 for air to ground 1 for ground to air, 2 for air to air, 3 for pilot to ground, 4 for pilot to air +* @param: transmitter position, frequency, flag to indicate if the transmission is from a ground station +* @return: signal level above receiver treshhold sensitivity +***/ double ITM_calculate_attenuation(SGGeod tx_pos, double freq, int ground_to_air); + +/*** a simple alternative LOS propagation model (WIP) +* @param: transmitter position, frequency, flag to indicate if the transmission is from a ground station +* @return: signal level above receiver treshhold sensitivity +***/ double LOS_calculate_attenuation(SGGeod tx_pos, double freq, int ground_to_air); + +/*** Calculate losses due to vegetation and urban clutter (WIP) +* We are only worried about clutter loss, terrain influence +* on the first Fresnel zone is calculated in the ITM functions +* @param: frequency, elevation data, terrain type, horizon distances, calculated loss +* @return: none +***/ void calculate_clutter_loss(double freq, double itm_elev[], std::deque &materials, double transmitter_height, double receiver_height, int p_mode, double horizons[], double &clutter_loss); + +/*** Temporary material properties database +* @param: terrain type, median clutter height, radiowave attenuation factor +* @return: none +***/ void get_material_properties(string mat_name, double &height, double &density); + public: FGRadioTransmission(); ~FGRadioTransmission(); - // a couple of setters and getters for convenience + /// a couple of setters and getters for convenience, call after initializing + /// frequency is in MHz, sensitivity in dBm, antenna gain and losses in dB, transmitter power in dBm + /// polarization can be: 0 horizontal, 1 vertical void setFrequency(double freq, int radio); double getFrequency(int radio); - void setTxPower(double txpower) { _transmitter_power = txpower; }; - void setRxSensitivity(double sensitivity) { _receiver_sensitivity = sensitivity; }; - void setTxAntennaHeight(double tx_antenna_height) { _tx_antenna_height = tx_antenna_height; }; - void setRxAntennaHeight(double rx_antenna_height) { _rx_antenna_height = rx_antenna_height; }; - void setTxAntennaGain(double tx_antenna_gain) { _tx_antenna_gain = tx_antenna_gain; }; - void setRxAntennaGain(double rx_antenna_gain) { _rx_antenna_gain = rx_antenna_gain; }; - void setTxLineLosses(double tx_line_losses) { _tx_line_losses = tx_line_losses; }; - void setRxLineLosses(double rx_line_losses) { _rx_line_losses = rx_line_losses; }; - void setPropagationModel(int model) { _propagation_model = model; }; - void setPolarization(int polarization) { _polarization = polarization; }; - // accessory functions for unit conversions - double watt_to_dbm(double power_watt); - double dbm_to_watt(double dbm); - double dbm_to_microvolt(double dbm); + inline void setTxPower(double txpower) { _transmitter_power = txpower; }; + inline void setRxSensitivity(double sensitivity) { _receiver_sensitivity = sensitivity; }; + inline void setTxAntennaHeight(double tx_antenna_height) { _tx_antenna_height = tx_antenna_height; }; + inline void setRxAntennaHeight(double rx_antenna_height) { _rx_antenna_height = rx_antenna_height; }; + inline void setTxAntennaGain(double tx_antenna_gain) { _tx_antenna_gain = tx_antenna_gain; }; + inline void setRxAntennaGain(double rx_antenna_gain) { _rx_antenna_gain = rx_antenna_gain; }; + inline void setTxLineLosses(double tx_line_losses) { _tx_line_losses = tx_line_losses; }; + inline void setRxLineLosses(double rx_line_losses) { _rx_line_losses = rx_line_losses; }; + inline void setPropagationModel(int model) { _propagation_model = model; }; + inline void setPolarization(int polarization) { _polarization = polarization; }; + + /// static convenience functions for unit conversions + static double watt_to_dbm(double power_watt); + static double dbm_to_watt(double dbm); + static double dbm_to_microvolt(double dbm); - // transmission_type: 0 for air to ground 1 for ground to air, 2 for air to air, 3 for pilot to ground, 4 for pilot to air +/*** Receive ATC radio communication as text +* transmission_type: 0 for air to ground 1 for ground to air, 2 for air to air, 3 for pilot to ground, 4 for pilot to air +* @param: transmitter position, frequency, ATC text, flag to indicate whether the transmission comes from an ATC groundstation +* @return: none +***/ void receiveATC(SGGeod tx_pos, double freq, string text, int transmission_type); + +/*** TODO: receive multiplayer chat message and voice +* @param: transmitter position, frequency, ATC text, flag to indicate whether the transmission comes from an ATC groundstation +* @return: none +***/ void receiveChat(SGGeod tx_pos, double freq, string text, int transmission_type); - // returns signal quality - // transmission_type: 0 for VOR, 1 for ILS + +/*** TODO: receive navaid +* @param: transmitter position, frequency, flag +* @return: signal level above receiver treshhold sensitivity +***/ double receiveNav(SGGeod tx_pos, double freq, int transmission_type); - double receiveBeacon(double lat, double lon, double elev, double heading, double pitch); + +/*** Call this function to receive an arbitrary signal +* for instance via the Nasal radioTransmission() function +* returns the signal value above receiver sensitivity treshhold +* @param: transmitter position, object heading in degrees (for antenna), object pitch angle in degrees +* @return: signal level above receiver treshhold sensitivity +***/ + double receiveBeacon(SGGeod &tx_pos, double heading, double pitch); }; diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx index d9a2cb6ff..7c4a387e6 100644 --- a/src/Scripting/NasalSys.cxx +++ b/src/Scripting/NasalSys.cxx @@ -515,8 +515,9 @@ static naRef f_radioTransmission(naContext c, naRef me, int argc, naRef* args) elev = naNumValue(args[2]).num; heading = naNumValue(args[3]).num; pitch = naNumValue(args[4]).num; + SGGeod geod = SGGeod::fromDegM(lon, lat, elev * SG_FEET_TO_METER); FGRadioTransmission *radio = new FGRadioTransmission; - double signal = radio->receiveBeacon(lat,lon,elev,heading,pitch); + double signal = radio->receiveBeacon(geod, heading, pitch); delete radio; return naNum(signal); }