#endif
#include <math.h>
+
+#include <simgear/sg_inlines.h>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/timing/sg_time.hxx>
#include <simgear/math/sg_random.h>
+
#include <simgear/scene/util/SGNodeMasks.hxx>
+#include <Scenery/scenery.hxx>
#include "AIShip.hxx"
FGAIShip::FGAIShip(object_type ot) :
- FGAIBase(ot),
- _dt_count(0),
- _next_run(0)
+// allow HOT to be enabled
+FGAIBase(ot, true),
+
+
+_waiting(false),
+_new_waypoint(true),
+_tunnel(false),
+_initial_tunnel(false),
+_restart(false),
+_hdg_constant(0.01),
+_limit(100),
+_elevation_m(0),
+_elevation_ft(0),
+_tow_angle(0),
+_missed_count(0),
+_wp_range(0),
+_dt_count(0),
+_next_run(0),
+_roll_constant(0.001),
+_roll_factor(-0.0083335),
+_old_range(0),
+_range_rate(0),
+_missed_time_sec(30),
+_day(86400),
+_lead_angle(0),
+_xtrack_error(0),
+_curr_alt(0),
+_prev_alt(0),
+_until_time(""),
+_fp_init(false),
+_missed(false)
{
+ invisible = false;
}
FGAIShip::~FGAIShip() {
setRadius(scFileNode->getDoubleValue("turn-radius-ft", 2000));
std::string flightplan = scFileNode->getStringValue("flightplan");
setRepeat(scFileNode->getBoolValue("repeat", false));
+ setRestart(scFileNode->getBoolValue("restart", false));
setStartTime(scFileNode->getStringValue("time", ""));
+ 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));
+ setSMPath(scFileNode->getStringValue("submodel-path", ""));
+ setRollFactor(scFileNode->getDoubleValue("roll-factor", 1));
if (!flightplan.empty()) {
+ SG_LOG(SG_AI, SG_ALERT, "getting flightplan: " << _name );
+
FGAIFlightPlan* fp = new FGAIFlightPlan(flightplan);
setFlightPlan(fp);
}
}
bool FGAIShip::init(bool search_in_AI_path) {
+ reinit();
+ return FGAIBase::init(search_in_AI_path);
+}
+
+void FGAIShip::reinit()
+{
prev = 0; // the one behind you
curr = 0; // the one ahead
next = 0; // the next plus 1
- _until_time = "";
-
props->setStringValue("name", _name.c_str());
- props->setStringValue("position/waypoint-name-prev", _prev_name.c_str());
- props->setStringValue("position/waypoint-name-curr", _curr_name.c_str());
- props->setStringValue("position/waypoint-name-next", _next_name.c_str());
+ 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());
props->setStringValue("submodels/path", _path.c_str());
- props->setStringValue("position/waypoint-start-time", _start_time.c_str());
- props->setStringValue("position/waypoint-wait-until-time", _until_time.c_str());
+ props->setStringValue("waypoint/start-time", _start_time.c_str());
+ props->setStringValue("waypoint/wait-until-time", _until_time.c_str());
_hdg_lock = false;
_rudder = 0.0;
no_roll = false;
- _rudder_constant = 0.5;
- _roll_constant = 0.001;
- _speed_constant = 0.05;
- _hdg_constant = 0.01;
- _roll_factor = -0.0083335;
-
_rd_turn_radius_ft = _sp_turn_radius_ft = turn_radius_ft;
- _fp_init = false;
- _missed = false;
- _waiting = false;
- _new_waypoint = true;
-
- _missed_count = 0;
- _wait_count = 0;
- _missed_time_sec = 30;
-
- _day = 86400;
-
-
- _wp_range = _old_range = 0;
- _range_rate = 0;
-
if (fp)
_fp_init = initFlightPlan();
- return FGAIBase::init(search_in_AI_path);
-}
-
-void FGAIShip::initModel(osg::Node *node) {
- FGAIBase::initModel(node);
- model->setNodeMask(model->getNodeMask() | SG_NODEMASK_TERRAIN_BIT);
+ FGAIBase::reinit();
}
void FGAIShip::bind() {
SGRawValuePointer<double>(&_rudder_constant));
props->tie("controls/constants/speed",
SGRawValuePointer<double>(&_speed_constant));
- props->tie("position/waypoint-range-nm",
+ props->tie("waypoint/range-nm",
SGRawValuePointer<double>(&_wp_range));
- props->tie("position/waypoint-range-old-nm",
- SGRawValuePointer<double>(&_old_range));
- props->tie("position/waypoint-range-rate-nm-sec",
+ props->tie("waypoint/brg-deg",
+ SGRawValuePointer<double>(&_course));
+ props->tie("waypoint/rangerate-nm-sec",
SGRawValuePointer<double>(&_range_rate));
- props->tie("position/waypoint-new",
+ props->tie("waypoint/new",
SGRawValuePointer<bool>(&_new_waypoint));
- props->tie("position/waypoint-missed",
+ props->tie("waypoint/missed",
SGRawValuePointer<bool>(&_missed));
- props->tie("position/waypoint-missed-count",
+ props->tie("waypoint/missed-count-sec",
SGRawValuePointer<double>(&_missed_count));
- props->tie("position/waypoint-missed-time-sec",
+ props->tie("waypoint/missed-range-nm",
+ SGRawValuePointer<double>(&_missed_range));
+ props->tie("waypoint/missed-time-sec",
SGRawValuePointer<double>(&_missed_time_sec));
- props->tie("position/waypoint-wait-count",
+ props->tie("waypoint/wait-count-sec",
SGRawValuePointer<double>(&_wait_count));
- props->tie("position/waypoint-waiting",
+ props->tie("waypoint/xtrack-error-ft",
+ SGRawValuePointer<double>(&_xtrack_error));
+ props->tie("waypoint/waiting",
SGRawValuePointer<bool>(&_waiting));
+ props->tie("waypoint/lead-angle-deg",
+ SGRawValuePointer<double>(&_lead_angle));
+ 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));
props->tie("submodels/serviceable",
SGRawValuePointer<bool>(&_serviceable));
+ props->tie("controls/turn-radius-ft",
+ SGRawValuePointer<double>(&turn_radius_ft));
+ 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));
+ props->tie("controls/restart",
+ SGRawValuePointer<bool>(&_restart));
+ props->tie("velocities/speed-kts",
+ SGRawValuePointer<double>(&speed));
}
void FGAIShip::unbind() {
props->untie("controls/constants/rudder");
props->untie("controls/constants/roll-factor");
props->untie("controls/constants/speed");
- props->untie("position/waypoint-range-nm");
- props->untie("position/waypoint-range-old-nm");
- props->untie("position/waypoint-range-rate-nm-sec");
- props->untie("position/waypoint-new");
- props->untie("position/waypoint-missed");
- props->untie("position/waypoint-missed-count");
- props->untie("position/waypoint-missed-time-sec");
- props->untie("position/waypoint-wait-count");
- props->untie("position/waypoint-waiting");
+ 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");
+ props->untie("waypoint/tunnel");
+ props->untie("waypoint/alt-curr-m");
+ props->untie("waypoint/alt-prev-m");
props->untie("submodels/serviceable");
+ props->untie("controls/turn-radius-ft");
+ 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");
+ props->untie("controls/restart");
+ props->untie("velocities/speed-kts");
+
}
void FGAIShip::update(double dt) {
+ //SG_LOG(SG_AI, SG_ALERT, "updating Ship: " << _name <<hdg<<pitch<<roll);
// 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);
- // The orientation of the carrier wrt the horizontal local frame
+ // The orientation of the ship wrt the horizontal local frame
SGQuatd hl2body = SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
// and postrotate the orientation of the AIModel wrt the horizontal
// local frame
SGQuatd ec2body = ec2hl*hl2body;
- // The cartesian position of the carrier in the wgs84 world
- SGVec3d cartPos = SGVec3d::fromGeod(pos);
+ // The cartesian position of the ship in the wgs84 world
+ //SGVec3d cartPos = SGVec3d::fromGeod(pos);
+
+ // The simulation time this transform is meant for
+ aip.setReferenceTime(globals->get_sim_time_sec());
// Compute the velocity in m/s in the body frame
aip.setBodyLinearVelocity(SGVec3d(0.51444444*speed, 0, 0));
FGAIBase::update(dt);
Run(dt);
Transform();
+ if (fp)
+ setXTrackError();
// Only change these values if we are able to compute them safely
if (SGLimits<double>::min() < dt) {
// Now here is the finite difference ...
-
+
// Transform that one to the horizontal local coordinate system.
SGQuatd ec2hlNew = SGQuatd::fromLonLat(pos);
// compute the new orientation
dOr.getAngleAxis(dOrAngleAxis);
// divided by the time difference provides a rotation speed vector
dOrAngleAxis /= dt;
-
+
aip.setBodyAngularVelocity(dOrAngleAxis);
}
}
void FGAIShip::Run(double dt) {
- //cout << _name << " init: " << _fp_init << endl;
if (_fp_init)
ProcessFlightPlan(dt);
- // double speed_north_deg_sec;
- // double speed_east_deg_sec;
+ string type = getTypeString();
+
double alpha;
double rudder_limit;
double raw_roll;
}
- // do not allow unreasonable ship speeds
- if (speed > 40)
- speed = 40;
+ // do not allow unreasonable speeds
+ SG_CLAMP_RANGE(speed, -_limit * 0.75, _limit);
// convert speed to degrees per second
speed_north_deg_sec = cos(hdg / SGD_RADIANS_TO_DEGREES)
* speed * 1.686 / ft_per_deg_lon;
// set new position
+ //cout << _name << " " << type << " run: " << _elevation_m << " " <<_elevation_ft << endl;
pos.setLatitudeDeg(pos.getLatitudeDeg() + speed_north_deg_sec * dt);
pos.setLongitudeDeg(pos.getLongitudeDeg() + speed_east_deg_sec * dt);
+ pos.setElevationFt(tgt_altitude_ft);
+ pitch = tgt_pitch;
// adjust heading based on current _rudder angle
-
- //cout << "turn_radius_ft " << turn_radius_ft ;
-
if (turn_radius_ft <= 0)
turn_radius_ft = 0; // don't allow nonsense values
if (_rudder < -45)
_rudder = -45;
+
//we assume that at slow speed ships will manoeuvre using engines/bow thruster
- if (fabs(speed)<=5)
- _sp_turn_radius_ft = 500;
- 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;
+ if(type == "ship" || type == "carrier" || type == "escort"){
+
+ 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;
+ }
+ }
- //cout << " speed turn radius " << _sp_turn_radius_ft ;
if (_rudder <= -0.25 || _rudder >= 0.25) {
// adjust turn radius for _rudder angle. The equation is even more approximate.
_rd_turn_radius_ft = (a * exp(b * fabs(_rudder)) + c) * _sp_turn_radius_ft;
- //cout <<" _rudder turn radius " << _rd_turn_radius_ft << endl;
-
// 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;
-
+ //cout << _name << " alpha " << alpha << endl;
// make sure that alpha is applied in the right direction
hdg += alpha * sign(_rudder);
- if (hdg > 360.0)
- hdg -= 360.0;
-
- if (hdg < 0.0)
- hdg += 360.0;
+ SG_NORMALIZE_RANGE(hdg, 0.0, 360.0);
//adjust roll for rudder angle and speed. Another bit of voodoo
raw_roll = _roll_factor * speed * _rudder;
}
// set the _rudder limit by speed
- if (speed <= 40)
- rudder_limit = (-0.825 * speed) + 35;
- else
- rudder_limit = 2;
+ if (type == "ship" || type == "carrier" || type == "escort"){
+
+ if (speed <= 40)
+ rudder_limit = (-0.825 * speed) + 35;
+ else
+ rudder_limit = 2;
+
+ } else
+ rudder_limit = 20;
if (fabs(rudder_diff)> 0.1) { // apply dead zone
}
void FGAIShip::ClimbTo(double altitude) {
+ tgt_altitude_ft = altitude;
+ _setAltitude(altitude);
}
-
void FGAIShip::TurnTo(double heading) {
- tgt_heading = heading;
+ tgt_heading = heading - _lead_angle + _tow_angle;
+ SG_NORMALIZE_RANGE(tgt_heading, 0.0, 360.0);
_hdg_lock = true;
}
fp = f;
}
-void FGAIShip::setName(const string& n) {
- _name = n;
-}
-
void FGAIShip::setStartTime(const string& st) {
_start_time = st;
}
void FGAIShip::setUntilTime(const string& ut) {
_until_time = ut;
- props->setStringValue("position/waypoint-wait-until-time", _until_time.c_str());
+ props->setStringValue("waypoint/wait-until-time", _until_time.c_str());
}
void FGAIShip::setCurrName(const string& c) {
_curr_name = c;
- props->setStringValue("position/waypoint-name-curr", _curr_name.c_str());
+ props->setStringValue("waypoint/name-curr", _curr_name.c_str());
}
void FGAIShip::setNextName(const string& n) {
_next_name = n;
- props->setStringValue("position/waypoint-name-next", _next_name.c_str());
+ props->setStringValue("waypoint/name-next", _next_name.c_str());
}
void FGAIShip::setPrevName(const string& p) {
_prev_name = p;
- props->setStringValue("position/waypoint-name-prev", _prev_name.c_str());
+ props->setStringValue("waypoint/name-prev", _prev_name.c_str());
}
void FGAIShip::setRepeat(bool r) {
_repeat = r;
}
+void FGAIShip::setRestart(bool r) {
+ _restart = r;
+}
+
void FGAIShip::setMissed(bool m) {
_missed = m;
- props->setBoolValue("position/waypoint-missed", _missed);
+ props->setBoolValue("waypoint/missed", _missed);
}
void FGAIShip::setRudder(float r) {
roll = rl;
}
+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;
+}
+
+void FGAIShip::setRollFactor(double rf) {
+ _roll_factor = rf * -0.0083335;
+}
+
+void FGAIShip::setInitialTunnel(bool t) {
+ _initial_tunnel = t;
+ setTunnel(_initial_tunnel);
+}
+
+void FGAIShip::setTunnel(bool t) {
+ _tunnel = t;
+}
+
void FGAIShip::setWPNames() {
if (prev != 0)
- setPrevName(prev->name);
+ setPrevName(prev->getName());
else
setPrevName("");
- setCurrName(curr->name);
+ if (curr != 0)
+ setCurrName(curr->getName());
+ else{
+ setCurrName("");
+ SG_LOG(SG_AI, SG_ALERT, "AIShip: current wp name error" );
+ }
if (next != 0)
- setNextName(next->name);
+ setNextName(next->getName());
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);
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: prev wp name " << prev->getName());
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: current wp name " << curr->getName());
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: next wp name " << next->getName());
}
geo_inverse_wgs_84(lat, lon, lat2, lon2, &course, &recip, &distance);
if (tgt_speed >= 0) {
return course;
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: course " << course);
} else {
return recip;
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: recip " << recip);
}
}
void FGAIShip::ProcessFlightPlan(double dt) {
+ if ( dt < 0.00001 ) {
+ return;
+ }
+
double time_sec = getDaySeconds();
- double until_time_sec = 0;
- _missed = false;
_dt_count += dt;
///////////////////////////////////////////////////////////////////////////
if (_dt_count < _next_run && _start_sec < time_sec)
return;
- _next_run = 1.0 + (0.5 * sg_random());
+ _next_run = 0.05 + (0.025 * sg_random());
+
+ double until_time_sec = 0;
+ _missed = false;
// 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);
+ _wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->getLatitude(), curr->getLongitude());
_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
- // calculate the time needed to turn through an arc of 90 degrees, and allow an error of 30 secs
+ // calculate the time needed to turn through an arc of 90 degrees,
+ // and allow a time error
if (speed != 0)
- _missed_time_sec = 30 + ((SGD_PI * sp_turn_radius_nm * 60 * 60) / (2 * fabs(speed)));
+ _missed_time_sec = 10 + ((SGD_PI * sp_turn_radius_nm * 60 * 60) / (2 * fabs(speed)));
else
- _missed_time_sec = 30;
+ _missed_time_sec = 10;
+
+ _missed_range = 4 * sp_turn_radius_nm;
- if ((_range_rate > 0) && (_wp_range < 3 * sp_turn_radius_nm) && !_new_waypoint)
+ //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){
_missed_count += _dt_count;
+ }
- if (_missed_count >= _missed_time_sec) {
+ if (_missed_count >= 120)
setMissed(true);
- } else {
+ else if (_missed_count >= _missed_time_sec)
+ setMissed(true);
+ else
setMissed(false);
- }
_old_range = _wp_range;
setWPNames();
- if ((_wp_range < sp_turn_radius_nm) || _missed || _waiting && !_new_waypoint) {
+ if ((_wp_range < (sp_turn_radius_nm * 1.25)) || _missed || (_waiting && !_new_waypoint)) {
+
+ if (_next_name == "TUNNEL"){
+ _tunnel = !_tunnel;
- if (_next_name == "END") {
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: " << _name << " " << sp_turn_radius_nm );
+
+ fp->IncrementWaypoint(false);
+ next = fp->getNextWaypoint();
+
+ if (next->getName() == "WAITUNTIL" || next->getName() == "WAIT"
+ || next->getName() == "END" || next->getName() == "TUNNEL")
+ return;
+
+ prev = curr;
+ fp->IncrementWaypoint(false);
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
+
+ }else if(_next_name == "END" || fp->getNextWaypoint() == 0) {
if (_repeat) {
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: Flightplan restarting ");
+ SG_LOG(SG_AI, SG_INFO, "AIShip: "<< _name << " Flightplan repeating ");
fp->restart();
prev = curr;
curr = fp->getCurrentWaypoint();
next = fp->getNextWaypoint();
setWPNames();
- _wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->latitude, curr->longitude);
+ _wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->getLatitude(), curr->getLongitude());
_old_range = _wp_range;
_range_rate = 0;
_new_waypoint = true;
_missed_count = 0;
- AccelTo(prev->speed);
+ _lead_angle = 0;
+ AccelTo(prev->getSpeed());
+ } else if (_restart){
+ SG_LOG(SG_AI, SG_INFO, "AIShip: " << _name << " Flightplan restarting ");
+ _missed_count = 0;
+ initFlightPlan();
} else {
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: Flightplan dieing ");
+ SG_LOG(SG_AI, SG_ALERT, "AIShip: " << _name << " Flightplan dying ");
setDie(true);
_dt_count = 0;
return;
} else if (_next_name == "WAIT") {
- if (_wait_count < next->time_sec) {
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name << " waiting ");
+ if (_wait_count < next->getTime_sec()) {
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: " << _name << " waiting ");
setSpeed(0);
_waiting = true;
_wait_count += _dt_count;
_dt_count = 0;
+ _lead_angle = 0;
return;
} else {
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name
+ SG_LOG(SG_AI, 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"
- || next->name == "END")
+ if (next->getName() == "WAITUNTIL" || next->getName() == "WAIT"
+ || next->getName() == "END" || next->getName() == "TUNNEL")
return;
prev = curr;
} else if (_next_name == "WAITUNTIL") {
time_sec = getDaySeconds();
- until_time_sec = processTimeString(next->time);
- _until_time = next->time;
- setUntilTime(next->time);
+ until_time_sec = processTimeString(next->getTime());
+ _until_time = next->getTime();
+ setUntilTime(next->getTime());
if (until_time_sec > time_sec) {
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name << " waiting until: "
+ SG_LOG(SG_AI, SG_INFO, "AIShip: " << _name << " "
+ << curr->getName() << " waiting until: "
<< _until_time << " " << until_time_sec << " now " << time_sec );
setSpeed(0);
+ _lead_angle = 0;
_waiting = true;
return;
} else {
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: "
+ SG_LOG(SG_AI, SG_INFO, "AIShip: "
<< _name << " wait until done: getting new waypoints ");
setUntilTime("");
fp->IncrementWaypoint(false);
- while (next->name == "WAITUNTIL") {
+ while (next->getName() == "WAITUNTIL") {
fp->IncrementWaypoint(false);
next = fp->getNextWaypoint();
}
- if (next->name == "WAIT")
+ if (next->getName() == "WAIT")
return;
prev = curr;
} else {
//now reorganise the waypoints, so that next becomes current and so on
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name << " getting new waypoints ");
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: " << _name << " getting new waypoints ");
fp->IncrementWaypoint(false);
prev = fp->getPreviousWaypoint(); //first waypoint
curr = fp->getCurrentWaypoint(); //second waypoint
_new_waypoint = true;
_missed_count = 0;
_range_rate = 0;
- _wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->latitude, curr->longitude);
+ _lead_angle = 0;
+ _wp_range = getRange(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->getLatitude(), curr->getLongitude());
_old_range = _wp_range;
- AccelTo(prev->speed);
+ setWPPos();
+ object_type type = getType();
+
+ if (type != 10)
+ AccelTo(prev->getSpeed());
+
+ _curr_alt = curr->getAltitude();
+ _prev_alt = prev->getAltitude();
+
} else {
_new_waypoint = false;
}
// now revise the required course for the next way point
- double course = getCourse(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->latitude, curr->longitude);
+ _course = getCourse(pos.getLatitudeDeg(), pos.getLongitudeDeg(), curr->getLatitude(), curr->getLongitude());
- if (finite(course))
- TurnTo(course);
+ if (finite(_course))
+ TurnTo(_course);
else
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: Bearing or Range is not a finite number");
+ SG_LOG(SG_AI, SG_ALERT, "AIShip: Bearing or Range is not a finite number");
- _dt_count = 0;
+ _dt_count = 0;
} // end Processing FlightPlan
bool FGAIShip::initFlightPlan() {
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name << " initializing waypoints ");
+ SG_LOG(SG_AI, SG_ALERT, "AIShip: " << _name << " initializing waypoints ");
bool init = false;
-
_start_sec = 0;
+ _tunnel = _initial_tunnel;
fp->restart();
fp->IncrementWaypoint(false);
curr = fp->getCurrentWaypoint(); //second waypoint
next = fp->getNextWaypoint(); //third waypoint (might not exist!)
- while (curr->name == "WAIT" || curr->name == "WAITUNTIL") { // don't wait when initialising
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name << " re-initializing waypoints ");
+ while (curr->getName() == "WAIT" || curr->getName() == "WAITUNTIL") { // don't wait when initialising
+ SG_LOG(SG_AI, SG_DEBUG, "AIShip: " << _name << " re-initializing waypoints ");
fp->IncrementWaypoint(false);
curr = fp->getCurrentWaypoint();
next = fp->getNextWaypoint();
- }
+ } // end while loop
if (!_start_time.empty()){
_start_sec = processTimeString(_start_time);
}
} else {
- setLatitude(prev->latitude);
- setLongitude(prev->longitude);
- setSpeed(prev->speed);
+ setLatitude(prev->getLatitude());
+ setLongitude(prev->getLongitude());
+ setSpeed(prev->getSpeed());
}
setWPNames();
- setHeading(getCourse(prev->latitude, prev->longitude, curr->latitude, curr->longitude));
- _wp_range = getRange(prev->latitude, prev->longitude, curr->latitude, curr->longitude);
+ setHeading(getCourse(prev->getLatitude(), prev->getLongitude(), curr->getLatitude(), curr->getLongitude()));
+ _wp_range = getRange(prev->getLatitude(), prev->getLongitude(), curr->getLatitude(), curr->getLongitude());
_old_range = _wp_range;
_range_rate = 0;
_hdg_lock = true;
_missed_count = 0;
_new_waypoint = true;
- SG_LOG(SG_GENERAL, SG_DEBUG, "AIShip: " << _name << " done initialising waypoints ");
+ SG_LOG(SG_AI, SG_ALERT, "AIShip: " << _name << " done initialising waypoints " << _tunnel);
if (prev)
init = true;
while ( elapsed_sec < day_sec ) {
- if (next->name == "END") {
+ if (next->getName() == "END" || fp->getNextWaypoint() == 0) {
if (_repeat ) {
//cout << _name << ": " << "restarting flightplan" << endl;
return false;
}
- } else if (next->name == "WAIT") {
+ } else if (next->getName() == "WAIT") {
//cout << _name << ": begin WAIT: " << prev->name << " ";
//cout << curr->name << " " << next->name << endl;
- elapsed_sec += next->time_sec;
+ elapsed_sec += next->getTime_sec();
if ( elapsed_sec >= day_sec)
continue;
fp->IncrementWaypoint(false);
next = fp->getNextWaypoint();
- if (next->name != "WAITUNTIL" && next->name != "WAIT"
- && next->name != "END") {
- prev = curr;
- fp->IncrementWaypoint(false);
- curr = fp->getCurrentWaypoint();
- next = fp->getNextWaypoint();
+ if (next->getName() != "WAITUNTIL" && next->getName() != "WAIT"
+ && next->getName() != "END") {
+ prev = curr;
+ fp->IncrementWaypoint(false);
+ curr = fp->getCurrentWaypoint();
+ next = fp->getNextWaypoint();
}
- } else if (next->name == "WAITUNTIL") {
- double until_sec = processTimeString(next->time);
+ } else if (next->getName() == "WAITUNTIL") {
+ double until_sec = processTimeString(next->getTime());
if (until_sec > _start_sec && start_sec < 0)
until_sec -= _day;
fp->IncrementWaypoint(false);
next = fp->getNextWaypoint();
- if (next->name != "WAITUNTIL" && next->name != "WAIT") {
+ if (next->getName() != "WAITUNTIL" && next->getName() != "WAIT") {
prev = curr;
fp->IncrementWaypoint(false);
curr = fp->getCurrentWaypoint();
//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;
+ distance_nm = getRange(prev->getLatitude(), prev->getLongitude(), curr->getLatitude(), curr->getLongitude());
+ elapsed_sec += distance_nm * 60 * 60 / prev->getSpeed();
if (elapsed_sec >= day_sec)
continue;
// 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.
- /*cout << "advancing flight plan done elapsed_sec: " << elapsed_sec
- << " " << day_sec << endl;*/
+
+ //cout << "advancing flight plan done elapsed_sec: " << elapsed_sec
+ // << " " << day_sec << endl;
double time_diff = elapsed_sec - day_sec;
double lat, lon, recip;
//cout << " time diff " << time_diff << endl;
- if (next->name == "WAIT" ){
+ if (next->getName() == "WAIT" ){
setSpeed(0);
- lat = curr->latitude;
- lon = curr->longitude;
+ lat = curr->getLatitude();
+ lon = curr->getLongitude();
_wait_count= time_diff;
_waiting = true;
- } else if (next->name == "WAITUNTIL") {
+ } else if (next->getName() == "WAITUNTIL") {
setSpeed(0);
- lat = curr->latitude;
- lon = curr->longitude;
+ lat = curr->getLatitude();
+ lon = curr->getLongitude();
_waiting = true;
} else {
- setSpeed(prev->speed);
+ setSpeed(prev->getSpeed());
distance_nm = speed * time_diff / (60 * 60);
- double brg = getCourse(curr->latitude, curr->longitude, prev->latitude, prev->longitude);
+ double brg = getCourse(curr->getLatitude(), curr->getLongitude(), prev->getLatitude(), prev->getLongitude());
//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,
+ lat = geo_direct_wgs_84 (curr->getLatitude(), curr->getLongitude(), brg,
distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
- lon = geo_direct_wgs_84 (curr->latitude, curr->longitude, brg,
+ lon = geo_direct_wgs_84 (curr->getLatitude(), curr->getLongitude(), brg,
distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
- recip = geo_direct_wgs_84 (curr->latitude, curr->longitude, brg,
+ recip = geo_direct_wgs_84 (curr->getLatitude(), curr->getLongitude(), brg,
distance_nm * SG_NM_TO_METER, &lat, &lon, &recip );
}
- //cout << "Pos " << lat << ", " << lon << " recip " << recip << endl;
-
setLatitude(lat);
setLongitude(lon);
+
return true;
}
+
+void FGAIShip::setWPPos() {
+
+ if (curr->getName() == "END" || curr->getName() == "WAIT"
+ || curr->getName() == "WAITUNTIL" || curr->getName() == "TUNNEL"){
+ //cout << curr->name << " returning" << endl;
+ return;
+ }
+
+ double elevation_m = 0;
+ wppos.setLatitudeDeg(curr->getLatitude());
+ wppos.setLongitudeDeg(curr->getLongitude());
+ wppos.setElevationM(0);
+
+ if (curr->getOn_ground()){
+
+ if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(wppos, 3000),
+ elevation_m, &_material, 0)){
+ wppos.setElevationM(elevation_m);
+ }
+
+ //cout << curr->name << " setting measured elev " << elevation_m << endl;
+
+ } else {
+ wppos.setElevationM(curr->getAltitude());
+ //cout << curr->name << " setting FP elev " << elevation_m << endl;
+ }
+
+ curr->setAltitude(wppos.getElevationM());
+
+}
+
+void FGAIShip::setXTrackError() {
+
+ double course = getCourse(prev->getLatitude(), prev->getLongitude(),
+ curr->getLatitude(), curr->getLongitude());
+ double brg = getCourse(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
+ curr->getLatitude(), curr->getLongitude());
+ double xtrack_error_nm = sin((course - brg)* SG_DEGREES_TO_RADIANS) * _wp_range;
+ double factor = -0.0045 * speed + 1;
+ double limit = _lead_angle_limit * factor;
+
+ if (_wp_range > 0){
+ _lead_angle = atan2(xtrack_error_nm,(_wp_range * _proportion)) * SG_RADIANS_TO_DEGREES;
+ } else
+ _lead_angle = 0;
+
+ _lead_angle *= _lead_angle_gain * factor;
+ _xtrack_error = xtrack_error_nm * 6076.1155;
+
+ SG_CLAMP_RANGE(_lead_angle, -limit, limit);
+
+}