]> git.mxchange.org Git - flightgear.git/blob - src/Autopilot/route_mgr.hxx
Ron Jensen: fixed a potential NaN and Segfault in JSBSim propeller code
[flightgear.git] / src / Autopilot / route_mgr.hxx
1 // route_mgr.hxx - manage a route (i.e. a collection of waypoints)
2 //
3 // Written by Curtis Olson, started January 2004.
4 //
5 // Copyright (C) 2004  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifndef _ROUTE_MGR_HXX
25 #define _ROUTE_MGR_HXX 1
26
27 #include <simgear/props/props.hxx>
28 #include <simgear/route/waypoint.hxx>
29 #include <simgear/structure/subsystem_mgr.hxx>
30
31 #include <Navaids/route.hxx>
32
33 // forward decls
34 class SGPath;
35 class PropertyWatcher;
36
37 class FGAirport;
38 class FGRunway;
39
40 typedef SGSharedPtr<FGAirport> FGAirportRef;
41
42 /**
43  * Top level route manager class
44  * 
45  */
46
47 class FGRouteMgr : public SGSubsystem
48 {
49 public:
50   FGRouteMgr();
51   ~FGRouteMgr();
52
53   void init ();
54   void postinit ();
55   void bind ();
56   void unbind ();
57   void update (double dt);
58
59   void insertWayptAtIndex(flightgear::Waypt* aWpt, int aIndex);
60   flightgear::WayptRef removeWayptAtIndex(int index);
61   
62   void clearRoute();
63   
64   typedef enum {
65     ROUTE_HIGH_AIRWAYS, ///< high-level airways routing
66     ROUTE_LOW_AIRWAYS, ///< low-level airways routing
67     ROUTE_VOR ///< VOR-VOR routing
68   } RouteType;
69   
70   /**
71    * Insert waypoints from index-1 to index. In practice this means you can
72    * 'fill in the gaps' between defined waypoints. If index=0, the departure
73    * airport is used as index-1; if index is -1, the destination airport is
74    * used as the final waypoint.
75    */
76   bool routeToIndex(int index, RouteType aRouteType);
77
78   void autoRoute();
79         
80   bool isRouteActive() const;
81          
82   int currentIndex() const
83     { return _currentIndex; }
84     
85   flightgear::Waypt* currentWaypt() const;
86   flightgear::Waypt* nextWaypt() const;
87   flightgear::Waypt* previousWaypt() const;
88   
89   const flightgear::WayptVec& waypts() const
90     { return _route; }
91   
92   int numWaypts() const
93     { return _route.size(); }
94     
95   flightgear::Waypt* wayptAtIndex(int index) const;
96              
97   SGPropertyNode_ptr wayptNodeAtIndex(int index) const;
98              
99   /**
100    * Find a waypoint in the route, by position, and return its index, or
101    * -1 if no matching waypoint was found in the route.
102    */
103   int findWayptIndex(const SGGeod& aPos) const;
104         
105   /**
106    * Activate a built route. This checks for various mandatory pieces of
107    * data, such as departure and destination airports, and creates waypoints
108    * for them on the route structure.
109    *
110    * returns true if the route was activated successfully, or false if the
111    * route could not be activated for some reason
112    */
113   bool activate();
114
115   /**
116    * Step to the next waypoint on the active route
117    */
118   void sequence();
119   
120   /**
121    * Set the current waypoint to the specified index.
122    */
123   void jumpToIndex(int index);
124   
125   bool saveRoute(const SGPath& p);
126   bool loadRoute(const SGPath& p);
127   
128   /**
129    * Helper command to setup current airport/runway if necessary
130    */
131   void initAtPosition();
132   
133     /**
134      * Create a WayPoint from a string in the following format:
135      *  - simple identifier
136      *  - decimal-lon,decimal-lat
137      *  - airport-id/runway-id
138      *  - navaid/radial-deg/offset-nm
139      */
140     flightgear::WayptRef waypointFromString(const std::string& target);
141     
142     FGAirportRef departureAirport() const;
143     FGAirportRef destinationAirport() const;
144     
145     FGRunway* departureRunway() const;
146     FGRunway* destinationRunway() const;
147 private:
148   flightgear::WayptVec _route;
149   int _currentIndex;
150   
151     time_t _takeoffTime;
152     time_t _touchdownTime;
153     FGAirportRef _departure;
154     FGAirportRef _destination;
155     
156     // automatic inputs
157     SGPropertyNode_ptr lon;
158     SGPropertyNode_ptr lat;
159     SGPropertyNode_ptr alt;
160     SGPropertyNode_ptr magvar;
161     
162     // automatic outputs    
163     SGPropertyNode_ptr departure; ///< departure airport information
164     SGPropertyNode_ptr destination; ///< destination airport information
165     SGPropertyNode_ptr alternate; ///< alternate airport information
166     SGPropertyNode_ptr cruise; ///< cruise information
167     
168     SGPropertyNode_ptr totalDistance;
169     SGPropertyNode_ptr ete;
170     SGPropertyNode_ptr elapsedFlightTime;
171     
172     SGPropertyNode_ptr active;
173     SGPropertyNode_ptr airborne;
174     
175     SGPropertyNode_ptr wp0;
176     SGPropertyNode_ptr wp1;
177     SGPropertyNode_ptr wpn;
178     
179     
180     SGPropertyNode_ptr _pathNode;
181     SGPropertyNode_ptr _currentWpt;
182     
183     /// integer property corresponding to the RouteType enum
184     SGPropertyNode_ptr _routingType;
185     
186     /** 
187      * Signal property to notify people that the route was edited
188      */
189     SGPropertyNode_ptr _edited;
190     
191     /**
192      * Signal property to notify when the last waypoint is reached
193      */
194     SGPropertyNode_ptr _finished;
195     
196     void setETAPropertyFromDistance(SGPropertyNode_ptr aProp, double aDistance);
197     
198     class InputListener : public SGPropertyChangeListener {
199     public:
200         InputListener(FGRouteMgr *m) : mgr(m) {}
201         virtual void valueChanged (SGPropertyNode * prop);
202     private:
203         FGRouteMgr *mgr;
204     };
205
206     SGPropertyNode_ptr input;
207     SGPropertyNode_ptr weightOnWheels;
208     
209     InputListener *listener;
210     SGPropertyNode_ptr mirror;    
211     
212     void departureChanged();
213     void buildDeparture(flightgear::WayptRef enroute, flightgear::WayptVec& wps);
214     
215     void arrivalChanged();
216     void buildArrival(flightgear::WayptRef enroute, flightgear::WayptVec& wps);
217     
218     /**
219      * Helper to keep various pieces of state in sync when the route is
220      * modified (waypoints added, inserted, removed). Notably, this fires the
221      * 'edited' signal.
222      */
223     void waypointsChanged();
224     
225     void update_mirror();
226     
227     void currentWaypointChanged();
228     
229     /**
230      * Parse a route/wp node (from a saved, property-lsit formatted route)
231      */
232     void parseRouteWaypoint(SGPropertyNode* aWP);
233     
234     /**
235      * Check if we've reached the final waypoint. 
236      * Returns true if we have.
237      */
238     bool checkFinished();
239     
240     
241     bool loadPlainTextRoute(const SGPath& path);
242     
243     void loadVersion1XMLRoute(SGPropertyNode_ptr routeData);
244     void loadVersion2XMLRoute(SGPropertyNode_ptr routeData);
245     void loadXMLRouteHeader(SGPropertyNode_ptr routeData);
246     flightgear::WayptRef parseVersion1XMLWaypt(SGPropertyNode* aWP);
247     
248     /**
249      * Predicate for helping the UI - test if at least one waypoint was
250      * entered by the user (as opposed to being generated by the route-manager)
251      */
252     bool haveUserWaypoints() const;
253     
254 // tied getters and setters
255     const char* getDepartureICAO() const;
256     const char* getDepartureName() const;
257     void setDepartureICAO(const char* aIdent);
258     
259     const char* getDestinationICAO() const;
260     const char* getDestinationName() const;
261     void setDestinationICAO(const char* aIdent);
262
263     PropertyWatcher* _departureWatcher;
264     PropertyWatcher* _arrivalWatcher;
265 };
266
267
268 #endif // _ROUTE_MGR_HXX