1 // gps.cxx - distance-measuring equipment.
2 // Written by David Megginson, started 2003.
4 // This file is in the Public Domain and comes with no warranty.
10 #include <simgear/compiler.h>
16 #include <Aircraft/aircraft.hxx>
17 #include <FDM/flight.hxx>
18 #include <Controls/controls.hxx>
19 #include <Scenery/scenery.hxx>
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>
28 #include <Airports/simple.hxx>
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>
44 _last_longitude_deg(0),
45 _last_latitude_deg(0),
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);
65 fgGetNode("/instrumentation/gps/wp-longitude-deg", true);
67 fgGetNode("/instrumentation/gps/wp-latitude-deg", true);
69 fgGetNode("/instrumentation/gps/wp-ID", true);
71 fgGetNode("/instrumentation/gps/wp-name", true);
73 fgGetNode("/instrumentation/gps/indicated-course-deg", true);
74 _get_nearest_airport_node =
75 fgGetNode("/instrumentation/gps/get-nearest-airport", true);
77 fgGetNode("/instrumentation/gps/waypoint-type", true);
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);
87 fgGetNode("/instrumentation/gps/indicated-track-true-deg", true);
88 _magnetic_track_node =
89 fgGetNode("/instrumentation/gps/indicated-track-magnetic-deg", true);
91 fgGetNode("/instrumentation/gps/indicated-ground-speed-kt", true);
93 fgGetNode("/instrumentation/gps/wp-distance-nm", true);
95 fgGetNode("/instrumentation/gps/TTW",true);
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);
103 fgGetNode("/instrumentation/gps/to-flag", true);
105 fgGetNode("/instrumentation/gps/odometer", true);
106 _trip_odometer_node =
107 fgGetNode("/instrumentation/gps/trip-odometer", true);
111 GPS::update (double delta_time_sec)
113 // If it's off, don't bother.
114 if (!_serviceable_node->getBoolValue() ||
115 !_electrical_node->getBoolValue()) {
117 _last_longitude_deg = 0;
118 _last_latitude_deg = 0;
119 _last_altitude_m = 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);
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();
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);
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);
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);
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();
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()) {
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();
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") {
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());
208 else if (waypont_type == "nav") {
210 if ( (n = current_navlist->findByIdent(wp_ID.c_str(),
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());
219 else if (waypont_type == "fix") {
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());
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);
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
245 if (speed_kt > 0.0 && wp_distance > 0.0) {
246 wp_TTW = (wp_distance * SG_METER_TO_NM) / (speed_kt / 3600);
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;
256 while (wp_TTW_seconds >= 3600) {
257 wp_TTW_seconds -= 3600;
260 while (wp_TTW_seconds >= 60) {
261 wp_TTW_seconds -= 60;
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);
269 _wp_ttw_node->setStringValue("--:--:--");
271 // Course deviation is the diffenrence between the bearing
273 wp_course_deviation_deg = wp_bearing_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; }
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; }
296 _wp_course_deviation_node->setDoubleValue(wp_course_deviation_deg);
298 // Cross track error.
299 wp_course_error_m = sin(wp_course_deviation_deg * SG_PI / 180.0)
301 _wp_course_error_nm_node->setDoubleValue(wp_course_error_m
305 _true_track_node->setDoubleValue(0.0);
306 _magnetic_track_node->setDoubleValue(0.0);
307 _speed_node->setDoubleValue(0.0);
311 _last_longitude_deg = longitude_deg;
312 _last_latitude_deg = latitude_deg;
313 _last_altitude_m = altitude_m;