]> git.mxchange.org Git - flightgear.git/blobdiff - src/Radio/antenna.cxx
Expose a radio function (receiveBeacon) to the Nasal subsystem
[flightgear.git] / src / Radio / antenna.cxx
index e3909103bd0dd2e958526213e0a7df51dec09f60..2be67f351132458790b2ea12df0044bd429c553a 100644 (file)
 // 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 <iostream>
+#include <stdlib.h>
+#include <fstream>
+#include <Scenery/scenery.hxx>
+#include "antenna.hxx"
+
+using namespace std;
+
+FGRadioAntenna::FGRadioAntenna(string type) {
+       
+       _mirror_y = 1;  // normally we want to mirror these axis because the pattern is simetric
+       _mirror_z = 1;
+       _invert_ground = 0;             
+       load_antenna_pattern(type);
+}
+
+FGRadioAntenna::~FGRadioAntenna() {
+       for (unsigned i =0; i < _pattern.size(); i++) {
+               AntennaGain *point_gain = _pattern[i];
+               delete point_gain;
+       }
+       _pattern.clear();
+       
+}
+
+double FGRadioAntenna::calculate_gain(double bearing, double angle) {
+       
+       // TODO: what if the pattern is assimetric?
+       bearing = fabs(bearing);
+       if (bearing > 180)
+               bearing = 360 - bearing;
+       // for plots with 2 degrees resolution:
+       int azimuth = (int)floor(bearing);
+       azimuth += azimuth % 2;
+       int elevation = (int)floor(angle);
+       elevation += elevation % 2;
+       cerr << "Bearing: " << bearing << " angle: " << angle << " azimuth: " << azimuth << " elevation: " << elevation << endl;
+       for (unsigned i =0; i < _pattern.size(); i++) {
+               AntennaGain *point_gain = _pattern[i];
+               
+               if ( (azimuth == point_gain->azimuth) && (elevation == point_gain->elevation)) {
+                       return point_gain->gain;
+               }
+       }
+               
+       return 0;
+}
+
+
+
+/*** load external plot file generated by NEC2
+***/
+void FGRadioAntenna::load_antenna_pattern(string type) {
+       
+       SGPath pattern_file(fgGetString("/sim/fg-home"));
+       pattern_file.append("antennas");
+       pattern_file.append(type + ".txt");
+       if (!pattern_file.exists()) {
+               return;
+       }
+       ifstream file_in(pattern_file.c_str());
+       int heading, elevation;
+       double gain;
+       while(!file_in.eof()) {
+               file_in >> heading >> elevation >> gain;
+               if( (_mirror_y == 1) && (heading > 180) ) {
+                       continue;
+               }
+               if ( (_mirror_z == 1) && (elevation < 0) ) {
+                       continue;
+               }
+               //cerr << "head: " << heading << " elev: " << elevation << " gain: " << gain << endl;
+               AntennaGain *datapoint = new AntennaGain;
+               datapoint->azimuth = heading;
+               datapoint->elevation = 90.0 - fabs(elevation);
+               datapoint->gain = gain;
+               _pattern.push_back(datapoint);
+       }
+               
+       
+}