| 1 | /* |
| 2 | * SPDX-FileName: attitude_indicator.cxx |
| 3 | * SPDX-FileComment: a vacuum-powered attitude indicator. |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | * SPDX-FileContributor: Written by David Megginson, started 2002. |
| 6 | * SPDX-FileContributor: Enhanced by Benedikt Hallinger, 2023 |
| 7 | */ |
| 8 | |
| 9 | #include "config.h" |
| 10 | |
| 11 | #include "attitude_indicator.hxx" |
| 12 | |
| 13 | #include <simgear/compiler.h> |
| 14 | |
| 15 | #include <iostream> |
| 16 | #include <string> |
| 17 | #include <sstream> |
| 18 | |
| 19 | #include <cmath> // fabs() |
| 20 | |
| 21 | #include <Main/fg_props.hxx> |
| 22 | #include <Main/util.hxx> |
| 23 | |
| 24 | using std::string; |
| 25 | |
| 26 | AttitudeIndicator::AttitudeIndicator ( SGPropertyNode *node ) |
| 27 | : |
| 28 | _name(node->getStringValue("name", "attitude-indicator")), |
| 29 | _num(node->getIntValue("number", 0)), |
| 30 | _suction(node->getStringValue("suction", "/systems/vacuum/suction-inhg")), |
| 31 | spin_thresh(0.8), |
| 32 | max_roll_error(40.0), |
| 33 | max_pitch_error(12.0) |
| 34 | { |
| 35 | _minVacuum = node->getDoubleValue("minimum-vacuum", 4.5); |
| 36 | |
| 37 | SGPropertyNode* limits_cfg = node->getChild("limits", 0, true); |
| 38 | spin_thresh = limits_cfg->getDoubleValue("spin-thresh", 0.8); |
| 39 | max_roll_error = limits_cfg->getDoubleValue("max-roll-error-deg", 40.0); |
| 40 | max_pitch_error = limits_cfg->getDoubleValue("max-pitch-error-deg", 12.0); |
| 41 | |
| 42 | SGPropertyNode* gyro_cfg = node->getChild("gyro", 0, true); |
| 43 | _gyro_spin_up = gyro_cfg->getDoubleValue("spin-up-sec", 4.0); |
| 44 | _gyro_spin_down = gyro_cfg->getDoubleValue("spin-down-sec", 180.0); |
| 45 | } |
| 46 | |
| 47 | AttitudeIndicator::~AttitudeIndicator () |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | AttitudeIndicator::init () |
| 53 | { |
| 54 | string branch; |
| 55 | branch = "/instrumentation/" + _name; |
| 56 | |
| 57 | SGPropertyNode *node = fgGetNode(branch, _num, true ); |
| 58 | SGPropertyNode *n; |
| 59 | |
| 60 | _pitch_in_node = fgGetNode("/orientation/pitch-deg", true); |
| 61 | _roll_in_node = fgGetNode("/orientation/roll-deg", true); |
| 62 | _suction_node = fgGetNode(_suction, true); |
| 63 | SGPropertyNode *cnode = node->getChild("config", 0, true); |
| 64 | _tumble_flag_node = cnode->getChild("tumble-flag", 0, true); |
| 65 | _caged_node = node->getChild("caged-flag", 0, true); |
| 66 | _tumble_node = node->getChild("tumble-norm", 0, true); |
| 67 | if( ( n = cnode->getChild("spin-thresh", 0, false ) ) != NULL ) |
| 68 | spin_thresh = n->getDoubleValue(); |
| 69 | if( ( n = cnode->getChild("max-roll-error-deg", 0, false ) ) != NULL ) |
| 70 | max_roll_error = n->getDoubleValue(); |
| 71 | if( ( n = cnode->getChild("max-pitch-error-deg", 0, false ) ) != NULL ) |
| 72 | max_pitch_error = n->getDoubleValue(); |
| 73 | _pitch_int_node = node->getChild("internal-pitch-deg", 0, true); |
| 74 | _roll_int_node = node->getChild("internal-roll-deg", 0, true); |
| 75 | _pitch_out_node = node->getChild("indicated-pitch-deg", 0, true); |
| 76 | _roll_out_node = node->getChild("indicated-roll-deg", 0, true); |
| 77 | |
| 78 | _spin_node = node->getChild("spin", 0, true); |
| 79 | |
| 80 | SGPropertyNode* gyro_node = node->getChild("gyro", 0, true); |
| 81 | _gyro_spin_up_node = gyro_node->getChild("spin-up-sec", 0, true); |
| 82 | _gyro_spin_down_node = gyro_node->getChild("spin-down-sec", 0, true); |
| 83 | if (!_gyro_spin_up_node->hasValue()) |
| 84 | _gyro_spin_up_node->setDoubleValue(_gyro_spin_up); |
| 85 | if (!_gyro_spin_down_node->hasValue()) |
| 86 | _gyro_spin_down_node->setDoubleValue(_gyro_spin_down); |
| 87 | _minVacuum_node = node->getChild("minimum-vacuum", 0, true); |
| 88 | if (!_minVacuum_node->hasValue()) |
| 89 | _minVacuum_node->setDoubleValue(_minVacuum); |
| 90 | |
| 91 | reinit(); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | AttitudeIndicator::reinit () |
| 96 | { |
| 97 | _roll_int_node->setDoubleValue(0.0); |
| 98 | _pitch_int_node->setDoubleValue(0.0); |
| 99 | _gyro.reinit(); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | AttitudeIndicator::bind () |
| 104 | { |
| 105 | std::ostringstream temp; |
| 106 | string branch; |
| 107 | temp << _num; |
| 108 | branch = "/instrumentation/" + _name + "[" + temp.str() + "]"; |
| 109 | |
| 110 | fgTie((branch + "/serviceable").c_str(), |
| 111 | &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | AttitudeIndicator::unbind () |
| 116 | { |
| 117 | std::ostringstream temp; |
| 118 | string branch; |
| 119 | temp << _num; |
| 120 | branch = "/instrumentation/" + _name + "[" + temp.str() + "]"; |
| 121 | |
| 122 | fgUntie((branch + "/serviceable").c_str()); |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | AttitudeIndicator::update (double dt) |
| 127 | { |
| 128 | // Get the spin from the gyro |
| 129 | _minVacuum = _minVacuum_node->getDoubleValue(); |
| 130 | _gyro.set_power_norm(_suction_node->getDoubleValue() / _minVacuum); |
| 131 | _gyro.set_spin_up(_gyro_spin_up_node->getDoubleValue()); |
| 132 | _gyro.set_spin_down(_gyro_spin_down_node->getDoubleValue()); |
| 133 | _gyro.set_spin_norm(_spin_node->getDoubleValue()); |
| 134 | _gyro.update(dt); |
| 135 | double spin = _gyro.get_spin_norm(); |
| 136 | _spin_node->setDoubleValue(spin); |
| 137 | |
| 138 | // Calculate the responsiveness |
| 139 | double responsiveness = spin * spin * spin * spin * spin * spin; |
| 140 | |
| 141 | // Get the indicated roll and pitch |
| 142 | double roll = _roll_in_node->getDoubleValue(); |
| 143 | double pitch = _pitch_in_node->getDoubleValue(); |
| 144 | bool isCaged = _caged_node->getBoolValue(); |
| 145 | |
| 146 | // Calculate the tumble for the |
| 147 | // next pass. |
| 148 | if (_tumble_flag_node->getBoolValue()) { |
| 149 | double tumble = _tumble_node->getDoubleValue(); |
| 150 | if (fabs(roll) > 45.0) { |
| 151 | double target = (fabs(roll) - 45.0) / 45.0; |
| 152 | target *= target; // exponential past +-45 degrees |
| 153 | if (roll < 0) |
| 154 | target = -target; |
| 155 | |
| 156 | if (fabs(target) > fabs(tumble)) |
| 157 | tumble = target; |
| 158 | |
| 159 | if (tumble > 1.0) |
| 160 | tumble = 1.0; |
| 161 | else if (tumble < -1.0) |
| 162 | tumble = -1.0; |
| 163 | } |
| 164 | // Reerect in 5 minutes |
| 165 | double t_reerect = isCaged ? 1.0 : 300.0; |
| 166 | double step = dt / t_reerect; |
| 167 | if (tumble < -step) |
| 168 | tumble += step; |
| 169 | else if (tumble > step) |
| 170 | tumble -= step; |
| 171 | |
| 172 | roll += tumble * 45; |
| 173 | _tumble_node->setDoubleValue(tumble); |
| 174 | } |
| 175 | |
| 176 | // If it's caged, it doesn't indicate |
| 177 | if (isCaged) { |
| 178 | _roll_int_node->setDoubleValue(0.0); |
| 179 | _pitch_int_node->setDoubleValue(0.0); |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll, |
| 184 | responsiveness); |
| 185 | pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch, |
| 186 | responsiveness); |
| 187 | |
| 188 | // Assign the new values |
| 189 | _roll_int_node->setDoubleValue(roll); |
| 190 | _pitch_int_node->setDoubleValue(pitch); |
| 191 | |
| 192 | // add in a gyro underspin "error" if gyro is spinning too slowly |
| 193 | double roll_error; |
| 194 | double pitch_error; |
| 195 | if ( spin <= spin_thresh ) { |
| 196 | double roll_error_factor = (spin_thresh - spin) / spin_thresh; |
| 197 | double pitch_error_factor = (spin_thresh - spin) / spin_thresh; |
| 198 | roll_error = roll_error_factor * roll_error_factor * max_roll_error; |
| 199 | pitch_error = pitch_error_factor * pitch_error_factor * max_pitch_error; |
| 200 | } else { |
| 201 | roll_error = 0.0; |
| 202 | pitch_error = 0.0; |
| 203 | } |
| 204 | |
| 205 | _roll_out_node->setDoubleValue(roll + roll_error); |
| 206 | _pitch_out_node->setDoubleValue(pitch + pitch_error); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | // Register the subsystem. |
| 211 | #if 0 |
| 212 | SGSubsystemMgr::InstancedRegistrant<AttitudeIndicator> registrantAttitudeIndicator( |
| 213 | SGSubsystemMgr::FDM, |
| 214 | {{"instrumentation", SGSubsystemMgr::Dependency::HARD}}); |
| 215 | #endif |
| 216 | |
| 217 | // end of attitude_indicator.cxx |