#include <string>
#include <vector>
-#include <osg/NodeVisitor>
-
#include <simgear/sg_inlines.h>
#include <simgear/math/SGMath.hxx>
#include <simgear/math/sg_geodesy.hxx>
-#include <simgear/scene/util/SGNodeMasks.hxx>
#include <math.h>
#include <Main/util.hxx>
#include "AICarrier.hxx"
-class FGCarrierVisitor : public osg::NodeVisitor {
-public:
- FGCarrierVisitor(FGAICarrier* carrier,
- const std::list<std::string>& wireObjects,
- const std::list<std::string>& catapultObjects,
- const std::list<std::string>& solidObjects) :
- osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR,
- osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
- mWireObjects(wireObjects),
- mCatapultObjects(catapultObjects),
- mSolidObjects(solidObjects),
- mFoundHot(false),
- mCarrier(carrier)
- { }
- virtual void apply(osg::Node& node)
- {
- osg::ref_ptr<osg::Referenced> oldUserData = mUserData;
- bool oldFoundHot = mFoundHot;
- mFoundHot = false;
-
- if (std::find(mWireObjects.begin(), mWireObjects.end(), node.getName())
- != mWireObjects.end()) {
- mFoundHot = true;
- mUserData = FGAICarrierHardware::newWire(mCarrier);
- }
- if (std::find(mCatapultObjects.begin(), mCatapultObjects.end(), node.getName())
- != mCatapultObjects.end()) {
- mFoundHot = true;
- mUserData = FGAICarrierHardware::newCatapult(mCarrier);
- }
- if (std::find(mSolidObjects.begin(), mSolidObjects.end(), node.getName())
- != mSolidObjects.end()) {
- mFoundHot = true;
- mUserData = FGAICarrierHardware::newSolid(mCarrier);
- //SG_LOG(SG_GENERAL, SG_ALERT, "AICarrierVisitor::apply() solidObject" );
- }
- node.setUserData(mUserData.get());
-
- traverse(node);
-
- mFoundHot = oldFoundHot || mFoundHot;
-
- if (mFoundHot) {
- node.setNodeMask(node.getNodeMask() | SG_NODEMASK_TERRAIN_BIT);
- } else
- node.setNodeMask(node.getNodeMask() & ~SG_NODEMASK_TERRAIN_BIT);
-
- mUserData = oldUserData;
- }
-
-private:
- std::list<std::string> mWireObjects;
- std::list<std::string> mCatapultObjects;
- std::list<std::string> mSolidObjects;
- bool mFoundHot;
- FGAICarrier* mCarrier;
- osg::ref_ptr<osg::Referenced> mUserData;
-};
-
FGAICarrier::FGAICarrier() : FGAIShip(otCarrier) {
}
setMinLat(scFileNode->getDoubleValue("min-lat", 0));
setMaxLong(scFileNode->getDoubleValue("max-long", 0));
setMinLong(scFileNode->getDoubleValue("min-long", 0));
+ setMPControl(scFileNode->getBoolValue("mp-control", false));
+ setAIControl(scFileNode->getBoolValue("ai-control", false));
+ setCallSign(scFileNode->getStringValue("callsign", ""));
+
SGPropertyNode* flols = scFileNode->getChild("flols-pos");
if (flols) {
} else
flols_off = SGVec3d::zeros();
- std::vector<SGPropertyNode_ptr> props = scFileNode->getChildren("wire");
+ std::vector<SGPropertyNode_ptr> props = scFileNode->getChildren("parking-pos");
std::vector<SGPropertyNode_ptr>::const_iterator it;
- for (it = props.begin(); it != props.end(); ++it) {
- std::string s = (*it)->getStringValue();
- if (!s.empty())
- wire_objects.push_back(s);
- }
-
- props = scFileNode->getChildren("catapult");
- for (it = props.begin(); it != props.end(); ++it) {
- std::string s = (*it)->getStringValue();
- if (!s.empty())
- catapult_objects.push_back(s);
- }
-
- props = scFileNode->getChildren("solid");
- for (it = props.begin(); it != props.end(); ++it) {
- std::string s = (*it)->getStringValue();
- if (!s.empty())
- solid_objects.push_back(s);
- }
-
- props = scFileNode->getChildren("parking-pos");
for (it = props.begin(); it != props.end(); ++it) {
string name = (*it)->getStringValue("name", "unnamed");
// Transform to the right coordinate frame, configuration is done in
TACAN_channel_id = id;
}
-void FGAICarrier::getVelocityWrtEarth(SGVec3d& v, SGVec3d& omega, SGVec3d& pivot) {
- v = vel_wrt_earth;
- omega = rot_wrt_earth;
- pivot = rot_pivot_wrt_earth;
+void FGAICarrier::setMPControl(bool c) {
+ MPControl = c;
}
-void FGAICarrier::update(double dt) {
- // 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
- 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);
- // Store for later use by the groundcache
- rot_pivot_wrt_earth = cartPos;
-
- // Compute the velocity in m/s in the earth centered coordinate system axis
- double v_north = 0.51444444*speed*cos(hdg * SGD_DEGREES_TO_RADIANS);
- double v_east = 0.51444444*speed*sin(hdg * SGD_DEGREES_TO_RADIANS);
- vel_wrt_earth = ec2hl.backTransform(SGVec3d(v_north, v_east, 0));
+void FGAICarrier::setAIControl(bool c) {
+ AIControl = c;
+}
+void FGAICarrier::update(double dt) {
// Now update the position and heading. This will compute new hdg and
// roll values required for the rotation speed computation.
FGAIShip::update(dt);
-
//automatic turn into wind with a target wind of 25 kts otd
- if(turn_to_launch_hdg){
- TurnToLaunch();
- } else if(OutsideBox() || returning) {// check that the carrier is inside the operating box
- ReturnToBox();
- } else {
- TurnToBase();
- }
+ //SG_LOG(SG_AI, SG_ALERT, "AICarrier: MPControl " << MPControl << " AIControl " << AIControl);
+ if (!MPControl && AIControl){
+
+ if(turn_to_launch_hdg){
+ TurnToLaunch();
+ } else if(turn_to_recovery_hdg ){
+ TurnToRecover();
+ } else if(OutsideBox() || returning ) {// check that the carrier is inside
+ ReturnToBox(); // the operating box,
+ } else {
+ TurnToBase();
+ }
- // Only change these values if we are able to compute them safely
- if (dt < DBL_MIN)
- rot_wrt_earth = SGVec3d::zeros();
- else {
- // Now here is the finite difference ...
-
- // 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;
-
- // now rotate the rotation speed vector back into the
- // earth centered frames coordinates
- dOrAngleAxis = ec2body.backTransform(dOrAngleAxis);
-// dOrAngleAxis = hl2body.backTransform(dOrAngleAxis);
-// dOrAngleAxis(1) = 0;
-// dOrAngleAxis = ec2hl.backTransform(dOrAngleAxis);
- rot_wrt_earth = dOrAngleAxis;
+ } else {
+ FGAIShip::TurnTo(tgt_heading);
+ FGAIShip::AccelTo(tgt_speed);
}
UpdateWind(dt);
UpdateElevator(dt, transition_time);
UpdateJBD(dt, jbd_transition_time);
- // For the flols reuse some computations done above ...
+
+ // Transform that one to the horizontal local coordinate system.
+ SGQuatd ec2hl = SGQuatd::fromLonLat(pos);
+ // The orientation of the carrier 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 position of the eyepoint - at least near that ...
SGVec3d eyePos(globals->get_current_view()->get_view_pos());
eyeWrtCarrier = ec2body.transform(eyeWrtCarrier);
// the eyepoints vector wrt the flols position
SGVec3d eyeWrtFlols = eyeWrtCarrier - flols_off;
-
+
// the distance from the eyepoint to the flols
dist = norm(eyeWrtFlols);
-
+
// now the angle, positive angles are upwards
if (fabs(dist) < SGLimits<float>::min()) {
angle = 0;
sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle));
angle = SGMiscd::rad2deg(asin(sAngle));
}
-
+
// set the value of source
if ( angle <= 4.35 && angle > 4.01 )
source = 1;
turn_to_launch_hdg = false;
+ turn_to_recovery_hdg = false;
+ turn_to_base_course = true;
returning = false;
+ in_to_wind = false;
mOpBoxPos = pos;
base_course = hdg;
base_speed = speed;
- pos_norm = 0;
+ pos_norm = raw_pos_norm = 0;
elevators = false;
transition_time = 150;
time_constant = 0.005;
return true;
}
-void FGAICarrier::initModel(osg::Node *node)
-{
- // SG_LOG(SG_GENERAL, SG_BULK, "AICarrier::initModel()" );
- FGAIShip::initModel(node);
- // process the 3d model here
- // mark some objects solid, mark the wires ...
-
- // The model should be used for altitude computations.
- // To avoid that every detail in a carrier 3D model will end into
- // the aircraft local cache, only set the HOT traversal bit on
- // selected objects.
-
- // Clear the HOT traversal flag
- // Selectively set that flag again for wires/cats/solid objects.
- // Attach a pointer to this carrier class to those objects.
- // SG_LOG(SG_GENERAL, SG_BULK, "AICarrier::initModel() visit" );
- FGCarrierVisitor carrierVisitor(this, wire_objects, catapult_objects, solid_objects);
- model->accept(carrierVisitor);
-// model->setNodeMask(node->getNodeMask() & SG_NODEMASK_TERRAIN_BIT | model->getNodeMask());
-}
-
void FGAICarrier::bind() {
FGAIShip::bind();
SGRawValueMethods<SGGeod,double>(pos, &SGGeod::getLatitudeDeg));
props->tie("controls/start-pos-long-deg",
SGRawValueMethods<SGGeod,double>(pos, &SGGeod::getLongitudeDeg));
- props->tie("velocities/speed-kts",
- SGRawValuePointer<double>(&speed));
+ props->tie("controls/mp-control",
+ SGRawValuePointer<bool>(&MPControl));
+ props->tie("controls/ai-control",
+ SGRawValuePointer<bool>(&AIControl));
props->tie("environment/surface-wind-speed-true-kts",
SGRawValuePointer<double>(&wind_speed_kts));
props->tie("environment/surface-wind-from-true-degs",
SGRawValuePointer<double>(&rel_wind));
props->tie("environment/rel-wind-speed-kts",
SGRawValuePointer<double>(&rel_wind_speed_kts));
- props->tie("controls/flols/wave-off-lights",
- SGRawValuePointer<bool>(&wave_off_lights));
+ props->tie("environment/in-to-wind",
+ SGRawValuePointer<bool>(&in_to_wind));
+ //props->tie("controls/flols/wave-off-lights",
+ // SGRawValuePointer<bool>(&wave_off_lights));
props->tie("controls/elevators",
SGRawValuePointer<bool>(&elevators));
props->tie("surface-positions/elevators-pos-norm",
SGRawValuePointer<double>(&pos_norm));
- props->tie("controls/elevators-trans-time-s",
+ props->tie("controls/constants/elevators/trans-time-s",
SGRawValuePointer<double>(&transition_time));
- props->tie("controls/elevators-time-constant",
+ props->tie("controls/constants/elevators/time-constant",
SGRawValuePointer<double>(&time_constant));
props->tie("controls/jbd",
SGRawValuePointer<bool>(&jbd));
props->tie("surface-positions/jbd-pos-norm",
SGRawValuePointer<double>(&jbd_pos_norm));
- props->tie("controls/jbd-trans-time-s",
+ props->tie("controls/constants/jbd/trans-time-s",
SGRawValuePointer<double>(&jbd_transition_time));
- props->tie("controls/jbd-time-constant",
+ props->tie("controls/constants/jbd/time-constant",
SGRawValuePointer<double>(&jbd_time_constant));
+ props->tie("controls/turn-to-recovery-hdg",
+ SGRawValuePointer<bool>(&turn_to_recovery_hdg));
+ props->tie("controls/turn-to-base-course",
+ SGRawValuePointer<bool>(&turn_to_base_course));
+
props->setBoolValue("controls/flols/cut-lights", false);
props->setBoolValue("controls/flols/wave-off-lights", false);
props->untie("controls/flols/distance-m");
props->untie("controls/flols/angle-degs");
props->untie("controls/turn-to-launch-hdg");
- props->untie("velocities/speed-kts");
props->untie("environment/wind-speed-true-kts");
props->untie("environment/wind-from-true-degs");
props->untie("environment/rel-wind-from-degs");
props->untie("environment/rel-wind-speed-kts");
- props->untie("controls/flols/wave-off-lights");
+ props->untie("environment/in-to-wind");
+ //props->untie("controls/flols/wave-off-lights");
props->untie("controls/elevators");
props->untie("surface-positions/elevators-pos-norm");
- props->untie("controls/elevators-trans-time-secs");
- props->untie("controls/elevators-time-constant");
+ props->untie("controls/constants/elevators/trans-time-secs");
+ props->untie("controls/constants/elevators/time-constant");
props->untie("controls/jbd");
- props->untie("surface-positions/jbd-pos-norm");
- props->untie("controls/jbd-trans-time-s");
+ props->untie("surface-positions/jbd/pos-norm");
+ props->untie("controls/constants/jbd/trans-time-s");
props->untie("controls/jbd-time-constant");
-
+ props->untie("controls/mp-control");
+ props->untie("controls/ai-control");
+ props->untie("controls/turn-to-recovery-hdg");
+ props->untie("controls/turn-to-base-course");
}
rel_wind = rel_wind_from_deg - hdg;
SG_NORMALIZE_RANGE(rel_wind, -180.0, 180.0);
+ //set in to wind property
+ InToWind();
+
//switch the wave-off lights
- if (InToWind())
- wave_off_lights = false;
- else
- wave_off_lights = true;
+ //if (InToWind())
+ // wave_off_lights = false;
+ //else
+ // wave_off_lights = true;
// cout << "rel wind: " << rel_wind << endl;
void FGAICarrier::TurnToLaunch(){
+ // calculate tgt heading
+ if (wind_speed_kts < 3){
+ tgt_heading = base_course;
+ } else {
+ tgt_heading = wind_from_deg;
+ }
+
//calculate tgt speed
double tgt_speed = 25 - wind_speed_kts;
if (tgt_speed < 10)
tgt_speed = 10;
//turn the carrier
- FGAIShip::TurnTo(wind_from_deg);
+ FGAIShip::TurnTo(tgt_heading);
FGAIShip::AccelTo(tgt_speed);
}
+void FGAICarrier::TurnToRecover(){
+
+ //these are the rules for adjusting heading to provide a relative wind
+ //down the angled flightdeck
+
+ if (wind_speed_kts < 3){
+ tgt_heading = base_course + 60;
+ } else if (rel_wind < -9 && rel_wind >= -180){
+ tgt_heading = wind_from_deg;
+ } else if (rel_wind > -7 && rel_wind < 45){
+ tgt_heading = wind_from_deg + 60;
+ } else if (rel_wind >=45 && rel_wind < 180){
+ tgt_heading = wind_from_deg + 45;
+ } else
+ tgt_heading = hdg;
+
+ SG_NORMALIZE_RANGE(tgt_heading, 0.0, 360.0);
+
+ //calculate tgt speed
+ double tgt_speed = 26 - wind_speed_kts;
+ if (tgt_speed < 10)
+ tgt_speed = 10;
+
+ //turn the carrier
+ FGAIShip::TurnTo(tgt_heading);
+ FGAIShip::AccelTo(tgt_speed);
+}
void FGAICarrier::TurnToBase(){
//turn the carrier
bool FGAICarrier::OutsideBox() { //returns true if the carrier is outside operating box
if ( max_lat == 0 && min_lat == 0 && max_long == 0 && min_long == 0) {
- SG_LOG(SG_GENERAL, SG_DEBUG, "AICarrier: No Operating Box defined" );
+ SG_LOG(SG_AI, SG_DEBUG, "AICarrier: No Operating Box defined" );
return false;
}
return true;
}
- SG_LOG(SG_GENERAL, SG_DEBUG, "AICarrier: Inside Operating Box" );
+ SG_LOG(SG_AI, SG_DEBUG, "AICarrier: Inside Operating Box" );
return false;
} // end OutsideBox
bool FGAICarrier::InToWind() {
- if ( fabs(rel_wind) < 5 )
- return true;
+ in_to_wind = false;
+ if ( fabs(rel_wind) < 10 ){
+ in_to_wind = true;
+ return true;
+ }
return false;
}
return;
} // end UpdateJBD
-
-
-int FGAICarrierHardware::unique_id = 1;
-