1 // dclgps.cxx - a class to extend the operation of FG's current GPS
2 // code, and provide support for a KLN89-specific instrument. It
3 // is envisioned that eventually this file and class will be split
4 // up between current FG code and new KLN89-specific code and removed.
6 // Written by David Luff, started 2005.
8 // Copyright (C) 2005 - David C Luff - david.luff@nottingham.ac.uk
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <simgear/sg_inlines.h>
32 //using namespace std;
34 // Command callbacks for FlightGear
36 static bool do_kln89_msg_pressed(const SGPropertyNode* arg) {
37 //cout << "do_kln89_msg_pressed called!\n";
38 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
43 static bool do_kln89_obs_pressed(const SGPropertyNode* arg) {
44 //cout << "do_kln89_obs_pressed called!\n";
45 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
50 static bool do_kln89_alt_pressed(const SGPropertyNode* arg) {
51 //cout << "do_kln89_alt_pressed called!\n";
52 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
57 static bool do_kln89_nrst_pressed(const SGPropertyNode* arg) {
58 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
63 static bool do_kln89_dto_pressed(const SGPropertyNode* arg) {
64 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
69 static bool do_kln89_clr_pressed(const SGPropertyNode* arg) {
70 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
75 static bool do_kln89_ent_pressed(const SGPropertyNode* arg) {
76 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
81 static bool do_kln89_crsr_pressed(const SGPropertyNode* arg) {
82 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
87 static bool do_kln89_knob1left1(const SGPropertyNode* arg) {
88 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
93 static bool do_kln89_knob1right1(const SGPropertyNode* arg) {
94 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
99 static bool do_kln89_knob2left1(const SGPropertyNode* arg) {
100 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
105 static bool do_kln89_knob2right1(const SGPropertyNode* arg) {
106 DCLGPS* gps = (DCLGPS*)globals->get_subsystem("kln89");
111 // End command callbacks
113 GPSWaypoint::GPSWaypoint() {
114 appType = GPS_APP_NONE;
117 GPSWaypoint::~GPSWaypoint() {}
119 string GPSWaypoint::GetAprId() {
120 if(appType == GPS_IAF) return(id + 'i');
121 else if(appType == GPS_FAF) return(id + 'f');
122 else if(appType == GPS_MAP) return(id + 'm');
123 else if(appType == GPS_MAHP) return(id + 'h');
127 ostream& operator << (ostream& os, GPSAppWpType type) {
129 case(GPS_IAF): return(os << "IAF");
130 case(GPS_IAP): return(os << "IAP");
131 case(GPS_FAF): return(os << "FAF");
132 case(GPS_MAP): return(os << "MAP");
133 case(GPS_MAHP): return(os << "MAHP");
134 case(GPS_HDR): return(os << "HEADER");
135 case(GPS_FENCE): return(os << "FENCE");
136 case(GPS_APP_NONE): return(os << "NONE");
138 return(os << "ERROR - Unknown switch in GPSAppWpType operator << ");
150 FGNPIAP::~FGNPIAP() {
153 GPSPage::GPSPage(DCLGPS* parent) {
158 GPSPage::~GPSPage() {
161 void GPSPage::Update(double dt) {}
163 void GPSPage::Knob1Left1() {}
164 void GPSPage::Knob1Right1() {}
166 void GPSPage::Knob2Left1() {
167 _parent->_activePage->LooseFocus();
169 if(_subPage < 0) _subPage = _nSubPages - 1;
172 void GPSPage::Knob2Right1() {
173 _parent->_activePage->LooseFocus();
175 if(_subPage >= _nSubPages) _subPage = 0;
178 void GPSPage::CrsrPressed() {}
179 void GPSPage::EntPressed() {}
180 void GPSPage::ClrPressed() {}
181 void GPSPage::DtoPressed() {}
182 void GPSPage::NrstPressed() {}
183 void GPSPage::AltPressed() {}
184 void GPSPage::OBSPressed() {}
185 void GPSPage::MsgPressed() {}
187 string GPSPage::GPSitoa(int n) {
189 // TODO - sanity check n!
190 sprintf(buf, "%i", n);
195 void GPSPage::CleanUp() {}
196 void GPSPage::LooseFocus() {}
197 void GPSPage::SetId(const string& s) {}
199 // ------------------------------------------------------------------------------------- //
201 DCLGPS::DCLGPS(RenderArea2D* instrument) {
202 _instrument = instrument;
207 // Units - lets default to US units - FG can set them to other units from config during startup if desired.
208 _altUnits = GPS_ALT_UNITS_FT;
209 _baroUnits = GPS_PRES_UNITS_IN;
210 _velUnits = GPS_VEL_UNITS_KT;
211 _distUnits = GPS_DIST_UNITS_NM;
213 _lon_node = fgGetNode("/instrumentation/gps/indicated-longitude-deg", true);
214 _lat_node = fgGetNode("/instrumentation/gps/indicated-latitude-deg", true);
215 _alt_node = fgGetNode("/instrumentation/gps/indicated-altitude-ft", true);
216 _grnd_speed_node = fgGetNode("/instrumentation/gps/indicated-ground-speed-kt", true);
217 _true_track_node = fgGetNode("/instrumentation/gps/indicated-track-true-deg", true);
218 _mag_track_node = fgGetNode("/instrumentation/gps/indicated-track-magnetic-deg", true);
220 // Use FG's position values at construction in case FG's gps has not run first update yet.
221 _lon = fgGetDouble("/position/longitude-deg") * SG_DEGREES_TO_RADIANS;
222 _lat = fgGetDouble("/position/latitude-deg") * SG_DEGREES_TO_RADIANS;
223 _alt = fgGetDouble("/position/altitude-ft");
224 // Note - we can depriciate _gpsLat and _gpsLon if we implement error handling in FG
225 // gps code and not our own.
230 _groundSpeed_ms = 0.0;
231 _groundSpeed_kts = 0.0;
235 // Sensible defaults. These can be overriden by derived classes if desired.
237 _cdiScales.push_back(5.0);
238 _cdiScales.push_back(1.0);
239 _cdiScales.push_back(0.3);
240 _currentCdiScaleIndex = 0;
241 _targetCdiScaleIndex = 0;
242 _sourceCdiScaleIndex = 0;
243 _cdiScaleTransition = false;
244 _currentCdiScale = 5.0;
248 _activeWaypoint.id.clear();
250 _crosstrackDist = 0.0;
251 _headingBugTo = true;
253 _waypointAlert = false;
255 _departureTimeString = "----";
258 // Configuration Initialisation
259 // Should this be in kln89.cxx ?
260 _turnAnticipationEnabled = false;
261 _suaAlertEnabled = false;
262 _altAlertEnabled = false;
266 _messageStack.clear();
270 _approachLoaded = false;
271 _approachArm = false;
272 _approachReallyArmed = false;
273 _approachActive = false;
274 _approachFP = new GPSFlightPlan;
279 for(gps_waypoint_map_iterator itr = _waypoints.begin(); itr != _waypoints.end(); ++itr) {
280 for(unsigned int i = 0; i < (*itr).second.size(); ++i) {
281 delete(((*itr).second)[i]);
284 delete _approachFP; // Don't need to delete the waypoints inside since they point to
285 // the waypoints in the approach database.
286 // TODO - may need to delete the approach database!!
289 void DCLGPS::draw() {
290 //cout << "draw called!\n";
294 void DCLGPS::init() {
295 globals->get_commands()->addCommand("kln89_msg_pressed", do_kln89_msg_pressed);
296 globals->get_commands()->addCommand("kln89_obs_pressed", do_kln89_obs_pressed);
297 globals->get_commands()->addCommand("kln89_alt_pressed", do_kln89_alt_pressed);
298 globals->get_commands()->addCommand("kln89_nrst_pressed", do_kln89_nrst_pressed);
299 globals->get_commands()->addCommand("kln89_dto_pressed", do_kln89_dto_pressed);
300 globals->get_commands()->addCommand("kln89_clr_pressed", do_kln89_clr_pressed);
301 globals->get_commands()->addCommand("kln89_ent_pressed", do_kln89_ent_pressed);
302 globals->get_commands()->addCommand("kln89_crsr_pressed", do_kln89_crsr_pressed);
303 globals->get_commands()->addCommand("kln89_knob1left1", do_kln89_knob1left1);
304 globals->get_commands()->addCommand("kln89_knob1right1", do_kln89_knob1right1);
305 globals->get_commands()->addCommand("kln89_knob2left1", do_kln89_knob2left1);
306 globals->get_commands()->addCommand("kln89_knob2right1", do_kln89_knob2right1);
308 // Build the GPS-specific databases.
309 // TODO - consider splitting into real life GPS database regions - eg Americas, Europe etc.
310 // Note that this needs to run after FG's airport and nav databases are up and running
312 const airport_list* apts = globals->get_airports()->getAirportList();
313 for(unsigned int i = 0; i < apts->size(); ++i) {
314 FGAirport* a = (*apts)[i];
315 GPSWaypoint* w = new GPSWaypoint;
317 w->lat = a->getLatitude() * SG_DEGREES_TO_RADIANS;
318 w->lon = a->getLongitude() * SG_DEGREES_TO_RADIANS;
319 w->type = GPS_WP_APT;
320 gps_waypoint_map_iterator wtr = _waypoints.find(a->getId());
321 if(wtr == _waypoints.end()) {
322 gps_waypoint_array arr;
324 _waypoints[w->id] = arr;
326 wtr->second.push_back(w);
329 nav_map_type navs = globals->get_navlist()->get_navaids();
330 for(nav_map_iterator itr = navs.begin(); itr != navs.end(); ++itr) {
331 nav_list_type nlst = itr->second;
332 for(unsigned int i = 0; i < nlst.size(); ++i) {
333 FGNavRecord* n = nlst[i];
334 if(n->get_fg_type() == FG_NAV_VOR || n->get_fg_type() == FG_NAV_NDB) { // We don't bother with ILS etc.
335 GPSWaypoint* w = new GPSWaypoint;
336 w->id = n->get_ident();
337 w->lat = n->get_lat() * SG_DEGREES_TO_RADIANS;
338 w->lon = n->get_lon() * SG_DEGREES_TO_RADIANS;
339 w->type = (n->get_fg_type() == FG_NAV_VOR ? GPS_WP_VOR : GPS_WP_NDB);
340 gps_waypoint_map_iterator wtr = _waypoints.find(n->get_ident());
341 if(wtr == _waypoints.end()) {
342 gps_waypoint_array arr;
344 _waypoints[w->id] = arr;
346 wtr->second.push_back(w);
351 const fix_map_type* fixes = globals->get_fixlist()->getFixList();
352 for(fix_map_const_iterator itr = fixes->begin(); itr != fixes->end(); ++itr) {
353 FGFix f = itr->second;
354 GPSWaypoint* w = new GPSWaypoint;
355 w->id = f.get_ident();
356 w->lat = f.get_lat() * SG_DEGREES_TO_RADIANS;
357 w->lon = f.get_lon() * SG_DEGREES_TO_RADIANS;
358 w->type = GPS_WP_INT;
359 gps_waypoint_map_iterator wtr = _waypoints.find(f.get_ident());
360 if(wtr == _waypoints.end()) {
361 gps_waypoint_array arr;
363 _waypoints[w->id] = arr;
365 wtr->second.push_back(w);
368 // TODO - add USR waypoints as well.
370 // Not sure if this should be here, but OK for now.
371 CreateDefaultFlightPlans();
373 // Hack - hardwire some instrument approaches for testing.
374 // TODO - read these from file - either all at startup or as needed.
375 FGNPIAP* iap = new FGNPIAP;
377 iap->_name = "VOR/DME OR GPS-B";
378 iap->_abbrev = "VOR/D";
384 GPSWaypoint* wp = new GPSWaypoint;
387 // Nasty using the find any function here, but it saves converting data from FGFix etc.
388 const GPSWaypoint* fp = FindFirstById(wp->id, multi, true);
390 wp->appType = GPS_IAF;
391 iap->_IAF.push_back(wp);
393 wp = new GPSWaypoint;
395 fp = FindFirstById(wp->id, multi, true);
397 wp->appType = GPS_IAF;
398 iap->_IAF.push_back(wp);
400 wp = new GPSWaypoint;
402 fp = FindFirstById(wp->id, multi, true);
404 wp->appType = GPS_IAP;
405 iap->_IAP.push_back(wp);
407 wp = new GPSWaypoint;
409 fp = FindFirstById(wp->id, multi, true);
411 wp->appType = GPS_FAF;
412 iap->_IAP.push_back(wp);
414 wp = new GPSWaypoint;
416 fp = FindFirstById(wp->id, multi, true);
418 wp->appType = GPS_MAP;
419 iap->_IAP.push_back(wp);
421 wp = new GPSWaypoint;
423 fp = FindFirstById(wp->id, multi, true);
425 wp->appType = GPS_MAHP;
426 iap->_MAP.push_back(wp);
428 _np_iap[iap->_id].push_back(iap);
429 // -----------------------
430 // -----------------------
433 iap->_name = "VOR OR GPS-A";
434 iap->_abbrev = "VOR-";
440 wp = new GPSWaypoint;
442 // Nasty using the find any function here, but it saves converting data from FGFix etc.
443 fp = FindFirstById(wp->id, multi, true);
445 wp->appType = GPS_IAF;
446 iap->_IAF.push_back(wp);
448 wp = new GPSWaypoint;
450 fp = FindFirstById(wp->id, multi, true);
452 wp->appType = GPS_IAF;
453 iap->_IAF.push_back(wp);
455 wp = new GPSWaypoint;
457 fp = FindFirstById(wp->id, multi, true);
459 wp->appType = GPS_IAP;
460 iap->_IAP.push_back(wp);
462 wp = new GPSWaypoint;
464 fp = FindFirstById(wp->id, multi, true);
466 wp->appType = GPS_FAF;
467 iap->_IAP.push_back(wp);
469 wp = new GPSWaypoint;
471 fp = FindFirstById(wp->id, multi, true);
473 wp->appType = GPS_MAP;
474 iap->_IAP.push_back(wp);
476 wp = new GPSWaypoint;
478 fp = FindFirstById(wp->id, multi, true);
480 wp->appType = GPS_MAHP;
481 iap->_MAP.push_back(wp);
483 _np_iap[iap->_id].push_back(iap);
484 // ------------------
485 // ------------------
487 // Ugh - don't load this one - the waypoints required aren't in fix.dat.gz - result: program crash!
488 // TODO - make the IAP loader robust to absent waypoints.
491 iap->_name = "GPS RWY 28L";
492 iap->_abbrev = "GPS";
493 iap->_rwyStr = "28L";
498 wp = new GPSWaypoint;
500 // Nasty using the find any function here, but it saves converting data from FGFix etc.
501 fp = FindFirstById(wp->id, multi, true);
503 wp->appType = GPS_IAF;
504 iap->_IAF.push_back(wp);
506 wp = new GPSWaypoint;
508 fp = FindFirstById(wp->id, multi, true);
510 wp->appType = GPS_IAF;
511 iap->_IAF.push_back(wp);
513 wp = new GPSWaypoint;
515 fp = FindFirstById(wp->id, multi, true);
517 wp->appType = GPS_IAP;
518 iap->_IAP.push_back(wp);
520 wp = new GPSWaypoint;
522 fp = FindFirstById(wp->id, multi, true);
524 wp->appType = GPS_FAF;
525 iap->_IAP.push_back(wp);
527 wp = new GPSWaypoint;
529 wp->appType = GPS_MAP;
530 if(wp->id.substr(0, 2) == "RW" && wp->appType == GPS_MAP) {
531 // Assume that this is a missed-approach point based on the runway number
532 // Get the runway threshold location etc
534 fp = FindFirstById(wp->id, multi, true);
536 cout << "Failed to find waypoint " << wp->id << " in database...\n";
541 iap->_IAP.push_back(wp);
543 wp = new GPSWaypoint;
545 fp = FindFirstById(wp->id, multi, true);
547 wp->appType = GPS_MAHP;
548 iap->_MAP.push_back(wp);
550 _np_iap[iap->_id].push_back(iap);
554 iap->_name = "GPS RWY 30";
555 iap->_abbrev = "GPS";
561 wp = new GPSWaypoint;
563 // Nasty using the find any function here, but it saves converting data from FGFix etc.
564 fp = FindFirstById(wp->id, multi, true);
566 wp->appType = GPS_IAF;
567 iap->_IAF.push_back(wp);
569 wp = new GPSWaypoint;
571 fp = FindFirstById(wp->id, multi, true);
573 wp->appType = GPS_IAF;
574 iap->_IAF.push_back(wp);
576 wp = new GPSWaypoint;
578 fp = FindFirstById(wp->id, multi, true);
580 wp->appType = GPS_IAP;
581 iap->_IAP.push_back(wp);
583 wp = new GPSWaypoint;
585 fp = FindFirstById(wp->id, multi, true);
587 wp->appType = GPS_FAF;
588 iap->_IAP.push_back(wp);
590 wp = new GPSWaypoint;
592 wp->appType = GPS_MAP;
593 if(wp->id.substr(0, 2) == "RW" && wp->appType == GPS_MAP) {
594 // Assume that this is a missed-approach point based on the runway number
595 // TODO: Get the runway threshold location etc
596 cout << "TODO - implement missed-approach point based on rwy no.\n";
598 fp = FindFirstById(wp->id, multi, true);
600 cout << "Failed to find waypoint " << wp->id << " in database...\n";
603 wp->appType = GPS_MAP;
606 iap->_IAP.push_back(wp);
608 wp = new GPSWaypoint;
610 fp = FindFirstById(wp->id, multi, true);
612 wp->appType = GPS_MAHP;
613 iap->_MAP.push_back(wp);
615 _np_iap[iap->_id].push_back(iap);
618 void DCLGPS::bind() {
619 fgTie("/instrumentation/gps/waypoint-alert", this, &DCLGPS::GetWaypointAlert);
620 fgTie("/instrumentation/gps/leg-mode", this, &DCLGPS::GetLegMode);
621 fgTie("/instrumentation/gps/obs-mode", this, &DCLGPS::GetOBSMode);
622 fgTie("/instrumentation/gps/approach-arm", this, &DCLGPS::GetApproachArm);
623 fgTie("/instrumentation/gps/approach-active", this, &DCLGPS::GetApproachActive);
624 fgTie("/instrumentation/gps/cdi-deflection", this, &DCLGPS::GetCDIDeflection);
625 fgTie("/instrumentation/gps/to-flag", this, &DCLGPS::GetToFlag);
628 void DCLGPS::unbind() {
629 fgUntie("/instrumentation/gps/waypoint-alert");
630 fgUntie("/instrumentation/gps/leg-mode");
631 fgUntie("/instrumentation/gps/obs-mode");
632 fgUntie("/instrumentation/gps/approach-arm");
633 fgUntie("/instrumentation/gps/approach-active");
634 fgUntie("/instrumentation/gps/cdi-deflection");
637 void DCLGPS::update(double dt) {
638 //cout << "update called!\n";
640 _lon = _lon_node->getDoubleValue() * SG_DEGREES_TO_RADIANS;
641 _lat = _lat_node->getDoubleValue() * SG_DEGREES_TO_RADIANS;
642 _alt = _alt_node->getDoubleValue();
643 _groundSpeed_kts = _grnd_speed_node->getDoubleValue();
644 _groundSpeed_ms = _groundSpeed_kts * 0.5144444444;
645 _track = _true_track_node->getDoubleValue();
646 _magTrackDeg = _mag_track_node->getDoubleValue();
647 // Note - we can depriciate _gpsLat and _gpsLon if we implement error handling in FG
648 // gps code and not our own.
651 // Check for abnormal position slew
652 if(GetGreatCircleDistance(_gpsLat, _gpsLon, _checkLat, _checkLon) > 1.0) {
653 OrientateToActiveFlightPlan();
659 if(_groundSpeed_kts > 30.0) {
661 string th = fgGetString("/instrumentation/clock/indicated-hour");
662 string tm = fgGetString("/instrumentation/clock/indicated-min");
663 if(th.size() == 1) th = "0" + th;
664 if(tm.size() == 1) tm = "0" + tm;
665 _departureTimeString = th + tm;
668 // TODO - check - is this prone to drift error over time?
669 // Should we difference the departure and current times?
670 // What about when the user resets the time of day from the menu?
674 _time->update(_gpsLon * SG_DEGREES_TO_RADIANS, _gpsLat * SG_DEGREES_TO_RADIANS, 0, 0);
675 // FIXME - currently all the below assumes leg mode and no DTO or OBS cancelled.
676 if(_activeFP->IsEmpty()) {
677 // Not sure if we need to reset these each update or only when fp altered
678 _activeWaypoint.id.clear();
680 } else if(_activeFP->waypoints.size() == 1) {
681 _activeWaypoint.id.clear();
684 if(_activeWaypoint.id.empty() || _fromWaypoint.id.empty()) {
685 //cout << "Error, in leg mode with flightplan of 2 or more waypoints, but either active or from wp is NULL!\n";
686 OrientateToActiveFlightPlan();
690 if(_approachLoaded) {
691 if(!_approachReallyArmed && !_approachActive) {
692 // arm if within 30nm of airport.
693 // TODO - let user cancel approach arm using external GPS-APR switch
695 const FGAirport* ap = FindFirstAptById(_approachID, multi, true);
697 double d = GetGreatCircleDistance(_gpsLat, _gpsLon, ap->getLatitude() * SG_DEGREES_TO_RADIANS, ap->getLongitude() * SG_DEGREES_TO_RADIANS);
700 _approachReallyArmed = true;
701 _messageStack.push_back("*Press ALT To Set Baro");
702 // Not sure what we do if the user has already set CDI to 0.3 nm?
703 _targetCdiScaleIndex = 1;
704 if(_currentCdiScaleIndex == 1) {
706 } else if(_currentCdiScaleIndex == 0) {
707 _sourceCdiScaleIndex = 0;
708 _cdiScaleTransition = true;
709 _cdiTransitionTime = 30.0;
710 _currentCdiScale = _cdiScales[_currentCdiScaleIndex];
715 // Check for approach active - we can only activate approach if it is really armed.
716 if(_activeWaypoint.appType == GPS_FAF) {
717 //cout << "Active waypoint is FAF, id is " << _activeWaypoint.id << '\n';
718 if(GetGreatCircleDistance(_gpsLat, _gpsLon, _activeWaypoint.lat, _activeWaypoint.lon) <= 2.0 && !_obsMode) {
719 // Assume heading is OK for now
720 _approachArm = false; // TODO - check - maybe arm is left on when actv comes on?
721 _approachReallyArmed = false;
722 _approachActive = true;
723 _targetCdiScaleIndex = 2;
724 if(_currentCdiScaleIndex == 2) {
726 } else if(_currentCdiScaleIndex == 1) {
727 _sourceCdiScaleIndex = 1;
728 _cdiScaleTransition = true;
729 _cdiTransitionTime = 30.0; // TODO - compress it if time to FAF < 30sec
730 _currentCdiScale = _cdiScales[_currentCdiScaleIndex];
732 // Abort going active?
733 _approachActive = false;
740 // CDI scale transition stuff
741 if(_cdiScaleTransition) {
742 if(fabs(_currentCdiScale - _cdiScales[_targetCdiScaleIndex]) < 0.001) {
743 _currentCdiScale = _cdiScales[_targetCdiScaleIndex];
744 _currentCdiScaleIndex = _targetCdiScaleIndex;
745 _cdiScaleTransition = false;
747 double scaleDiff = (_targetCdiScaleIndex > _sourceCdiScaleIndex
748 ? _cdiScales[_sourceCdiScaleIndex] - _cdiScales[_targetCdiScaleIndex]
749 : _cdiScales[_targetCdiScaleIndex] - _cdiScales[_sourceCdiScaleIndex]);
750 //cout << "ScaleDiff = " << scaleDiff << '\n';
751 if(_targetCdiScaleIndex > _sourceCdiScaleIndex) {
752 // Scaling down eg. 5nm -> 1nm
753 _currentCdiScale -= (scaleDiff * dt / _cdiTransitionTime);
754 if(_currentCdiScale < _cdiScales[_targetCdiScaleIndex]) {
755 _currentCdiScale = _cdiScales[_targetCdiScaleIndex];
756 _currentCdiScaleIndex = _targetCdiScaleIndex;
757 _cdiScaleTransition = false;
760 _currentCdiScale += (scaleDiff * dt / _cdiTransitionTime);
761 if(_currentCdiScale > _cdiScales[_targetCdiScaleIndex]) {
762 _currentCdiScale = _cdiScales[_targetCdiScaleIndex];
763 _currentCdiScaleIndex = _targetCdiScaleIndex;
764 _cdiScaleTransition = false;
767 //cout << "_currentCdiScale = " << _currentCdiScale << '\n';
770 _currentCdiScale = _cdiScales[_currentCdiScaleIndex];
774 // Urgh - I've been setting the heading bug based on DTK,
775 // bug I think it should be based on heading re. active waypoint
776 // based on what the sim does after the final waypoint is passed.
777 // (DTK remains the same, but if track is held == DTK heading bug
778 // reverses to from once wp is passed).
780 if(_fromWaypoint != NULL) {
781 // TODO - how do we handle the change of track with distance over long legs?
782 _dtkTrue = GetGreatCircleCourse(_fromWaypoint->lat, _fromWaypoint->lon, _activeWaypoint->lat, _activeWaypoint->lon) * SG_RADIANS_TO_DEGREES;
783 _dtkMag = GetMagHeadingFromTo(_fromWaypoint->lat, _fromWaypoint->lon, _activeWaypoint->lat, _activeWaypoint->lon);
784 // Don't change the heading bug if speed is too low otherwise it flickers to/from at rest
785 if(_groundSpeed_ms > 5) {
786 //cout << "track = " << _track << ", dtk = " << _dtkTrue << '\n';
787 double courseDev = _track - _dtkTrue;
788 //cout << "courseDev = " << courseDev << ", normalized = ";
789 SG_NORMALIZE_RANGE(courseDev, -180.0, 180.0);
790 //cout << courseDev << '\n';
791 _headingBugTo = (fabs(courseDev) > 90.0 ? false : true);
796 // TODO - in DTO operation the position of initiation of DTO defines the "from waypoint".
799 if(!_activeWaypoint.id.empty()) {
800 double hdgTrue = GetGreatCircleCourse(_gpsLat, _gpsLon, _activeWaypoint.lat, _activeWaypoint.lon) * SG_RADIANS_TO_DEGREES;
801 if(_groundSpeed_ms > 5) {
802 //cout << "track = " << _track << ", hdgTrue = " << hdgTrue << '\n';
803 double courseDev = _track - hdgTrue;
804 //cout << "courseDev = " << courseDev << ", normalized = ";
805 SG_NORMALIZE_RANGE(courseDev, -180.0, 180.0);
806 //cout << courseDev << '\n';
807 _headingBugTo = (fabs(courseDev) > 90.0 ? false : true);
809 if(!_fromWaypoint.id.empty()) {
810 _dtkTrue = GetGreatCircleCourse(_fromWaypoint.lat, _fromWaypoint.lon, _activeWaypoint.lat, _activeWaypoint.lon) * SG_RADIANS_TO_DEGREES;
811 _dtkMag = GetMagHeadingFromTo(_fromWaypoint.lat, _fromWaypoint.lon, _activeWaypoint.lat, _activeWaypoint.lon);
818 _dist2Act = GetGreatCircleDistance(_gpsLat, _gpsLon, _activeWaypoint.lat, _activeWaypoint.lon) * SG_NM_TO_METER;
819 if(_groundSpeed_ms > 10.0) {
820 _eta = _dist2Act / _groundSpeed_ms;
821 if(_eta <= 36) { // TODO - this is slightly different if turn anticipation is enabled.
823 _waypointAlert = true; // TODO - not if the from flag is set.
827 // Check if we should sequence to next leg.
828 // Perhaps this should be done on distance instead, but 60s time (about 1 - 2 nm) seems reasonable for now.
829 //double reverseHeading = GetGreatCircleCourse(_activeWaypoint->lat, _activeWaypoint->lon, _fromWaypoint->lat, _fromWaypoint->lon);
830 // Hack - let's cheat and do it on heading bug for now. TODO - that stops us 'cutting the corner'
831 // when we happen to approach the inside turn of a waypoint - we should probably sequence at the midpoint
832 // of the heading difference between legs in this instance.
833 int idx = GetActiveWaypointIndex();
834 bool finalLeg = (idx == (int)(_activeFP->waypoints.size()) - 1 ? true : false);
835 bool finalDto = (_dto && idx == -1); // Dto operation to a waypoint not in the flightplan - we don't sequence in this instance
838 // Do nothing - not sure if Dto should switch off when arriving at the final waypoint of a flightplan
839 } else if(finalDto) {
841 } else if(_activeWaypoint.appType == GPS_MAP) {
842 // Don't sequence beyond the missed approach point
843 cout << "ACTIVE WAYPOINT is MAP - not sequencing!!!!!\n";
845 cout << "Sequencing...\n";
846 _fromWaypoint = _activeWaypoint;
847 _activeWaypoint = *_activeFP->waypoints[idx + 1];
849 // TODO - course alteration message format is dependent on whether we are slaved HSI/CDI indicator or not.
850 // For now assume we are not.
852 if(fgGetBool("/instrumentation/nav[0]/slaved-to-gps")) {
853 // TODO - avoid the hardwiring on nav[0]
854 s = "Adj Nav Crs to ";
856 string s = "GPS Course is ";
858 double d = GetMagHeadingFromTo(_fromWaypoint.lat, _fromWaypoint.lon, _activeWaypoint.lat, _activeWaypoint.lon);
859 while(d < 0.0) d += 360.0;
860 while(d >= 360.0) d -= 360.0;
862 snprintf(buf, 4, "%03i", (int)(d + 0.5));
864 _messageStack.push_back(s);
866 _waypointAlert = false;
874 // First attempt at a sensible cross-track correction calculation
875 // Uh? - I think this is implemented further down the file!
876 if(_fromWaypoint != NULL) {
879 _crosstrackDist = 0.0;
885 double DCLGPS::GetCDIDeflection() const {
886 double xtd = CalcCrossTrackDeviation(); //nm
887 return((xtd / _currentCdiScale) * 5.0 * 2.5 * -1.0);
890 void DCLGPS::DtoInitiate(const string& s) {
891 cout << "DtoInitiate, s = " << s << '\n';
893 const GPSWaypoint* wp = FindFirstById(s, multi, true);
895 cout << "Waypoint found, starting dto operation!\n";
897 _activeWaypoint = *wp;
898 _fromWaypoint.lat = _gpsLat;
899 _fromWaypoint.lon = _gpsLon;
900 _fromWaypoint.type = GPS_WP_VIRT;
901 _fromWaypoint.id = "DTOWP";
903 cout << "Waypoint not found, ignoring dto request\n";
904 // Should bring up the user waypoint page, but we're not implementing that yet.
905 _dto = false; // TODO - implement this some day.
909 void DCLGPS::DtoCancel() {
911 // i.e. don't bother reorientating if we're just cancelling a DTO button press
912 // without having previously initiated DTO.
913 OrientateToActiveFlightPlan();
918 void DCLGPS::Knob1Left1() {}
919 void DCLGPS::Knob1Right1() {}
920 void DCLGPS::Knob2Left1() {}
921 void DCLGPS::Knob2Right1() {}
922 void DCLGPS::CrsrPressed() { _activePage->CrsrPressed(); }
923 void DCLGPS::EntPressed() { _activePage->EntPressed(); }
924 void DCLGPS::ClrPressed() { _activePage->ClrPressed(); }
925 void DCLGPS::DtoPressed() {}
926 void DCLGPS::NrstPressed() {}
927 void DCLGPS::AltPressed() {}
929 void DCLGPS::OBSPressed() {
930 _obsMode = !_obsMode;
932 if(!_activeWaypoint.id.empty()) {
933 _obsHeading = _dtkMag;
935 // TODO - the _fromWaypoint location will change as the OBS heading changes.
936 // Might need to store the OBS initiation position somewhere in case it is needed again.
937 SetOBSFromWaypoint();
941 // Set the _fromWaypoint position based on the active waypoint and OBS radial.
942 void DCLGPS::SetOBSFromWaypoint() {
943 if(!_obsMode) return;
944 if(_activeWaypoint.id.empty()) return;
946 // TODO - base the 180 deg correction on the to/from flag.
947 _fromWaypoint = GetPositionOnMagRadial(_activeWaypoint, 10, _obsHeading + 180.0);
948 _fromWaypoint.id = "OBSWP";
951 void DCLGPS::MsgPressed() {}
953 void DCLGPS::CDIFSDIncrease() {
954 if(_currentCdiScaleIndex == 0) {
955 _currentCdiScaleIndex = _cdiScales.size() - 1;
957 _currentCdiScaleIndex--;
961 void DCLGPS::CDIFSDDecrease() {
962 _currentCdiScaleIndex++;
963 if(_currentCdiScaleIndex == _cdiScales.size()) {
964 _currentCdiScaleIndex = 0;
968 void DCLGPS::DrawChar(char c, int field, int px, int py, bool bold) {
971 void DCLGPS::DrawText(const string& s, int field, int px, int py, bool bold) {
974 void DCLGPS::SetBaroUnits(int n, bool wrap) {
976 _baroUnits = (GPSPressureUnits)(wrap ? 3 : 1);
978 _baroUnits = (GPSPressureUnits)(wrap ? 1 : 3);
980 _baroUnits = (GPSPressureUnits)n;
984 void DCLGPS::CreateDefaultFlightPlans() {}
986 // Get the time to the active waypoint in seconds.
987 // Returns -1 if groundspeed < 30 kts
988 double DCLGPS::GetTimeToActiveWaypoint() {
989 if(_groundSpeed_kts < 30.0) {
996 // Get the time to the final waypoint in seconds.
997 // Returns -1 if groundspeed < 30 kts
998 double DCLGPS::GetETE() {
999 if(_groundSpeed_kts < 30.0) {
1002 // TODO - handle OBS / DTO operation appropriately
1003 if(_activeFP->waypoints.empty()) {
1006 return(GetTimeToWaypoint(_activeFP->waypoints[_activeFP->waypoints.size() - 1]->id));
1011 // Get the time to a given waypoint (spec'd by ID) in seconds.
1012 // returns -1 if groundspeed is less than 30kts.
1013 // If the waypoint is an unreached part of the active flight plan the time will be via each leg.
1014 // otherwise it will be a direct-to time.
1015 double DCLGPS::GetTimeToWaypoint(const string& id) {
1016 if(_groundSpeed_kts < 30.0) {
1021 int n1 = GetActiveWaypointIndex();
1022 int n2 = GetWaypointIndex(id);
1025 for(unsigned int i=n1+1; i<_activeFP->waypoints.size(); ++i) {
1026 GPSWaypoint* wp1 = _activeFP->waypoints[i-1];
1027 GPSWaypoint* wp2 = _activeFP->waypoints[i];
1028 double distm = GetGreatCircleDistance(wp1->lat, wp1->lon, wp2->lat, wp2->lon) * SG_NM_TO_METER;
1029 eta += (distm / _groundSpeed_ms);
1032 } else if(id == _activeWaypoint.id) {
1036 const GPSWaypoint* wp = FindFirstById(id, multi, true);
1037 if(wp == NULL) return(-1.0);
1038 double distm = GetGreatCircleDistance(_gpsLat, _gpsLon, wp->lat, wp->lon);
1039 return(distm / _groundSpeed_ms);
1041 return(-1.0); // Hopefully we never get here!
1044 // Returns magnetic great-circle heading
1045 // TODO - document units.
1046 float DCLGPS::GetHeadingToActiveWaypoint() {
1047 if(_activeWaypoint.id.empty()) {
1050 double h = GetMagHeadingFromTo(_gpsLat, _gpsLon, _activeWaypoint.lat, _activeWaypoint.lon);
1051 while(h <= 0.0) h += 360.0;
1052 while(h > 360.0) h -= 360.0;
1057 // Returns magnetic great-circle heading
1058 // TODO - what units?
1059 float DCLGPS::GetHeadingFromActiveWaypoint() {
1060 if(_activeWaypoint.id.empty()) {
1063 double h = GetMagHeadingFromTo(_activeWaypoint.lat, _activeWaypoint.lon, _gpsLat, _gpsLon);
1064 while(h <= 0.0) h += 360.0;
1065 while(h > 360.0) h -= 360.0;
1070 void DCLGPS::ClearFlightPlan(int n) {
1071 for(unsigned int i=0; i<_flightPlans[n]->waypoints.size(); ++i) {
1072 delete _flightPlans[n]->waypoints[i];
1074 _flightPlans[n]->waypoints.clear();
1077 void DCLGPS::ClearFlightPlan(GPSFlightPlan* fp) {
1078 for(unsigned int i=0; i<fp->waypoints.size(); ++i) {
1079 delete fp->waypoints[i];
1081 fp->waypoints.clear();
1084 int DCLGPS::GetActiveWaypointIndex() {
1085 for(unsigned int i=0; i<_flightPlans[0]->waypoints.size(); ++i) {
1086 if(_flightPlans[0]->waypoints[i]->id == _activeWaypoint.id) return((int)i);
1091 int DCLGPS::GetWaypointIndex(const string& id) {
1092 for(unsigned int i=0; i<_flightPlans[0]->waypoints.size(); ++i) {
1093 if(_flightPlans[0]->waypoints[i]->id == id) return((int)i);
1098 void DCLGPS::OrientateToFlightPlan(GPSFlightPlan* fp) {
1099 //cout << "Orientating...\n";
1100 //cout << "_lat = " << _lat << ", _lon = " << _lon << ", _gpsLat = " << _gpsLat << ", gpsLon = " << _gpsLon << '\n';
1102 _activeWaypoint.id.clear();
1105 _navFlagged = false;
1106 if(fp->waypoints.size() == 1) {
1107 // TODO - may need to flag nav here if not dto or obs, or possibly handle it somewhere else.
1108 _activeWaypoint = *fp->waypoints[0];
1109 _fromWaypoint.id.clear();
1111 // FIXME FIXME FIXME
1112 _fromWaypoint = *fp->waypoints[0];
1113 _activeWaypoint = *fp->waypoints[1];
1114 double dmin = 1000000; // nm!!
1115 // For now we will simply start on the leg closest to our current position.
1116 // It's possible that more fancy algorithms may take either heading or track
1117 // into account when setting inital leg - I'm not sure.
1118 // This method should handle most cases perfectly OK though.
1119 for(unsigned int i = 1; i < fp->waypoints.size(); ++i) {
1120 //cout << "Pass " << i << ", dmin = " << dmin << ", leg is " << fp->waypoints[i-1]->id << " to " << fp->waypoints[i]->id << '\n';
1121 // First get the cross track correction.
1122 double d0 = fabs(CalcCrossTrackDeviation(*fp->waypoints[i-1], *fp->waypoints[i]));
1123 // That is the shortest distance away we could be though - check for
1124 // longer distances if we are 'off the end' of the leg.
1125 double ht1 = GetGreatCircleCourse(fp->waypoints[i-1]->lat, fp->waypoints[i-1]->lon,
1126 fp->waypoints[i]->lat, fp->waypoints[i]->lon)
1127 * SG_RADIANS_TO_DEGREES;
1128 // not simply the reverse of the above due to great circle navigation.
1129 double ht2 = GetGreatCircleCourse(fp->waypoints[i]->lat, fp->waypoints[i]->lon,
1130 fp->waypoints[i-1]->lat, fp->waypoints[i-1]->lon)
1131 * SG_RADIANS_TO_DEGREES;
1132 double hw1 = GetGreatCircleCourse(_gpsLat, _gpsLon,
1133 fp->waypoints[i]->lat, fp->waypoints[i]->lon)
1134 * SG_RADIANS_TO_DEGREES;
1135 double hw2 = GetGreatCircleCourse(_gpsLat, _gpsLon,
1136 fp->waypoints[i-1]->lat, fp->waypoints[i-1]->lon)
1137 * SG_RADIANS_TO_DEGREES;
1138 double h1 = ht1 - hw1;
1139 double h2 = ht2 - hw2;
1140 //cout << "d0, h1, h2 = " << d0 << ", " << h1 << ", " << h2 << '\n';
1141 //cout << "Normalizing...\n";
1142 SG_NORMALIZE_RANGE(h1, -180.0, 180.0);
1143 SG_NORMALIZE_RANGE(h2, -180.0, 180.0);
1144 //cout << "d0, h1, h2 = " << d0 << ", " << h1 << ", " << h2 << '\n';
1145 if(fabs(h1) > 90.0) {
1146 // We are past the end of the to waypoint
1147 double d = GetGreatCircleDistance(_gpsLat, _gpsLon, fp->waypoints[i]->lat, fp->waypoints[i]->lon);
1149 //cout << "h1 triggered, d0 now = " << d0 << '\n';
1150 } else if(fabs(h2) > 90.0) {
1151 // We are past the end (not yet at!) the from waypoint
1152 double d = GetGreatCircleDistance(_gpsLat, _gpsLon, fp->waypoints[i-1]->lat, fp->waypoints[i-1]->lon);
1154 //cout << "h2 triggered, d0 now = " << d0 << '\n';
1157 //cout << "THIS LEG NOW ACTIVE!\n";
1159 _fromWaypoint = *fp->waypoints[i-1];
1160 _activeWaypoint = *fp->waypoints[i];
1167 void DCLGPS::OrientateToActiveFlightPlan() {
1168 OrientateToFlightPlan(_activeFP);
1171 /***************************************/
1173 // Utility function - create a flightplan from a list of waypoint ids and types
1174 void DCLGPS::CreateFlightPlan(GPSFlightPlan* fp, vector<string> ids, vector<GPSWpType> wps) {
1175 if(fp == NULL) fp = new GPSFlightPlan;
1177 if(!fp->waypoints.empty()) {
1178 for(i=0; i<fp->waypoints.size(); ++i) {
1179 delete fp->waypoints[i];
1181 fp->waypoints.clear();
1183 if(ids.size() != wps.size()) {
1184 cout << "ID and Waypoint types list size mismatch in GPS::CreateFlightPlan - no flightplan created!\n";
1187 for(i=0; i<ids.size(); ++i) {
1189 const FGAirport* ap;
1191 GPSWaypoint* wp = new GPSWaypoint;
1195 ap = FindFirstAptById(ids[i], multi, true);
1200 wp->lat = ap->getLatitude() * SG_DEGREES_TO_RADIANS;
1201 wp->lon = ap->getLongitude() * SG_DEGREES_TO_RADIANS;
1203 fp->waypoints.push_back(wp);
1207 np = FindFirstVorById(ids[i], multi, true);
1212 wp->lat = np->get_lat() * SG_DEGREES_TO_RADIANS;
1213 wp->lon = np->get_lon() * SG_DEGREES_TO_RADIANS;
1215 fp->waypoints.push_back(wp);
1219 np = FindFirstNDBById(ids[i], multi, true);
1224 wp->lat = np->get_lat() * SG_DEGREES_TO_RADIANS;
1225 wp->lon = np->get_lon() * SG_DEGREES_TO_RADIANS;
1227 fp->waypoints.push_back(wp);
1240 /***************************************/
1242 const GPSWaypoint* DCLGPS::ActualFindFirstById(const string& id, bool exact) {
1243 gps_waypoint_map_const_iterator itr;
1245 itr = _waypoints.find(id);
1247 itr = _waypoints.lower_bound(id);
1249 if(itr == _waypoints.end()) {
1252 // TODO - don't just return the first one - either return all or the nearest one.
1253 return((itr->second)[0]);
1257 const GPSWaypoint* DCLGPS::FindFirstById(const string& id, bool &multi, bool exact) {
1259 if(exact) return(ActualFindFirstById(id, exact));
1261 // OK, that was the easy case, now the fuzzy case
1262 const GPSWaypoint* w1 = ActualFindFirstById(id);
1263 if(w1 == NULL) return(w1);
1265 // The non-trivial code from here to the end of the function is all to deal with the fact that
1266 // the KLN89 alphabetical order (numbers AFTER letters) differs from ASCII order (numbers BEFORE letters).
1268 //string id3 = id+'0';
1269 string id4 = id+'A';
1270 // Increment the last char to provide the boundary. Note that 'Z' -> '[' but we also need to check '0' for all since GPS has numbers after letters
1271 //bool alfa = isalpha(id2[id2.size() - 1]);
1272 id2[id2.size() - 1] = id2[id2.size() - 1] + 1;
1273 const GPSWaypoint* w2 = ActualFindFirstById(id2);
1274 //FGAirport* a3 = globals->get_airports()->findFirstById(id3);
1275 const GPSWaypoint* w4 = ActualFindFirstById(id4);
1276 //cout << "Strings sent were " << id << ", " << id2 << " and " << id4 << '\n';
1277 //cout << "Airports returned were (a1, a2, a4): " << a1->getId() << ", " << a2->getId() << ", " << a4->getId() << '\n';
1278 //cout << "Pointers were " << a1 << ", " << a2 << ", " << a4 << '\n';
1280 // TODO - the below handles the imediately following char OK
1281 // eg id = "KD" returns "KDAA" instead of "KD5"
1282 // but it doesn't handle numbers / letters further down the string,
1283 // eg - id = "I" returns "IA01" instead of "IAN"
1284 // We either need to provide a custom comparison operator, or recurse this function if !isalpha further down the string.
1285 // (Currenly fixed with recursion).
1287 if(w4 != w2) { // A-Z match - preferred
1288 //cout << "A-Z match!\n";
1289 if(w4->id.size() - id.size() > 2) {
1290 // Check for numbers further on
1291 for(unsigned int i=id.size(); i<w4->id.size(); ++i) {
1292 if(!isalpha(w4->id[i])) {
1293 //cout << "SUBSTR is " << (a4->getId()).substr(0, i) << '\n';
1294 return(FindFirstById(w4->id.substr(0, i), multi, exact));
1299 } else if(w1 != w2) { // 0-9 match
1300 //cout << "0-9 match!\n";
1301 if(w1->id.size() - id.size() > 2) {
1302 // Check for numbers further on
1303 for(unsigned int i=id.size(); i<w1->id.size(); ++i) {
1304 if(!isalpha(w1->id[i])) {
1305 //cout << "SUBSTR2 is " << (a4->getId()).substr(0, i) << '\n';
1306 return(FindFirstById(w1->id.substr(0, i), multi, exact));
1311 } else { // No match
1317 // Host specific lookup functions
1318 // TODO - add the ASCII / alphabetical stuff from the Atlas version
1319 FGNavRecord* DCLGPS::FindFirstVorById(const string& id, bool &multi, bool exact) {
1320 // NOTE - at the moment multi is never set.
1322 //if(exact) return(_overlays->FindFirstVorById(id, exact));
1324 nav_list_type nav = globals->get_navlist()->findFirstByIdent(id, FG_NAV_VOR, exact);
1326 if(nav.size() > 1) multi = true;
1327 //return(nav.empty() ? NULL : *(nav.begin()));
1329 // The above is sort of what we want - unfortunately we can't guarantee no NDB/ILS at the moment
1330 if(nav.empty()) return(NULL);
1332 for(nav_list_iterator it = nav.begin(); it != nav.end(); ++it) {
1333 if((*it)->get_type() == 3) return(*it);
1335 return(NULL); // Shouldn't get here!
1338 Overlays::NAV* DCLGPS::FindFirstVorById(const string& id, bool &multi, bool exact) {
1339 // NOTE - at the moment multi is never set.
1341 if(exact) return(_overlays->FindFirstVorById(id, exact));
1343 // OK, that was the easy case, now the fuzzy case
1344 Overlays::NAV* n1 = _overlays->FindFirstVorById(id);
1345 if(n1 == NULL) return(n1);
1348 string id3 = id+'0';
1349 string id4 = id+'A';
1350 // Increment the last char to provide the boundary. Note that 'Z' -> '[' but we also need to check '0' for all since GPS has numbers after letters
1351 bool alfa = isalpha(id2[id2.size() - 1]);
1352 id2[id2.size() - 1] = id2[id2.size() - 1] + 1;
1353 Overlays::NAV* n2 = _overlays->FindFirstVorById(id2);
1354 //Overlays::NAV* n3 = _overlays->FindFirstVorById(id3);
1355 //Overlays::NAV* n4 = _overlays->FindFirstVorById(id4);
1356 //cout << "Strings sent were " << id << ", " << id2 << ", " << id3 << ", " << id4 << '\n';
1360 if(n1 != n2) { // match
1369 // Something matches - the problem is the number/letter preference order is reversed between the GPS and the STL
1371 // There's a letter match - return that
1374 // By definition we must have a number match
1375 if(n3 == n2) cout << "HELP - LOGIC FLAW in find VOR!\n";
1387 // TODO - add the ASCII / alphabetical stuff from the Atlas version
1388 FGNavRecord* DCLGPS::FindFirstNDBById(const string& id, bool &multi, bool exact) {
1389 // NOTE - at the moment multi is never set.
1391 //if(exact) return(_overlays->FindFirstVorById(id, exact));
1393 nav_list_type nav = globals->get_navlist()->findFirstByIdent(id, FG_NAV_NDB, exact);
1395 if(nav.size() > 1) multi = true;
1396 //return(nav.empty() ? NULL : *(nav.begin()));
1398 // The above is sort of what we want - unfortunately we can't guarantee no NDB/ILS at the moment
1399 if(nav.empty()) return(NULL);
1401 for(nav_list_iterator it = nav.begin(); it != nav.end(); ++it) {
1402 if((*it)->get_type() == 2) return(*it);
1404 return(NULL); // Shouldn't get here!
1407 Overlays::NAV* DCLGPS::FindFirstNDBById(const string& id, bool &multi, bool exact) {
1408 // NOTE - at the moment multi is never set.
1410 if(exact) return(_overlays->FindFirstNDBById(id, exact));
1412 // OK, that was the easy case, now the fuzzy case
1413 Overlays::NAV* n1 = _overlays->FindFirstNDBById(id);
1414 if(n1 == NULL) return(n1);
1417 string id3 = id+'0';
1418 string id4 = id+'A';
1419 // Increment the last char to provide the boundary. Note that 'Z' -> '[' but we also need to check '0' for all since GPS has numbers after letters
1420 bool alfa = isalpha(id2[id2.size() - 1]);
1421 id2[id2.size() - 1] = id2[id2.size() - 1] + 1;
1422 Overlays::NAV* n2 = _overlays->FindFirstNDBById(id2);
1423 //Overlays::NAV* n3 = _overlays->FindFirstNDBById(id3);
1424 //Overlays::NAV* n4 = _overlays->FindFirstNDBById(id4);
1425 //cout << "Strings sent were " << id << ", " << id2 << ", " << id3 << ", " << id4 << '\n';
1429 if(n1 != n2) { // match
1438 // Something matches - the problem is the number/letter preference order is reversed between the GPS and the STL
1440 // There's a letter match - return that
1443 // By definition we must have a number match
1444 if(n3 == n2) cout << "HELP - LOGIC FLAW in find VOR!\n";
1456 // TODO - add the ASCII / alphabetical stuff from the Atlas version
1457 const FGFix* DCLGPS::FindFirstIntById(const string& id, bool &multi, bool exact) {
1458 // NOTE - at the moment multi is never set, and indeed can't be
1459 // since FG can only map one Fix per ID at the moment.
1461 if(exact) return(globals->get_fixlist()->findFirstByIdent(id, exact));
1463 const FGFix* f1 = globals->get_fixlist()->findFirstByIdent(id, exact);
1464 if(f1 == NULL) return(f1);
1466 // The non-trivial code from here to the end of the function is all to deal with the fact that
1467 // the KLN89 alphabetical order (numbers AFTER letters) differs from ASCII order (numbers BEFORE letters).
1468 // It is copied from the airport version which is definately needed, but at present I'm not actually
1469 // sure if any fixes in FG or real-life have numbers in them!
1471 //string id3 = id+'0';
1472 string id4 = id+'A';
1473 // Increment the last char to provide the boundary. Note that 'Z' -> '[' but we also need to check '0' for all since GPS has numbers after letters
1474 //bool alfa = isalpha(id2[id2.size() - 1]);
1475 id2[id2.size() - 1] = id2[id2.size() - 1] + 1;
1476 const FGFix* f2 = globals->get_fixlist()->findFirstByIdent(id2);
1477 //const FGFix* a3 = globals->get_fixlist()->findFirstByIdent(id3);
1478 const FGFix* f4 = globals->get_fixlist()->findFirstByIdent(id4);
1480 // TODO - the below handles the imediately following char OK
1481 // eg id = "KD" returns "KDAA" instead of "KD5"
1482 // but it doesn't handle numbers / letters further down the string,
1483 // eg - id = "I" returns "IA01" instead of "IAN"
1484 // We either need to provide a custom comparison operator, or recurse this function if !isalpha further down the string.
1485 // (Currenly fixed with recursion).
1487 if(f4 != f2) { // A-Z match - preferred
1488 //cout << "A-Z match!\n";
1489 if(f4->get_ident().size() - id.size() > 2) {
1490 // Check for numbers further on
1491 for(unsigned int i=id.size(); i<f4->get_ident().size(); ++i) {
1492 if(!isalpha(f4->get_ident()[i])) {
1493 //cout << "SUBSTR is " << (a4->getId()).substr(0, i) << '\n';
1494 return(FindFirstIntById(f4->get_ident().substr(0, i), multi, exact));
1499 } else if(f1 != f2) { // 0-9 match
1500 //cout << "0-9 match!\n";
1501 if(f1->get_ident().size() - id.size() > 2) {
1502 // Check for numbers further on
1503 for(unsigned int i=id.size(); i<f1->get_ident().size(); ++i) {
1504 if(!isalpha(f1->get_ident()[i])) {
1505 //cout << "SUBSTR2 is " << (a4->getId()).substr(0, i) << '\n';
1506 return(FindFirstIntById(f1->get_ident().substr(0, i), multi, exact));
1511 } else { // No match
1515 return NULL; // Don't think we can ever get here.
1518 const FGAirport* DCLGPS::FindFirstAptById(const string& id, bool &multi, bool exact) {
1519 // NOTE - at the moment multi is never set.
1520 //cout << "FindFirstAptById, id = " << id << '\n';
1522 if(exact) return(globals->get_airports()->findFirstById(id, exact));
1524 // OK, that was the easy case, now the fuzzy case
1525 const FGAirport* a1 = globals->get_airports()->findFirstById(id);
1526 if(a1 == NULL) return(a1);
1528 // The non-trivial code from here to the end of the function is all to deal with the fact that
1529 // the KLN89 alphabetical order (numbers AFTER letters) differs from ASCII order (numbers BEFORE letters).
1531 //string id3 = id+'0';
1532 string id4 = id+'A';
1533 // Increment the last char to provide the boundary. Note that 'Z' -> '[' but we also need to check '0' for all since GPS has numbers after letters
1534 //bool alfa = isalpha(id2[id2.size() - 1]);
1535 id2[id2.size() - 1] = id2[id2.size() - 1] + 1;
1536 const FGAirport* a2 = globals->get_airports()->findFirstById(id2);
1537 //FGAirport* a3 = globals->get_airports()->findFirstById(id3);
1538 const FGAirport* a4 = globals->get_airports()->findFirstById(id4);
1539 //cout << "Strings sent were " << id << ", " << id2 << " and " << id4 << '\n';
1540 //cout << "Airports returned were (a1, a2, a4): " << a1->getId() << ", " << a2->getId() << ", " << a4->getId() << '\n';
1541 //cout << "Pointers were " << a1 << ", " << a2 << ", " << a4 << '\n';
1543 // TODO - the below handles the imediately following char OK
1544 // eg id = "KD" returns "KDAA" instead of "KD5"
1545 // but it doesn't handle numbers / letters further down the string,
1546 // eg - id = "I" returns "IA01" instead of "IAN"
1547 // We either need to provide a custom comparison operator, or recurse this function if !isalpha further down the string.
1548 // (Currenly fixed with recursion).
1550 if(a4 != a2) { // A-Z match - preferred
1551 //cout << "A-Z match!\n";
1552 if(a4->getId().size() - id.size() > 2) {
1553 // Check for numbers further on
1554 for(unsigned int i=id.size(); i<a4->getId().size(); ++i) {
1555 if(!isalpha(a4->getId()[i])) {
1556 //cout << "SUBSTR is " << (a4->getId()).substr(0, i) << '\n';
1557 return(FindFirstAptById(a4->getId().substr(0, i), multi, exact));
1562 } else if(a1 != a2) { // 0-9 match
1563 //cout << "0-9 match!\n";
1564 if(a1->getId().size() - id.size() > 2) {
1565 // Check for numbers further on
1566 for(unsigned int i=id.size(); i<a1->getId().size(); ++i) {
1567 if(!isalpha(a1->getId()[i])) {
1568 //cout << "SUBSTR2 is " << (a4->getId()).substr(0, i) << '\n';
1569 return(FindFirstAptById(a1->getId().substr(0, i), multi, exact));
1574 } else { // No match
1581 FGNavRecord* DCLGPS::FindClosestVor(double lat_rad, double lon_rad) {
1582 return(globals->get_navlist()->findClosest(lon_rad, lat_rad, 0.0, FG_NAV_VOR));
1585 //----------------------------------------------------------------------------------------------------------
1587 // Takes lat and lon in RADIANS!!!!!!!
1588 double DCLGPS::GetMagHeadingFromTo(double latA, double lonA, double latB, double lonB) {
1589 double h = GetGreatCircleCourse(latA, lonA, latB, lonB);
1590 h *= SG_RADIANS_TO_DEGREES;
1591 // TODO - use the real altitude below instead of 0.0!
1592 //cout << "MagVar = " << sgGetMagVar(_gpsLon, _gpsLat, 0.0, _time->getJD()) * SG_RADIANS_TO_DEGREES << '\n';
1593 h -= sgGetMagVar(_gpsLon, _gpsLat, 0.0, _time->getJD()) * SG_RADIANS_TO_DEGREES;
1594 while(h >= 360.0) h -= 360.0;
1595 while(h < 0.0) h += 360.0;
1599 // ---------------- Great Circle formulae from "The Aviation Formulary" -------------
1600 // Note that all of these assume that the world is spherical.
1602 double Rad2Nm(double theta) {
1603 return(((180.0*60.0)/SG_PI)*theta);
1606 double Nm2Rad(double d) {
1607 return((SG_PI/(180.0*60.0))*d);
1612 The great circle distance d between two points with coordinates {lat1,lon1} and {lat2,lon2} is given by:
1614 d=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))
1616 A mathematically equivalent formula, which is less subject to rounding error for short distances is:
1618 d=2*asin(sqrt((sin((lat1-lat2)/2))^2 +
1619 cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
1623 // Returns distance in nm, takes lat & lon in RADIANS
1624 double DCLGPS::GetGreatCircleDistance(double lat1, double lon1, double lat2, double lon2) const {
1625 double d = 2.0 * asin(sqrt(((sin((lat1-lat2)/2.0))*(sin((lat1-lat2)/2.0))) +
1626 cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2.0))*(sin((lon1-lon2)/2.0))));
1630 // fmod dosen't do what we want :-(
1631 static double mod(double d1, double d2) {
1632 return(d1 - d2*floor(d1/d2));
1635 // Returns great circle course from point 1 to point 2
1636 // Input and output in RADIANS.
1637 double DCLGPS::GetGreatCircleCourse (double lat1, double lon1, double lat2, double lon2) const {
1640 // Special case the poles
1641 if(cos(lat1) < SG_EPSILON) {
1643 // Starting from North Pole
1646 // Starting from South Pole
1650 // Urgh - the formula below is for negative lon +ve !!!???
1651 double d = GetGreatCircleDistance(lat1, lon1, lat2, lon2);
1652 cout << "d = " << d;
1654 //cout << ", d_theta = " << d;
1655 //cout << ", and d = " << Rad2Nm(d) << ' ';
1656 if(sin(lon2 - lon1) < 0) {
1658 h = acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)));
1661 h = 2.0 * SG_PI - acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)));
1664 cout << h * SG_RADIANS_TO_DEGREES << '\n';
1667 return( mod(atan2(sin(lon2-lon1)*cos(lat2),
1668 cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon2-lon1)),
1672 // Return a position on a radial from wp1 given distance d (nm) and magnetic heading h (degrees)
1673 // Note that d should be less that 1/4 Earth diameter!
1674 GPSWaypoint DCLGPS::GetPositionOnMagRadial(const GPSWaypoint& wp1, double d, double h) {
1675 h += sgGetMagVar(wp1.lon, wp1.lat, 0.0, _time->getJD()) * SG_RADIANS_TO_DEGREES;
1676 return(GetPositionOnRadial(wp1, d, h));
1679 // Return a position on a radial from wp1 given distance d (nm) and TRUE heading h (degrees)
1680 // Note that d should be less that 1/4 Earth diameter!
1681 GPSWaypoint DCLGPS::GetPositionOnRadial(const GPSWaypoint& wp1, double d, double h) {
1682 while(h < 0.0) h += 360.0;
1683 while(h > 360.0) h -= 360.0;
1685 h *= SG_DEGREES_TO_RADIANS;
1686 d *= (SG_PI / (180.0 * 60.0));
1688 double lat=asin(sin(wp1.lat)*cos(d)+cos(wp1.lat)*sin(d)*cos(h));
1691 lon=wp1.lon; // endpoint a pole
1693 lon=mod(wp1.lon+asin(sin(h)*sin(d)/cos(lat))+SG_PI,2*SG_PI)-SG_PI;
1699 wp.type = GPS_WP_VIRT;
1703 // Returns cross-track deviation in Nm.
1704 double DCLGPS::CalcCrossTrackDeviation() const {
1705 return(CalcCrossTrackDeviation(_fromWaypoint, _activeWaypoint));
1708 // Returns cross-track deviation of the current position between two arbitary waypoints in nm.
1709 double DCLGPS::CalcCrossTrackDeviation(const GPSWaypoint& wp1, const GPSWaypoint& wp2) const {
1710 //if(wp1 == NULL || wp2 == NULL) return(0.0);
1711 if(wp1.id.empty() || wp2.id.empty()) return(0.0);
1712 double xtd = asin(sin(Nm2Rad(GetGreatCircleDistance(wp1.lat, wp1.lon, _gpsLat, _gpsLon)))
1713 * sin(GetGreatCircleCourse(wp1.lat, wp1.lon, _gpsLat, _gpsLon) - GetGreatCircleCourse(wp1.lat, wp1.lon, wp2.lat, wp2.lon)));
1714 return(Rad2Nm(xtd));