2003-11-28 15:48:05 +00:00
|
|
|
// FGAIShip - FGAIBase-derived class creates an AI ship
|
|
|
|
//
|
|
|
|
// Written by David Culp, started October 2003.
|
2007-03-05 21:40:39 +00:00
|
|
|
// with major amendments and additions by Vivian Meazza, 2004 - 2007
|
2003-11-28 15:48:05 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-11-28 15:48:05 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# include <float.h>
|
|
|
|
# define finite _finite
|
|
|
|
#elif defined(__sun) || defined(sgi)
|
|
|
|
# include <ieeefp.h>
|
|
|
|
#endif
|
|
|
|
|
2003-11-28 15:48:05 +00:00
|
|
|
#include <math.h>
|
2009-05-04 14:16:00 +00:00
|
|
|
|
|
|
|
#include <simgear/sg_inlines.h>
|
2007-03-05 21:40:39 +00:00
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
2007-03-30 22:51:52 +00:00
|
|
|
#include <simgear/timing/sg_time.hxx>
|
2007-03-05 21:40:39 +00:00
|
|
|
#include <simgear/math/sg_random.h>
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
#include <simgear/scene/util/SGNodeMasks.hxx>
|
|
|
|
#include <Scenery/scenery.hxx>
|
|
|
|
|
2003-11-28 15:48:05 +00:00
|
|
|
#include "AIShip.hxx"
|
|
|
|
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
FGAIShip::FGAIShip(object_type ot) :
|
2009-09-18 12:51:23 +00:00
|
|
|
FGAIBase(ot),
|
2010-09-27 15:48:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
_waiting(false),
|
|
|
|
_new_waypoint(true),
|
|
|
|
_tunnel(false),
|
|
|
|
_initial_tunnel(false),
|
|
|
|
_restart(false),
|
|
|
|
_hdg_constant(0.01),
|
2009-12-09 06:44:50 +00:00
|
|
|
_limit(100),
|
2009-09-18 12:51:23 +00:00
|
|
|
_elevation_m(0),
|
|
|
|
_elevation_ft(0),
|
|
|
|
_tow_angle(0),
|
2010-09-27 15:48:38 +00:00
|
|
|
_missed_count(0),
|
|
|
|
_wp_range(0),
|
2009-09-18 12:51:23 +00:00
|
|
|
_dt_count(0),
|
|
|
|
_next_run(0),
|
2010-09-27 15:48:38 +00:00
|
|
|
_roll_constant(0.001),
|
|
|
|
_roll_factor(-0.0083335),
|
|
|
|
_old_range(0),
|
|
|
|
_range_rate(0),
|
|
|
|
_missed_time_sec(30),
|
|
|
|
_day(86400),
|
2009-09-18 12:51:23 +00:00
|
|
|
_lead_angle(0),
|
|
|
|
_xtrack_error(0),
|
|
|
|
_curr_alt(0),
|
|
|
|
_prev_alt(0),
|
|
|
|
_until_time(""),
|
|
|
|
_fp_init(false),
|
2010-09-27 15:48:38 +00:00
|
|
|
_missed(false)
|
2007-03-05 21:40:39 +00:00
|
|
|
{
|
2009-09-18 12:51:23 +00:00
|
|
|
invisible = false;
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FGAIShip::~FGAIShip() {
|
|
|
|
}
|
|
|
|
|
2006-02-11 13:16:56 +00:00
|
|
|
void FGAIShip::readFromScenario(SGPropertyNode* scFileNode) {
|
2007-03-30 22:51:52 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
if (!scFileNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FGAIBase::readFromScenario(scFileNode);
|
|
|
|
|
|
|
|
setRudder(scFileNode->getFloatValue("rudder", 0.0));
|
|
|
|
setName(scFileNode->getStringValue("name", "Titanic"));
|
|
|
|
setRadius(scFileNode->getDoubleValue("turn-radius-ft", 2000));
|
|
|
|
std::string flightplan = scFileNode->getStringValue("flightplan");
|
|
|
|
setRepeat(scFileNode->getBoolValue("repeat", false));
|
2009-10-31 23:15:14 +00:00
|
|
|
setRestart(scFileNode->getBoolValue("restart", false));
|
2007-03-30 22:51:52 +00:00
|
|
|
setStartTime(scFileNode->getStringValue("time", ""));
|
2009-08-25 09:54:49 +00:00
|
|
|
setLeadAngleGain(scFileNode->getDoubleValue("lead-angle-gain", 1.5));
|
|
|
|
setLeadAngleLimit(scFileNode->getDoubleValue("lead-angle-limit-deg", 15));
|
|
|
|
setLeadAngleProp(scFileNode->getDoubleValue("lead-angle-proportion", 0.75));
|
|
|
|
setRudderConstant(scFileNode->getDoubleValue("rudder-constant", 0.5));
|
|
|
|
setFixedTurnRadius(scFileNode->getDoubleValue("fixed-turn-radius-ft", 500));
|
|
|
|
setSpeedConstant(scFileNode->getDoubleValue("speed-constant", 0.5));
|
2010-09-02 08:24:45 +00:00
|
|
|
setSMPath(scFileNode->getStringValue("submodel-path", ""));
|
2007-03-05 21:40:39 +00:00
|
|
|
|
|
|
|
if (!flightplan.empty()) {
|
2009-09-18 12:51:23 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "getting flightplan: " << _name );
|
2009-08-25 09:54:49 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
FGAIFlightPlan* fp = new FGAIFlightPlan(flightplan);
|
|
|
|
setFlightPlan(fp);
|
|
|
|
}
|
|
|
|
|
2006-02-11 13:16:56 +00:00
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2007-01-13 16:04:28 +00:00
|
|
|
bool FGAIShip::init(bool search_in_AI_path) {
|
2007-03-05 21:40:39 +00:00
|
|
|
prev = 0; // the one behind you
|
|
|
|
curr = 0; // the one ahead
|
|
|
|
next = 0; // the next plus 1
|
|
|
|
|
|
|
|
props->setStringValue("name", _name.c_str());
|
2009-08-25 09:54:49 +00:00
|
|
|
props->setStringValue("waypoint/name-prev", _prev_name.c_str());
|
|
|
|
props->setStringValue("waypoint/name-curr", _curr_name.c_str());
|
|
|
|
props->setStringValue("waypoint/name-next", _next_name.c_str());
|
2007-03-30 22:51:52 +00:00
|
|
|
props->setStringValue("submodels/path", _path.c_str());
|
2009-08-25 09:54:49 +00:00
|
|
|
props->setStringValue("waypoint/start-time", _start_time.c_str());
|
|
|
|
props->setStringValue("waypoint/wait-until-time", _until_time.c_str());
|
2007-03-05 21:40:39 +00:00
|
|
|
|
|
|
|
_hdg_lock = false;
|
|
|
|
_rudder = 0.0;
|
|
|
|
no_roll = false;
|
|
|
|
|
|
|
|
_rd_turn_radius_ft = _sp_turn_radius_ft = turn_radius_ft;
|
|
|
|
|
|
|
|
if (fp)
|
|
|
|
_fp_init = initFlightPlan();
|
|
|
|
|
|
|
|
return FGAIBase::init(search_in_AI_path);
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
2003-12-21 20:12:55 +00:00
|
|
|
void FGAIShip::bind() {
|
|
|
|
FGAIBase::bind();
|
2003-12-22 10:24:15 +00:00
|
|
|
|
2004-11-13 14:45:57 +00:00
|
|
|
props->tie("surface-positions/rudder-pos-deg",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<float>(&_rudder));
|
2005-08-16 09:37:23 +00:00
|
|
|
props->tie("controls/heading-lock",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<bool>(&_hdg_lock));
|
2005-08-16 09:37:23 +00:00
|
|
|
props->tie("controls/tgt-speed-kts",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&tgt_speed));
|
2005-08-16 09:37:23 +00:00
|
|
|
props->tie("controls/tgt-heading-degs",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&tgt_heading));
|
2005-08-16 09:37:23 +00:00
|
|
|
props->tie("controls/constants/rudder",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&_rudder_constant));
|
2007-03-30 22:51:52 +00:00
|
|
|
props->tie("controls/constants/roll-factor",
|
|
|
|
SGRawValuePointer<double>(&_roll_factor));
|
2005-08-16 09:37:23 +00:00
|
|
|
props->tie("controls/constants/roll",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&_roll_constant));
|
2005-08-16 09:37:23 +00:00
|
|
|
props->tie("controls/constants/rudder",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&_rudder_constant));
|
2005-08-16 09:37:23 +00:00
|
|
|
props->tie("controls/constants/speed",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&_speed_constant));
|
2009-08-25 09:54:49 +00:00
|
|
|
props->tie("waypoint/range-nm",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&_wp_range));
|
2009-08-25 09:54:49 +00:00
|
|
|
props->tie("waypoint/brg-deg",
|
|
|
|
SGRawValuePointer<double>(&_course));
|
|
|
|
props->tie("waypoint/rangerate-nm-sec",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&_range_rate));
|
2009-08-25 09:54:49 +00:00
|
|
|
props->tie("waypoint/new",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<bool>(&_new_waypoint));
|
2009-08-25 09:54:49 +00:00
|
|
|
props->tie("waypoint/missed",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<bool>(&_missed));
|
2009-08-25 09:54:49 +00:00
|
|
|
props->tie("waypoint/missed-count-sec",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&_missed_count));
|
2009-08-25 09:54:49 +00:00
|
|
|
props->tie("waypoint/missed-range-nm",
|
|
|
|
SGRawValuePointer<double>(&_missed_range));
|
|
|
|
props->tie("waypoint/missed-time-sec",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&_missed_time_sec));
|
2009-08-25 09:54:49 +00:00
|
|
|
props->tie("waypoint/wait-count-sec",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<double>(&_wait_count));
|
2009-08-25 09:54:49 +00:00
|
|
|
props->tie("waypoint/xtrack-error-ft",
|
|
|
|
SGRawValuePointer<double>(&_xtrack_error));
|
|
|
|
props->tie("waypoint/waiting",
|
2007-03-05 21:40:39 +00:00
|
|
|
SGRawValuePointer<bool>(&_waiting));
|
2009-08-25 09:54:49 +00:00
|
|
|
props->tie("waypoint/lead-angle-deg",
|
|
|
|
SGRawValuePointer<double>(&_lead_angle));
|
2009-09-18 12:51:23 +00:00
|
|
|
props->tie("waypoint/tunnel",
|
|
|
|
SGRawValuePointer<bool>(&_tunnel));
|
|
|
|
props->tie("waypoint/alt-curr-m",
|
|
|
|
SGRawValuePointer<double>(&_curr_alt));
|
|
|
|
props->tie("waypoint/alt-prev-m",
|
|
|
|
SGRawValuePointer<double>(&_prev_alt));
|
2007-03-30 22:51:52 +00:00
|
|
|
props->tie("submodels/serviceable",
|
|
|
|
SGRawValuePointer<bool>(&_serviceable));
|
2009-05-04 14:16:00 +00:00
|
|
|
props->tie("controls/turn-radius-ft",
|
|
|
|
SGRawValuePointer<double>(&turn_radius_ft));
|
2009-08-25 09:54:49 +00:00
|
|
|
props->tie("controls/turn-radius-corrected-ft",
|
|
|
|
SGRawValuePointer<double>(&_rd_turn_radius_ft));
|
|
|
|
props->tie("controls/constants/lead-angle/gain",
|
|
|
|
SGRawValuePointer<double>(&_lead_angle_gain));
|
|
|
|
props->tie("controls/constants/lead-angle/limit-deg",
|
|
|
|
SGRawValuePointer<double>(&_lead_angle_limit));
|
|
|
|
props->tie("controls/constants/lead-angle/proportion",
|
|
|
|
SGRawValuePointer<double>(&_proportion));
|
|
|
|
props->tie("controls/fixed-turn-radius-ft",
|
|
|
|
SGRawValuePointer<double>(&_fixed_turn_radius));
|
2009-10-31 23:15:14 +00:00
|
|
|
props->tie("controls/restart",
|
|
|
|
SGRawValuePointer<bool>(&_restart));
|
2009-12-09 06:44:50 +00:00
|
|
|
props->tie("velocities/speed-kts",
|
|
|
|
SGRawValuePointer<double>(&speed));
|
2003-12-21 20:12:55 +00:00
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2003-12-21 20:12:55 +00:00
|
|
|
void FGAIShip::unbind() {
|
|
|
|
FGAIBase::unbind();
|
2004-11-13 14:45:57 +00:00
|
|
|
props->untie("surface-positions/rudder-pos-deg");
|
2005-08-16 09:37:23 +00:00
|
|
|
props->untie("controls/heading-lock");
|
|
|
|
props->untie("controls/tgt-speed-kts");
|
|
|
|
props->untie("controls/tgt-heading-degs");
|
|
|
|
props->untie("controls/constants/roll");
|
|
|
|
props->untie("controls/constants/rudder");
|
2007-03-30 22:51:52 +00:00
|
|
|
props->untie("controls/constants/roll-factor");
|
2007-03-05 21:40:39 +00:00
|
|
|
props->untie("controls/constants/speed");
|
2009-08-25 09:54:49 +00:00
|
|
|
props->untie("waypoint/range-nm");
|
|
|
|
props->untie("waypoint/range-brg-deg");
|
|
|
|
props->untie("waypoint/rangerate-nm-sec");
|
|
|
|
props->untie("waypoint/new");
|
|
|
|
props->untie("waypoint/missed");
|
|
|
|
props->untie("waypoint/missed-count-sec");
|
|
|
|
props->untie("waypoint/missed-time-sec");
|
|
|
|
props->untie("waypoint/missed-range");
|
|
|
|
props->untie("waypoint/wait-count-sec");
|
|
|
|
props->untie("waypoint/lead-angle-deg");
|
|
|
|
props->untie("waypoint/xtrack-error-ft");
|
|
|
|
props->untie("waypoint/waiting");
|
2009-09-18 12:51:23 +00:00
|
|
|
props->untie("waypoint/tunnel");
|
|
|
|
props->untie("waypoint/alt-curr-m");
|
|
|
|
props->untie("waypoint/alt-prev-m");
|
2007-03-30 22:51:52 +00:00
|
|
|
props->untie("submodels/serviceable");
|
2009-05-04 14:16:00 +00:00
|
|
|
props->untie("controls/turn-radius-ft");
|
2009-08-25 09:54:49 +00:00
|
|
|
props->untie("controls/turn-radius-corrected-ft");
|
|
|
|
props->untie("controls/constants/lead-angle/gain");
|
|
|
|
props->untie("controls/constants/lead-angle/limit-deg");
|
|
|
|
props->untie("controls/constants/lead-angle/proportion");
|
|
|
|
props->untie("controls/fixed-turn-radius-ft");
|
|
|
|
props->untie("controls/constants/speed");
|
2009-10-31 23:15:14 +00:00
|
|
|
props->untie("controls/restart");
|
2009-12-09 06:44:50 +00:00
|
|
|
props->untie("velocities/speed-kts");
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
void FGAIShip::update(double dt) {
|
2009-08-25 09:54:49 +00:00
|
|
|
//SG_LOG(SG_GENERAL, SG_ALERT, "updating Ship: " << _name <<hdg<<pitch<<roll);
|
2009-03-01 16:40:32 +00:00
|
|
|
// For computation of rotation speeds we just use finite differences here.
|
|
|
|
// That is perfectly valid since this thing is not driven by accelerations
|
|
|
|
// but by just apply discrete changes at its velocity variables.
|
|
|
|
// Update the velocity information stored in those nodes.
|
|
|
|
// Transform that one to the horizontal local coordinate system.
|
|
|
|
SGQuatd ec2hl = SGQuatd::fromLonLat(pos);
|
2010-09-02 08:24:45 +00:00
|
|
|
// The orientation of the ship wrt the horizontal local frame
|
2009-03-01 16:40:32 +00:00
|
|
|
SGQuatd hl2body = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
|
|
|
|
// and postrotate the orientation of the AIModel wrt the horizontal
|
|
|
|
// local frame
|
|
|
|
SGQuatd ec2body = ec2hl*hl2body;
|
2010-09-02 08:24:45 +00:00
|
|
|
// The cartesian position of the ship in the wgs84 world
|
2009-03-01 16:40:32 +00:00
|
|
|
SGVec3d cartPos = SGVec3d::fromGeod(pos);
|
|
|
|
|
2009-03-14 12:27:12 +00:00
|
|
|
// The simulation time this transform is meant for
|
2009-03-15 11:05:28 +00:00
|
|
|
aip.setReferenceTime(globals->get_sim_time_sec());
|
2009-03-14 12:27:12 +00:00
|
|
|
|
2009-03-01 16:40:32 +00:00
|
|
|
// Compute the velocity in m/s in the body frame
|
|
|
|
aip.setBodyLinearVelocity(SGVec3d(0.51444444*speed, 0, 0));
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
FGAIBase::update(dt);
|
|
|
|
Run(dt);
|
|
|
|
Transform();
|
2009-08-25 09:54:49 +00:00
|
|
|
if (fp)
|
|
|
|
setXTrackError();
|
2009-03-01 16:40:32 +00:00
|
|
|
|
|
|
|
// Only change these values if we are able to compute them safely
|
|
|
|
if (SGLimits<double>::min() < dt) {
|
|
|
|
// Now here is the finite difference ...
|
2009-09-18 12:51:23 +00:00
|
|
|
|
2009-03-01 16:40:32 +00:00
|
|
|
// Transform that one to the horizontal local coordinate system.
|
|
|
|
SGQuatd ec2hlNew = SGQuatd::fromLonLat(pos);
|
|
|
|
// compute the new orientation
|
|
|
|
SGQuatd hl2bodyNew = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
|
|
|
|
// The rotation difference
|
|
|
|
SGQuatd dOr = inverse(ec2body)*ec2hlNew*hl2bodyNew;
|
|
|
|
SGVec3d dOrAngleAxis;
|
|
|
|
dOr.getAngleAxis(dOrAngleAxis);
|
|
|
|
// divided by the time difference provides a rotation speed vector
|
|
|
|
dOrAngleAxis /= dt;
|
2009-09-18 12:51:23 +00:00
|
|
|
|
2009-03-01 16:40:32 +00:00
|
|
|
aip.setBodyAngularVelocity(dOrAngleAxis);
|
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
void FGAIShip::Run(double dt) {
|
|
|
|
if (_fp_init)
|
|
|
|
ProcessFlightPlan(dt);
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
string type = getTypeString();
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
double alpha;
|
|
|
|
double rudder_limit;
|
|
|
|
double raw_roll;
|
|
|
|
|
|
|
|
// adjust speed
|
|
|
|
double speed_diff = tgt_speed - speed;
|
|
|
|
|
|
|
|
if (fabs(speed_diff) > 0.1) {
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
if (speed_diff > 0.0)
|
|
|
|
speed += _speed_constant * dt;
|
|
|
|
|
|
|
|
if (speed_diff < 0.0)
|
|
|
|
speed -= _speed_constant * dt;
|
2007-03-30 22:51:52 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
// do not allow unreasonable speeds
|
2009-12-09 06:44:50 +00:00
|
|
|
SG_CLAMP_RANGE(speed, -_limit * 0.75, _limit);
|
2007-03-05 21:40:39 +00:00
|
|
|
|
|
|
|
// convert speed to degrees per second
|
|
|
|
speed_north_deg_sec = cos(hdg / SGD_RADIANS_TO_DEGREES)
|
|
|
|
* speed * 1.686 / ft_per_deg_lat;
|
|
|
|
speed_east_deg_sec = sin(hdg / SGD_RADIANS_TO_DEGREES)
|
|
|
|
* speed * 1.686 / ft_per_deg_lon;
|
|
|
|
|
|
|
|
// set new position
|
2009-08-25 09:54:49 +00:00
|
|
|
//cout << _name << " " << type << " run: " << _elevation_m << " " <<_elevation_ft << endl;
|
2007-03-05 21:40:39 +00:00
|
|
|
pos.setLatitudeDeg(pos.getLatitudeDeg() + speed_north_deg_sec * dt);
|
|
|
|
pos.setLongitudeDeg(pos.getLongitudeDeg() + speed_east_deg_sec * dt);
|
2009-08-25 09:54:49 +00:00
|
|
|
pos.setElevationFt(tgt_altitude_ft);
|
|
|
|
pitch = tgt_pitch;
|
2007-03-05 21:40:39 +00:00
|
|
|
|
|
|
|
// adjust heading based on current _rudder angle
|
|
|
|
if (turn_radius_ft <= 0)
|
|
|
|
turn_radius_ft = 0; // don't allow nonsense values
|
|
|
|
|
|
|
|
if (_rudder > 45)
|
|
|
|
_rudder = 45;
|
|
|
|
|
|
|
|
if (_rudder < -45)
|
|
|
|
_rudder = -45;
|
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
//we assume that at slow speed ships will manoeuvre using engines/bow thruster
|
2009-12-09 06:44:50 +00:00
|
|
|
if(type == "ship" || type == "carrier" || type == "escort"){
|
2009-10-31 23:15:14 +00:00
|
|
|
|
|
|
|
if (fabs(speed)<=5)
|
|
|
|
_sp_turn_radius_ft = _fixed_turn_radius;
|
|
|
|
else {
|
|
|
|
// adjust turn radius for speed. The equation is very approximate.
|
|
|
|
// we need to allow for negative speeds
|
|
|
|
_sp_turn_radius_ft = 10 * pow ((fabs(speed) - 15), 2) + turn_radius_ft;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (fabs(speed) <= 40)
|
|
|
|
_sp_turn_radius_ft = _fixed_turn_radius;
|
|
|
|
else {
|
|
|
|
// adjust turn radius for speed.
|
|
|
|
_sp_turn_radius_ft = turn_radius_ft;
|
|
|
|
}
|
|
|
|
}
|
2009-08-25 09:54:49 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
|
|
|
|
if (_rudder <= -0.25 || _rudder >= 0.25) {
|
|
|
|
// adjust turn radius for _rudder angle. The equation is even more approximate.
|
|
|
|
float a = 19;
|
|
|
|
float b = -0.2485;
|
|
|
|
float c = 0.543;
|
|
|
|
|
|
|
|
_rd_turn_radius_ft = (a * exp(b * fabs(_rudder)) + c) * _sp_turn_radius_ft;
|
|
|
|
|
|
|
|
// calculate the angle, alpha, subtended by the arc traversed in time dt
|
|
|
|
alpha = ((speed * 1.686 * dt) / _rd_turn_radius_ft) * SG_RADIANS_TO_DEGREES;
|
2009-08-25 09:54:49 +00:00
|
|
|
//cout << _name << " alpha " << alpha << endl;
|
2007-03-05 21:40:39 +00:00
|
|
|
// make sure that alpha is applied in the right direction
|
|
|
|
hdg += alpha * sign(_rudder);
|
|
|
|
|
2009-05-04 14:16:00 +00:00
|
|
|
SG_NORMALIZE_RANGE(hdg, 0.0, 360.0);
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
//adjust roll for rudder angle and speed. Another bit of voodoo
|
|
|
|
raw_roll = _roll_factor * speed * _rudder;
|
2007-03-05 21:40:39 +00:00
|
|
|
} else {
|
|
|
|
// _rudder angle is 0
|
|
|
|
raw_roll = 0;
|
|
|
|
}
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2005-08-16 09:37:23 +00:00
|
|
|
//low pass filter
|
2007-03-05 21:40:39 +00:00
|
|
|
if (speed < 0)
|
|
|
|
roll = -roll;
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
roll = (raw_roll * _roll_constant) + (roll * (1 - _roll_constant));
|
2003-11-28 15:48:05 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
// adjust target _rudder angle if heading lock engaged
|
2007-07-11 10:56:33 +00:00
|
|
|
double rudder_diff = 0.0;
|
2007-03-05 21:40:39 +00:00
|
|
|
if (_hdg_lock) {
|
|
|
|
double rudder_sense = 0.0;
|
|
|
|
double diff = fabs(hdg - tgt_heading);
|
|
|
|
//cout << "_rudder diff" << diff << endl;
|
|
|
|
if (diff > 180)
|
|
|
|
diff = fabs(diff - 360);
|
2005-08-16 09:37:23 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
double sum = hdg + diff;
|
|
|
|
|
|
|
|
if (sum > 360.0)
|
|
|
|
sum -= 360.0;
|
2005-08-16 09:37:23 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
if (fabs(sum - tgt_heading)< 1.0)
|
|
|
|
rudder_sense = 1.0;
|
|
|
|
else
|
|
|
|
rudder_sense = -1.0;
|
|
|
|
|
|
|
|
if (speed < 0)
|
|
|
|
rudder_sense = -rudder_sense;
|
|
|
|
|
|
|
|
if (diff < 15)
|
|
|
|
_tgt_rudder = diff * rudder_sense;
|
|
|
|
else
|
|
|
|
_tgt_rudder = 45 * rudder_sense;
|
|
|
|
|
2007-07-11 10:56:33 +00:00
|
|
|
rudder_diff = _tgt_rudder - _rudder;
|
|
|
|
}
|
2007-03-30 22:51:52 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
// set the _rudder limit by speed
|
2009-12-09 06:44:50 +00:00
|
|
|
if (type == "ship" || type == "carrier" || type == "escort"){
|
2009-08-25 09:54:49 +00:00
|
|
|
|
2009-09-18 12:51:23 +00:00
|
|
|
if (speed <= 40)
|
|
|
|
rudder_limit = (-0.825 * speed) + 35;
|
|
|
|
else
|
|
|
|
rudder_limit = 2;
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
} else
|
|
|
|
rudder_limit = 20;
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
if (fabs(rudder_diff)> 0.1) { // apply dead zone
|
|
|
|
|
|
|
|
if (rudder_diff > 0.0) {
|
|
|
|
_rudder += _rudder_constant * dt;
|
|
|
|
|
|
|
|
if (_rudder > rudder_limit) // apply the _rudder limit
|
|
|
|
_rudder = rudder_limit;
|
|
|
|
|
|
|
|
} else if (rudder_diff < 0.0) {
|
|
|
|
_rudder -= _rudder_constant * dt;
|
|
|
|
|
|
|
|
if (_rudder < -rudder_limit)
|
|
|
|
_rudder = -rudder_limit;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-06-24 07:57:45 +00:00
|
|
|
// do calculations for radar
|
|
|
|
UpdateRadar(manager);
|
2007-03-05 21:40:39 +00:00
|
|
|
}
|
|
|
|
}//end function
|
2005-08-16 09:37:23 +00:00
|
|
|
|
2003-11-28 15:48:05 +00:00
|
|
|
void FGAIShip::AccelTo(double speed) {
|
2007-03-05 21:40:39 +00:00
|
|
|
tgt_speed = speed;
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::PitchTo(double angle) {
|
2007-03-05 21:40:39 +00:00
|
|
|
tgt_pitch = angle;
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::RollTo(double angle) {
|
2007-03-05 21:40:39 +00:00
|
|
|
tgt_roll = angle;
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::YawTo(double angle) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::ClimbTo(double altitude) {
|
2009-08-25 09:54:49 +00:00
|
|
|
tgt_altitude_ft = altitude;
|
|
|
|
_setAltitude(altitude);
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::TurnTo(double heading) {
|
2009-08-25 09:54:49 +00:00
|
|
|
tgt_heading = heading - _lead_angle + _tow_angle;
|
|
|
|
SG_NORMALIZE_RANGE(tgt_heading, 0.0, 360.0);
|
2007-03-05 21:40:39 +00:00
|
|
|
_hdg_lock = true;
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double FGAIShip::sign(double x) {
|
2007-03-05 21:40:39 +00:00
|
|
|
if (x < 0.0)
|
|
|
|
return -1.0;
|
|
|
|
else
|
|
|
|
return 1.0;
|
2003-11-28 15:48:05 +00:00
|
|
|
}
|
2004-05-29 11:39:10 +00:00
|
|
|
|
|
|
|
void FGAIShip::setFlightPlan(FGAIFlightPlan* f) {
|
2007-03-05 21:40:39 +00:00
|
|
|
fp = f;
|
2004-05-29 11:39:10 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
void FGAIShip::setStartTime(const string& st) {
|
|
|
|
_start_time = st;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setUntilTime(const string& ut) {
|
|
|
|
_until_time = ut;
|
2009-08-25 09:54:49 +00:00
|
|
|
props->setStringValue("waypoint/wait-until-time", _until_time.c_str());
|
2007-03-30 22:51:52 +00:00
|
|
|
}
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
void FGAIShip::setCurrName(const string& c) {
|
|
|
|
_curr_name = c;
|
2009-08-25 09:54:49 +00:00
|
|
|
props->setStringValue("waypoint/name-curr", _curr_name.c_str());
|
2004-05-29 11:39:10 +00:00
|
|
|
}
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
void FGAIShip::setNextName(const string& n) {
|
|
|
|
_next_name = n;
|
2009-08-25 09:54:49 +00:00
|
|
|
props->setStringValue("waypoint/name-next", _next_name.c_str());
|
2007-03-05 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setPrevName(const string& p) {
|
|
|
|
_prev_name = p;
|
2009-08-25 09:54:49 +00:00
|
|
|
props->setStringValue("waypoint/name-prev", _prev_name.c_str());
|
2007-03-05 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setRepeat(bool r) {
|
|
|
|
_repeat = r;
|
|
|
|
}
|
|
|
|
|
2009-10-31 23:15:14 +00:00
|
|
|
void FGAIShip::setRestart(bool r) {
|
|
|
|
_restart = r;
|
|
|
|
}
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
void FGAIShip::setMissed(bool m) {
|
|
|
|
_missed = m;
|
2009-08-25 09:54:49 +00:00
|
|
|
props->setBoolValue("waypoint/missed", _missed);
|
2007-03-05 21:40:39 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
void FGAIShip::setRudder(float r) {
|
|
|
|
_rudder = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setRoll(double rl) {
|
|
|
|
roll = rl;
|
|
|
|
}
|
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
void FGAIShip::setLeadAngleGain(double g) {
|
|
|
|
_lead_angle_gain = g;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setLeadAngleLimit(double l) {
|
|
|
|
_lead_angle_limit = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setLeadAngleProp(double p) {
|
|
|
|
_proportion = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setRudderConstant(double rc) {
|
|
|
|
_rudder_constant = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setSpeedConstant(double sc) {
|
|
|
|
_speed_constant = sc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setFixedTurnRadius(double ftr) {
|
|
|
|
_fixed_turn_radius = ftr;
|
|
|
|
}
|
|
|
|
|
2009-10-31 23:15:14 +00:00
|
|
|
void FGAIShip::setInitialTunnel(bool t) {
|
|
|
|
_initial_tunnel = t;
|
|
|
|
setTunnel(_initial_tunnel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setTunnel(bool t) {
|
|
|
|
_tunnel = t;
|
|
|
|
}
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
void FGAIShip::setWPNames() {
|
|
|
|
|
|
|
|
if (prev != 0)
|
|
|
|
setPrevName(prev->name);
|
|
|
|
else
|
|
|
|
setPrevName("");
|
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
if (curr != 0)
|
2009-09-18 12:51:23 +00:00
|
|
|
setCurrName(curr->name);
|
2009-08-25 09:54:49 +00:00
|
|
|
else{
|
|
|
|
setCurrName("");
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "AIShip: current wp name error" );
|
|
|
|
}
|
2007-03-30 22:51:52 +00:00
|
|
|
|
|
|
|
if (next != 0)
|
|
|
|
setNextName(next->name);
|
|
|
|
else
|
|
|
|
setNextName("");
|
|
|
|
|
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: prev wp name " << prev->name);
|
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: current wp name " << curr->name);
|
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: next wp name " << next->name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
double FGAIShip::getRange(double lat, double lon, double lat2, double lon2) const {
|
|
|
|
|
|
|
|
double course, distance, az2;
|
|
|
|
|
|
|
|
//calculate the bearing and range of the second pos from the first
|
|
|
|
geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &az2, &distance);
|
|
|
|
distance *= SG_METER_TO_NM;
|
|
|
|
return distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
double FGAIShip::getCourse(double lat, double lon, double lat2, double lon2) const {
|
|
|
|
|
|
|
|
double course, distance, recip;
|
|
|
|
|
|
|
|
//calculate the bearing and range of the second pos from the first
|
|
|
|
geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &recip, &distance);
|
|
|
|
if (tgt_speed >= 0) {
|
|
|
|
return course;
|
2009-08-25 09:54:49 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: course " << course);
|
2007-03-30 22:51:52 +00:00
|
|
|
} else {
|
|
|
|
return recip;
|
2009-08-25 09:54:49 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: recip " << recip);
|
2007-03-30 22:51:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
void FGAIShip::ProcessFlightPlan(double dt) {
|
|
|
|
|
2010-12-20 18:20:57 +00:00
|
|
|
if ( dt < 0.00001 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
double time_sec = getDaySeconds();
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
_dt_count += dt;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Check Execution time (currently once every 1 sec)
|
|
|
|
// Add a bit of randomization to prevent the execution of all flight plans
|
|
|
|
// in synchrony, which can add significant periodic framerate flutter.
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2007-03-30 22:51:52 +00:00
|
|
|
|
|
|
|
//cout << "_start_sec " << _start_sec << " time_sec " << time_sec << endl;
|
|
|
|
if (_dt_count < _next_run && _start_sec < time_sec)
|
2007-03-05 21:40:39 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-31 23:15:14 +00:00
|
|
|
_next_run = 0.05 + (0.025 * sg_random());
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2009-09-18 12:51:23 +00:00
|
|
|
double until_time_sec = 0;
|
|
|
|
_missed = false;
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
// check to see if we've reached the point for our next turn
|
|
|
|
// if the range to the waypoint is less than the calculated turn
|
|
|
|
// radius we can start the turn to the next leg
|
|
|
|
_wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->latitude, curr->longitude);
|
|
|
|
_range_rate = (_wp_range - _old_range) / _dt_count;
|
|
|
|
double sp_turn_radius_nm = _sp_turn_radius_ft / 6076.1155;
|
|
|
|
// we need to try to identify a _missed waypoint
|
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
// calculate the time needed to turn through an arc of 90 degrees,
|
|
|
|
// and allow a time error
|
2007-03-05 21:40:39 +00:00
|
|
|
if (speed != 0)
|
2009-08-25 09:54:49 +00:00
|
|
|
_missed_time_sec = 10 + ((SGD_PI * sp_turn_radius_nm * 60 * 60) / (2 * fabs(speed)));
|
2007-03-05 21:40:39 +00:00
|
|
|
else
|
2009-08-25 09:54:49 +00:00
|
|
|
_missed_time_sec = 10;
|
|
|
|
|
|
|
|
_missed_range = 4 * sp_turn_radius_nm;
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
//cout << _name << " range_rate " << _range_rate << " " << _new_waypoint<< endl ;
|
|
|
|
//if ((_range_rate > 0) && !_new_waypoint){
|
|
|
|
if (_range_rate > 0 && _wp_range < _missed_range && !_new_waypoint){
|
2007-03-05 21:40:39 +00:00
|
|
|
_missed_count += _dt_count;
|
2009-08-25 09:54:49 +00:00
|
|
|
}
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
if (_missed_count >= 120)
|
2007-03-05 21:40:39 +00:00
|
|
|
setMissed(true);
|
2009-08-25 09:54:49 +00:00
|
|
|
else if (_missed_count >= _missed_time_sec)
|
|
|
|
setMissed(true);
|
|
|
|
else
|
2007-03-05 21:40:39 +00:00
|
|
|
setMissed(false);
|
|
|
|
|
|
|
|
_old_range = _wp_range;
|
2007-03-30 22:51:52 +00:00
|
|
|
setWPNames();
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
if ((_wp_range < (sp_turn_radius_nm * 1.25)) || _missed || (_waiting && !_new_waypoint)) {
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2009-09-18 12:51:23 +00:00
|
|
|
if (_next_name == "TUNNEL"){
|
|
|
|
_tunnel = !_tunnel;
|
|
|
|
|
2010-09-02 08:24:45 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name << " " << sp_turn_radius_nm );
|
2009-09-18 12:51:23 +00:00
|
|
|
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
|
|
|
|
if (next->name == "WAITUNTIL" || next->name == "WAIT"
|
|
|
|
|| next->name == "END" || next->name == "TUNNEL")
|
|
|
|
return;
|
|
|
|
|
|
|
|
prev = curr;
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
curr = fp->getCurrentWaypoint();
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
|
|
|
|
}else if(_next_name == "END" || fp->getNextWaypoint() == 0) {
|
2007-03-05 21:40:39 +00:00
|
|
|
|
|
|
|
if (_repeat) {
|
2010-09-02 08:24:45 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_INFO, "AIShip: "<< _name << " Flightplan repeating ");
|
2007-03-05 21:40:39 +00:00
|
|
|
fp->restart();
|
|
|
|
prev = curr;
|
|
|
|
curr = fp->getCurrentWaypoint();
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
setWPNames();
|
|
|
|
_wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->latitude, curr->longitude);
|
|
|
|
_old_range = _wp_range;
|
|
|
|
_range_rate = 0;
|
|
|
|
_new_waypoint = true;
|
|
|
|
_missed_count = 0;
|
2009-08-25 09:54:49 +00:00
|
|
|
_lead_angle = 0;
|
2007-03-05 21:40:39 +00:00
|
|
|
AccelTo(prev->speed);
|
2009-10-31 23:15:14 +00:00
|
|
|
} else if (_restart){
|
2010-09-02 08:24:45 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_INFO, "AIShip: " << _name << " Flightplan restarting ");
|
2009-10-31 23:15:14 +00:00
|
|
|
_missed_count = 0;
|
|
|
|
initFlightPlan();
|
2007-03-05 21:40:39 +00:00
|
|
|
} else {
|
2009-08-25 09:54:49 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "AIShip: " << _name << " Flightplan dying ");
|
2007-03-05 21:40:39 +00:00
|
|
|
setDie(true);
|
|
|
|
_dt_count = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (_next_name == "WAIT") {
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
if (_wait_count < next->time_sec) {
|
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name << " waiting ");
|
2007-03-05 21:40:39 +00:00
|
|
|
setSpeed(0);
|
|
|
|
_waiting = true;
|
|
|
|
_wait_count += _dt_count;
|
|
|
|
_dt_count = 0;
|
2009-08-25 09:54:49 +00:00
|
|
|
_lead_angle = 0;
|
2007-03-05 21:40:39 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2007-03-30 22:51:52 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name
|
|
|
|
<< " wait done: getting new waypoints ");
|
|
|
|
_waiting = false;
|
|
|
|
_wait_count = 0;
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
|
|
|
|
if (next->name == "WAITUNTIL" || next->name == "WAIT"
|
2009-09-18 12:51:23 +00:00
|
|
|
|| next->name == "END" || next->name == "TUNNEL")
|
2007-03-30 22:51:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
prev = curr;
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
curr = fp->getCurrentWaypoint();
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (_next_name == "WAITUNTIL") {
|
|
|
|
time_sec = getDaySeconds();
|
|
|
|
until_time_sec = processTimeString(next->time);
|
|
|
|
_until_time = next->time;
|
|
|
|
setUntilTime(next->time);
|
|
|
|
if (until_time_sec > time_sec) {
|
2010-09-02 08:24:45 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_INFO, "AIShip: " << _name << " "
|
2009-08-25 09:54:49 +00:00
|
|
|
<< curr->name << " waiting until: "
|
2007-03-30 22:51:52 +00:00
|
|
|
<< _until_time << " " << until_time_sec << " now " << time_sec );
|
|
|
|
setSpeed(0);
|
2009-08-25 09:54:49 +00:00
|
|
|
_lead_angle = 0;
|
2007-03-30 22:51:52 +00:00
|
|
|
_waiting = true;
|
|
|
|
return;
|
|
|
|
} else {
|
2010-09-02 08:24:45 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_INFO, "AIShip: "
|
2007-03-30 22:51:52 +00:00
|
|
|
<< _name << " wait until done: getting new waypoints ");
|
|
|
|
setUntilTime("");
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
|
|
|
|
while (next->name == "WAITUNTIL") {
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next->name == "WAIT")
|
|
|
|
return;
|
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
prev = curr;
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
curr = fp->getCurrentWaypoint();
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
_waiting = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//now reorganise the waypoints, so that next becomes current and so on
|
2007-03-30 22:51:52 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name << " getting new waypoints ");
|
2007-03-05 21:40:39 +00:00
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
prev = fp->getPreviousWaypoint(); //first waypoint
|
|
|
|
curr = fp->getCurrentWaypoint(); //second waypoint
|
|
|
|
next = fp->getNextWaypoint(); //third waypoint (might not exist!)
|
|
|
|
}
|
|
|
|
|
|
|
|
setWPNames();
|
|
|
|
_new_waypoint = true;
|
|
|
|
_missed_count = 0;
|
|
|
|
_range_rate = 0;
|
2009-08-25 09:54:49 +00:00
|
|
|
_lead_angle = 0;
|
2007-03-05 21:40:39 +00:00
|
|
|
_wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->latitude, curr->longitude);
|
|
|
|
_old_range = _wp_range;
|
2009-08-25 09:54:49 +00:00
|
|
|
setWPPos();
|
2009-09-18 12:51:23 +00:00
|
|
|
object_type type = getType();
|
|
|
|
|
|
|
|
if (type != 10)
|
|
|
|
AccelTo(prev->speed);
|
|
|
|
|
|
|
|
_curr_alt = curr->altitude;
|
|
|
|
_prev_alt = prev->altitude;
|
2009-08-25 09:54:49 +00:00
|
|
|
|
2007-03-05 21:40:39 +00:00
|
|
|
} else {
|
|
|
|
_new_waypoint = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now revise the required course for the next way point
|
2009-08-25 09:54:49 +00:00
|
|
|
_course = getCourse(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->latitude, curr->longitude);
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
if (finite(_course))
|
|
|
|
TurnTo(_course);
|
2007-03-05 21:40:39 +00:00
|
|
|
else
|
2009-08-25 09:54:49 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "AIShip: Bearing or Range is not a finite number");
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2009-09-18 12:51:23 +00:00
|
|
|
_dt_count = 0;
|
2007-03-05 21:40:39 +00:00
|
|
|
} // end Processing FlightPlan
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
bool FGAIShip::initFlightPlan() {
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "AIShip: " << _name << " initializing waypoints ");
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
bool init = false;
|
|
|
|
_start_sec = 0;
|
2009-10-31 23:15:14 +00:00
|
|
|
_tunnel = _initial_tunnel;
|
2007-03-05 21:40:39 +00:00
|
|
|
|
|
|
|
fp->restart();
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
|
|
|
|
prev = fp->getPreviousWaypoint(); //first waypoint
|
|
|
|
curr = fp->getCurrentWaypoint(); //second waypoint
|
|
|
|
next = fp->getNextWaypoint(); //third waypoint (might not exist!)
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
while (curr->name == "WAIT" || curr->name == "WAITUNTIL") { // don't wait when initialising
|
|
|
|
SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name << " re-initializing waypoints ");
|
2007-03-05 21:40:39 +00:00
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
curr = fp->getCurrentWaypoint();
|
|
|
|
next = fp->getNextWaypoint();
|
2009-09-18 12:51:23 +00:00
|
|
|
} // end while loop
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
if (!_start_time.empty()){
|
|
|
|
_start_sec = processTimeString(_start_time);
|
|
|
|
double day_sec = getDaySeconds();
|
|
|
|
|
|
|
|
if (_start_sec < day_sec){
|
|
|
|
//cout << "flight plan has already started " << _start_time << endl;
|
|
|
|
init = advanceFlightPlan(_start_sec, day_sec);
|
|
|
|
|
|
|
|
} else if (_start_sec > day_sec && _repeat) {
|
|
|
|
//cout << "flight plan has not started, " << _start_time;
|
|
|
|
//cout << "offsetting start time by -24 hrs" << endl;
|
|
|
|
_start_sec -= _day;
|
|
|
|
init = advanceFlightPlan(_start_sec, day_sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (init)
|
|
|
|
_start_sec = 0; // set to zero for an immediate start of the Flight Plan
|
|
|
|
else {
|
|
|
|
fp->restart();
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
prev = fp->getPreviousWaypoint();
|
|
|
|
curr = fp->getCurrentWaypoint();
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2009-09-18 12:51:23 +00:00
|
|
|
setLatitude(prev->latitude);
|
|
|
|
setLongitude(prev->longitude);
|
|
|
|
setSpeed(prev->speed);
|
2007-03-30 22:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setWPNames();
|
2007-03-05 21:40:39 +00:00
|
|
|
setHeading(getCourse(prev->latitude, prev->longitude, curr->latitude, curr->longitude));
|
2007-03-30 22:51:52 +00:00
|
|
|
_wp_range = getRange(prev->latitude, prev->longitude, curr->latitude, curr->longitude);
|
2007-03-05 21:40:39 +00:00
|
|
|
_old_range = _wp_range;
|
|
|
|
_range_rate = 0;
|
2007-03-30 22:51:52 +00:00
|
|
|
_hdg_lock = true;
|
2007-03-05 21:40:39 +00:00
|
|
|
_missed = false;
|
|
|
|
_missed_count = 0;
|
|
|
|
_new_waypoint = true;
|
|
|
|
|
2009-10-31 23:15:14 +00:00
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "AIShip: " << _name << " done initialising waypoints " << _tunnel);
|
2007-03-05 21:40:39 +00:00
|
|
|
if (prev)
|
2007-03-30 22:51:52 +00:00
|
|
|
init = true;
|
|
|
|
|
|
|
|
if (init)
|
2007-03-05 21:40:39 +00:00
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} // end of initialization
|
|
|
|
|
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
double FGAIShip::processTimeString(const string& theTime) {
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
int Hour;
|
|
|
|
int Minute;
|
|
|
|
int Second;
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
// first split theTime string into
|
|
|
|
// hour, minute, second and convert to int;
|
|
|
|
Hour = atoi(theTime.substr(0,2).c_str());
|
|
|
|
Minute = atoi(theTime.substr(3,5).c_str());
|
|
|
|
Second = atoi(theTime.substr(6,8).c_str());
|
|
|
|
|
|
|
|
// offset by a day-sec to allow for starting a day earlier
|
|
|
|
double time_seconds = Hour * 3600
|
|
|
|
+ Minute * 60
|
|
|
|
+ Second;
|
|
|
|
|
|
|
|
return time_seconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
double FGAIShip::getDaySeconds () {
|
|
|
|
// Date and time
|
|
|
|
struct tm *t = globals->get_time_params()->getGmt();
|
|
|
|
|
|
|
|
double day_seconds = t->tm_hour * 3600
|
|
|
|
+ t->tm_min * 60
|
|
|
|
+ t->tm_sec;
|
|
|
|
|
|
|
|
return day_seconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FGAIShip::advanceFlightPlan (double start_sec, double day_sec) {
|
|
|
|
|
|
|
|
double elapsed_sec = start_sec;
|
|
|
|
double distance_nm = 0;
|
|
|
|
|
|
|
|
//cout << "advancing flight plan start_sec: " << start_sec << " " << day_sec << endl;
|
|
|
|
|
|
|
|
while ( elapsed_sec < day_sec ) {
|
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
if (next->name == "END" || fp->getNextWaypoint() == 0) {
|
2007-03-30 22:51:52 +00:00
|
|
|
|
|
|
|
if (_repeat ) {
|
|
|
|
//cout << _name << ": " << "restarting flightplan" << endl;
|
|
|
|
fp->restart();
|
|
|
|
curr = fp->getCurrentWaypoint();
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
} else {
|
|
|
|
//cout << _name << ": " << "ending flightplan" << endl;
|
|
|
|
setDie(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (next->name == "WAIT") {
|
|
|
|
//cout << _name << ": begin WAIT: " << prev->name << " ";
|
|
|
|
//cout << curr->name << " " << next->name << endl;
|
|
|
|
|
|
|
|
elapsed_sec += next->time_sec;
|
|
|
|
|
|
|
|
if ( elapsed_sec >= day_sec)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
|
|
|
|
if (next->name != "WAITUNTIL" && next->name != "WAIT"
|
2009-09-18 12:51:23 +00:00
|
|
|
&& next->name != "END") {
|
|
|
|
prev = curr;
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
curr = fp->getCurrentWaypoint();
|
|
|
|
next = fp->getNextWaypoint();
|
2007-03-30 22:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (next->name == "WAITUNTIL") {
|
|
|
|
double until_sec = processTimeString(next->time);
|
|
|
|
|
|
|
|
if (until_sec > _start_sec && start_sec < 0)
|
|
|
|
until_sec -= _day;
|
|
|
|
|
|
|
|
if (elapsed_sec < until_sec)
|
|
|
|
elapsed_sec = until_sec;
|
|
|
|
|
|
|
|
if (elapsed_sec >= day_sec )
|
|
|
|
break;
|
|
|
|
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
|
|
|
|
if (next->name != "WAITUNTIL" && next->name != "WAIT") {
|
|
|
|
prev = curr;
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
curr = fp->getCurrentWaypoint();
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
//cout << _name << ": end WAITUNTIL: ";
|
|
|
|
//cout << prev->name << " " << curr->name << " " << next->name << endl;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
distance_nm = getRange(prev->latitude, prev->longitude, curr->latitude, curr->longitude);
|
|
|
|
elapsed_sec += distance_nm * 60 * 60 / prev->speed;
|
|
|
|
|
|
|
|
if (elapsed_sec >= day_sec)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fp->IncrementWaypoint(false);
|
|
|
|
prev = fp->getPreviousWaypoint();
|
|
|
|
curr = fp->getCurrentWaypoint();
|
|
|
|
next = fp->getNextWaypoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end while
|
|
|
|
|
|
|
|
// the required position lies between the previous and current waypoints
|
|
|
|
// so we will calculate the distance back up the track from the current waypoint
|
|
|
|
// then calculate the lat and lon.
|
2009-08-25 09:54:49 +00:00
|
|
|
|
2009-09-18 12:51:23 +00:00
|
|
|
//cout << "advancing flight plan done elapsed_sec: " << elapsed_sec
|
|
|
|
// << " " << day_sec << endl;
|
2007-03-30 22:51:52 +00:00
|
|
|
|
|
|
|
double time_diff = elapsed_sec - day_sec;
|
|
|
|
double lat, lon, recip;
|
|
|
|
|
|
|
|
//cout << " time diff " << time_diff << endl;
|
|
|
|
|
|
|
|
if (next->name == "WAIT" ){
|
|
|
|
setSpeed(0);
|
|
|
|
lat = curr->latitude;
|
|
|
|
lon = curr->longitude;
|
|
|
|
_wait_count= time_diff;
|
|
|
|
_waiting = true;
|
|
|
|
} else if (next->name == "WAITUNTIL") {
|
|
|
|
setSpeed(0);
|
|
|
|
lat = curr->latitude;
|
|
|
|
lon = curr->longitude;
|
|
|
|
_waiting = true;
|
|
|
|
} else {
|
|
|
|
setSpeed(prev->speed);
|
|
|
|
distance_nm = speed * time_diff / (60 * 60);
|
|
|
|
double brg = getCourse(curr->latitude, curr->longitude, prev->latitude, prev->longitude);
|
|
|
|
|
|
|
|
//cout << " brg " << brg << " from " << curr->name << " to " << prev->name << " "
|
|
|
|
// << " lat " << curr->latitude << " lon " << curr->longitude
|
|
|
|
// << " distance m " << distance_nm * SG_NM_TO_METER << endl;
|
|
|
|
|
|
|
|
lat = geo_direct_wgs_84 (curr->latitude, curr->longitude, brg,
|
|
|
|
distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
|
|
|
|
lon = geo_direct_wgs_84 (curr->latitude, curr->longitude, brg,
|
|
|
|
distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
|
|
|
|
recip = geo_direct_wgs_84 (curr->latitude, curr->longitude, brg,
|
|
|
|
distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
|
|
|
|
}
|
2007-03-05 21:40:39 +00:00
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
setLatitude(lat);
|
|
|
|
setLongitude(lon);
|
2009-08-25 09:54:49 +00:00
|
|
|
|
2007-03-30 22:51:52 +00:00
|
|
|
return true;
|
2005-08-16 09:37:23 +00:00
|
|
|
}
|
2009-08-25 09:54:49 +00:00
|
|
|
|
|
|
|
void FGAIShip::setWPPos() {
|
|
|
|
|
2009-09-18 12:51:23 +00:00
|
|
|
if (curr->name == "END" || curr->name == "WAIT"
|
|
|
|
|| curr->name == "WAITUNTIL" || curr->name == "TUNNEL"){
|
|
|
|
//cout << curr->name << " returning" << endl;
|
|
|
|
return;
|
2009-08-25 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double elevation_m = 0;
|
|
|
|
wppos.setLatitudeDeg(curr->latitude);
|
|
|
|
wppos.setLongitudeDeg(curr->longitude);
|
2009-09-18 12:51:23 +00:00
|
|
|
wppos.setElevationM(0);
|
2009-08-25 09:54:49 +00:00
|
|
|
|
|
|
|
if (curr->on_ground){
|
|
|
|
|
2009-09-30 06:03:46 +00:00
|
|
|
if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(wppos, 3000),
|
|
|
|
elevation_m, &_material, 0)){
|
2009-08-25 09:54:49 +00:00
|
|
|
wppos.setElevationM(elevation_m);
|
|
|
|
}
|
|
|
|
|
2009-09-18 12:51:23 +00:00
|
|
|
//cout << curr->name << " setting measured elev " << elevation_m << endl;
|
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
} else {
|
2009-09-18 12:51:23 +00:00
|
|
|
wppos.setElevationM(curr->altitude);
|
|
|
|
//cout << curr->name << " setting FP elev " << elevation_m << endl;
|
2009-08-25 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
2009-09-18 12:51:23 +00:00
|
|
|
curr->altitude = wppos.getElevationM();
|
|
|
|
|
2009-08-25 09:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGAIShip::setXTrackError() {
|
|
|
|
|
|
|
|
double course = getCourse(prev->latitude, prev->longitude,
|
|
|
|
curr->latitude, curr->longitude);
|
|
|
|
double brg = getCourse(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
|
|
|
|
curr->latitude, curr->longitude);
|
|
|
|
double xtrack_error_nm = sin((course - brg)* SG_DEGREES_TO_RADIANS) * _wp_range;
|
2009-10-31 23:15:14 +00:00
|
|
|
double factor = -0.0045 * speed + 1;
|
|
|
|
double limit = _lead_angle_limit * factor;
|
2009-08-25 09:54:49 +00:00
|
|
|
|
|
|
|
if (_wp_range > 0){
|
|
|
|
_lead_angle = atan2(xtrack_error_nm,(_wp_range * _proportion)) * SG_RADIANS_TO_DEGREES;
|
|
|
|
} else
|
|
|
|
_lead_angle = 0;
|
|
|
|
|
2009-10-31 23:15:14 +00:00
|
|
|
_lead_angle *= _lead_angle_gain * factor;
|
2009-08-25 09:54:49 +00:00
|
|
|
_xtrack_error = xtrack_error_nm * 6076.1155;
|
|
|
|
|
2009-10-31 23:15:14 +00:00
|
|
|
SG_CLAMP_RANGE(_lead_angle, -limit, limit);
|
2009-08-25 09:54:49 +00:00
|
|
|
|
|
|
|
}
|