1 // FGAIAircraft - FGAIBase-derived class creates an AI airplane
3 // Written by David Culp, started October 2003.
5 // Copyright (C) 2003 David P. Culp - davidculp2@comcast.net
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <simgear/math/point3d.hxx>
26 #include <Main/fg_props.hxx>
27 #include <Main/globals.hxx>
28 #include <Scenery/scenery.hxx>
34 #include "AIAircraft.hxx"
37 // accel, decel, climb_rate, descent_rate, takeoff_speed, climb_speed,
38 // cruise_speed, descent_speed, land_speed
40 const FGAIAircraft::PERF_STRUCT FGAIAircraft::settings[] = {
42 {2.0, 2.0, 450.0, 1000.0, 70.0, 80.0, 100.0, 80.0, 60.0},
44 {4.0, 2.0, 3000.0, 1500.0, 110.0, 180.0, 250.0, 200.0, 100.0},
46 {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0},
48 {7.0, 3.0, 4000.0, 2000.0, 150.0, 350.0, 500.0, 350.0, 150.0},
50 {5.0, 2.0, 3000.0, 1500.0, 140.0, 300.0, 430.0, 300.0, 130.0}
54 FGAIAircraft::FGAIAircraft(FGAIManager* mgr) {
56 _type_str = "aircraft";
63 // set heading and altitude locks
69 FGAIAircraft::~FGAIAircraft() {
73 bool FGAIAircraft::init() {
74 refuel_node = fgGetNode("systems/refuel/contact", true);
75 return FGAIBase::init();
78 void FGAIAircraft::bind() {
81 props->tie("controls/gear/gear-down",
82 SGRawValueMethods<FGAIAircraft,bool>(*this,
83 &FGAIAircraft::_getGearDown));
85 props->getNode("controls/lighting/landing-lights", true)
86 ->alias("controls/gear/gear-down");
89 void FGAIAircraft::unbind() {
92 props->untie("controls/gear/gear-down");
93 props->getNode("controls/lighting/landing-lights")->unalias();
97 void FGAIAircraft::update(double dt) {
104 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
110 void FGAIAircraft::Run(double dt) {
112 FGAIAircraft::dt = dt;
114 if (fp) ProcessFlightPlan(dt);
116 double turn_radius_ft;
117 double turn_circum_ft;
118 double speed_north_deg_sec;
119 double speed_east_deg_sec;
120 double dist_covered_ft;
124 double speed_diff = tgt_speed - speed;
125 if (fabs(speed_diff) > 0.2) {
126 if (speed_diff > 0.0) speed += performance->accel * dt;
127 if (speed_diff < 0.0) {
129 speed -= performance->decel * dt * 3;
131 speed -= performance->decel * dt;
136 // convert speed to degrees per second
137 speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
138 * speed * 1.686 / ft_per_deg_lat;
139 speed_east_deg_sec = sin( hdg / SG_RADIANS_TO_DEGREES )
140 * speed * 1.686 / ft_per_deg_lon;
143 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
144 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
146 // adjust heading based on current bank angle
148 turn_radius_ft = 0.088362 * speed * speed
149 / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
150 turn_circum_ft = SGD_2PI * turn_radius_ft;
151 dist_covered_ft = speed * 1.686 * dt;
152 alpha = dist_covered_ft / turn_circum_ft * 360.0;
153 hdg += alpha * sign( roll );
154 if ( hdg > 360.0 ) hdg -= 360.0;
155 if ( hdg < 0.0) hdg += 360.0;
158 // adjust target bank angle if heading lock engaged
160 double bank_sense = 0.0;
161 double diff = fabs(hdg - tgt_heading);
162 if (diff > 180) diff = fabs(diff - 360);
163 double sum = hdg + diff;
164 if (sum > 360.0) sum -= 360.0;
165 if (fabs(sum - tgt_heading) < 1.0) {
166 bank_sense = 1.0; // right turn
168 bank_sense = -1.0; // left turn
171 tgt_roll = diff * bank_sense;
173 tgt_roll = 30.0 * bank_sense;
177 // adjust bank angle, use 9 degrees per second
178 double bank_diff = tgt_roll - roll;
179 if (fabs(bank_diff) > 0.2) {
180 if (bank_diff > 0.0) roll += 9.0 * dt;
181 if (bank_diff < 0.0) roll -= 9.0 * dt;
184 // adjust altitude (meters) based on current vertical speed (fpm)
185 altitude += vs / 60.0 * dt;
186 pos.setelev(altitude * SG_FEET_TO_METER);
187 double altitude_ft = altitude;
189 // find target vertical speed if altitude lock engaged
190 if (alt_lock && use_perf_vs) {
191 if (altitude_ft < tgt_altitude) {
192 tgt_vs = tgt_altitude - altitude_ft;
193 if (tgt_vs > performance->climb_rate)
194 tgt_vs = performance->climb_rate;
196 tgt_vs = tgt_altitude - altitude_ft;
197 if (tgt_vs < (-performance->descent_rate))
198 tgt_vs = -performance->descent_rate;
202 if (alt_lock && !use_perf_vs) {
203 double max_vs = 4*(tgt_altitude - altitude);
205 if (tgt_altitude < altitude) min_vs = -100.0;
206 if ((fabs(tgt_altitude - altitude) < 1500.0) &&
207 (fabs(max_vs) < fabs(tgt_vs))) tgt_vs = max_vs;
208 if (fabs(tgt_vs) < fabs(min_vs)) tgt_vs = min_vs;
211 // adjust vertical speed
212 double vs_diff = tgt_vs - vs;
213 if (fabs(vs_diff) > 10.0) {
216 if (vs > tgt_vs) vs = tgt_vs;
219 if (vs < tgt_vs) vs = tgt_vs;
223 // match pitch angle to vertical speed
230 //###########################//
231 // do calculations for radar //
232 //###########################//
233 double range_ft2 = UpdateRadar(manager);
235 //************************************//
237 //************************************//
240 if ( (range_ft2 < 250.0 * 250.0) &&
242 (elevation > 0.0) ) {
243 refuel_node->setBoolValue(true);
245 refuel_node->setBoolValue(false);
251 void FGAIAircraft::AccelTo(double speed) {
256 void FGAIAircraft::PitchTo(double angle) {
262 void FGAIAircraft::RollTo(double angle) {
268 void FGAIAircraft::YawTo(double angle) {
273 void FGAIAircraft::ClimbTo(double altitude) {
274 tgt_altitude = altitude;
279 void FGAIAircraft::TurnTo(double heading) {
280 tgt_heading = heading;
285 double FGAIAircraft::sign(double x) {
286 if ( x < 0.0 ) { return -1.0; }
290 void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f) {
294 void FGAIAircraft::ProcessFlightPlan( double dt ) {
295 FGAIFlightPlan::waypoint* prev = 0; // the one behind you
296 FGAIFlightPlan::waypoint* curr = 0; // the one ahead
297 FGAIFlightPlan::waypoint* next = 0; // the next plus 1
298 prev = fp->getPreviousWaypoint();
299 curr = fp->getCurrentWaypoint();
300 next = fp->getNextWaypoint();
303 if (!prev) { //beginning of flightplan, do this initialization once
304 fp->IncrementWaypoint();
305 prev = fp->getPreviousWaypoint(); //first waypoint
306 curr = fp->getCurrentWaypoint(); //second waypoint
307 next = fp->getNextWaypoint(); //third waypoint (might not exist!)
308 setLatitude(prev->latitude);
309 setLongitude(prev->longitude);
310 setSpeed(prev->speed);
311 setAltitude(prev->altitude);
312 setHeading(fp->getBearing(prev->latitude, prev->longitude, curr));
313 if (next) fp->setLeadDistance(speed, hdg, curr, next);
315 if (curr->crossat > -1000.0) { //use a calculated descent/climb rate
317 tgt_vs = (curr->crossat - prev->altitude)/
318 (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/
319 6076.0/prev->speed*60.0);
320 tgt_altitude = curr->crossat;
323 tgt_altitude = prev->altitude;
325 alt_lock = hdg_lock = true;
326 no_roll = prev->on_ground;
327 //cout << "First waypoint: " << prev->name << endl;
328 //cout << " Target speed: " << tgt_speed << endl;
329 //cout << " Target altitude: " << tgt_altitude << endl;
330 //cout << " Target heading: " << tgt_heading << endl << endl;
332 } // end of initialization
334 // let's only process the flight plan every 100 ms.
335 if (dt_count < 0.1) {
340 // check to see if we've reached the lead point for our next turn
341 double dist_to_go = fp->getDistanceToGo(pos.lat(), pos.lon(), curr);
342 double lead_dist = fp->getLeadDistance();
343 if (lead_dist < (2*speed)) lead_dist = 2*speed; //don't skip over the waypoint
344 //cout << "dist_to_go: " << dist_to_go << ", lead_dist: " << lead_dist << endl;
346 if ( dist_to_go < lead_dist ) {
347 if (curr->finished) { //end of the flight plan, so terminate
351 // we've reached the lead-point for the waypoint ahead
352 if (next) tgt_heading = fp->getBearing(curr, next);
353 fp->IncrementWaypoint();
354 prev = fp->getPreviousWaypoint();
355 curr = fp->getCurrentWaypoint();
356 next = fp->getNextWaypoint();
357 if (next) fp->setLeadDistance(speed, tgt_heading, curr, next);
358 if (curr->crossat > -1000.0) {
360 tgt_vs = (curr->crossat - altitude)/
361 (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/6076.0/speed*60.0);
362 tgt_altitude = curr->crossat;
365 tgt_altitude = prev->altitude;
367 tgt_speed = prev->speed;
368 hdg_lock = alt_lock = true;
369 no_roll = prev->on_ground;
370 //cout << "Crossing waypoint: " << prev->name << endl;
371 //cout << " Target speed: " << tgt_speed << endl;
372 //cout << " Target altitude: " << tgt_altitude << endl;
373 //cout << " Target heading: " << tgt_heading << endl << endl;
375 double calc_bearing = fp->getBearing(pos.lat(), pos.lon(), curr);
376 double hdg_error = calc_bearing - tgt_heading;
377 if (fabs(hdg_error) > 1.0) {
378 TurnTo( calc_bearing );
386 bool FGAIAircraft::_getGearDown() const {
387 return ((props->getFloatValue("position/altitude-agl-ft") < 900.0)
388 && (props->getFloatValue("velocities/airspeed-kt")
389 < performance->land_speed*1.25));