]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/rnav_waypt_controller.cxx
Support for multiple data dirs.
[flightgear.git] / src / Instrumentation / rnav_waypt_controller.cxx
1 // rnav_waypt_controller.cxx - Waypoint-specific behaviours for RNAV systems
2 // Written by James Turner, started 2009.
3 //
4 // Copyright (C) 2009  Curtis L. Olson
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
20 #include "rnav_waypt_controller.hxx"
21
22 #include <cassert>
23
24 #include <simgear/sg_inlines.h>
25 #include <simgear/structure/exception.hxx>
26
27 #include <Airports/runways.hxx>
28
29 namespace flightgear
30 {
31
32 const double KNOTS_TO_METRES_PER_SECOND = SG_NM_TO_METER / 3600.0;
33
34 double pmod(double x, double y)
35 {
36   if (x < 0.0) {
37     return -fmod(x, y);
38   } else {
39     return fmod(x,y);
40   }
41 }
42
43 // implementation of
44 // http://williams.best.vwh.net/avform.htm#Intersection
45 bool geocRadialIntersection(const SGGeoc& a, double r1, const SGGeoc& b, double r2, SGGeoc& result)
46 {
47   double crs13 = r1 * SG_DEGREES_TO_RADIANS;
48   double crs23 = r2 * SG_DEGREES_TO_RADIANS;
49   double dst12 = SGGeodesy::distanceRad(a, b);
50   
51   //IF sin(lon2-lon1)<0
52   // crs12=acos((sin(lat2)-sin(lat1)*cos(dst12))/(sin(dst12)*cos(lat1)))
53   // crs21=2.*pi-acos((sin(lat1)-sin(lat2)*cos(dst12))/(sin(dst12)*cos(lat2)))
54   // ELSE
55   // crs12=2.*pi-acos((sin(lat2)-sin(lat1)*cos(dst12))/(sin(dst12)*cos(lat1)))
56   // crs21=acos((sin(lat1)-sin(lat2)*cos(dst12))/(sin(dst12)*cos(lat2)))
57   // ENDIF
58   
59   
60  // double diffLon = b.getLongitudeRad() - a.getLongitudeRad();
61   
62   double sinLat1 = sin(a.getLatitudeRad());
63   double cosLat1 = cos(a.getLatitudeRad());
64  // double sinLat2 = sin(b.getLatitudeRad());
65   //double cosLat2 = cos(b.getLatitudeRad());
66   double sinDst12 = sin(dst12);
67   double cosDst12 = cos(dst12);
68   
69   double crs12 = SGGeodesy::courseRad(a, b),
70     crs21 = SGGeodesy::courseRad(b, a);
71     
72   //double degCrs12 = crs12 * SG_RADIANS_TO_DEGREES;
73   //double degCrs21 = crs21 * SG_RADIANS_TO_DEGREES;
74     
75  /* 
76   if (sin(diffLon) < 0.0) {
77     crs12 = acos((sinLat2 - sinLat1 * cosDst12) / (sinDst12 * cosLat1));
78     crs21 = SGMiscd::twopi() - acos((sinLat1 - sinLat2*cosDst12)/(sinDst12*cosLat2));
79   } else {
80     crs12 = SGMiscd::twopi() - acos((sinLat2 - sinLat1 * cosDst12)/(sinDst12 * cosLat1));
81     crs21 = acos((sinLat1 - sinLat2 * cosDst12)/(sinDst12 * cosLat2));
82   }
83   */
84   
85   double ang1 = SGMiscd::normalizeAngle2(crs13-crs12);
86   double ang2 = SGMiscd::normalizeAngle2(crs21-crs23);
87     
88   if ((sin(ang1) == 0.0) && (sin(ang2) == 0.0)) {
89     SG_LOG(SG_INSTR, SG_WARN, "geocRadialIntersection: infinity of intersections");
90     return false;
91   }
92   
93   if ((sin(ang1)*sin(ang2))<0.0) {
94     SG_LOG(SG_INSTR, SG_WARN, "geocRadialIntersection: intersection ambiguous");
95     return false;
96   }
97   
98   ang1 = fabs(ang1);
99   ang2 = fabs(ang2);
100
101   //ang3=acos(-cos(ang1)*cos(ang2)+sin(ang1)*sin(ang2)*cos(dst12)) 
102   //dst13=atan2(sin(dst12)*sin(ang1)*sin(ang2),cos(ang2)+cos(ang1)*cos(ang3))
103   //lat3=asin(sin(lat1)*cos(dst13)+cos(lat1)*sin(dst13)*cos(crs13))
104   
105   //lon3=mod(lon1-dlon+pi,2*pi)-pi
106
107   double ang3 = acos(-cos(ang1) * cos(ang2) + sin(ang1) * sin(ang2) * cosDst12);
108   double dst13 = atan2(sinDst12 * sin(ang1) * sin(ang2), cos(ang2) + cos(ang1)*cos(ang3));
109
110   SGGeoc pt3;
111   SGGeodesy::advanceRadM(a, crs13, dst13 * SG_RAD_TO_NM * SG_NM_TO_METER, pt3);
112
113   double lat3 = asin(sinLat1 * cos(dst13) + cosLat1 * sin(dst13) * cos(crs13));
114   
115   //dlon=atan2(sin(crs13)*sin(dst13)*cos(lat1),cos(dst13)-sin(lat1)*sin(lat3))
116   double dlon = atan2(sin(crs13)*sin(dst13)*cosLat1, cos(dst13)- (sinLat1 * sin(lat3)));
117   double lon3 = SGMiscd::normalizeAngle(-a.getLongitudeRad()-dlon);
118   
119   result = SGGeoc::fromRadM(-lon3, lat3, a.getRadiusM());
120   //result = pt3;
121   return true;
122 }
123
124 ////////////////////////////////////////////////////////////////////////////
125
126 WayptController::~WayptController()
127 {
128 }
129
130 void WayptController::init()
131 {
132 }
133
134 void WayptController::setDone()
135 {
136   if (_isDone) {
137     SG_LOG(SG_AUTOPILOT, SG_WARN, "already done @ WayptController::setDone");
138   }
139   
140   _isDone = true;
141 }
142
143 double WayptController::timeToWaypt() const
144 {
145   double gs = _rnav->groundSpeedKts();
146   if (gs < 1.0) {
147     return -1.0; // stationary
148   }
149   
150   gs*= KNOTS_TO_METRES_PER_SECOND;
151   return (distanceToWayptM() / gs);
152 }
153
154 //////////////
155
156 class BasicWayptCtl : public WayptController
157 {
158 public:
159   BasicWayptCtl(RNAV* aRNAV, const WayptRef& aWpt) :
160     WayptController(aRNAV, aWpt),
161     _distanceM(0.0),
162     _courseDev(0.0)
163   {
164     if (aWpt->flag(WPT_DYNAMIC)) {
165       throw sg_exception("BasicWayptCtrl doesn't work with dynamic waypoints");
166     }
167   }
168   
169   virtual void init()
170   {
171     _targetTrack = SGGeodesy::courseDeg(_rnav->position(), _waypt->position());
172   }
173
174   virtual void update()
175   {
176     double brg, az2;
177     SGGeodesy::inverse(_rnav->position(), _waypt->position(), brg, az2, _distanceM); 
178     _courseDev = brg - _targetTrack;
179     SG_NORMALIZE_RANGE(_courseDev, -180.0, 180.0);
180     
181     if ((fabs(_courseDev) > 90.0) && (_distanceM < _rnav->overflightArmDistanceM())) {
182       setDone();
183     }
184   } 
185
186   virtual double distanceToWayptM() const
187   {
188     return _distanceM;
189   }
190   
191   virtual double xtrackErrorNm() const
192   {
193     double x = sin(courseDeviationDeg() * SG_DEGREES_TO_RADIANS) * _distanceM;
194     return x * SG_METER_TO_NM;
195   }
196   
197   virtual bool toFlag() const
198   {
199     return (fabs(_courseDev) < 90.0);
200   }
201   
202   virtual double courseDeviationDeg() const
203   {
204     return _courseDev;
205   }
206   
207   virtual double trueBearingDeg() const
208   {
209     return SGGeodesy::courseDeg(_rnav->position(), _waypt->position());
210   }
211   
212   virtual SGGeod position() const
213   {
214     return _waypt->position();
215   }
216
217 private:
218   double _distanceM;
219   double _courseDev;
220 };
221
222 /**
223  * Special controller for runways. For runways, we want very narrow deviation
224  * constraints, and to understand that any point along the paved area is
225  * equivalent to being 'at' the runway.
226  */
227 class RunwayCtl : public WayptController
228 {
229 public:
230   RunwayCtl(RNAV* aRNAV, const WayptRef& aWpt) :
231     WayptController(aRNAV, aWpt),
232     _runway(NULL),
233     _distanceM(0.0),
234     _courseDev(0.0)
235   {
236   }
237   
238   virtual void init()
239   {
240     _runway = static_cast<RunwayWaypt*>(_waypt.get())->runway();
241     _targetTrack = _runway->headingDeg();
242   }
243
244   virtual void update()
245   {
246     double brg, az2;
247     // use the far end of the runway for course deviation calculations. 
248     // this should do the correct thing both for takeoffs (including entering 
249     // the runway at a taxiway after the threshold) and also landings.
250     // seperately compute the distance to the threshold for timeToWaypt calc
251     SGGeodesy::inverse(_rnav->position(), _runway->end(), brg, az2, _distanceM); 
252     double _courseDev = brg - _targetTrack;
253     SG_NORMALIZE_RANGE(_courseDev, -180.0, 180.0);
254     
255     if ((fabs(_courseDev) > 90.0) && (_distanceM < _rnav->overflightArmDistanceM())) {
256       setDone();
257     }
258   } 
259   
260   virtual double distanceToWayptM() const
261   {
262     return SGGeodesy::distanceM(_rnav->position(), _runway->threshold());
263   }
264   
265   virtual double xtrackErrorNm() const
266   {
267     double x = sin(_courseDev * SG_RADIANS_TO_DEGREES) * _distanceM;
268     return x * SG_METER_TO_NM;
269   }
270
271   virtual double courseDeviationDeg() const
272   {
273     return _courseDev;
274   }
275
276   virtual double trueBearingDeg() const
277   {
278     // as in update(), use runway->end here, so the value remains
279     // sensible whether taking off or landing.
280     return SGGeodesy::courseDeg(_rnav->position(), _runway->end());
281   }
282   
283   virtual SGGeod position() const
284   {
285     return _runway->threshold();
286   }
287 private:
288   FGRunway* _runway;
289   double _distanceM;
290   double _courseDev;
291 };
292
293 class ConstHdgToAltCtl : public WayptController
294 {
295 public:
296   ConstHdgToAltCtl(RNAV* aRNAV, const WayptRef& aWpt) :
297     WayptController(aRNAV, aWpt)
298     
299   {
300     if (_waypt->type() != "hdgToAlt") {
301       throw sg_exception("invalid waypoint type", "ConstHdgToAltCtl ctor");
302     }
303     
304     if (_waypt->altitudeRestriction() == RESTRICT_NONE) {
305       throw sg_exception("invalid waypoint alt restriction", "ConstHdgToAltCtl ctor");
306     }
307   }
308
309   virtual void init()
310   {
311     HeadingToAltitude* w = (HeadingToAltitude*) _waypt.get();
312     _targetTrack = w->headingDegMagnetic() + _rnav->magvarDeg();
313   }
314   
315   virtual void update()
316   {
317     double curAlt = _rnav->position().getElevationFt();
318     
319     switch (_waypt->altitudeRestriction()) {
320     case RESTRICT_AT: 
321     case RESTRICT_COMPUTED:  
322     {
323       double d = curAlt - _waypt->altitudeFt();
324       if (fabs(d) < 50.0) {
325         SG_LOG(SG_INSTR, SG_INFO, "ConstHdgToAltCtl, reached target altitude " << _waypt->altitudeFt());
326         setDone();
327       }
328     } break;
329       
330     case RESTRICT_ABOVE:
331       if (curAlt >= _waypt->altitudeFt()) {
332         SG_LOG(SG_INSTR, SG_INFO, "ConstHdgToAltCtl, above target altitude " << _waypt->altitudeFt());
333         setDone();
334       }
335       break;
336       
337     case RESTRICT_BELOW:
338       if (curAlt <= _waypt->altitudeFt()) {
339         SG_LOG(SG_INSTR, SG_INFO, "ConstHdgToAltCtl, below target altitude " << _waypt->altitudeFt());
340         setDone();
341       }
342       break;
343     
344     default:
345       break;
346     }
347   }
348   
349   virtual double timeToWaypt() const
350   {
351     double d = fabs(_rnav->position().getElevationFt() - _waypt->altitudeFt());
352     return (d / _rnav->vspeedFPM()) * 60.0; // low pass filter here, probably
353   }
354   
355   virtual double distanceToWayptM() const
356   {
357     double gsMsec = _rnav->groundSpeedKts() * KNOTS_TO_METRES_PER_SECOND;
358     return timeToWaypt() * gsMsec;
359   }
360   
361   virtual SGGeod position() const
362   {
363     SGGeod p;
364     double az2;
365     SGGeodesy::direct(_rnav->position(), _targetTrack, distanceToWayptM(), p, az2);
366     return p;
367   }
368 };
369
370 class InterceptCtl : public WayptController
371 {
372 public:
373   InterceptCtl(RNAV* aRNAV, const WayptRef& aWpt) :
374     WayptController(aRNAV, aWpt),
375     _trueRadial(0.0)
376   {
377     if (_waypt->type() != "radialIntercept") {
378       throw sg_exception("invalid waypoint type", "InterceptCtl ctor");
379     }
380   }
381
382   virtual void init()
383   {
384     RadialIntercept* w = (RadialIntercept*) _waypt.get();
385     _trueRadial = w->radialDegMagnetic() + _rnav->magvarDeg();
386     _targetTrack = w->courseDegMagnetic() + _rnav->magvarDeg();
387   }
388   
389   virtual void update()
390   {
391     // note we want the outbound radial from the waypt, hence the ordering
392     // of arguments to courseDeg
393     double r = SGGeodesy::courseDeg(_waypt->position(), _rnav->position());
394     SG_LOG(SG_AUTOPILOT, SG_INFO, "current radial=" << r);
395     if (fabs(r - _trueRadial) < 0.5) {
396       SG_LOG(SG_INSTR, SG_INFO, "InterceptCtl, intercepted radial " << _trueRadial);
397       setDone();
398     }
399   }
400   
401   virtual double distanceToWayptM() const
402   {
403     return SGGeodesy::distanceM(_rnav->position(), position());
404   }
405
406   virtual SGGeod position() const
407   {
408     SGGeoc c;
409     geocRadialIntersection(SGGeoc::fromGeod(_rnav->position()), _rnav->trackDeg(), 
410       SGGeoc::fromGeod(_waypt->position()), _trueRadial, c);
411     return SGGeod::fromGeoc(c);
412   }
413 private:
414   double _trueRadial;
415 };
416
417 class DMEInterceptCtl : public WayptController
418 {
419 public:
420   DMEInterceptCtl(RNAV* aRNAV, const WayptRef& aWpt) :
421     WayptController(aRNAV, aWpt),
422     _dme(NULL),
423     _distanceNm(0.0)
424   {
425     if (_waypt->type() != "dmeIntercept") {
426       throw sg_exception("invalid waypoint type", "DMEInterceptCtl ctor");
427     }
428   }
429
430   virtual void init()
431   {
432     _dme  = (DMEIntercept*) _waypt.get();
433     _targetTrack = _dme->courseDegMagnetic() + _rnav->magvarDeg();
434   }
435   
436   virtual void update()
437   {
438     _distanceNm = SGGeodesy::distanceNm(_rnav->position(), _dme->position());
439     double d = fabs(_distanceNm - _dme->dmeDistanceNm());
440     if (d < 0.1) {
441       SG_LOG(SG_INSTR, SG_INFO, "DMEInterceptCtl, intercepted DME " << _dme->dmeDistanceNm());
442       setDone();
443     }
444   }
445   
446   virtual double distanceToWayptM() const
447   {
448     return fabs(_distanceNm - _dme->dmeDistanceNm()) * SG_NM_TO_METER;
449   }
450   
451   virtual SGGeod position() const
452   {
453     SGGeod p;
454     double az2;
455     SGGeodesy::direct(_rnav->position(), _targetTrack, distanceToWayptM(), p, az2);
456     return p;
457   }
458
459 private:
460   DMEIntercept* _dme;
461   double _distanceNm;
462 };
463
464 class HoldCtl : public WayptController
465 {
466 public:
467   HoldCtl(RNAV* aRNAV, const WayptRef& aWpt) :
468     WayptController(aRNAV, aWpt)
469     
470   {
471
472   }
473
474   virtual void init()
475   {
476   }
477   
478   virtual void update()
479   {
480     // fly inbound / outbound sides, or execute the turn
481   #if 0
482     if (inTurn) {
483     
484       targetTrack += dt * turnRateSec * turnDirection;
485       if (inbound) {
486         if .. targetTrack has passed inbound radial, doen with this turn
487       } else {
488         if target track has passed reciprocal radial done with turn
489       }
490     } else {
491       check time / distance elapsed
492       
493       if (sideDone) {
494         inTurn = true;
495         inbound = !inbound;
496         nextHeading = inbound;
497         if (!inbound) {
498           nextHeading += 180.0;
499           SG_NORMALIZE_RANGE(nextHeading, 0.0, 360.0);
500         }
501       }
502     
503     }
504   
505   #endif
506     setDone();
507   }
508   
509   virtual double distanceToWayptM() const
510   {
511     return -1.0;
512   }
513
514   virtual SGGeod position() const
515   {
516     return _waypt->position();
517   }
518 };
519
520 class VectorsCtl : public WayptController
521 {
522 public:
523   VectorsCtl(RNAV* aRNAV, const WayptRef& aWpt) :
524     WayptController(aRNAV, aWpt)
525     
526   {
527   }
528
529   virtual void init()
530   {
531  
532   }
533   
534   virtual void update()
535   {
536     setDone();
537   }
538   
539   virtual double distanceToWayptM() const
540   {
541     return -1.0;
542   }
543   
544   virtual SGGeod position() const
545   {
546     return _waypt->position();
547   }
548
549 private:
550 };
551
552 ///////////////////////////////////////////////////////////////////////////////
553
554 DirectToController::DirectToController(RNAV* aRNAV, const WayptRef& aWpt, const SGGeod& aOrigin) :
555   WayptController(aRNAV, aWpt),
556   _origin(aOrigin),
557   _distanceM(0.0),
558   _courseDev(0.0)
559 {
560 }
561
562 void DirectToController::init()
563 {
564   if (_waypt->flag(WPT_DYNAMIC)) {
565     throw sg_exception("can't direct-to a dynamic waypoint");
566   }
567   
568   _targetTrack = SGGeodesy::courseDeg(_origin, _waypt->position());
569 }
570
571 void DirectToController::update()
572 {
573   double brg, az2;
574   SGGeodesy::inverse(_rnav->position(), _waypt->position(), brg, az2, _distanceM); 
575   _courseDev = brg - _targetTrack;
576   SG_NORMALIZE_RANGE(_courseDev, -180.0, 180.0);
577     
578   if ((fabs(_courseDev) > 90.0) && (_distanceM < _rnav->overflightArmDistanceM())) {
579     setDone();
580   }
581 }
582
583 double DirectToController::distanceToWayptM() const
584 {
585   return _distanceM;
586 }
587
588 double DirectToController::xtrackErrorNm() const
589 {
590   double x = sin(courseDeviationDeg() * SG_DEGREES_TO_RADIANS) * _distanceM;
591   return x * SG_METER_TO_NM;
592 }
593
594 double DirectToController::courseDeviationDeg() const
595 {
596   return _courseDev;
597 }
598
599 double DirectToController::trueBearingDeg() const
600 {
601   return SGGeodesy::courseDeg(_rnav->position(), _waypt->position());
602 }
603
604 SGGeod DirectToController::position() const
605 {
606   return _waypt->position();
607 }
608
609 ///////////////////////////////////////////////////////////////////////////////
610
611 OBSController::OBSController(RNAV* aRNAV, const WayptRef& aWpt) :
612   WayptController(aRNAV, aWpt),
613   _distanceM(0.0),
614   _courseDev(0.0)
615 {
616 }
617
618 void OBSController::init()
619 {
620   if (_waypt->flag(WPT_DYNAMIC)) {
621     throw sg_exception("can't use a dynamic waypoint for OBS mode");
622   }
623   
624   _targetTrack = _rnav->selectedMagCourse() + _rnav->magvarDeg();
625 }
626
627 void OBSController::update()
628 {
629   _targetTrack = _rnav->selectedMagCourse() + _rnav->magvarDeg();
630   double brg, az2;
631   SGGeodesy::inverse(_rnav->position(), _waypt->position(), brg, az2, _distanceM); 
632   _courseDev = brg - _targetTrack;
633   SG_NORMALIZE_RANGE(_courseDev, -180.0, 180.0);
634 }
635
636 bool OBSController::toFlag() const
637 {
638   return (fabs(_courseDev) < 90.0);
639 }
640
641 double OBSController::distanceToWayptM() const
642 {
643   return _distanceM;
644 }
645
646 double OBSController::xtrackErrorNm() const
647 {
648   double x = sin(_courseDev * SG_DEGREES_TO_RADIANS) * _distanceM;
649   return x * SG_METER_TO_NM;
650 }
651
652 double OBSController::courseDeviationDeg() const
653 {
654 //  if (fabs(_courseDev) > 90.0) {
655  //   double d = -_courseDev;
656  //   SG_NORMALIZE_RANGE(d, -90.0, 90.0);
657   //  return d;
658   //}
659   
660   return _courseDev;
661 }
662
663 double OBSController::trueBearingDeg() const
664 {
665   return SGGeodesy::courseDeg(_rnav->position(), _waypt->position());
666 }
667
668 SGGeod OBSController::position() const
669 {
670   return _waypt->position();
671 }
672
673 ///////////////////////////////////////////////////////////////////////////////
674
675 WayptController* WayptController::createForWaypt(RNAV* aRNAV, const WayptRef& aWpt)
676 {
677   if (!aWpt) {
678     throw sg_exception("Passed null waypt", "WayptController::createForWaypt");
679   }
680   
681   const std::string& wty(aWpt->type());
682   if (wty == "runway") {
683     return new RunwayCtl(aRNAV, aWpt);
684   }
685   
686   if (wty == "radialIntercept") {
687     return new InterceptCtl(aRNAV, aWpt);
688   }
689   
690   if (wty == "dmeIntercept") {
691     return new DMEInterceptCtl(aRNAV, aWpt);
692   }
693   
694   if (wty == "hdgToAlt") {
695     return new ConstHdgToAltCtl(aRNAV, aWpt);
696   }
697   
698   if (wty == "vectors") {
699     return new VectorsCtl(aRNAV, aWpt);
700   }
701   
702   if (wty == "hold") {
703     return new HoldCtl(aRNAV, aWpt);
704   }
705   
706   return new BasicWayptCtl(aRNAV, aWpt);
707 }
708
709 } // of namespace flightgear
710