1
0
Fork 0

Merge branch 'merge-requests/1555' into next

This commit is contained in:
Durk Talsma 2011-12-04 17:33:04 +01:00
commit 32eb0fd00c
10 changed files with 3045 additions and 11 deletions

View file

@ -47,6 +47,7 @@
#include <Airports/groundnetwork.hxx>
#include <Airports/dynamics.hxx>
#include <Airports/simple.hxx>
#include <Radio/radio.hxx>
using std::sort;
@ -508,7 +509,7 @@ bool FGATCController::isUserAircraft(FGAIAircraft* ac)
return (ac->getCallSign() == fgGetString("/sim/multiplay/callsign")) ? true : false;
};
void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
void FGATCController::transmit(FGTrafficRecord * rec, FGAirportDynamics *parent, AtcMsgId msgId,
AtcMsgDir msgDir, bool audible)
{
string sender, receiver;
@ -529,6 +530,7 @@ void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
FGAIFlightPlan *fp;
string fltRules;
string instructionText;
int ground_to_air=0;
//double commFreqD;
sender = rec->getAircraft()->getTrafficRef()->getCallSign();
@ -569,6 +571,7 @@ void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
string tmp = sender;
sender = receiver;
receiver = tmp;
ground_to_air=1;
}
switch (msgId) {
case MSG_ANNOUNCE_ENGINE_START:
@ -736,10 +739,35 @@ void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
// Display ATC message only when one of the radios is tuned
// the relevant frequency.
// Note that distance attenuation is currently not yet implemented
if ((onBoardRadioFreqI0 == stationFreq)
|| (onBoardRadioFreqI1 == stationFreq)) {
if (rec->allowTransmissions()) {
fgSetString("/sim/messages/atc", text.c_str());
if( fgGetBool( "/sim/radio/use-itm-attenuation", false ) ) {
//cerr << "Using ITM radio propagation" << endl;
FGRadioTransmission* radio = new FGRadioTransmission();
SGGeod sender_pos;
double sender_alt_ft, sender_alt;
if(ground_to_air) {
sender_alt_ft = parent->getElevation();
sender_alt = sender_alt_ft * SG_FEET_TO_METER;
sender_pos= SGGeod::fromDegM( parent->getLongitude(),
parent->getLatitude(), sender_alt );
}
else {
sender_alt_ft = rec->getAltitude();
sender_alt = sender_alt_ft * SG_FEET_TO_METER;
sender_pos= SGGeod::fromDegM( rec->getLongitude(),
rec->getLatitude(), sender_alt );
}
double frequency = ((double)stationFreq) / 100;
radio->receiveATC(sender_pos, frequency, text, ground_to_air);
delete radio;
}
else {
fgSetString("/sim/messages/atc", text.c_str());
}
}
}
} else {
@ -747,6 +775,7 @@ void FGATCController::transmit(FGTrafficRecord * rec, AtcMsgId msgId,
}
}
string FGATCController::formatATCFrequency3_2(int freq)
{
char buffer[7];
@ -1186,13 +1215,13 @@ bool FGStartupController::checkTransmissionState(int st, time_t now, time_t star
FGATCDialogNew::instance()->removeEntry(1);
} else {
//cerr << "creading message for " << i->getAircraft()->getCallSign() << endl;
transmit(&(*i), msgId, msgDir, false);
transmit(&(*i), &(*parent), msgId, msgDir, false);
return false;
}
}
if (now > startTime) {
//cerr << "Transmitting startup msg" << endl;
transmit(&(*i), msgId, msgDir, true);
transmit(&(*i), &(*parent), msgId, msgDir, true);
i->updateState();
lastTransmission = now;
available = false;
@ -1261,11 +1290,11 @@ void FGStartupController::updateAircraftInformation(int id, double lat, double l
if (now > startTime + 200) {
if (i->pushBackAllowed()) {
i->allowRepeatedTransmissions();
transmit(&(*i), MSG_PERMIT_PUSHBACK_CLEARANCE,
transmit(&(*i), &(*parent), MSG_PERMIT_PUSHBACK_CLEARANCE,
ATC_GROUND_TO_AIR, true);
i->updateState();
} else {
transmit(&(*i), MSG_HOLD_PUSHBACK_CLEARANCE,
transmit(&(*i), &(*parent), MSG_HOLD_PUSHBACK_CLEARANCE,
ATC_GROUND_TO_AIR, true);
i->suppressRepeatedTransmissions();
}

View file

@ -453,7 +453,7 @@ public:
void setDt(double dt) {
dt_count = dt;
};
void transmit(FGTrafficRecord *rec, AtcMsgId msgId, AtcMsgDir msgDir, bool audible);
void transmit(FGTrafficRecord *rec, FGAirportDynamics *parent, AtcMsgId msgId, AtcMsgDir msgDir, bool audible);
string getGateName(FGAIAircraft *aircraft);
virtual void render(bool) = 0;
virtual string getName() = 0;

View file

@ -756,11 +756,11 @@ bool FGGroundNetwork::checkTransmissionState(int minState, int maxState, Traffic
FGATCDialogNew::instance()->removeEntry(1);
} else {
//cerr << "creating message for " << i->getAircraft()->getCallSign() << endl;
transmit(&(*i), msgId, msgDir, false);
transmit(&(*i), &(*parent->getDynamics()), msgId, msgDir, false);
return false;
}
}
transmit(&(*i), msgId, msgDir, true);
transmit(&(*i), &(*parent->getDynamics()), msgId, msgDir, true);
i->updateState();
lastTransmission = now;
available = false;
@ -1098,11 +1098,11 @@ void FGGroundNetwork::checkHoldPosition(int id, double lat,
if ((origStatus != currStatus) && available) {
//cerr << "Issueing hold short instrudtion " << currStatus << " " << available << endl;
if (currStatus == true) { // No has a hold short instruction
transmit(&(*current), MSG_HOLD_POSITION, ATC_GROUND_TO_AIR, true);
transmit(&(*current), &(*parent->getDynamics()), MSG_HOLD_POSITION, ATC_GROUND_TO_AIR, true);
//cerr << "Transmittin hold short instrudtion " << currStatus << " " << available << endl;
current->setState(1);
} else {
transmit(&(*current), MSG_RESUME_TAXI, ATC_GROUND_TO_AIR, true);
transmit(&(*current), &(*parent->getDynamics()), MSG_RESUME_TAXI, ATC_GROUND_TO_AIR, true);
//cerr << "Transmittig resume instrudtion " << currStatus << " " << available << endl;
current->setState(2);
}

View file

@ -8,6 +8,7 @@ foreach( mylibfolder
Aircraft
ATC
ATCDCL
Radio
Autopilot
Cockpit
Environment

16
src/Radio/CMakeLists.txt Normal file
View file

@ -0,0 +1,16 @@
include(FlightGearComponent)
set(SOURCES
antenna.cxx
radio.cxx
itm.cpp
)
set(HEADERS
antenna.hxx
radio.hxx
)
flightgear_component(Radio "${SOURCES}" "${HEADERS}")

53
src/Radio/antenna.cxx Normal file
View file

@ -0,0 +1,53 @@
// antenna.cxx -- implementation of FGRadioAntenna
// Class to represent a virtual radio antenna properties
// Written by Adrian Musceac, started December 2011.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <math.h>
#include <stdlib.h>
#include <Scenery/scenery.hxx>
#include "antenna.hxx"
FGRadioAntenna::FGRadioAntenna() {
_mirror_y = 1;
_mirror_z = 1;
_invert_ground = 0;
}
FGRadioAntenna::~FGRadioAntenna() {
}
double FGRadioAntenna::calculate_gain(double azimuth, double elevation) {
return 0;
}
/*** load external plot file generated by NEC4
***/
void FGRadioAntenna::load_antenna_pattern() {
}

55
src/Radio/antenna.hxx Normal file
View file

@ -0,0 +1,55 @@
// antenna.hxx -- FGRadioAntenna: class to represent antenna properties
//
// Written by Adrian Musceac, started December 2011.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef __cplusplus
# error This library requires C++
#endif
#include <simgear/compiler.h>
#include <simgear/structure/subsystem_mgr.hxx>
#include <Main/fg_props.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/debug/logstream.hxx>
class FGRadioAntenna
{
private:
void load_antenna_pattern();
int _mirror_y;
int _mirror_z;
int _invert_ground;
double _heading_deg;
double _elevation_angle_deg;
struct AntennaGain {
double azimuth;
double elevation;
double gain;
};
typedef std::vector<AntennaGain> AntennaPattern;
AntennaPattern _pattern;
public:
FGRadioAntenna();
~FGRadioAntenna();
double calculate_gain(double azimuth, double elevation);
};

1851
src/Radio/itm.cpp Normal file

File diff suppressed because it is too large Load diff

932
src/Radio/radio.cxx Normal file
View file

@ -0,0 +1,932 @@
// radio.cxx -- implementation of FGRadio
// Class to manage radio propagation using the ITM model
// Written by Adrian Musceac, started August 2011.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <math.h>
#include <stdlib.h>
#include <deque>
#include "radio.hxx"
#include <simgear/scene/material/mat.hxx>
#include <Scenery/scenery.hxx>
#define WITH_POINT_TO_POINT 1
#include "itm.cpp"
FGRadioTransmission::FGRadioTransmission() {
_receiver_sensitivity = -110.0; // typical AM receiver sensitivity seems to be 0.8 microVolt at 12dB SINAD
/** AM transmitter power in dBm.
* Typical output powers for ATC ground equipment, VHF-UHF:
* 40 dBm - 10 W (ground, clearance)
* 44 dBm - 20 W (tower)
* 47 dBm - 50 W (center, sectors)
* 50 dBm - 100 W (center, sectors)
* 53 dBm - 200 W (sectors, on directional arrays)
**/
_transmitter_power = 43.0;
_tx_antenna_height = 2.0; // TX antenna height above ground level
_rx_antenna_height = 2.0; // RX antenna height above ground level
_rx_antenna_gain = 1.0; // gain expressed in dBi
_tx_antenna_gain = 1.0;
_rx_line_losses = 2.0; // to be configured for each station
_tx_line_losses = 2.0;
_polarization = 1; // default vertical
_propagation_model = 2;
_root_node = fgGetNode("sim/radio", true);
_terrain_sampling_distance = _root_node->getDoubleValue("sampling-distance", 90.0); // regular SRTM is 90 meters
}
FGRadioTransmission::~FGRadioTransmission()
{
}
double FGRadioTransmission::getFrequency(int radio) {
double freq = 118.0;
switch (radio) {
case 1:
freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
break;
case 2:
freq = fgGetDouble("/instrumentation/comm[1]/frequencies/selected-mhz");
break;
default:
freq = fgGetDouble("/instrumentation/comm[0]/frequencies/selected-mhz");
}
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
// vor/loc typical sensitivity between -107 and -101 dBm
// glideslope sensitivity between -85 and -81 dBm
if ( _propagation_model == 1) {
return LOS_calculate_attenuation(tx_pos, freq, 1);
}
else if ( _propagation_model == 2) {
return ITM_calculate_attenuation(tx_pos, freq, 1);
}
return -1;
}
/*** Receive ATC radio communication as text
***/
void FGRadioTransmission::receiveATC(SGGeod tx_pos, double freq, string text, int ground_to_air) {
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) ) {
return;
}
else {
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
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
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
* 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
* the workaround below is more akin to what would happen on a FM transmission
* therefore the correct way would be to work on the volume
**/
/*
string hash_noise = " ";
int reps = (int) (fabs(floor(signal - 11.0)) * 2);
int t_size = text.size();
for (int n = 1; n <= reps; ++n) {
int pos = rand() % (t_size -1);
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);
fgSetString("/sim/messages/atc", text.c_str());
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) {
/** ITM default parameters
TODO: take them from tile materials (especially for sea)?
**/
double eps_dielect=15.0;
double sgm_conductivity = 0.005;
double eno = 301.0;
double frq_mhz;
if( (freq < 118.0) || (freq > 137.0) )
frq_mhz = 125.0; // sane value, middle of bandplan
else
frq_mhz = freq;
int radio_climate = 5; // continental temperate
int pol= _polarization;
double conf = 0.90; // 90% of situations and time, take into account speed
double rel = 0.90;
double dbloss;
char strmode[150];
int p_mode = 0; // propgation mode selector: 0 LOS, 1 diffraction dominant, 2 troposcatter
double horizons[2];
int errnum;
double clutter_loss = 0.0; // loss due to vegetation and urban
double tx_pow = _transmitter_power;
double ant_gain = _rx_antenna_gain + _tx_antenna_gain;
double signal = 0.0;
double link_budget = tx_pow - _receiver_sensitivity - _rx_line_losses - _tx_line_losses + ant_gain;
double signal_strength = tx_pow - _rx_line_losses - _tx_line_losses + ant_gain;
double tx_erp = dbm_to_watt(tx_pow + _tx_antenna_gain - _tx_line_losses);
FGScenery * scenery = globals->get_scenery();
double own_lat = fgGetDouble("/position/latitude-deg");
double own_lon = fgGetDouble("/position/longitude-deg");
double own_alt_ft = fgGetDouble("/position/altitude-ft");
double own_alt= own_alt_ft * SG_FEET_TO_METER;
//cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
SGGeod max_own_pos = SGGeod::fromDegM( own_lon, own_lat, SG_MAX_ELEVATION_M );
SGGeoc center = SGGeoc::fromGeod( max_own_pos );
SGGeoc own_pos_c = SGGeoc::fromGeod( own_pos );
double sender_alt_ft,sender_alt;
double transmitter_height=0.0;
double receiver_height=0.0;
SGGeod sender_pos = pos;
sender_alt_ft = sender_pos.getElevationFt();
sender_alt = sender_alt_ft * SG_FEET_TO_METER;
SGGeod max_sender_pos = SGGeod::fromGeodM( pos, SG_MAX_ELEVATION_M );
SGGeoc sender_pos_c = SGGeoc::fromGeod( sender_pos );
//cerr << "ITM:: sender Lat: " << parent->getLatitude() << ", Lon: " << parent->getLongitude() << ", Alt: " << sender_alt << endl;
double point_distance= _terrain_sampling_distance;
double course = SGGeodesy::courseRad(own_pos_c, sender_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_m > 300000)
return -1.0;
/** If above 8000 meters, consider LOS mode and calculate free-space att */
if (own_alt > 8000) {
dbloss = 20 * log10(distance_m) +20 * log10(frq_mhz) -27.55;
SG_LOG(SG_GENERAL, SG_BULK,
"ITM Free-space mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation");
//cerr << "ITM Free-space mode:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, free-space attenuation" << endl;
signal = link_budget - dbloss;
return signal;
}
int max_points = (int)floor(distance_m / point_distance);
double delta_last = fmod(distance_m, point_distance);
deque<double> _elevations;
deque<string> materials;
double elevation_under_pilot = 0.0;
if (scenery->get_elevation_m( max_own_pos, elevation_under_pilot, NULL )) {
receiver_height = own_alt - elevation_under_pilot;
}
double elevation_under_sender = 0.0;
if (scenery->get_elevation_m( max_sender_pos, elevation_under_sender, NULL )) {
transmitter_height = sender_alt - elevation_under_sender;
}
else {
transmitter_height = sender_alt;
}
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);
_root_node->setDoubleValue("station[0]/distance", distance_m / 1000);
unsigned int e_size = (deque<unsigned>::size_type)max_points;
while (_elevations.size() <= e_size) {
probe_distance += point_distance;
SGGeod probe = SGGeod::fromGeoc(center.advanceRadM( course, probe_distance ));
const SGMaterial *mat = 0;
double elevation_m = 0.0;
if (scenery->get_elevation_m( probe, elevation_m, &mat )) {
if((transmission_type == 3) || (transmission_type == 4)) {
_elevations.push_back(elevation_m);
if(mat) {
const std::vector<string> mat_names = mat->get_names();
materials.push_back(mat_names[0]);
}
else {
materials.push_back("None");
}
}
else {
_elevations.push_front(elevation_m);
if(mat) {
const std::vector<string> mat_names = mat->get_names();
materials.push_front(mat_names[0]);
}
else {
materials.push_front("None");
}
}
}
else {
if((transmission_type == 3) || (transmission_type == 4)) {
_elevations.push_back(0.0);
materials.push_back("None");
}
else {
_elevations.push_front(0.0);
materials.push_front("None");
}
}
}
if((transmission_type == 3) || (transmission_type == 4)) {
_elevations.push_front(elevation_under_pilot);
if (delta_last > (point_distance / 2) ) // only add last point if it's farther than half point_distance
_elevations.push_back(elevation_under_sender);
}
else {
_elevations.push_back(elevation_under_pilot);
if (delta_last > (point_distance / 2) )
_elevations.push_front(elevation_under_sender);
}
double num_points= (double)_elevations.size();
_elevations.push_front(point_distance);
_elevations.push_front(num_points -1);
int size = _elevations.size();
double itm_elev[size];
for(int i=0;i<size;i++) {
itm_elev[i]=_elevations[i];
//cerr << "ITM:: itm_elev: " << _elevations[i] << endl;
}
if((transmission_type == 3) || (transmission_type == 4)) {
// the sender and receiver roles are switched
point_to_point(itm_elev, receiver_height, transmitter_height,
eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
pol, conf, rel, dbloss, strmode, p_mode, horizons, errnum);
if( _root_node->getBoolValue( "use-clutter-attenuation", false ) )
clutterLoss(frq_mhz, distance_m, itm_elev, materials, receiver_height, transmitter_height, p_mode, horizons, clutter_loss);
}
else {
point_to_point(itm_elev, transmitter_height, receiver_height,
eps_dielect, sgm_conductivity, eno, frq_mhz, radio_climate,
pol, conf, rel, dbloss, strmode, p_mode, horizons, errnum);
if( _root_node->getBoolValue( "use-clutter-attenuation", false ) )
clutterLoss(frq_mhz, distance_m, itm_elev, materials, transmitter_height, receiver_height, p_mode, horizons, clutter_loss);
}
double pol_loss = 0.0;
if (_polarization == 1) {
pol_loss = polarization_loss();
}
SG_LOG(SG_GENERAL, SG_BULK,
"ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum);
//cerr << "ITM:: Link budget: " << link_budget << ", Attenuation: " << dbloss << " dBm, " << strmode << ", Error: " << errnum << endl;
_root_node->setDoubleValue("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);
//cerr << "Clutter loss: " << clutter_loss << endl;
//if (errnum == 4) // if parameters are outside sane values for lrprop, the alternative method is used
// return -1;
signal = link_budget - dbloss - clutter_loss + pol_loss;
double signal_strength_dbm = signal_strength - dbloss - clutter_loss + pol_loss;
double field_strength_uV = dbm_to_microvolt(signal_strength_dbm);
_root_node->setDoubleValue("station[0]/signal-dbm", signal_strength_dbm);
_root_node->setDoubleValue("station[0]/field-strength-uV", field_strength_uV);
_root_node->setDoubleValue("station[0]/signal", signal);
_root_node->setDoubleValue("station[0]/tx-erp", tx_erp);
return signal;
}
/*** 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::clutterLoss(double freq, double distance_m, double itm_elev[], deque<string> materials,
double transmitter_height, double receiver_height, int p_mode,
double horizons[], double &clutter_loss) {
distance_m = itm_elev[0] * itm_elev[1]; // only consider elevation points
if (p_mode == 0) { // LOS: take each point and see how clutter height affects first Fresnel zone
int mat = 0;
int j=1;
for (int k=3;k < (int)(itm_elev[0]) + 2;k++) {
double clutter_height = 0.0; // mean clutter height for a certain terrain type
double clutter_density = 0.0; // percent of reflected wave
get_material_properties(materials[mat], clutter_height, clutter_density);
double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
// First Fresnel radius
double frs_rad = 548 * sqrt( (j * itm_elev[1] * (itm_elev[0] - j) * itm_elev[1] / 1000000) / ( distance_m * freq / 1000) );
//double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
double d1 = j * itm_elev[1];
if ((itm_elev[2] + transmitter_height) > ( itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) {
d1 = (itm_elev[0] - j) * itm_elev[1];
}
double ray_height = (grad * d1) + min_elev;
double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
double intrusion = fabs(clearance);
if (clearance >= 0) {
// no losses
}
else if (clearance < 0 && (intrusion < clutter_height)) {
clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
}
else if (clearance < 0 && (intrusion > clutter_height)) {
clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
}
else {
// no losses
}
j++;
mat++;
}
}
else if (p_mode == 1) { // diffraction
if (horizons[1] == 0.0) { // single horizon: same as above, except pass twice using the highest point
int num_points_1st = (int)floor( horizons[0] * itm_elev[0]/ distance_m );
int num_points_2nd = (int)ceil( (distance_m - horizons[0]) * itm_elev[0] / distance_m );
//cerr << "Diffraction 1 horizon:: points1: " << num_points_1st << " points2: " << num_points_2nd << endl;
int last = 1;
/** perform the first pass */
int mat = 0;
int j=1;
for (int k=3;k < num_points_1st + 2;k++) {
if (num_points_1st < 1)
break;
double clutter_height = 0.0; // mean clutter height for a certain terrain type
double clutter_density = 0.0; // percent of reflected wave
get_material_properties(materials[mat], clutter_height, clutter_density);
double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[num_points_1st + 2] + clutter_height) / distance_m;
// First Fresnel radius
double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_1st - j) * itm_elev[1] / 1000000) / ( num_points_1st * itm_elev[1] * freq / 1000) );
//double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[num_points_1st + 2] + clutter_height);
double d1 = j * itm_elev[1];
if ( (itm_elev[2] + transmitter_height) > (itm_elev[num_points_1st + 2] + clutter_height) ) {
d1 = (num_points_1st - j) * itm_elev[1];
}
double ray_height = (grad * d1) + min_elev;
double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
double intrusion = fabs(clearance);
if (clearance >= 0) {
// no losses
}
else if (clearance < 0 && (intrusion < clutter_height)) {
clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
}
else if (clearance < 0 && (intrusion > clutter_height)) {
clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
}
else {
// no losses
}
j++;
mat++;
last = k;
}
/** and the second pass */
mat +=1;
j =1; // first point is diffraction edge, 2nd the RX elevation
for (int k=last+2;k < (int)(itm_elev[0]) + 2;k++) {
if (num_points_2nd < 1)
break;
double clutter_height = 0.0; // mean clutter height for a certain terrain type
double clutter_density = 0.0; // percent of reflected wave
get_material_properties(materials[mat], clutter_height, clutter_density);
double grad = fabs(itm_elev[last+1] + clutter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
// First Fresnel radius
double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_2nd - j) * itm_elev[1] / 1000000) / ( num_points_2nd * itm_elev[1] * freq / 1000) );
//double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
double min_elev = SGMiscd::min(itm_elev[last+1] + clutter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
double d1 = j * itm_elev[1];
if ( (itm_elev[last+1] + clutter_height) > (itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) {
d1 = (num_points_2nd - j) * itm_elev[1];
}
double ray_height = (grad * d1) + min_elev;
double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
double intrusion = fabs(clearance);
if (clearance >= 0) {
// no losses
}
else if (clearance < 0 && (intrusion < clutter_height)) {
clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
}
else if (clearance < 0 && (intrusion > clutter_height)) {
clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
}
else {
// no losses
}
j++;
mat++;
}
}
else { // double horizon: same as single horizon, except there are 3 segments
int num_points_1st = (int)floor( horizons[0] * itm_elev[0] / distance_m );
int num_points_2nd = (int)floor(horizons[1] * itm_elev[0] / distance_m );
int num_points_3rd = (int)itm_elev[0] - num_points_1st - num_points_2nd;
//cerr << "Double horizon:: horizon1: " << horizons[0] << " horizon2: " << horizons[1] << " distance: " << distance_m << endl;
//cerr << "Double horizon:: points1: " << num_points_1st << " points2: " << num_points_2nd << " points3: " << num_points_3rd << endl;
int last = 1;
/** perform the first pass */
int mat = 0;
int j=1; // first point is TX elevation, 2nd is obstruction elevation
for (int k=3;k < num_points_1st +2;k++) {
if (num_points_1st < 1)
break;
double clutter_height = 0.0; // mean clutter height for a certain terrain type
double clutter_density = 0.0; // percent of reflected wave
get_material_properties(materials[mat], clutter_height, clutter_density);
double grad = fabs(itm_elev[2] + transmitter_height - itm_elev[num_points_1st + 2] + clutter_height) / distance_m;
// First Fresnel radius
double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_1st - j) * itm_elev[1] / 1000000) / ( num_points_1st * itm_elev[1] * freq / 1000) );
//double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
double min_elev = SGMiscd::min(itm_elev[2] + transmitter_height, itm_elev[num_points_1st + 2] + clutter_height);
double d1 = j * itm_elev[1];
if ( (itm_elev[2] + transmitter_height) > (itm_elev[num_points_1st + 2] + clutter_height) ) {
d1 = (num_points_1st - j) * itm_elev[1];
}
double ray_height = (grad * d1) + min_elev;
double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
double intrusion = fabs(clearance);
if (clearance >= 0) {
// no losses
}
else if (clearance < 0 && (intrusion < clutter_height)) {
clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
}
else if (clearance < 0 && (intrusion > clutter_height)) {
clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
}
else {
// no losses
}
j++;
last = k;
}
mat +=1;
/** and the second pass */
int last2=1;
j =1; // first point is 1st obstruction elevation, 2nd is 2nd obstruction elevation
for (int k=last+2;k < num_points_1st + num_points_2nd +2;k++) {
if (num_points_2nd < 1)
break;
double clutter_height = 0.0; // mean clutter height for a certain terrain type
double clutter_density = 0.0; // percent of reflected wave
get_material_properties(materials[mat], clutter_height, clutter_density);
double grad = fabs(itm_elev[last+1] + clutter_height - itm_elev[num_points_1st + num_points_2nd + 2] + clutter_height) / distance_m;
// First Fresnel radius
double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_2nd - j) * itm_elev[1] / 1000000) / ( num_points_2nd * itm_elev[1] * freq / 1000) );
//double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
double min_elev = SGMiscd::min(itm_elev[last+1] + clutter_height, itm_elev[num_points_1st + num_points_2nd +2] + clutter_height);
double d1 = j * itm_elev[1];
if ( (itm_elev[last+1] + clutter_height) > (itm_elev[num_points_1st + num_points_2nd + 2] + clutter_height) ) {
d1 = (num_points_2nd - j) * itm_elev[1];
}
double ray_height = (grad * d1) + min_elev;
double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
double intrusion = fabs(clearance);
if (clearance >= 0) {
// no losses
}
else if (clearance < 0 && (intrusion < clutter_height)) {
clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
}
else if (clearance < 0 && (intrusion > clutter_height)) {
clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
}
else {
// no losses
}
j++;
mat++;
last2 = k;
}
/** third and final pass */
mat +=1;
j =1; // first point is 2nd obstruction elevation, 3rd is RX elevation
for (int k=last2+2;k < (int)itm_elev[0] + 2;k++) {
if (num_points_3rd < 1)
break;
double clutter_height = 0.0; // mean clutter height for a certain terrain type
double clutter_density = 0.0; // percent of reflected wave
get_material_properties(materials[mat], clutter_height, clutter_density);
double grad = fabs(itm_elev[last2+1] + clutter_height - itm_elev[(int)itm_elev[0] + 2] + receiver_height) / distance_m;
// First Fresnel radius
double frs_rad = 548 * sqrt( (j * itm_elev[1] * (num_points_3rd - j) * itm_elev[1] / 1000000) / ( num_points_3rd * itm_elev[1] * freq / 1000) );
//double earth_h = distance_m * (distance_m - j * itm_elev[1]) / ( 1000000 * 12.75 * 1.33 ); // K=4/3
double min_elev = SGMiscd::min(itm_elev[last2+1] + clutter_height, itm_elev[(int)itm_elev[0] + 2] + receiver_height);
double d1 = j * itm_elev[1];
if ( (itm_elev[last2+1] + clutter_height) > (itm_elev[(int)itm_elev[0] + 2] + receiver_height) ) {
d1 = (num_points_3rd - j) * itm_elev[1];
}
double ray_height = (grad * d1) + min_elev;
double clearance = ray_height - (itm_elev[k] + clutter_height) - frs_rad * 8/10;
double intrusion = fabs(clearance);
if (clearance >= 0) {
// no losses
}
else if (clearance < 0 && (intrusion < clutter_height)) {
clutter_loss += clutter_density * (intrusion / (frs_rad * 2) ) * (freq/100) * (itm_elev[1]/100);
}
else if (clearance < 0 && (intrusion > clutter_height)) {
clutter_loss += clutter_density * (clutter_height / (frs_rad * 2 ) ) * (freq/100) * (itm_elev[1]/100);
}
else {
// no losses
}
j++;
mat++;
}
}
}
else if (p_mode == 2) { // troposcatter: ignore ground clutter for now...
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") {
height = 15.0;
density = 0.2;
}
else if(mat_name == "SomeSort") {
height = 15.0;
density = 0.2;
}
else if(mat_name == "Island") {
height = 15.0;
density = 0.2;
}
else if(mat_name == "Default") {
height = 15.0;
density = 0.2;
}
else if(mat_name == "EvergreenBroadCover") {
height = 20.0;
density = 0.2;
}
else if(mat_name == "EvergreenForest") {
height = 20.0;
density = 0.2;
}
else if(mat_name == "DeciduousBroadCover") {
height = 15.0;
density = 0.3;
}
else if(mat_name == "DeciduousForest") {
height = 15.0;
density = 0.3;
}
else if(mat_name == "MixedForestCover") {
height = 20.0;
density = 0.25;
}
else if(mat_name == "MixedForest") {
height = 15.0;
density = 0.25;
}
else if(mat_name == "RainForest") {
height = 25.0;
density = 0.55;
}
else if(mat_name == "EvergreenNeedleCover") {
height = 15.0;
density = 0.2;
}
else if(mat_name == "WoodedTundraCover") {
height = 5.0;
density = 0.15;
}
else if(mat_name == "DeciduousNeedleCover") {
height = 5.0;
density = 0.2;
}
else if(mat_name == "ScrubCover") {
height = 3.0;
density = 0.15;
}
else if(mat_name == "BuiltUpCover") {
height = 30.0;
density = 0.7;
}
else if(mat_name == "Urban") {
height = 30.0;
density = 0.7;
}
else if(mat_name == "Construction") {
height = 30.0;
density = 0.7;
}
else if(mat_name == "Industrial") {
height = 30.0;
density = 0.7;
}
else if(mat_name == "Port") {
height = 30.0;
density = 0.7;
}
else if(mat_name == "Town") {
height = 10.0;
density = 0.5;
}
else if(mat_name == "SubUrban") {
height = 10.0;
density = 0.5;
}
else if(mat_name == "CropWoodCover") {
height = 10.0;
density = 0.1;
}
else if(mat_name == "CropWood") {
height = 10.0;
density = 0.1;
}
else if(mat_name == "AgroForest") {
height = 10.0;
density = 0.1;
}
else {
height = 0.0;
density = 0.0;
}
}
/*** 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 dbloss;
double tx_pow = _transmitter_power;
double ant_gain = _rx_antenna_gain + _tx_antenna_gain;
double signal = 0.0;
double sender_alt_ft,sender_alt;
double transmitter_height=0.0;
double receiver_height=0.0;
double own_lat = fgGetDouble("/position/latitude-deg");
double own_lon = fgGetDouble("/position/longitude-deg");
double own_alt_ft = fgGetDouble("/position/altitude-ft");
double own_alt= own_alt_ft * SG_FEET_TO_METER;
double link_budget = tx_pow - _receiver_sensitivity - _rx_line_losses - _tx_line_losses + ant_gain;
//cerr << "ITM:: pilot Lat: " << own_lat << ", Lon: " << own_lon << ", Alt: " << own_alt << endl;
SGGeod own_pos = SGGeod::fromDegM( own_lon, own_lat, own_alt );
SGGeod sender_pos = pos;
sender_alt_ft = sender_pos.getElevationFt();
sender_alt = sender_alt_ft * SG_FEET_TO_METER;
receiver_height = own_alt;
transmitter_height = sender_alt;
double distance_m = SGGeodesy::distanceM(own_pos, sender_pos);
transmitter_height += _tx_antenna_height;
receiver_height += _rx_antenna_height;
/** radio horizon calculation with wave bending k=4/3 */
double receiver_horizon = 4.12 * sqrt(receiver_height);
double transmitter_horizon = 4.12 * sqrt(transmitter_height);
double total_horizon = receiver_horizon + transmitter_horizon;
if (distance_m > total_horizon) {
return -1;
}
double pol_loss = 0.0;
if (_polarization == 1) {
pol_loss = polarization_loss();
}
// 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;
}
/*** calculate loss due to polarization mismatch
* this function is only reliable for vertical polarization
* due to the V-shape of horizontally polarized antennas
***/
double FGRadioTransmission::polarization_loss() {
double theta_deg;
double roll = fgGetDouble("/orientation/roll-deg");
if (fabs(roll) > 85.0)
roll = 85.0;
double pitch = fgGetDouble("/orientation/pitch-deg");
if (fabs(pitch) > 85.0)
pitch = 85.0;
double theta = fabs( atan( sqrt(
pow(tan(roll * SGD_DEGREES_TO_RADIANS), 2) +
pow(tan(pitch * SGD_DEGREES_TO_RADIANS), 2) )) * SGD_RADIANS_TO_DEGREES);
if (_polarization == 0)
theta_deg = 90.0 - theta;
else
theta_deg = theta;
if (theta_deg > 85.0) // we don't want to converge into infinity
theta_deg = 85.0;
double loss = 10 * log10( pow(cos(theta_deg * SGD_DEGREES_TO_RADIANS), 2) );
//cerr << "Polarization loss: " << loss << " dBm " << endl;
return loss;
}
double FGRadioTransmission::watt_to_dbm(double power_watt) {
return 10 * log10(1000 * power_watt); // returns dbm
}
double FGRadioTransmission::dbm_to_watt(double dbm) {
return exp( (dbm-30) * log(10) / 10); // returns Watts
}
double FGRadioTransmission::dbm_to_microvolt(double dbm) {
return sqrt(dbm_to_watt(dbm) * 50) * 1000000; // returns microvolts
}

97
src/Radio/radio.hxx Normal file
View file

@ -0,0 +1,97 @@
// radio.hxx -- FGRadio: class to manage radio propagation
//
// Written by Adrian Musceac, started August 2011.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#ifndef __cplusplus
# error This library requires C++
#endif
#include <simgear/compiler.h>
#include <simgear/structure/subsystem_mgr.hxx>
#include <deque>
#include <Main/fg_props.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/debug/logstream.hxx>
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;
double _tx_antenna_height;
double _rx_antenna_height;
double _rx_antenna_gain;
double _tx_antenna_gain;
double _rx_line_losses;
double _tx_line_losses;
double _terrain_sampling_distance;
int _polarization;
std::map<string, double[2]> _mat_database;
SGPropertyNode *_root_node;
int _propagation_model; /// 0 none, 1 round Earth, 2 ITM
double polarization_loss();
double ITM_calculate_attenuation(SGGeod tx_pos, double freq, int ground_to_air);
double LOS_calculate_attenuation(SGGeod tx_pos, double freq, int ground_to_air);
void clutterLoss(double freq, double distance_m, double itm_elev[], std::deque<string> materials,
double transmitter_height, double receiver_height, int p_mode,
double horizons[], double &clutter_loss);
void get_material_properties(string mat_name, double &height, double &density);
public:
FGRadioTransmission();
~FGRadioTransmission();
// a couple of setters and getters for convenience
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);
// 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
void receiveATC(SGGeod tx_pos, double freq, string text, int transmission_type);
void receiveChat(SGGeod tx_pos, double freq, string text, int transmission_type);
// returns signal quality
// transmission_type: 0 for VOR, 1 for ILS
double receiveNav(SGGeod tx_pos, double freq, int transmission_type);
};