1 // newauto.hxx -- autopilot defines and prototypes (very alpha)
3 // Started April 1998 Copyright (C) 1998
5 // Contributions by Jeff Goeke-Smith <jgoeke@voyager.net>
6 // Norman Vine <nhv@cape.com>
7 // Curtis Olson <curt@flightgear.org>
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <simgear/props/props.hxx>
31 #include <simgear/structure/subsystem_mgr.hxx>
32 #include <simgear/route/waypoint.hxx>
34 #include <Main/fg_props.hxx>
37 class FGAutopilot : public SGSubsystem
42 enum fgAutoHeadingMode {
43 FG_DG_HEADING_LOCK = 0, // follow bug on directional gryo (vacuum)
44 FG_TC_HEADING_LOCK = 1, // keep turn coordinator zero'd
45 FG_TRUE_HEADING_LOCK = 2, // lock to true heading (i.e. a perfect world)
46 FG_HEADING_NAV1 = 3, // follow nav1 radial
47 FG_HEADING_NAV2 = 4, // follow nav2 radial
48 FG_HEADING_WAYPOINT = 5 // track next waypoint
51 enum fgAutoAltitudeMode {
52 FG_ALTITUDE_LOCK = 0, // lock to a specific altitude (indicated)
53 FG_ALTITUDE_TERRAIN = 1, // try to maintain a specific AGL
54 FG_ALTITUDE_GS1 = 2, // follow glide slope 1
55 FG_ALTITUDE_GS2 = 3, // follow glide slope 2
56 FG_ALTITUDE_ARM = 4, // ascend to selected altitude
57 FG_TRUE_ALTITUDE_LOCK = 5, // lock to a specific true altitude
58 // (i.e. a perfect world)
59 FG_VERT_SPEED = 6 // ascend or descend at selected fpm
64 bool heading_hold; // the current state of the heading hold
65 bool altitude_hold; // the current state of the altitude hold
66 bool auto_throttle; // the current state of the auto throttle
68 fgAutoHeadingMode heading_mode;
69 fgAutoAltitudeMode altitude_mode;
71 SGWayPoint waypoint; // the waypoint the AP should steer to.
73 // double TargetLatitude; // the latitude the AP should steer to.
74 // double TargetLongitude; // the longitude the AP should steer to.
75 double TargetDistance; // the distance to Target.
76 double DGTargetHeading; // the apparent DG heading to steer towards.
77 double TargetHeading; // the true heading the AP should steer to.
78 double TargetAltitude; // altitude to hold
79 double TargetAGL; // the terrain separation
81 double TargetVertSpeed; // vertical speed to shoot for
83 double TargetSpeed; // speed to shoot for
84 double alt_error_accum; // altitude error accumulator
85 double climb_error_accum; // climb error accumulator (for GS)
86 double speed_error_accum; // speed error accumulator
88 double current_ap_throttle; // current ap stored throttle setting used to set all engines
89 double previous_speed; // used to detect acceleration rate
91 double TargetSlope; // the glide slope hold value
93 double MaxRoll ; // the max the plane can roll for the turn
94 double RollOut; // when the plane should roll out
95 // measured from Heading
96 double MaxAileron; // how far to move the aleroin from center
97 double RollOutSmooth; // deg to use for smoothing Aileron Control
98 double MaxElevator; // the maximum elevator allowed
99 double SlopeSmooth; // smoothing angle for elevator
101 // following for testing disengagement of autopilot upon pilot
102 // interaction with controls
105 double old_elevator_trim;
108 // manual controls override beyond this value
109 double disengage_threshold;
111 // For future cross track error adjust
115 // keeping these locally to save work inside main loop
116 char TargetLatitudeStr[64];
117 char TargetLongitudeStr[64];
118 char TargetLatLonStr[64];
119 char TargetWP1Str[64];
120 char TargetWP2Str[64];
121 char TargetWP3Str[64];
122 char TargetHeadingStr[64];
123 char TargetAltitudeStr[64];
126 SGPropertyNode *latitude_node;
127 SGPropertyNode *longitude_node;
128 SGPropertyNode *altitude_node;
129 SGPropertyNode *altitude_agl_node;
130 SGPropertyNode *vertical_speed_node;
131 SGPropertyNode *heading_node;
132 SGPropertyNode *indicated_heading_node;
133 SGPropertyNode *roll_node;
134 SGPropertyNode *pitch_node;
135 SGPropertyNode *airspeed_node;
137 SGPropertyNode *min_climb; // minimum climb speed
138 SGPropertyNode *best_climb; // best climb speed
139 SGPropertyNode *elevator_adj_factor; // factor to optimize altitude hold adjustments
140 SGPropertyNode *integral_contrib; // amount of contribution of the integral
141 // component of the pid
142 SGPropertyNode *zero_pitch_throttle; // amount of throttle at which the aircraft does not pitch up
143 SGPropertyNode *zero_pitch_trim_full_throttle; // amount of trim required to level at full throttle
144 SGPropertyNode *max_aileron_node; // maximum aileron setting range -1 ~ 1
145 SGPropertyNode *max_roll_node; // maximum roll setting in degrees
146 SGPropertyNode *roll_out_node; // start rollout offset from desired heading in degrees
147 SGPropertyNode *roll_out_smooth_node; // rollout smoothing offset in degrees
148 SGPropertyNode *throttle_adj_factor; // factor to optimize autothrottle adjustments
149 SGPropertyNode *throttle_integral; // amount of contribution of the integral
150 // component of the pid
151 SGPropertyNode *speed_change_node; // anticipated speed change
153 SGPropertyNode *TargetClimbRate; // target climb rate
154 SGPropertyNode *TargetDescentRate; // target decent rate
155 SGPropertyNode *current_throttle; // current throttle (engine 0)
156 SGPropertyNode *terrain_follow_factor; // modifies the climb rate to
157 // permit more control when using terrain following mode
168 ////////////////////////////////////////////////////////////////////
169 // Implementation of SGSubsystem.
170 ////////////////////////////////////////////////////////////////////
175 void update (double dt);
177 // Reset the autopilot system
180 void AltitudeSet( double new_altitude );
181 void AltitudeAdjust( double inc );
182 void HeadingAdjust( double inc );
183 void AutoThrottleAdjust( double inc );
185 void HeadingSet( double value );
187 inline bool get_HeadingEnabled() const { return heading_hold; }
188 inline void set_HeadingEnabled( bool value ) { heading_hold = value; }
189 inline fgAutoHeadingMode get_HeadingMode() const { return heading_mode; }
190 void set_HeadingMode( fgAutoHeadingMode mode );
192 inline bool get_AltitudeEnabled() const { return altitude_hold; }
193 inline void set_AltitudeEnabled( bool value ) { altitude_hold = value; }
194 inline fgAutoAltitudeMode get_AltitudeMode() const { return altitude_mode;}
195 void set_AltitudeMode( fgAutoAltitudeMode mode );
197 inline bool get_AutoThrottleEnabled() const { return auto_throttle; }
198 void set_AutoThrottleEnabled( bool value );
200 /* inline void set_WayPoint( const double lon, const double lat,
201 const double alt, const string s ) {
202 waypoint = SGWayPoint( lon, lat, alt, SGWayPoint::WGS84, "Current WP" );
204 inline double get_TargetLatitude() const {
205 return waypoint.get_target_lat();
207 inline double get_TargetLongitude() const {
208 return waypoint.get_target_lon();
210 inline void set_old_lat( double val ) { old_lat = val; }
211 inline void set_old_lon( double val ) { old_lon = val; }
212 inline double get_TargetHeading() const { return TargetHeading; }
213 inline void set_TargetHeading( double val ) { TargetHeading = val; }
214 inline double get_DGTargetHeading() const { return DGTargetHeading; }
215 inline void set_DGTargetHeading( double val ) { DGTargetHeading = val; }
216 inline double get_TargetDistance() const { return TargetDistance; }
217 inline void set_TargetDistance( double val ) { TargetDistance = val; }
218 inline double get_TargetAltitude() const { return TargetAltitude; }
219 inline void set_TargetAltitude( double val ) { TargetAltitude = val; }
220 inline double get_TargetSpeed() const { return TargetSpeed; }
221 inline void set_TargetSpeed( double val ) { TargetSpeed = val; }
222 inline double get_TargetAGL() const { return TargetAGL; }
223 inline void set_TargetAGL( double val ) { TargetAGL = val; }
224 inline double get_TargetClimbRate() const {
225 return TargetClimbRate->getFloatValue();
227 inline void set_TargetClimbRate( double val ) {
228 fgSetFloat( "/autopilot/config/target-climb-rate-fpm", val);
231 inline char *get_TargetLatitudeStr() { return TargetLatitudeStr; }
232 inline char *get_TargetLongitudeStr() { return TargetLongitudeStr; }
233 inline char *get_TargetWP1Str() { return TargetWP1Str; }
234 inline char *get_TargetWP2Str() { return TargetWP2Str; }
235 inline char *get_TargetWP3Str() { return TargetWP3Str; }
236 inline char *get_TargetHeadingStr() { return TargetHeadingStr; }
237 inline char *get_TargetAltitudeStr() { return TargetAltitudeStr; }
238 inline char *get_TargetLatLonStr() { return TargetLatLonStr; }
241 void MakeTargetLatLonStr( double lat, double lon );
242 void MakeTargetAltitudeStr( double altitude );
243 void MakeTargetHeadingStr( double bearing );
244 void MakeTargetWPStr( double distance );
245 void update_old_control_values();
248 inline double get_MaxRoll() {
249 return fgGetFloat( "/autopilot/config/max-roll-deg" );
251 inline double get_RollOut() {
252 return fgGetFloat( "/autopilot/config/roll-out-deg" );
254 inline double get_MaxAileron() {
255 return fgGetFloat( "/autopilot/config/max-aileron" );
257 inline double get_RollOutSmooth() {
258 return fgGetFloat( "/autopilot/config/roll-out-smooth-deg" );
260 inline void set_MaxRoll( double val ) {
261 fgSetFloat( "/autopilot/config/max-roll-deg", val );
263 inline void set_RollOut( double val ) {
264 fgSetFloat( "/autopilot/config/roll-out-deg", val );
266 inline void set_MaxAileron( double val ) {
267 fgSetFloat( "/autopilot/config/max-aileron", val );
269 inline void set_RollOutSmooth( double val ) {
270 fgSetFloat( "/autopilot/config/roll-out-smooth-deg", val );
275 bool getAPAltitudeLock () const;
276 void setAPAltitudeLock (bool lock);
277 double getAPAltitude () const;
278 void setAPAltitude (double altitude);
279 bool getAPGSLock () const;
280 void setAPGSLock (bool lock);
281 bool getAPVertSpeedLock () const;
282 void setAPVertSpeedLock (bool lock);
283 bool getAPTerrainLock () const;
284 void setAPTerrainLock (bool lock);
285 double getAPClimb () const;
286 void setAPClimb (double rate);
287 bool getAPHeadingLock () const;
288 void setAPHeadingLock (bool lock);
289 double getAPHeadingBug () const;
290 void setAPHeadingBug (double heading);
291 const char * getAPwaypoint () const;
292 void setAPwaypoint (const char * apt);
293 bool getAPWingLeveler () const;
294 void setAPWingLeveler (bool lock);
295 bool getAPNAV1Lock () const;
296 void setAPNAV1Lock (bool lock);
297 bool getAPAutoThrottleLock () const;
298 void setAPAutoThrottleLock (bool lock);
299 double getAPAutoThrottle () const;
300 void setAPAutoThrottle (double altitude);
301 double getAPRudderControl () const;
302 void setAPRudderControl (double value);
303 double getAPElevatorControl () const;
304 void setAPElevatorControl (double value);
305 double getAPThrottleControl () const;
306 void setAPThrottleControl (double value);
307 double getAPVertSpeed () const;
308 void setAPVertSpeed (double speed);
313 #define DEFAULT_AP_HEADING_LOCK FGAutopilot::FG_DG_HEADING_LOCK
314 #define DEFAULT_AP_ALTITUDE_LOCK FGAutopilot::FG_ALTITUDE_LOCK
315 //#define DEFAULT_AP_HEADING_LOCK FGAutopilot::FG_TRUE_HEADING_LOCK
316 //#define DEFAULT_AP_ALTITUDE_LOCK FGAutopilot::FG_TRUE_ALTITUDE_LOCK
319 * static functions for autopilot properties
322 #endif // _NEWAUTO_HXX