1 // attitude_indicator.cxx - a vacuum-powered attitude indicator.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain and comes with no warranty.
13 #include <simgear/compiler.h>
19 #include <math.h> // fabs()
21 #include "attitude_indicator.hxx"
22 #include <Main/fg_props.hxx>
23 #include <Main/util.hxx>
27 AttitudeIndicator::AttitudeIndicator ( SGPropertyNode *node )
29 _name(node->getStringValue("name", "attitude-indicator")),
30 _num(node->getIntValue("number", 0)),
31 _suction(node->getStringValue("suction", "/systems/vacuum/suction-inhg")),
38 AttitudeIndicator::~AttitudeIndicator ()
43 AttitudeIndicator::init ()
46 branch = "/instrumentation/" + _name;
48 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
51 _pitch_in_node = fgGetNode("/orientation/pitch-deg", true);
52 _roll_in_node = fgGetNode("/orientation/roll-deg", true);
53 _suction_node = fgGetNode(_suction.c_str(), true);
54 SGPropertyNode *cnode = node->getChild("config", 0, true);
55 _tumble_flag_node = cnode->getChild("tumble-flag", 0, true);
56 _caged_node = node->getChild("caged-flag", 0, true);
57 _tumble_node = node->getChild("tumble-norm", 0, true);
58 if( ( n = cnode->getChild("spin-thresh", 0, false ) ) != NULL )
59 spin_thresh = n->getDoubleValue();
60 if( ( n = cnode->getChild("max-roll-error-deg", 0, false ) ) != NULL )
61 max_roll_error = n->getDoubleValue();
62 if( ( n = cnode->getChild("max-pitch-error-deg", 0, false ) ) != NULL )
63 max_pitch_error = n->getDoubleValue();
64 _pitch_int_node = node->getChild("internal-pitch-deg", 0, true);
65 _roll_int_node = node->getChild("internal-roll-deg", 0, true);
66 _pitch_out_node = node->getChild("indicated-pitch-deg", 0, true);
67 _roll_out_node = node->getChild("indicated-roll-deg", 0, true);
71 AttitudeIndicator::bind ()
73 std::ostringstream temp;
76 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
78 fgTie((branch + "/serviceable").c_str(),
79 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
80 fgTie((branch + "/spin").c_str(),
81 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
85 AttitudeIndicator::unbind ()
87 std::ostringstream temp;
90 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
92 fgUntie((branch + "/serviceable").c_str());
93 fgUntie((branch + "/spin").c_str());
97 AttitudeIndicator::update (double dt)
99 // If it's caged, it doesn't indicate
100 if (_caged_node->getBoolValue()) {
101 _roll_int_node->setDoubleValue(0.0);
102 _pitch_int_node->setDoubleValue(0.0);
106 // Get the spin from the gyro
107 _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
109 double spin = _gyro.get_spin_norm();
111 // Calculate the responsiveness
112 double responsiveness = spin * spin * spin * spin * spin * spin;
114 // Get the indicated roll and pitch
115 double roll = _roll_in_node->getDoubleValue();
116 double pitch = _pitch_in_node->getDoubleValue();
118 // Calculate the tumble for the
120 if (_tumble_flag_node->getBoolValue()) {
121 double tumble = _tumble_node->getDoubleValue();
122 if (fabs(roll) > 45.0) {
123 double target = (fabs(roll) - 45.0) / 45.0;
124 target *= target; // exponential past +-45 degrees
128 if (fabs(target) > fabs(tumble))
133 else if (tumble < -1.0)
136 // Reerect in 5 minutes
137 double step = dt/300.0;
140 else if (tumble > step)
144 _tumble_node->setDoubleValue(tumble);
147 roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll,
149 pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch,
152 // Assign the new values
153 _roll_int_node->setDoubleValue(roll);
154 _pitch_int_node->setDoubleValue(pitch);
156 // add in a gyro underspin "error" if gyro is spinning too slowly
159 if ( spin <= spin_thresh ) {
160 double roll_error_factor = (spin_thresh - spin) / spin_thresh;
161 double pitch_error_factor = (spin_thresh - spin) / spin_thresh;
162 roll_error = roll_error_factor * roll_error_factor * max_roll_error;
163 pitch_error = pitch_error_factor * pitch_error_factor * max_pitch_error;
169 _roll_out_node->setDoubleValue(roll + roll_error);
170 _pitch_out_node->setDoubleValue(pitch + pitch_error);
173 // end of attitude_indicator.cxx