]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/gps.cxx
3f65e3e949932e6f5a918cec40b603887ef702b5
[flightgear.git] / src / Instrumentation / gps.cxx
1 // gps.cxx - distance-measuring equipment.
2 // Written by David Megginson, started 2003.
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 <simgear/compiler.h>
11
12 #include <string.h>
13
14 #include STL_STRING
15
16 #include <Aircraft/aircraft.hxx>
17 #include <FDM/flight.hxx>
18 #include <Controls/controls.hxx>
19 #include <Scenery/scenery.hxx>
20
21 #include <simgear/constants.h>
22 #include <simgear/sg_inlines.h>
23 #include <simgear/debug/logstream.hxx>
24 #include <simgear/math/sg_geodesy.hxx>
25 #include <simgear/misc/sg_path.hxx>
26 #include <simgear/route/route.hxx>
27
28 #include <Airports/simple.hxx>
29 #include <GUI/gui.h>
30 #include <Main/fg_init.hxx>
31 #include <Main/globals.hxx>
32 #include <Main/fg_props.hxx>
33 #include <Main/util.hxx>
34 #include <Navaids/fixlist.hxx>
35 #include <Navaids/navlist.hxx>
36
37 #include "gps.hxx"
38
39 SG_USING_STD(string);
40
41
42 GPS::GPS ()
43     : _last_valid(false),
44       _last_longitude_deg(0),
45       _last_latitude_deg(0),
46       _last_altitude_m(0),
47       _last_speed_kts(0)
48 {
49 }
50
51 GPS::~GPS ()
52 {
53 }
54
55 void
56 GPS::init ()
57 {
58     _longitude_node = fgGetNode("/position/longitude-deg", true);
59     _latitude_node = fgGetNode("/position/latitude-deg", true);
60     _altitude_node = fgGetNode("/position/altitude-ft", true);
61     _magvar_node = fgGetNode("/environment/magnetic-variation-deg", true);
62     _serviceable_node = fgGetNode("/instrumentation/gps/serviceable", true);
63     _electrical_node = fgGetNode("/systems/electrical/outputs/gps", true);
64     _wp_longitude_node = 
65         fgGetNode("/instrumentation/gps/wp-longitude-deg", true);
66     _wp_latitude_node =
67         fgGetNode("/instrumentation/gps/wp-latitude-deg", true);
68     _wp_ID_node =
69         fgGetNode("/instrumentation/gps/wp-ID", true);
70     _wp_name_node =
71         fgGetNode("/instrumentation/gps/wp-name", true);
72     _wp_course_node = 
73         fgGetNode("/instrumentation/gps/indicated-course-deg", true);
74     _get_nearest_airport_node =
75       fgGetNode("/instrumentation/gps/get-nearest-airport", true);
76     _waypoint_type_node =
77       fgGetNode("/instrumentation/gps/waypoint-type", true);
78
79     _raim_node = fgGetNode("/instrumentation/gps/raim", true);
80     _indicated_longitude_node =
81         fgGetNode("/instrumentation/gps/indicated-longitude-deg", true);
82     _indicated_latitude_node =
83         fgGetNode("/instrumentation/gps/indicated-latitude-deg", true);
84     _indicated_altitude_node =
85         fgGetNode("/instrumentation/gps/indicated-altitude-ft", true);
86     _true_track_node =
87         fgGetNode("/instrumentation/gps/indicated-track-true-deg", true);
88     _magnetic_track_node =
89         fgGetNode("/instrumentation/gps/indicated-track-magnetic-deg", true);
90     _speed_node =
91         fgGetNode("/instrumentation/gps/indicated-ground-speed-kt", true);
92     _wp_distance_node =
93         fgGetNode("/instrumentation/gps/wp-distance-nm", true);
94     _wp_ttw_node =
95         fgGetNode("/instrumentation/gps/TTW",true);
96     _wp_bearing_node =
97         fgGetNode("/instrumentation/gps/wp-bearing-deg", true);
98     _wp_course_deviation_node =
99         fgGetNode("/instrumentation/gps/course-deviation-deg", true);
100     _wp_course_error_nm_node =
101         fgGetNode("/instrumentation/gps/course-error-nm", true);
102     _wp_to_flag_node =
103         fgGetNode("/instrumentation/gps/to-flag", true);
104     _odometer_node =
105         fgGetNode("/instrumentation/gps/odometer", true);
106     _trip_odometer_node =
107       fgGetNode("/instrumentation/gps/trip-odometer", true);
108 }
109
110 void
111 GPS::update (double delta_time_sec)
112 {
113                                 // If it's off, don't bother.
114     if (!_serviceable_node->getBoolValue() ||
115         !_electrical_node->getBoolValue()) {
116         _last_valid = false;
117         _last_longitude_deg = 0;
118         _last_latitude_deg = 0;
119         _last_altitude_m = 0;
120         _last_speed_kts = 0;
121         _raim_node->setDoubleValue(false);
122         _indicated_longitude_node->setDoubleValue(0);
123         _indicated_latitude_node->setDoubleValue(0);
124         _indicated_altitude_node->setDoubleValue(0);
125         _true_track_node->setDoubleValue(0);
126         _magnetic_track_node->setDoubleValue(0);
127         _speed_node->setDoubleValue(0);
128         _wp_distance_node->setDoubleValue(0);
129         _wp_bearing_node->setDoubleValue(0);
130         _wp_longitude_node->setDoubleValue(0);
131         _wp_latitude_node->setDoubleValue(0);
132         _wp_course_node->setDoubleValue(0);
133         _odometer_node->setDoubleValue(0);
134         _trip_odometer_node->setDoubleValue(0);
135         return;
136     }
137
138                                 // Get the aircraft position
139     double longitude_deg = _longitude_node->getDoubleValue();
140     double latitude_deg = _latitude_node->getDoubleValue();
141     double altitude_m = _altitude_node->getDoubleValue() * SG_FEET_TO_METER;
142     double magvar_deg = _magvar_node->getDoubleValue();
143
144     double speed_kt;
145
146     _raim_node->setBoolValue(true);
147     _indicated_longitude_node->setDoubleValue(longitude_deg);
148     _indicated_latitude_node->setDoubleValue(latitude_deg);
149     _indicated_altitude_node->setDoubleValue(altitude_m * SG_METER_TO_FEET);
150
151     if (_last_valid) {
152         double track1_deg, track2_deg, distance_m, odometer;
153         geo_inverse_wgs_84(altitude_m,
154                            _last_latitude_deg, _last_longitude_deg,
155                            latitude_deg, longitude_deg,
156                            &track1_deg, &track2_deg, &distance_m);
157         speed_kt = ((distance_m * SG_METER_TO_NM) *
158                     ((1 / delta_time_sec) * 3600.0));
159         _true_track_node->setDoubleValue(track1_deg);
160         _magnetic_track_node->setDoubleValue(track1_deg - magvar_deg);
161         speed_kt = fgGetLowPass(_last_speed_kts, speed_kt, delta_time_sec/20.0);
162         _last_speed_kts = speed_kt;
163         _speed_node->setDoubleValue(speed_kt);
164
165         odometer = _odometer_node->getDoubleValue();
166         _odometer_node->setDoubleValue(odometer + distance_m * SG_METER_TO_NM);
167         odometer = _trip_odometer_node->getDoubleValue();
168         _trip_odometer_node->setDoubleValue(odometer + distance_m * SG_METER_TO_NM);
169
170         // Get waypoint position
171         double wp_longitude_deg = _wp_longitude_node->getDoubleValue();
172         double wp_latitude_deg = _wp_latitude_node->getDoubleValue();
173         double wp_course_deg = 
174           _wp_course_node->getDoubleValue();
175         double wp_distance, wp_bearing_deg, wp_course_deviation_deg,
176             wp_course_error_m, wp_TTW;
177         // double wp_actual_radial_deg;
178         string wp_ID = _wp_ID_node->getStringValue();
179
180         // If the get-nearest-airport-node is true.
181         // Get the nearest airport, and set it as waypoint.
182         if (_get_nearest_airport_node->getBoolValue()) {
183           FGAirport a;
184           cout << "Airport found" << endl;
185           a = globals->get_airports()->search(longitude_deg, latitude_deg, false);
186           _wp_ID_node->setStringValue(a.id.c_str());
187           wp_longitude_deg = a.longitude;
188           wp_latitude_deg = a.latitude;
189           _wp_name_node->setStringValue(a.name.c_str());
190           _get_nearest_airport_node->setBoolValue(false);
191           _last_wp_ID = wp_ID = a.id.c_str();
192         }
193
194         // If the waypoint ID has changed, try to find the new ID
195         // in the airport-, fix-, nav-database.
196         if ( !(_last_wp_ID == wp_ID) ) {
197           string waypont_type =
198             _waypoint_type_node->getStringValue();
199           if (waypont_type == "airport") {
200             FGAirport a;
201             a = globals->get_airports()->search( wp_ID );
202             if ( a.id == wp_ID ) {
203               cout << "Airport found" << endl;
204               wp_longitude_deg = a.longitude;
205               wp_latitude_deg = a.latitude;
206               _wp_name_node->setStringValue(a.name.c_str());
207             }
208           }
209           else if (waypont_type == "nav") {
210             FGNav * n;
211             if ( (n = current_navlist->findByIdent(wp_ID.c_str(), 
212                                                       longitude_deg, 
213                                                       latitude_deg)) != NULL) {
214               cout << "Nav found" << endl;
215               wp_longitude_deg = n->get_lon();
216               wp_latitude_deg = n->get_lat();
217               _wp_name_node->setStringValue(n->get_name().c_str());
218             }
219           }
220           else if (waypont_type == "fix") {
221             FGFix f;
222             if ( current_fixlist->query(wp_ID, &f) ) {
223               cout << "Fix found" << endl;
224               wp_longitude_deg = f.get_lon();
225               wp_latitude_deg = f.get_lat();
226               _wp_name_node->setStringValue(wp_ID.c_str());
227             }
228           }
229           _last_wp_ID = wp_ID;
230         }
231
232         // Find the bearing and distance to the waypoint.
233         SGWayPoint wp(wp_longitude_deg, wp_latitude_deg, altitude_m);
234         wp.CourseAndDistance(longitude_deg, latitude_deg, altitude_m,
235                             &wp_bearing_deg, &wp_distance);
236         _wp_longitude_node->setDoubleValue(wp_longitude_deg);
237         _wp_latitude_node->setDoubleValue(wp_latitude_deg);
238         _wp_distance_node->setDoubleValue(wp_distance * SG_METER_TO_NM);
239         _wp_bearing_node->setDoubleValue(wp_bearing_deg);
240         
241         // Estimate time to waypoint.
242         // The estimation does not take track into consideration,
243         // so if you are going away from the waypoint the TTW will
244         // increase. Makes most sense when travelling directly towards
245         // the waypoint.
246         if (speed_kt > 0.0 && wp_distance > 0.0) {
247           wp_TTW = (wp_distance * SG_METER_TO_NM) / (speed_kt / 3600);
248         }
249         else {
250           wp_TTW = 0.0;
251         }
252         unsigned int wp_TTW_seconds = (int) (wp_TTW + 0.5);
253         if (wp_TTW_seconds < 356400) { // That's 99 hours
254           unsigned int wp_TTW_minutes = 0;
255           unsigned int wp_TTW_hours   = 0;
256           char wp_TTW_str[8];
257           while (wp_TTW_seconds >= 3600) {
258             wp_TTW_seconds -= 3600;
259             wp_TTW_hours++;
260           }
261           while (wp_TTW_seconds >= 60) {
262             wp_TTW_seconds -= 60;
263             wp_TTW_minutes++;
264           }
265           sprintf(wp_TTW_str, "%02d:%02d:%02d",
266             wp_TTW_hours, wp_TTW_minutes, wp_TTW_seconds);
267           _wp_ttw_node->setStringValue(wp_TTW_str);
268         }
269         else
270           _wp_ttw_node->setStringValue("--:--:--");
271
272         // Course deviation is the diffenrence between the bearing
273         // and the course.
274         wp_course_deviation_deg = wp_bearing_deg -
275           wp_course_deg;
276         while (wp_course_deviation_deg < -180.0) {
277           wp_course_deviation_deg += 360.0; }
278         while (wp_course_deviation_deg > 180.0) {
279           wp_course_deviation_deg -= 360.0; }
280
281         // If the course deviation is less than 90 degrees to either side,
282         // our desired course is towards the waypoint.
283         // It does not matter if we are actually moving towards or from the waypoint.
284         if (fabs(wp_course_deviation_deg) < 90.0) {
285             _wp_to_flag_node->setBoolValue(true); }
286         // If it's more than 90 degrees the desired course is from the waypoint.
287         else if (fabs(wp_course_deviation_deg) > 90.0) {
288           _wp_to_flag_node->setBoolValue(false);
289           // When the course is away from the waypoint, 
290           // it makes sense to change the sign of the deviation.
291           wp_course_deviation_deg *= -1.0;
292           while (wp_course_deviation_deg < -90.0)
293             wp_course_deviation_deg += 180.0;
294           while (wp_course_deviation_deg >  90.0)
295             wp_course_deviation_deg -= 180.0; }
296
297         _wp_course_deviation_node->setDoubleValue(wp_course_deviation_deg);
298
299         // Cross track error.
300         wp_course_error_m = sin(wp_course_deviation_deg * SG_PI / 180.0) 
301           * (wp_distance);
302         _wp_course_error_nm_node->setDoubleValue(wp_course_error_m 
303                                                  * SG_METER_TO_NM);
304
305     } else {
306         _true_track_node->setDoubleValue(0.0);
307         _magnetic_track_node->setDoubleValue(0.0);
308         _speed_node->setDoubleValue(0.0);
309     }
310
311     _last_valid = true;
312     _last_longitude_deg = longitude_deg;
313     _last_latitude_deg = latitude_deg;
314     _last_altitude_m = altitude_m;
315 }
316
317 // end of gps.cxx