From ba783ccce99d3edca78212e52852204fcd1d1635 Mon Sep 17 00:00:00 2001 From: Richard Harrison Date: Sun, 25 Apr 2021 17:30:02 +0200 Subject: [PATCH] AI Carriers: tidy and resolve ctor order Tidying up: * change to use direct initializtion * put all of member variables in alpha order * adjust init spacing to make init values clear. * change all member variables to start with an underscore. The reason for this isn't clear but some had an underscore and some didn't; so although I don't know what the convention was intended for this seems to be the appropriate changes. --- src/AIModel/AICarrier.cxx | 402 +++++++++++++++++--------------------- src/AIModel/AICarrier.hxx | 203 ++++++++++--------- 2 files changed, 281 insertions(+), 324 deletions(-) diff --git a/src/AIModel/AICarrier.cxx b/src/AIModel/AICarrier.cxx index f8c62019f..7f42b0123 100644 --- a/src/AIModel/AICarrier.cxx +++ b/src/AIModel/AICarrier.cxx @@ -37,44 +37,7 @@ #include "AICarrier.hxx" #include "AINotifications.hxx" -FGAICarrier::FGAICarrier() : FGAIShip(otCarrier), - deck_altitude_ft(65.0065), - AIControl(false), - MPControl(false), - angle(0), - base_course(0), - base_speed(0), - dist(0), - elevators(false), - in_to_wind(false), - jbd(false), - jbd_pos_norm(0), - jbd_time_constant(0), - jbd_transition_time(0), - lineup(0), - max_lat(0), - max_long(0), - min_lat(0), - min_long(0), - pos_norm(0), - raw_jbd_pos_norm(0), - raw_pos_norm(0), - rel_wind(0), - rel_wind_from_deg(0), - rel_wind_speed_kts(0), - returning(false), - source(0), - time_constant(0), - transition_time(0), - turn_to_base_course(true), - turn_to_recovery_hdg(true), - turn_to_launch_hdg(true), - view_index(0), - wave_off_lights_demand(false), - wind_from_deg(0), - wind_from_east(0), - wind_from_north(0), - wind_speed_kts(0) +FGAICarrier::FGAICarrier() : FGAIShip(otCarrier) { simgear::Emesary::GlobalTransmitter::instance()->Register(this); } @@ -104,7 +67,7 @@ void FGAICarrier::readFromScenario(SGPropertyNode* scFileNode) { setAIControl(scFileNode->getBoolValue("ai-control", false)); setCallSign(scFileNode->getStringValue("callsign", "")); - angled_deck_degrees = scFileNode->getDoubleValue("angled-deck-degrees", -8.5); + _angled_deck_degrees = scFileNode->getDoubleValue("angled-deck-degrees", -8.5); SGPropertyNode* flolsNode = getPositionFromNode(scFileNode, "flols-pos", _flolsPosOffset); if (flolsNode) { @@ -112,7 +75,7 @@ void FGAICarrier::readFromScenario(SGPropertyNode* scFileNode) { _flolsApproachAngle = flolsNode->getDoubleValue("glidepath-angle-deg", 3.5); } else { - _flolsPosOffset(2) = -(deck_altitude_ft * SG_FEET_TO_METER + 10); + _flolsPosOffset(2) = -(_deck_altitude_ft * SG_FEET_TO_METER + 10); } //// the FLOLS (or IFLOLS) position doesn't produce an accurate angle; @@ -123,12 +86,12 @@ void FGAICarrier::readFromScenario(SGPropertyNode* scFileNode) { getPositionFromNode(scFileNode, "flols-touchdown-position", _flolsTouchdownPosition); if (!getPositionFromNode(scFileNode, "tower-position", _towerPosition)) { - _towerPosition(2) = -(deck_altitude_ft * SG_FEET_TO_METER + 10); + _towerPosition(2) = -(_deck_altitude_ft * SG_FEET_TO_METER + 10); SG_LOG(SG_AI, SG_INFO, "AICarrier: tower-position not defined - using default"); } if (!getPositionFromNode(scFileNode, "lso-position", _lsoPosition)){ - _lsoPosition(2) = -(deck_altitude_ft * SG_FEET_TO_METER + 10); + _lsoPosition(2) = -(_deck_altitude_ft * SG_FEET_TO_METER + 10); SG_LOG(SG_AI, SG_INFO, "AICarrier: lso-position not defined - using default"); } @@ -145,53 +108,53 @@ void FGAICarrier::readFromScenario(SGPropertyNode* scFileNode) { double offset_z = -(*it)->getDoubleValue("z-offset-m", 0); double hd = (*it)->getDoubleValue("heading-offset-deg", 0); ParkPosition pp(name, SGVec3d(offset_x, offset_y, offset_z), hd); - ppositions.push_back(pp); + _ppositions.push_back(pp); } } void FGAICarrier::setWind_from_east(double fps) { - wind_from_east = fps; + _wind_from_east = fps; } void FGAICarrier::setWind_from_north(double fps) { - wind_from_north = fps; + _wind_from_north = fps; } void FGAICarrier::setMaxLat(double deg) { - max_lat = fabs(deg); + _max_lat = fabs(deg); } void FGAICarrier::setMinLat(double deg) { - min_lat = fabs(deg); + _min_lat = fabs(deg); } void FGAICarrier::setMaxLong(double deg) { - max_long = fabs(deg); + _max_lon = fabs(deg); } void FGAICarrier::setMinLong(double deg) { - min_long = fabs(deg); + _min_lon = fabs(deg); } void FGAICarrier::setDeckAltitudeFt(const double altitude_feet) { - deck_altitude_ft = altitude_feet; + _deck_altitude_ft = altitude_feet; } void FGAICarrier::setSign(const string& s) { - sign = s; + _sign = s; } void FGAICarrier::setTACANChannelID(const string& id) { - TACAN_channel_id = id; + _TACAN_channel_id = id; } void FGAICarrier::setMPControl(bool c) { - MPControl = c; + _MPControl = c; } void FGAICarrier::setAIControl(bool c) { - AIControl = c; + _AIControl = c; } void FGAICarrier::update(double dt) { @@ -201,13 +164,13 @@ void FGAICarrier::update(double dt) { //automatic turn into wind with a target wind of 25 kts otd //SG_LOG(SG_AI, SG_ALERT, "AICarrier: MPControl " << MPControl << " AIControl " << AIControl); - if (!MPControl && AIControl){ + if (!_MPControl && _AIControl){ - if(turn_to_launch_hdg){ + if(_turn_to_launch_hdg){ TurnToLaunch(); - } else if(turn_to_recovery_hdg ){ + } else if(_turn_to_recovery_hdg ){ TurnToRecover(); - } else if(OutsideBox() || returning ) {// check that the carrier is inside + } else if(OutsideBox() || _returning ) {// check that the carrier is inside ReturnToBox(); // the operating box, } else { TurnToBase(); @@ -219,8 +182,8 @@ void FGAICarrier::update(double dt) { } UpdateWind(dt); - UpdateElevator(dt, transition_time); - UpdateJBD(dt, jbd_transition_time); + UpdateElevator(dt); + UpdateJBD(dt); // Transform that one to the horizontal local coordinate system. SGQuatd ec2hl = SGQuatd::fromLonLat(pos); @@ -245,7 +208,7 @@ void FGAICarrier::update(double dt) { SGVec3d flols_location = getCartPosAt(_flolsPosOffset); // the distance from the eyepoint to the flols - dist = norm(eyeWrtFlols); + _flols_dist = norm(eyeWrtFlols); // lineup (left/right) - stern lights and Carrier landing system (Aircraft/Generic/an_spn_46.nas) double lineup_hdg, lineup_az2, lineup_s; @@ -256,7 +219,7 @@ void FGAICarrier::update(double dt) { // // set the view as requested by control/view-index. SGGeod viewPosition; - switch (view_index) { + switch (_view_index) { default: case 0: viewPosition = SGGeod::fromCart(getCartPosAt(_towerPosition)); @@ -276,19 +239,19 @@ void FGAICarrier::update(double dt) { SGGeodesy::inverse(g_carrier, g_eyePos, lineup_hdg, lineup_az2, lineup_s); - double target_lineup = _getHeading() + angled_deck_degrees + 180.0; + double target_lineup = _getHeading() + _angled_deck_degrees + 180.0; SG_NORMALIZE_RANGE(target_lineup, 0.0, 360.0); - lineup = lineup_hdg - target_lineup; + _lineup = lineup_hdg - target_lineup; // now the angle, positive angles are upwards - if (fabs(dist) < SGLimits::min()) { - angle = 0; + if (fabs(_flols_dist) < SGLimits::min()) { + _flols_angle = 0; } else { - double sAngle = -eyeWrtFlols(2) / dist; + double sAngle = -eyeWrtFlols(2) / _flols_dist; sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle)); - angle = SGMiscd::rad2deg(asin(sAngle)); + _flols_angle = SGMiscd::rad2deg(asin(sAngle)); } - if (dist < 8000){ + if (_flols_dist < 8000){ SGVec3d eyeWrtFlols_tdp = eyeWrtCarrier - _flolsTouchdownPosition; // the distance from the eyepoint to the flols @@ -304,47 +267,46 @@ void FGAICarrier::update(double dt) { sAngle = SGMiscd::min(1, SGMiscd::max(-1, sAngle)); angle_tdp = SGMiscd::rad2deg(asin(sAngle)); } - -// printf("angle %5.2f td angle %5.2f \n", angle, angle_tdp); +// printf("angle %5.2f td angle %5.2f \n", _flols_angle, angle_tdp); //angle += 1.481; // adjust for FLOLS offset (measured on Nimitz class) } - // set the value of source - if ( angle <= 4.35 && angle > 4.01 ) - source = 1; - else if ( angle <= 4.01 && angle > 3.670 ) - source = 2; - else if ( angle <= 3.670 && angle > 3.330 ) - source = 3; - else if ( angle <= 3.330 && angle > 2.990 ) - source = 4; - else if ( angle <= 2.990 && angle > 2.650 ) - source = 5; - else if ( angle <= 2.650 ) - source = 6; + // set the value of _flols_visible_light + if ( _flols_angle <= 4.35 && _flols_angle > 4.01 ) + _flols_visible_light = 1; + else if ( _flols_angle <= 4.01 && _flols_angle > 3.670 ) + _flols_visible_light = 2; + else if ( _flols_angle <= 3.670 && _flols_angle > 3.330 ) + _flols_visible_light = 3; + else if ( _flols_angle <= 3.330 && _flols_angle > 2.990 ) + _flols_visible_light = 4; + else if ( _flols_angle <= 2.990 && _flols_angle > 2.650 ) + _flols_visible_light = 5; + else if ( _flols_angle <= 2.650 ) + _flols_visible_light = 6; else - source = 0; + _flols_visible_light = 0; // only bother with waveoff FLOLS when ownship within a reasonable range. // red ball is <= 3.075 to 2.65, below this is off. above this is orange. // only do this when within ~1.8nm - if (dist < 3200) { - if (dist > 100) { - bool new_wave_off_lights_demand = (angle <= 3.0); + if (_flols_dist < 3200) { + if (_flols_dist > 100) { + bool new_wave_off_lights_demand = (_flols_angle <= 3.0); - if (new_wave_off_lights_demand != wave_off_lights_demand) { + if (new_wave_off_lights_demand != _wave_off_lights_demand) { // start timing when the lights come up. - wave_off_lights_demand = new_wave_off_lights_demand; + _wave_off_lights_demand = new_wave_off_lights_demand; } //// below 1degrees close in is to low to continue; wave them off. - if (angle < 2 && dist < 800) { - wave_off_lights_demand = true; + if (_flols_angle < 2 && _flols_dist < 800) { + _wave_off_lights_demand = true; } } } else { - wave_off_lights_demand = true; // sensible default when very far away. + _wave_off_lights_demand = true; // sensible default when very far away. } } //end update @@ -358,45 +320,45 @@ bool FGAICarrier::init(ModelSearchOrder searchOrder) { _launchbar_state_node = fgGetNode("/gear/launchbar/state", true); - _surface_wind_from_deg_node = - fgGetNode("/environment/config/boundary/entry[0]/wind-from-heading-deg", true); - _surface_wind_speed_node = - fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt", true); + _surface_wind_from_deg_node = fgGetNode("/environment/config/boundary/entry[0]/wind-from-heading-deg", true); + _surface_wind_speed_node = fgGetNode("/environment/config/boundary/entry[0]/wind-speed-kt", true); int dmd_course = fgGetInt("/sim/presets/carrier-course"); if (dmd_course == 2) { // launch - turn_to_launch_hdg = true; - turn_to_recovery_hdg = false; - turn_to_base_course = false; + _turn_to_launch_hdg = true; + _turn_to_recovery_hdg = false; + _turn_to_base_course = false; } else if (dmd_course == 3) { // recovery - turn_to_launch_hdg = false; - turn_to_recovery_hdg = true; - turn_to_base_course = false; + _turn_to_launch_hdg = false; + _turn_to_recovery_hdg = true; + _turn_to_base_course = false; } else { // default to base - turn_to_launch_hdg = false; - turn_to_recovery_hdg = false; - turn_to_base_course = true; + _turn_to_launch_hdg = false; + _turn_to_recovery_hdg = false; + _turn_to_base_course = true; } - returning = false; - in_to_wind = false; + _returning = false; + _in_to_wind = false; - mOpBoxPos = pos; - base_course = hdg; - base_speed = speed; + _mOpBoxPos = pos; + _base_course = hdg; + _base_speed = speed; - pos_norm = raw_pos_norm = 0; - elevators = false; - transition_time = 150; - time_constant = 0.005; - jbd_pos_norm = raw_jbd_pos_norm = 0; - jbd = false ; - jbd_transition_time = 3; - jbd_time_constant = 0.1; + _elevator_pos_norm = 0; + _elevator_pos_norm_raw = 0; + _elevators = false; + _elevator_transition_time = 150; + _elevator_time_constant = 0.005; + _jbd_elevator_pos_norm = 0; + _jbd_elevator_pos_norm_raw = 0; + _jbd = false ; + _jbd_transition_time = 3; + _jbd_time_constant = 0.1; return true; } @@ -405,76 +367,76 @@ void FGAICarrier::bind(){ props->untie("velocities/true-airspeed-kt"); - props->getNode("position/deck-altitude-feet", true)->setDoubleValue(deck_altitude_ft); + props->getNode("position/deck-altitude-feet", true)->setDoubleValue(_deck_altitude_ft); tie("controls/flols/source-lights", - SGRawValuePointer(&source)); + SGRawValuePointer(&_flols_visible_light)); tie("controls/flols/distance-m", - SGRawValuePointer(&dist)); + SGRawValuePointer(&_flols_dist)); tie("controls/flols/angle-degs", - SGRawValuePointer(&angle)); + SGRawValuePointer(&_flols_angle)); tie("controls/flols/lineup-degs", - SGRawValuePointer(&lineup)); + SGRawValuePointer(&_lineup)); tie("controls/turn-to-launch-hdg", - SGRawValuePointer(&turn_to_launch_hdg)); + SGRawValuePointer(&_turn_to_launch_hdg)); tie("controls/in-to-wind", - SGRawValuePointer(&turn_to_launch_hdg)); + SGRawValuePointer(&_turn_to_launch_hdg)); tie("controls/base-course-deg", - SGRawValuePointer(&base_course)); + SGRawValuePointer(&_base_course)); tie("controls/base-speed-kts", - SGRawValuePointer(&base_speed)); + SGRawValuePointer(&_base_speed)); tie("controls/start-pos-lat-deg", SGRawValueMethods(pos, &SGGeod::getLatitudeDeg)); tie("controls/start-pos-long-deg", SGRawValueMethods(pos, &SGGeod::getLongitudeDeg)); tie("controls/mp-control", - SGRawValuePointer(&MPControl)); + SGRawValuePointer(&_MPControl)); tie("controls/ai-control", - SGRawValuePointer(&AIControl)); + SGRawValuePointer(&_AIControl)); tie("environment/surface-wind-speed-true-kts", - SGRawValuePointer(&wind_speed_kts)); + SGRawValuePointer(&_wind_speed_kts)); tie("environment/surface-wind-from-true-degs", - SGRawValuePointer(&wind_from_deg)); + SGRawValuePointer(&_wind_from_deg)); tie("environment/rel-wind-from-degs", - SGRawValuePointer(&rel_wind_from_deg)); + SGRawValuePointer(&_rel_wind_from_deg)); tie("environment/rel-wind-from-carrier-hdg-degs", - SGRawValuePointer(&rel_wind)); + SGRawValuePointer(&_rel_wind)); tie("environment/rel-wind-speed-kts", - SGRawValuePointer(&rel_wind_speed_kts)); + SGRawValuePointer(&_rel_wind_speed_kts)); tie("environment/in-to-wind", - SGRawValuePointer(&in_to_wind)); + SGRawValuePointer(&_in_to_wind)); tie("controls/flols/wave-off-lights-demand", - SGRawValuePointer(&wave_off_lights_demand)); + SGRawValuePointer(&_wave_off_lights_demand)); tie("controls/elevators", - SGRawValuePointer(&elevators)); + SGRawValuePointer(&_elevators)); tie("surface-positions/elevators-pos-norm", - SGRawValuePointer(&pos_norm)); + SGRawValuePointer(&_elevator_pos_norm)); tie("controls/constants/elevators/trans-time-s", - SGRawValuePointer(&transition_time)); + SGRawValuePointer(&_elevator_transition_time)); tie("controls/constants/elevators/time-constant", - SGRawValuePointer(&time_constant)); + SGRawValuePointer(&_elevator_time_constant)); tie("controls/jbd", - SGRawValuePointer(&jbd)); + SGRawValuePointer(&_jbd)); tie("surface-positions/jbd-pos-norm", - SGRawValuePointer(&jbd_pos_norm)); + SGRawValuePointer(&_jbd_elevator_pos_norm)); tie("controls/constants/jbd/trans-time-s", - SGRawValuePointer(&jbd_transition_time)); + SGRawValuePointer(&_jbd_transition_time)); tie("controls/constants/jbd/time-constant", - SGRawValuePointer(&jbd_time_constant)); + SGRawValuePointer(&_jbd_time_constant)); tie("controls/turn-to-recovery-hdg", - SGRawValuePointer(&turn_to_recovery_hdg)); + SGRawValuePointer(&_turn_to_recovery_hdg)); tie("controls/turn-to-base-course", - SGRawValuePointer(&turn_to_base_course)); + SGRawValuePointer(&_turn_to_base_course)); - tie("controls/view-index", SGRawValuePointer(&view_index)); + tie("controls/view-index", SGRawValuePointer(&_view_index)); props->setBoolValue("controls/flols/cut-lights", false); props->setBoolValue("controls/flols/wave-off-lights", false); props->setBoolValue("controls/flols/wave-off-lights-emergency", false); props->setBoolValue("controls/flols/cond-datum-lights", true); props->setBoolValue("controls/crew", false); - props->setStringValue("navaids/tacan/channel-ID", TACAN_channel_id.c_str()); - props->setStringValue("sign", sign.c_str()); + props->setStringValue("navaids/tacan/channel-ID", _TACAN_channel_id.c_str()); + props->setStringValue("sign", _sign.c_str()); props->setBoolValue("controls/lighting/deck-lights", false); props->setDoubleValue("controls/lighting/flood-lights-red-norm", 0); @@ -488,7 +450,7 @@ void FGAICarrier::bind(){ // Write out a list of the parking positions - useful for the UI to select // from - for (const auto& ppos : ppositions) { + for (const auto& ppos : _ppositions) { if (ppos.name != "") props->addChild("parking-pos")->setStringValue("name", ppos.name); } } @@ -498,7 +460,7 @@ bool FGAICarrier::getParkPosition(const string& id, SGGeod& geodPos, { // FIXME: does not yet cover rotation speeds. - for (const auto& ppos : ppositions) { + for (const auto& ppos : _ppositions) { // Take either the specified one or the first one ... if (ppos.name == id || id.empty()) { SGVec3d cartPos = getCartPosAt(ppos.offset); @@ -536,12 +498,12 @@ double FGAICarrier::getFLOLFSGlidepathAngleDeg() const void FGAICarrier::UpdateWind( double dt) { //get the surface wind speed and direction - wind_from_deg = _surface_wind_from_deg_node->getDoubleValue(); - wind_speed_kts = _surface_wind_speed_node->getDoubleValue(); + _wind_from_deg = _surface_wind_from_deg_node->getDoubleValue(); + _wind_speed_kts = _surface_wind_speed_node->getDoubleValue(); //calculate the surface wind speed north and east in kts - double wind_speed_from_north_kts = cos( wind_from_deg / SGD_RADIANS_TO_DEGREES )* wind_speed_kts ; - double wind_speed_from_east_kts = sin( wind_from_deg / SGD_RADIANS_TO_DEGREES )* wind_speed_kts ; + double wind_speed_from_north_kts = cos( _wind_from_deg / SGD_RADIANS_TO_DEGREES )* _wind_speed_kts ; + double wind_speed_from_east_kts = sin( _wind_from_deg / SGD_RADIANS_TO_DEGREES )* _wind_speed_kts ; //calculate the carrier speed north and east in kts double speed_north_kts = cos( hdg / SGD_RADIANS_TO_DEGREES )* speed ; @@ -552,15 +514,15 @@ void FGAICarrier::UpdateWind( double dt) { double rel_wind_speed_from_north_kts = wind_speed_from_north_kts + speed_north_kts; //combine relative speeds north and east to get relative windspeed in kts - rel_wind_speed_kts = sqrt((rel_wind_speed_from_east_kts * rel_wind_speed_from_east_kts) - + (rel_wind_speed_from_north_kts * rel_wind_speed_from_north_kts)); + _rel_wind_speed_kts = sqrt((rel_wind_speed_from_east_kts * rel_wind_speed_from_east_kts) + + (rel_wind_speed_from_north_kts * rel_wind_speed_from_north_kts)); //calculate the relative wind direction - rel_wind_from_deg = SGMiscd::rad2deg(atan2(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts)); + _rel_wind_from_deg = SGMiscd::rad2deg(atan2(rel_wind_speed_from_east_kts, rel_wind_speed_from_north_kts)); //calculate rel wind - rel_wind = rel_wind_from_deg - hdg; - SG_NORMALIZE_RANGE(rel_wind, -180.0, 180.0); + _rel_wind = _rel_wind_from_deg - hdg; + SG_NORMALIZE_RANGE(_rel_wind, -180.0, 180.0); //set in to wind property InToWind(); @@ -579,14 +541,14 @@ void FGAICarrier::UpdateWind( double dt) { void FGAICarrier::TurnToLaunch(){ // calculate tgt heading - if (wind_speed_kts < 3){ - tgt_heading = base_course; + if (_wind_speed_kts < 3){ + tgt_heading = _base_course; } else { - tgt_heading = wind_from_deg; + tgt_heading = _wind_from_deg; } //calculate tgt speed - double tgt_speed = 25 - wind_speed_kts; + double tgt_speed = 25 - _wind_speed_kts; if (tgt_speed < 10) tgt_speed = 10; @@ -601,21 +563,21 @@ 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; + 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; + double tgt_speed = 26 - _wind_speed_kts; if (tgt_speed < 10) tgt_speed = 10; @@ -627,8 +589,8 @@ void FGAICarrier::TurnToRecover(){ void FGAICarrier::TurnToBase(){ //turn the carrier - FGAIShip::TurnTo(base_course); - FGAIShip::AccelTo(base_speed); + FGAIShip::TurnTo(_base_course); + FGAIShip::AccelTo(_base_speed); } @@ -637,57 +599,57 @@ void FGAICarrier::ReturnToBox(){ double course, distance, az2; //calculate the bearing and range of the initial position from the carrier - geo_inverse_wgs_84(pos, mOpBoxPos, &course, &az2, &distance); + geo_inverse_wgs_84(pos, _mOpBoxPos, &course, &az2, &distance); distance *= SG_METER_TO_NM; //cout << "return course: " << course << " distance: " << distance << endl; //turn the carrier FGAIShip::TurnTo(course); - FGAIShip::AccelTo(base_speed); + FGAIShip::AccelTo(_base_speed); if (distance >= 1) - returning = true; + _returning = true; else - returning = false; + _returning = false; } // end turn to base 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) { + if ( _max_lat == 0 && _min_lat == 0 && _max_lon == 0 && _min_lon == 0) { SG_LOG(SG_AI, SG_DEBUG, "AICarrier: No Operating Box defined" ); return false; } - if (mOpBoxPos.getLatitudeDeg() >= 0) { //northern hemisphere - if (pos.getLatitudeDeg() >= mOpBoxPos.getLatitudeDeg() + max_lat) + if (_mOpBoxPos.getLatitudeDeg() >= 0) { //northern hemisphere + if (pos.getLatitudeDeg() >= _mOpBoxPos.getLatitudeDeg() + _max_lat) return true; - if (pos.getLatitudeDeg() <= mOpBoxPos.getLatitudeDeg() - min_lat) + if (pos.getLatitudeDeg() <= _mOpBoxPos.getLatitudeDeg() - _min_lat) return true; } else { //southern hemisphere - if (pos.getLatitudeDeg() <= mOpBoxPos.getLatitudeDeg() - max_lat) + if (pos.getLatitudeDeg() <= _mOpBoxPos.getLatitudeDeg() - _max_lat) return true; - if (pos.getLatitudeDeg() >= mOpBoxPos.getLatitudeDeg() + min_lat) + if (pos.getLatitudeDeg() >= _mOpBoxPos.getLatitudeDeg() + _min_lat) return true; } - if (mOpBoxPos.getLongitudeDeg() >=0) { //eastern hemisphere - if (pos.getLongitudeDeg() >= mOpBoxPos.getLongitudeDeg() + max_long) + if (_mOpBoxPos.getLongitudeDeg() >=0) { //eastern hemisphere + if (pos.getLongitudeDeg() >= _mOpBoxPos.getLongitudeDeg() + _max_lon) return true; - if (pos.getLongitudeDeg() <= mOpBoxPos.getLongitudeDeg() - min_long) + if (pos.getLongitudeDeg() <= _mOpBoxPos.getLongitudeDeg() - _min_lon) return true; } else { //western hemisphere - if (pos.getLongitudeDeg() <= mOpBoxPos.getLongitudeDeg() - max_long) + if (pos.getLongitudeDeg() <= _mOpBoxPos.getLongitudeDeg() - _max_lon) return true; - if (pos.getLongitudeDeg() >= mOpBoxPos.getLongitudeDeg() + min_long) + if (pos.getLongitudeDeg() >= _mOpBoxPos.getLongitudeDeg() + _min_lon) return true; } @@ -697,86 +659,86 @@ bool FGAICarrier::OutsideBox() { //returns true if the carrier is outside operat bool FGAICarrier::InToWind() { - in_to_wind = false; + _in_to_wind = false; - if ( fabs(rel_wind) < 10 ){ - in_to_wind = true; + if ( fabs(_rel_wind) < 10 ){ + _in_to_wind = true; return true; } return false; } -void FGAICarrier::UpdateElevator(double dt, double transition_time) { +void FGAICarrier::UpdateElevator(double dt) { double step = 0; - if ((elevators && pos_norm >= 1 ) || (!elevators && pos_norm <= 0 )) + if ((_elevators && _elevator_pos_norm >= 1 ) || (!_elevators && _elevator_pos_norm <= 0 )) return; // move the elevators - if ( elevators ) { - step = dt/transition_time; + if (_elevators ) { + step = dt / _elevator_transition_time; if ( step > 1 ) step = 1; } else { - step = -dt/transition_time; + step = -dt / _elevator_transition_time; if ( step < -1 ) step = -1; } // assume a linear relationship - raw_pos_norm += step; + _elevator_pos_norm_raw += step; //low pass filter - pos_norm = (raw_pos_norm * time_constant) + (pos_norm * (1 - time_constant)); + _elevator_pos_norm = (_elevator_pos_norm_raw * _elevator_time_constant) + (_elevator_pos_norm * (1 - _elevator_time_constant)); //sanitise the output - if (raw_pos_norm >= 1) { - raw_pos_norm = 1; - } else if (raw_pos_norm <= 0) { - raw_pos_norm = 0; + if (_elevator_pos_norm_raw >= 1) { + _elevator_pos_norm_raw = 1; + } else if (_elevator_pos_norm_raw <= 0) { + _elevator_pos_norm_raw = 0; } return; } // end UpdateElevator -void FGAICarrier::UpdateJBD(double dt, double jbd_transition_time) { +void FGAICarrier::UpdateJBD(double dt) { const string launchbar_state = _launchbar_state_node->getStringValue(); double step = 0; if (launchbar_state == "Engaged"){ - jbd = true; + _jbd = true; } else { - jbd = false; + _jbd = false; } - if (( jbd && jbd_pos_norm >= 1 ) || ( !jbd && jbd_pos_norm <= 0 )){ + if ((_jbd && _jbd_elevator_pos_norm >= 1 ) || ( !_jbd && _jbd_elevator_pos_norm <= 0 )){ return; } // move the jbds - if ( jbd ) { - step = dt/jbd_transition_time; + if ( _jbd ) { + step = dt / _jbd_transition_time; if ( step > 1 ) step = 1; } else { - step = -dt/jbd_transition_time; + step = -dt / _jbd_transition_time; if ( step < -1 ) step = -1; } // assume a linear relationship - raw_jbd_pos_norm += step; + _jbd_elevator_pos_norm_raw += step; //low pass filter - jbd_pos_norm = (raw_jbd_pos_norm * jbd_time_constant) + (jbd_pos_norm * (1 - jbd_time_constant)); + _jbd_elevator_pos_norm = (_jbd_elevator_pos_norm_raw * _jbd_time_constant) + (_jbd_elevator_pos_norm * (1 - _jbd_time_constant)); //sanitise the output - if (jbd_pos_norm >= 1) { - jbd_pos_norm = 1; - } else if (jbd_pos_norm <= 0) { - jbd_pos_norm = 0; + if (_jbd_elevator_pos_norm >= 1) { + _jbd_elevator_pos_norm = 1; + } else if (_jbd_elevator_pos_norm <= 0) { + _jbd_elevator_pos_norm = 0; } return; @@ -827,7 +789,7 @@ SGSharedPtr FGAICarrier::findCarrierByNameOrPennant(const std::stri for (const auto& aiObject : aiManager->get_ai_list()) { if (aiObject->isa(FGAIBase::otCarrier)) { SGSharedPtr c = static_cast(aiObject.get()); - if ((c->sign == namePennant) || (c->_getName() == namePennant)) { + if ((c->_sign == namePennant) || (c->_getName() == namePennant)) { return c; } } @@ -882,7 +844,7 @@ simgear::Emesary::ReceiptStatus FGAICarrier::Receive(simgear::Emesary::INotifica nctn->SetViewPositionLatNode(_view_position_lat_deg_node); nctn->SetViewPositionLonNode(_view_position_lon_deg_node); nctn->SetViewPositionAltNode(_view_position_alt_ft_node); - nctn->SetDeckheight(deck_altitude_ft); + nctn->SetDeckheight(_deck_altitude_ft); nctn->SetHeading(hdg); nctn->SetVckts(speed); nctn->SetCarrierIdent(this->_getName()); diff --git a/src/AIModel/AICarrier.hxx b/src/AIModel/AICarrier.hxx index 54cf1c074..c9c0023c4 100644 --- a/src/AIModel/AICarrier.hxx +++ b/src/AIModel/AICarrier.hxx @@ -21,19 +21,19 @@ #ifndef _FG_AICARRIER_HXX #define _FG_AICARRIER_HXX -#include #include +#include #include #include -using std::string; using std::list; +using std::string; #include "AIShip.hxx" -#include "AIManager.hxx" #include "AIBase.hxx" +#include "AIManager.hxx" class FGAIManager; class FGAICarrier; @@ -41,27 +41,26 @@ class FGAICarrier; class FGAICarrier : public FGAIShip, simgear::Emesary::IReceiver { public: - FGAICarrier(); virtual ~FGAICarrier(); void readFromScenario(SGPropertyNode* scFileNode) override; - void setSign(const string& ); + void setSign(const string&); void setDeckAltitudeFt(const double altitude_feet); - void setTACANChannelID(const string &); + void setTACANChannelID(const string&); double getDefaultModelRadius() override { return 350.0; } void bind() override; - void UpdateWind ( double dt ); - void setWind_from_east( double fps ); - void setWind_from_north( double fps ); - void setMaxLat( double deg ); - void setMinLat( double deg ); - void setMaxLong( double deg ); - void setMinLong( double deg ); - void setMPControl( bool c ); - void setAIControl( bool c ); + void UpdateWind(double dt); + void setWind_from_east(double fps); + void setWind_from_north(double fps); + void setMaxLat(double deg); + void setMinLat(double deg); + void setMaxLong(double deg); + void setMinLong(double deg); + void setMPControl(bool c); + void setAIControl(bool c); void TurnToLaunch(); void TurnToRecover(); void TurnToBase(); @@ -72,16 +71,15 @@ public: const char* getTypeString(void) const override { return "carrier"; } - bool getParkPosition(const string& id, SGGeod& geodPos, - double& hdng, SGVec3d& uvw); - + bool getParkPosition(const string& id, SGGeod& geodPos, double& hdng, SGVec3d& uvw); + /** * @brief type-safe wrapper around AIManager::getObjectFromProperty */ - static SGSharedPtr findCarrierByNameOrPennant(const std::string& namePennant); - + static SGSharedPtr findCarrierByNameOrPennant(const std::string& namePennant); + static std::pair initialPositionForCarrier(const std::string& namePennant); - + /** * for a given scenario node, check for carriers within, and write nodes with * names, pennants and initial position into the second argument. @@ -90,99 +88,96 @@ public: */ static void extractCarriersFromScenario(SGPropertyNode_ptr xmlNode, SGPropertyNode_ptr scenario); - bool getFLOLSPositionHeading(SGGeod &pos, double &heading) const; + bool getFLOLSPositionHeading(SGGeod& pos, double& heading) const; double getFLOLFSGlidepathAngleDeg() const; - double getDeckAltitudeFt() const { return deck_altitude_ft; } + double getDeckAltitudeFt() const { return _deck_altitude_ft; } virtual simgear::Emesary::ReceiptStatus Receive(simgear::Emesary::INotificationPtr n) override; private: - /// Is sufficient to be private, stores a possible position to place an - /// aircraft on start - struct ParkPosition { - ParkPosition(const ParkPosition& pp) - : name(pp.name), offset(pp.offset), heading_deg(pp.heading_deg) - {} - ParkPosition(const string& n, const SGVec3d& off = SGVec3d(), double heading = 0) - : name(n), offset(off), heading_deg(heading) - {} - string name; - SGVec3d offset; - double heading_deg; - }; + /// Is sufficient to be private, stores a possible position to place an + /// aircraft on start + struct ParkPosition { + ParkPosition(const ParkPosition& pp) + : name(pp.name), offset(pp.offset), heading_deg(pp.heading_deg) + { + } + ParkPosition(const string& n, const SGVec3d& off = SGVec3d(), double heading = 0) + : name(n), offset(off), heading_deg(heading) + { + } + string name; + SGVec3d offset; + double heading_deg; + }; void update(double dt) override; - double wind_from_east; // fps - double wind_from_north; // fps - double rel_wind_speed_kts; - double rel_wind_from_deg; + bool InToWind(); // set if the carrier is in to wind + void UpdateElevator(double dt); + void UpdateJBD(double dt); - list ppositions; // List of positions where an aircraft can start. - string sign; // The sign of this carrier. + bool _AIControl = false; // under AI control. Either this or MPControl will be true + SGPropertyNode_ptr _altitude_node; + double _angled_deck_degrees = -8.55; // angled deck offset from carrier heading. usually negative + double _base_course = 0; + double _base_speed = 0; + double _deck_altitude_ft = 65.0065; + double _elevator_pos_norm = 0; + double _elevator_pos_norm_raw = 0; + double _elevator_time_constant = 0; + double _elevator_transition_time = 0; + bool _elevators = false; + double _flols_angle = 0; + double _flols_dist = 0; // the distance of the eyepoint from the flols + int _flols_visible_light = 0; // the flols light which is visible at the moment + SGPropertyNode_ptr _flols_x_node; + SGPropertyNode_ptr _flols_y_node; + SGPropertyNode_ptr _flols_z_node; + double _flolsApproachAngle = 3.0; ///< glidepath angle for the FLOLS + double _flolsHeadingOffsetDeg = 0.0; /// angle in degrees offset from the carrier centerline + SGVec3d _flolsPosOffset; + SGVec3d _flolsTouchdownPosition; + bool _in_to_wind = false; + bool _jbd = false; + double _jbd_elevator_pos_norm = 0; + double _jbd_elevator_pos_norm_raw = 0; + double _jbd_time_constant = 0; + double _jbd_transition_time = 0; + SGPropertyNode_ptr _latitude_node; + SGPropertyNode_ptr _launchbar_state_node; + double _lineup = 0; // lineup angle deviation from carrier; + SGPropertyNode_ptr _longitude_node; + SGVec3d _lsoPosition; /// LSO position + double _max_lat = 0; + double _max_lon = 0; + double _min_lat = 0; + double _min_lon = 0; + SGGeod _mOpBoxPos; // operational box limit for carrier. + bool _MPControl = false; // being controlled by MP. Either this or AIControl will be true + list _ppositions; // List of positions where an aircraft can start. + double _rel_wind = 0; + double _rel_wind_from_deg = 0; + double _rel_wind_speed_kts = 0; + bool _returning = false; // set if the carrier is returning to an operating box + string _sign; // The sign (pennant) of this carrier; e.g. CVN-68 + SGPropertyNode_ptr _surface_wind_from_deg_node; + SGPropertyNode_ptr _surface_wind_speed_node; + string _TACAN_channel_id; + SGVec3d _towerPosition; + bool _turn_to_base_course = true; + bool _turn_to_launch_hdg = true; + bool _turn_to_recovery_hdg = true; + int _view_index = 0; + SGPropertyNode_ptr _view_position_alt_ft_node; + SGPropertyNode_ptr _view_position_lat_deg_node; + SGPropertyNode_ptr _view_position_lon_deg_node; + bool _wave_off_lights_demand = false; // when waveoff requested. + double _wind_from_deg = 0; //true wind direction + double _wind_from_east = 0; // fps + double _wind_from_north = 0; // fps + double _wind_speed_kts = 0; //true wind speed - // these describe the flols - SGVec3d _flolsPosOffset, _flolsTouchdownPosition, _towerPosition, _lsoPosition; - double _flolsHeadingOffsetDeg = 0.0; ///< angle in degrees offset from the carrier centerline - double _flolsApproachAngle = 3.0; ///< glidepath angle for the FLOLS - double dist; // the distance of the eyepoint from the flols - double angle; - double deck_altitude_ft; - double lineup; // lineup angle deviation from carrier; - int source; // the flols light which is visible at the moment - bool in_to_wind; - - // when waveoff should be requested. - bool wave_off_lights_demand; - - // these are for maneuvering the carrier - SGGeod mOpBoxPos; - - double wind_speed_kts; //true wind speed - double wind_from_deg; //true wind direction - double rel_wind; - double max_lat, min_lat, max_long, min_long; - double base_course, base_speed; - double angled_deck_degrees; // angled deck offset from carrier heading. usually negative - - bool turn_to_launch_hdg; - bool turn_to_recovery_hdg; - bool turn_to_base_course; - bool returning; // set if the carrier is returning to an operating box - bool InToWind(); // set if the carrier is in to wind - bool MPControl, AIControl; - - int view_index; - - SGPropertyNode_ptr _longitude_node; - SGPropertyNode_ptr _latitude_node; - SGPropertyNode_ptr _altitude_node; - SGPropertyNode_ptr _surface_wind_from_deg_node; - SGPropertyNode_ptr _surface_wind_speed_node; - SGPropertyNode_ptr _launchbar_state_node; - - SGPropertyNode_ptr _flols_x_node; - SGPropertyNode_ptr _flols_y_node; - SGPropertyNode_ptr _flols_z_node; - // this is for tacan - - string TACAN_channel_id; - - SGPropertyNode_ptr _view_position_lat_deg_node; - SGPropertyNode_ptr _view_position_lon_deg_node; - SGPropertyNode_ptr _view_position_alt_ft_node; - - // these are for moving the elevators - void UpdateElevator( double dt, double transition_time); - double pos_norm, raw_pos_norm; - double transition_time, time_constant; - bool elevators; - - // these are for moving the jet blast deflectors - void UpdateJBD( double dt, double jbd_transition_time); - double jbd_pos_norm, raw_jbd_pos_norm; - double jbd_transition_time, jbd_time_constant; - bool jbd; }; #endif // _FG_AICARRIER_HXX