]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/gps.cxx
1d584e685deae9c6fb97e5f715e8ba89c2af10e4
[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_actual_radial_deg,
176           wp_course_deviation_deg, wp_course_error_m, wp_TTW;
177         string wp_ID = _wp_ID_node->getStringValue();
178
179         // If the get-nearest-airport-node is true.
180         // Get the nearest airport, and set it as waypoint.
181         if (_get_nearest_airport_node->getBoolValue()) {
182           FGAirport a;
183           cout << "Airport found" << endl;
184           a = globals->get_airports()->search(longitude_deg, latitude_deg, false);
185           _wp_ID_node->setStringValue(a.id.c_str());
186           wp_longitude_deg = a.longitude;
187           wp_latitude_deg = a.latitude;
188           _wp_name_node->setStringValue(a.name.c_str());
189           _get_nearest_airport_node->setBoolValue(false);
190           _last_wp_ID = wp_ID = a.id.c_str();
191         }
192
193         // If the waypoint ID has changed, try to find the new ID
194         // in the airport-, fix-, nav-database.
195         if ( !(_last_wp_ID == wp_ID) ) {
196           string waypont_type =
197             _waypoint_type_node->getStringValue();
198           if (waypont_type == "airport") {
199             FGAirport a;
200             a = globals->get_airports()->search( wp_ID );
201             if ( a.id == wp_ID ) {
202               cout << "Airport found" << endl;
203               wp_longitude_deg = a.longitude;
204               wp_latitude_deg = a.latitude;
205               _wp_name_node->setStringValue(a.name.c_str());
206             }
207           }
208           else if (waypont_type == "nav") {
209             FGNav * n;
210             if ( (n = current_navlist->findByIdent(wp_ID.c_str(), 
211                                                       longitude_deg, 
212                                                       latitude_deg)) != NULL) {
213               cout << "Nav found" << endl;
214               wp_longitude_deg = n->get_lon();
215               wp_latitude_deg = n->get_lat();
216               _wp_name_node->setStringValue(n->get_name().c_str());
217             }
218           }
219           else if (waypont_type == "fix") {
220             FGFix f;
221             if ( current_fixlist->query(wp_ID, &f) ) {
222               cout << "Fix found" << endl;
223               wp_longitude_deg = f.get_lon();
224               wp_latitude_deg = f.get_lat();
225               _wp_name_node->setStringValue(wp_ID.c_str());
226             }
227           }
228           _last_wp_ID = wp_ID;
229         }
230
231         // Find the bearing and distance to the waypoint.
232         SGWayPoint wp(wp_longitude_deg, wp_latitude_deg, altitude_m);
233         wp.CourseAndDistance(longitude_deg, latitude_deg, altitude_m,
234                             &wp_bearing_deg, &wp_distance);
235         _wp_longitude_node->setDoubleValue(wp_longitude_deg);
236         _wp_latitude_node->setDoubleValue(wp_latitude_deg);
237         _wp_distance_node->setDoubleValue(wp_distance * SG_METER_TO_NM);
238         _wp_bearing_node->setDoubleValue(wp_bearing_deg);
239         
240         // Estimate time to waypoint.
241         // The estimation does not take track into consideration,
242         // so if you are going away from the waypoint the TTW will
243         // increase. Makes most sense when travelling directly towards
244         // the waypoint.
245         if (speed_kt > 0.0 && wp_distance > 0.0) {
246           wp_TTW = (wp_distance * SG_METER_TO_NM) / (speed_kt / 3600);
247         }
248         else {
249           wp_TTW = 0.0;
250         }
251         unsigned int wp_TTW_seconds = (int) (wp_TTW + 0.5);
252         if (wp_TTW_seconds < 356400) { // That's 99 hours
253           unsigned int wp_TTW_minutes = 0;
254           unsigned int wp_TTW_hours   = 0;
255           char wp_TTW_str[8];
256           while (wp_TTW_seconds >= 3600) {
257             wp_TTW_seconds -= 3600;
258             wp_TTW_hours++;
259           }
260           while (wp_TTW_seconds >= 60) {
261             wp_TTW_seconds -= 60;
262             wp_TTW_minutes++;
263           }
264           sprintf(wp_TTW_str, "%02d:%02d:%02d",
265             wp_TTW_hours, wp_TTW_minutes, wp_TTW_seconds);
266           _wp_ttw_node->setStringValue(wp_TTW_str);
267         }
268         else
269           _wp_ttw_node->setStringValue("--:--:--");
270
271         // Course deviation is the diffenrence between the bearing
272         // and the course.
273         wp_course_deviation_deg = wp_bearing_deg -
274           wp_course_deg;
275         while (wp_course_deviation_deg < -180.0) {
276           wp_course_deviation_deg += 360.0; }
277         while (wp_course_deviation_deg > 180.0) {
278           wp_course_deviation_deg -= 360.0; }
279
280         // If the course deviation is less than 90 degrees to either side,
281         // our desired course is towards the waypoint.
282         // It does not matter if we are actually moving towards or from the waypoint.
283         if (fabs(wp_course_deviation_deg) < 90.0) {
284             _wp_to_flag_node->setBoolValue(true); }
285         // If it's more than 90 degrees the desired course is from the waypoint.
286         else if (fabs(wp_course_deviation_deg) > 90.0) {
287           _wp_to_flag_node->setBoolValue(false);
288           // When the course is away from the waypoint, 
289           // it makes sense to change the sign of the deviation.
290           wp_course_deviation_deg *= -1.0;
291           while (wp_course_deviation_deg < -90.0)
292             wp_course_deviation_deg += 180.0;
293           while (wp_course_deviation_deg >  90.0)
294             wp_course_deviation_deg -= 180.0; }
295
296         _wp_course_deviation_node->setDoubleValue(wp_course_deviation_deg);
297
298         // Cross track error.
299         wp_course_error_m = sin(wp_course_deviation_deg * SG_PI / 180.0) 
300           * (wp_distance);
301         _wp_course_error_nm_node->setDoubleValue(wp_course_error_m 
302                                                  * SG_METER_TO_NM);
303
304     } else {
305         _true_track_node->setDoubleValue(0.0);
306         _magnetic_track_node->setDoubleValue(0.0);
307         _speed_node->setDoubleValue(0.0);
308     }
309
310     _last_valid = true;
311     _last_longitude_deg = longitude_deg;
312     _last_latitude_deg = latitude_deg;
313     _last_altitude_m = altitude_m;
314 }
315
316 // end of gps.cxx