X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FAIModel%2FAICarrier.cxx;h=b6487519d026c05ea55bc026423f093d30ce9375;hb=b4fbde72b275dc75e8ecf7b44261aba642e5da0f;hp=ff0d5441ff46d6db15054f593288cace1da66cc8;hpb=1708e43a39ce96b703a4aa21a8455964415dd499;p=flightgear.git diff --git a/src/AIModel/AICarrier.cxx b/src/AIModel/AICarrier.cxx index ff0d5441f..b6487519d 100644 --- a/src/AIModel/AICarrier.cxx +++ b/src/AIModel/AICarrier.cxx @@ -25,19 +25,9 @@ #include #include -#include -#include -#include -#include -#include - #include #include #include -#include -#include -#include -#include #include #include
@@ -45,135 +35,6 @@ #include "AICarrier.hxx" -/// Hmm: move that kind of configuration into the model file??? -class LineCollector : public osg::NodeVisitor { - struct LinePrimitiveFunctor { - LinePrimitiveFunctor() : _lineCollector(0) - { } - void operator() (const osg::Vec3&, bool) - { } - void operator() (const osg::Vec3& v1, const osg::Vec3& v2, bool) - { if (_lineCollector) _lineCollector->addLine(v1, v2); } - void operator() (const osg::Vec3&, const osg::Vec3&, const osg::Vec3&, - bool) - { } - void operator() (const osg::Vec3&, const osg::Vec3&, const osg::Vec3&, - const osg::Vec3&, bool) - { } - LineCollector* _lineCollector; - }; - -public: - LineCollector() : - osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR, - osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) - { } - virtual void apply(osg::Geode& geode) - { - osg::TemplatePrimitiveFunctor pf; - pf._lineCollector = this; - for (unsigned i = 0; i < geode.getNumDrawables(); ++i) { - geode.getDrawable(i)->accept(pf); - } - } - virtual void apply(osg::Node& node) - { - traverse(node); - } - virtual void apply(osg::Transform& transform) - { - osg::Matrix matrix = _matrix; - if (transform.computeLocalToWorldMatrix(_matrix, this)) - traverse(transform); - _matrix = matrix; - } - - const std::vector& getLineSegments() const - { return _lineSegments; } - - void addLine(const osg::Vec3& v1, const osg::Vec3& v2) - { - // Trick to get the ends in the right order. - // Use the x axis in the original coordinate system. Choose the - // most negative x-axis as the one pointing forward - SGVec3f tv1(_matrix.preMult(v1)); - SGVec3f tv2(_matrix.preMult(v2)); - if (tv1[0] > tv2[0]) - _lineSegments.push_back(SGLineSegmentf(tv1, tv2)); - else - _lineSegments.push_back(SGLineSegmentf(tv2, tv1)); - } - - void addBVHElements(osg::Node& node, simgear::BVHLineGeometry::Type type) - { - if (_lineSegments.empty()) - return; - - SGSceneUserData* userData; - userData = SGSceneUserData::getOrCreateSceneUserData(&node); - - simgear::BVHNode* bvNode = userData->getBVHNode(); - if (!bvNode && _lineSegments.size() == 1) { - simgear::BVHLineGeometry* bvLine; - bvLine = new simgear::BVHLineGeometry(_lineSegments.front(), type); - userData->setBVHNode(bvLine); - return; - } - - simgear::BVHGroup* group = new simgear::BVHGroup; - if (bvNode) - group->addChild(bvNode); - - for (unsigned i = 0; i < _lineSegments.size(); ++i) { - simgear::BVHLineGeometry* bvLine; - bvLine = new simgear::BVHLineGeometry(_lineSegments[i], type); - group->addChild(bvLine); - } - userData->setBVHNode(group); - } - -private: - osg::Matrix _matrix; - std::vector _lineSegments; -}; - -class FGCarrierVisitor : public osg::NodeVisitor { -public: - FGCarrierVisitor(FGAICarrier* carrier, - const std::list& wireObjects, - const std::list& catapultObjects) : - osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR, - osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), - mWireObjects(wireObjects), - mCatapultObjects(catapultObjects) - { } - virtual void apply(osg::Node& node) - { - if (std::find(mWireObjects.begin(), mWireObjects.end(), node.getName()) - != mWireObjects.end()) { - LineCollector lineCollector; - node.accept(lineCollector); - simgear::BVHLineGeometry::Type type; - type = simgear::BVHLineGeometry::CarrierWire; - lineCollector.addBVHElements(node, type); - } - if (std::find(mCatapultObjects.begin(), mCatapultObjects.end(), - node.getName()) != mCatapultObjects.end()) { - LineCollector lineCollector; - node.accept(lineCollector); - simgear::BVHLineGeometry::Type type; - type = simgear::BVHLineGeometry::CarrierCatapult; - lineCollector.addBVHElements(node, type); - } - - traverse(node); - } - -private: - std::list mWireObjects; - std::list mCatapultObjects; -}; - FGAICarrier::FGAICarrier() : FGAIShip(otCarrier) { } @@ -195,6 +56,8 @@ void FGAICarrier::readFromScenario(SGPropertyNode* scFileNode) { 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)); SGPropertyNode* flols = scFileNode->getChild("flols-pos"); if (flols) { @@ -207,22 +70,8 @@ void FGAICarrier::readFromScenario(SGPropertyNode* scFileNode) { } else flols_off = SGVec3d::zeros(); - std::vector props = scFileNode->getChildren("wire"); + std::vector props = scFileNode->getChildren("parking-pos"); std::vector::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("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 @@ -269,60 +118,51 @@ void FGAICarrier::setTACANChannelID(const string& id) { TACAN_channel_id = id; } -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); +void FGAICarrier::setMPControl(bool c) { + MPControl = c; +} - // Compute the velocity in m/s in the body frame - aip.setBodyLinearVelocity(SGVec3d(0.51444444*speed, 0, 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_GENERAL, 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 (SGLimits::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 - 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; - - aip.setBodyAngularVelocity(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()); @@ -333,10 +173,10 @@ void FGAICarrier::update(double dt) { 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::min()) { angle = 0; @@ -345,7 +185,7 @@ void FGAICarrier::update(double dt) { 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; @@ -380,13 +220,16 @@ bool FGAICarrier::init(bool search_in_AI_path) { 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; @@ -397,17 +240,6 @@ bool FGAICarrier::init(bool search_in_AI_path) { 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 ... - FGCarrierVisitor carrierVisitor(this, wire_objects, catapult_objects); - model->accept(carrierVisitor); - model->setNodeMask(model->getNodeMask() | SG_NODEMASK_TERRAIN_BIT); -} - void FGAICarrier::bind() { FGAIShip::bind(); @@ -431,8 +263,10 @@ void FGAICarrier::bind() { SGRawValueMethods(pos, &SGGeod::getLatitudeDeg)); props->tie("controls/start-pos-long-deg", SGRawValueMethods(pos, &SGGeod::getLongitudeDeg)); - props->tie("velocities/speed-kts", - SGRawValuePointer(&speed)); + props->tie("controls/mp-control", + SGRawValuePointer(&MPControl)); + props->tie("controls/ai-control", + SGRawValuePointer(&AIControl)); props->tie("environment/surface-wind-speed-true-kts", SGRawValuePointer(&wind_speed_kts)); props->tie("environment/surface-wind-from-true-degs", @@ -443,24 +277,31 @@ void FGAICarrier::bind() { SGRawValuePointer(&rel_wind)); props->tie("environment/rel-wind-speed-kts", SGRawValuePointer(&rel_wind_speed_kts)); - props->tie("controls/flols/wave-off-lights", - SGRawValuePointer(&wave_off_lights)); + props->tie("environment/in-to-wind", + SGRawValuePointer(&in_to_wind)); + //props->tie("controls/flols/wave-off-lights", + // SGRawValuePointer(&wave_off_lights)); props->tie("controls/elevators", SGRawValuePointer(&elevators)); props->tie("surface-positions/elevators-pos-norm", SGRawValuePointer(&pos_norm)); - props->tie("controls/elevators-trans-time-s", + props->tie("controls/constants/elevators/trans-time-s", SGRawValuePointer(&transition_time)); - props->tie("controls/elevators-time-constant", + props->tie("controls/constants/elevators/time-constant", SGRawValuePointer(&time_constant)); props->tie("controls/jbd", SGRawValuePointer(&jbd)); props->tie("surface-positions/jbd-pos-norm", SGRawValuePointer(&jbd_pos_norm)); - props->tie("controls/jbd-trans-time-s", + props->tie("controls/constants/jbd/trans-time-s", SGRawValuePointer(&jbd_transition_time)); - props->tie("controls/jbd-time-constant", + props->tie("controls/constants/jbd/time-constant", SGRawValuePointer(&jbd_time_constant)); + props->tie("controls/turn-to-recovery-hdg", + SGRawValuePointer(&turn_to_recovery_hdg)); + props->tie("controls/turn-to-base-course", + SGRawValuePointer(&turn_to_base_course)); + props->setBoolValue("controls/flols/cut-lights", false); props->setBoolValue("controls/flols/wave-off-lights", false); @@ -481,21 +322,24 @@ void FGAICarrier::unbind() { 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"); } @@ -555,11 +399,14 @@ void FGAICarrier::UpdateWind( double dt) { 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; @@ -568,18 +415,52 @@ void FGAICarrier::UpdateWind( double dt) { 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 @@ -654,9 +535,12 @@ bool FGAICarrier::OutsideBox() { //returns true if the carrier is outside operat 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; }