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");
90 void FGAIAircraft::unbind() {
93 props->untie("controls/gear/gear-down");
95 props->getNode("controls/lighting/landing-lights")->unalias();
100 void FGAIAircraft::update(double dt) {
102 FGAIBase::update(dt);
107 void FGAIAircraft::SetPerformance(const PERF_STRUCT *ps) {
113 void FGAIAircraft::Run(double dt) {
115 FGAIAircraft::dt = dt;
117 if (fp) ProcessFlightPlan(dt);
119 double turn_radius_ft;
120 double turn_circum_ft;
121 double speed_north_deg_sec;
122 double speed_east_deg_sec;
123 double dist_covered_ft;
127 double speed_diff = tgt_speed - speed;
128 if (fabs(speed_diff) > 0.2) {
129 if (speed_diff > 0.0) speed += performance->accel * dt;
130 if (speed_diff < 0.0) {
132 speed -= performance->decel * dt * 3;
134 speed -= performance->decel * dt;
139 // convert speed to degrees per second
140 speed_north_deg_sec = cos( hdg / SG_RADIANS_TO_DEGREES )
141 * speed * 1.686 / ft_per_deg_lat;
142 speed_east_deg_sec = sin( hdg / SG_RADIANS_TO_DEGREES )
143 * speed * 1.686 / ft_per_deg_lon;
146 pos.setlat( pos.lat() + speed_north_deg_sec * dt);
147 pos.setlon( pos.lon() + speed_east_deg_sec * dt);
149 // adjust heading based on current bank angle
151 turn_radius_ft = 0.088362 * speed * speed
152 / tan( fabs(roll) / SG_RADIANS_TO_DEGREES );
153 turn_circum_ft = SGD_2PI * turn_radius_ft;
154 dist_covered_ft = speed * 1.686 * dt;
155 alpha = dist_covered_ft / turn_circum_ft * 360.0;
156 hdg += alpha * sign( roll );
157 if ( hdg > 360.0 ) hdg -= 360.0;
158 if ( hdg < 0.0) hdg += 360.0;
161 // adjust target bank angle if heading lock engaged
163 double bank_sense = 0.0;
164 double diff = fabs(hdg - tgt_heading);
165 if (diff > 180) diff = fabs(diff - 360);
166 double sum = hdg + diff;
167 if (sum > 360.0) sum -= 360.0;
168 if (fabs(sum - tgt_heading) < 1.0) {
169 bank_sense = 1.0; // right turn
171 bank_sense = -1.0; // left turn
174 tgt_roll = diff * bank_sense;
176 tgt_roll = 30.0 * bank_sense;
180 // adjust bank angle, use 9 degrees per second
181 double bank_diff = tgt_roll - roll;
182 if (fabs(bank_diff) > 0.2) {
183 if (bank_diff > 0.0) roll += 9.0 * dt;
184 if (bank_diff < 0.0) roll -= 9.0 * dt;
187 // adjust altitude (meters) based on current vertical speed (fpm)
188 altitude += vs / 60.0 * dt;
189 pos.setelev(altitude * SG_FEET_TO_METER);
190 double altitude_ft = altitude;
192 // find target vertical speed if altitude lock engaged
193 if (alt_lock && use_perf_vs) {
194 if (altitude_ft < tgt_altitude) {
195 tgt_vs = tgt_altitude - altitude_ft;
196 if (tgt_vs > performance->climb_rate)
197 tgt_vs = performance->climb_rate;
199 tgt_vs = tgt_altitude - altitude_ft;
200 if (tgt_vs < (-performance->descent_rate))
201 tgt_vs = -performance->descent_rate;
205 if (alt_lock && !use_perf_vs) {
206 double max_vs = 4*(tgt_altitude - altitude);
208 if (tgt_altitude < altitude) min_vs = -100.0;
209 if ((fabs(tgt_altitude - altitude) < 1500.0) &&
210 (fabs(max_vs) < fabs(tgt_vs))) tgt_vs = max_vs;
211 if (fabs(tgt_vs) < fabs(min_vs)) tgt_vs = min_vs;
214 // adjust vertical speed
215 double vs_diff = tgt_vs - vs;
216 if (fabs(vs_diff) > 10.0) {
219 if (vs > tgt_vs) vs = tgt_vs;
222 if (vs < tgt_vs) vs = tgt_vs;
226 // match pitch angle to vertical speed
233 //###########################//
234 // do calculations for radar //
235 //###########################//
236 double range_ft2 = UpdateRadar(manager);
238 //************************************//
240 //************************************//
243 if ( (range_ft2 < 250.0 * 250.0) &&
245 (elevation > 0.0) ) {
246 refuel_node->setBoolValue(true);
248 refuel_node->setBoolValue(false);
254 void FGAIAircraft::AccelTo(double speed) {
259 void FGAIAircraft::PitchTo(double angle) {
265 void FGAIAircraft::RollTo(double angle) {
271 void FGAIAircraft::YawTo(double angle) {
276 void FGAIAircraft::ClimbTo(double altitude) {
277 tgt_altitude = altitude;
282 void FGAIAircraft::TurnTo(double heading) {
283 tgt_heading = heading;
288 double FGAIAircraft::sign(double x) {
289 if ( x < 0.0 ) { return -1.0; }
293 void FGAIAircraft::SetFlightPlan(FGAIFlightPlan *f) {
297 void FGAIAircraft::ProcessFlightPlan( double dt ) {
298 FGAIFlightPlan::waypoint* prev = 0; // the one behind you
299 FGAIFlightPlan::waypoint* curr = 0; // the one ahead
300 FGAIFlightPlan::waypoint* next = 0; // the next plus 1
301 prev = fp->getPreviousWaypoint();
302 curr = fp->getCurrentWaypoint();
303 next = fp->getNextWaypoint();
306 if (!prev) { //beginning of flightplan, do this initialization once
307 fp->IncrementWaypoint();
308 prev = fp->getPreviousWaypoint(); //first waypoint
309 curr = fp->getCurrentWaypoint(); //second waypoint
310 next = fp->getNextWaypoint(); //third waypoint (might not exist!)
311 setLatitude(prev->latitude);
312 setLongitude(prev->longitude);
313 setSpeed(prev->speed);
314 setAltitude(prev->altitude);
315 setHeading(fp->getBearing(prev->latitude, prev->longitude, curr));
316 if (next) fp->setLeadDistance(speed, hdg, curr, next);
318 if (curr->crossat > -1000.0) { //use a calculated descent/climb rate
320 tgt_vs = (curr->crossat - prev->altitude)/
321 (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/
322 6076.0/prev->speed*60.0);
323 tgt_altitude = curr->crossat;
326 tgt_altitude = prev->altitude;
328 alt_lock = hdg_lock = true;
329 no_roll = prev->on_ground;
330 //cout << "First waypoint: " << prev->name << endl;
331 //cout << " Target speed: " << tgt_speed << endl;
332 //cout << " Target altitude: " << tgt_altitude << endl;
333 //cout << " Target heading: " << tgt_heading << endl << endl;
335 } // end of initialization
337 // let's only process the flight plan every 100 ms.
338 if (dt_count < 0.1) {
343 // check to see if we've reached the lead point for our next turn
344 double dist_to_go = fp->getDistanceToGo(pos.lat(), pos.lon(), curr);
345 double lead_dist = fp->getLeadDistance();
346 if (lead_dist < (2*speed)) lead_dist = 2*speed; //don't skip over the waypoint
347 //cout << "dist_to_go: " << dist_to_go << ", lead_dist: " << lead_dist << endl;
349 if ( dist_to_go < lead_dist ) {
350 if (curr->finished) { //end of the flight plan, so terminate
354 // we've reached the lead-point for the waypoint ahead
355 if (next) tgt_heading = fp->getBearing(curr, next);
356 fp->IncrementWaypoint();
357 prev = fp->getPreviousWaypoint();
358 curr = fp->getCurrentWaypoint();
359 next = fp->getNextWaypoint();
360 if (next) fp->setLeadDistance(speed, tgt_heading, curr, next);
361 if (curr->crossat > -1000.0) {
363 tgt_vs = (curr->crossat - altitude)/
364 (fp->getDistanceToGo(pos.lat(), pos.lon(), curr)/6076.0/speed*60.0);
365 tgt_altitude = curr->crossat;
368 tgt_altitude = prev->altitude;
370 tgt_speed = prev->speed;
371 hdg_lock = alt_lock = true;
372 no_roll = prev->on_ground;
373 //cout << "Crossing waypoint: " << prev->name << endl;
374 //cout << " Target speed: " << tgt_speed << endl;
375 //cout << " Target altitude: " << tgt_altitude << endl;
376 //cout << " Target heading: " << tgt_heading << endl << endl;
378 double calc_bearing = fp->getBearing(pos.lat(), pos.lon(), curr);
379 double hdg_error = calc_bearing - tgt_heading;
380 if (fabs(hdg_error) > 1.0) {
381 TurnTo( calc_bearing );
389 bool FGAIAircraft::_getGearDown() const {
390 return ((props->getFloatValue("position/altitude-agl-ft") < 900.0)
391 && (props->getFloatValue("velocities/airspeed-kt")
392 < performance->land_speed*1.25));