]> git.mxchange.org Git - flightgear.git/blob - src/Radio/antenna.cxx
Read antenna radiation pattern from file
[flightgear.git] / src / Radio / antenna.cxx
1 // antenna.cxx -- implementation of FGRadioAntenna
2 // Class to represent a virtual radio antenna properties 
3 // Written by Adrian Musceac, started December 2011.
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <math.h>
25 #include <iostream>
26 #include <stdlib.h>
27 #include <fstream>
28 #include <Scenery/scenery.hxx>
29 #include "antenna.hxx"
30
31 using namespace std;
32
33 FGRadioAntenna::FGRadioAntenna(string type) {
34         
35         _mirror_y = 1;  // normally we want to mirror these axis because the pattern is simetric
36         _mirror_z = 1;
37         _invert_ground = 0;             
38         load_antenna_pattern(type);
39 }
40
41 FGRadioAntenna::~FGRadioAntenna() {
42         _pattern.clear();
43         
44 }
45
46 double FGRadioAntenna::calculate_gain(double bearing, double angle) {
47         
48         // TODO: what if the pattern is assimetric?
49         bearing = fabs(bearing);
50         if (bearing > 180)
51                 bearing = 360 - bearing;
52         // for plots with 2 degrees resolution:
53         int azimuth = (int)floor(bearing);
54         azimuth += azimuth % 2;
55         int elevation = (int)floor(angle);
56         elevation += elevation % 2;
57         
58         for (unsigned int i =0; i < _pattern.size(); i++) {
59                 AntennaGain point_gain = _pattern[i];
60                 
61                 if ( (azimuth == point_gain.azimuth) && (elevation == point_gain.elevation)) {
62                         return point_gain.gain;
63                 }
64         }
65                 
66         return 0;
67 }
68
69
70
71 /*** load external plot file generated by NEC4
72 ***/
73 void FGRadioAntenna::load_antenna_pattern(string type) {
74         
75         SGPath pattern_file(fgGetString("/sim/fg-home"));
76         pattern_file.append("antennas");
77         pattern_file.append(type + ".txt");
78         if (!pattern_file.exists()) {
79                 return;
80         }
81         ifstream file_in(pattern_file.c_str());
82         int heading, elevation;
83         double gain;
84         while(!file_in.eof()) {
85                 file_in >> heading >> elevation >> gain;
86                 if( (_mirror_y == 1) && (heading > 180) ) {
87                         continue;
88                 }
89                 if ( (_mirror_z == 1) && (elevation < 0) ) {
90                         continue;
91                 }
92                 //cerr << "head: " << heading << " elev: " << elevation << " gain: " << gain << endl;
93                 AntennaGain datapoint;
94                 datapoint.azimuth = heading;
95                 datapoint.elevation = 90.0 - fabs(elevation);
96                 datapoint.gain = gain;
97                 _pattern.push_back(datapoint);
98         }
99                 
100         
101 }