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.
9 #include <simgear/compiler.h>
15 #include <math.h> // fabs()
17 #include "attitude_indicator.hxx"
18 #include <Main/fg_props.hxx>
19 #include <Main/util.hxx>
23 AttitudeIndicator::AttitudeIndicator ( SGPropertyNode *node )
25 _name(node->getStringValue("name", "attitude-indicator")),
26 _num(node->getIntValue("number", 0)),
27 _suction(node->getStringValue("suction", "/systems/vacuum/suction-inhg")),
34 AttitudeIndicator::~AttitudeIndicator ()
39 AttitudeIndicator::init ()
42 branch = "/instrumentation/" + _name;
44 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
47 _pitch_in_node = fgGetNode("/orientation/pitch-deg", true);
48 _roll_in_node = fgGetNode("/orientation/roll-deg", true);
49 _suction_node = fgGetNode(_suction.c_str(), true);
50 SGPropertyNode *cnode = node->getChild("config", 0, true);
51 _tumble_flag_node = cnode->getChild("tumble-flag", 0, true);
52 _caged_node = node->getChild("caged-flag", 0, true);
53 _tumble_node = node->getChild("tumble-norm", 0, true);
54 if( ( n = cnode->getChild("spin-thresh", 0, false ) ) != NULL )
55 spin_thresh = n->getDoubleValue();
56 if( ( n = cnode->getChild("max-roll-error-deg", 0, false ) ) != NULL )
57 max_roll_error = n->getDoubleValue();
58 if( ( n = cnode->getChild("max-pitch-error-deg", 0, false ) ) != NULL )
59 max_pitch_error = n->getDoubleValue();
60 _pitch_int_node = node->getChild("internal-pitch-deg", 0, true);
61 _roll_int_node = node->getChild("internal-roll-deg", 0, true);
62 _pitch_out_node = node->getChild("indicated-pitch-deg", 0, true);
63 _roll_out_node = node->getChild("indicated-roll-deg", 0, true);
67 AttitudeIndicator::bind ()
69 std::ostringstream temp;
72 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
74 fgTie((branch + "/serviceable").c_str(),
75 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
76 fgTie((branch + "/spin").c_str(),
77 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
81 AttitudeIndicator::unbind ()
83 std::ostringstream temp;
86 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
88 fgUntie((branch + "/serviceable").c_str());
89 fgUntie((branch + "/spin").c_str());
93 AttitudeIndicator::update (double dt)
95 // If it's caged, it doesn't indicate
96 if (_caged_node->getBoolValue()) {
97 _roll_int_node->setDoubleValue(0.0);
98 _pitch_int_node->setDoubleValue(0.0);
102 // Get the spin from the gyro
103 _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
105 double spin = _gyro.get_spin_norm();
107 // Calculate the responsiveness
108 double responsiveness = spin * spin * spin * spin * spin * spin;
110 // Get the indicated roll and pitch
111 double roll = _roll_in_node->getDoubleValue();
112 double pitch = _pitch_in_node->getDoubleValue();
114 // Calculate the tumble for the
116 if (_tumble_flag_node->getBoolValue()) {
117 double tumble = _tumble_node->getDoubleValue();
118 if (fabs(roll) > 45.0) {
119 double target = (fabs(roll) - 45.0) / 45.0;
120 target *= target; // exponential past +-45 degrees
124 if (fabs(target) > fabs(tumble))
129 else if (tumble < -1.0)
132 // Reerect in 5 minutes
133 double step = dt/300.0;
136 else if (tumble > step)
140 _tumble_node->setDoubleValue(tumble);
143 roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll,
145 pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch,
148 // Assign the new values
149 _roll_int_node->setDoubleValue(roll);
150 _pitch_int_node->setDoubleValue(pitch);
152 // add in a gyro underspin "error" if gyro is spinning too slowly
155 if ( spin <= spin_thresh ) {
156 double roll_error_factor = (spin_thresh - spin) / spin_thresh;
157 double pitch_error_factor = (spin_thresh - spin) / spin_thresh;
158 roll_error = roll_error_factor * roll_error_factor * max_roll_error;
159 pitch_error = pitch_error_factor * pitch_error_factor * max_pitch_error;
165 _roll_out_node->setDoubleValue(roll + roll_error);
166 _pitch_out_node->setDoubleValue(pitch + pitch_error);
169 // end of attitude_indicator.cxx