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>
22 AttitudeIndicator::AttitudeIndicator ( SGPropertyNode *node )
24 _name(node->getStringValue("name", "attitude-indicator")),
25 _num(node->getIntValue("number", 0)),
26 _suction(node->getStringValue("suction", "/systems/vacuum/suction-inhg")),
33 AttitudeIndicator::~AttitudeIndicator ()
38 AttitudeIndicator::init ()
41 branch = "/instrumentation/" + _name;
43 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
46 _pitch_in_node = fgGetNode("/orientation/pitch-deg", true);
47 _roll_in_node = fgGetNode("/orientation/roll-deg", true);
48 _suction_node = fgGetNode(_suction.c_str(), true);
49 SGPropertyNode *cnode = node->getChild("config", 0, true);
50 _tumble_flag_node = cnode->getChild("tumble-flag", 0, true);
51 _caged_node = node->getChild("caged-flag", 0, true);
52 _tumble_node = node->getChild("tumble-norm", 0, true);
53 if( ( n = cnode->getChild("spin-thresh", 0, false ) ) != NULL )
54 spin_thresh = n->getDoubleValue();
55 if( ( n = cnode->getChild("max-roll-error-deg", 0, false ) ) != NULL )
56 max_roll_error = n->getDoubleValue();
57 if( ( n = cnode->getChild("max-pitch-error-deg", 0, false ) ) != NULL )
58 max_pitch_error = n->getDoubleValue();
59 _pitch_int_node = node->getChild("internal-pitch-deg", 0, true);
60 _roll_int_node = node->getChild("internal-roll-deg", 0, true);
61 _pitch_out_node = node->getChild("indicated-pitch-deg", 0, true);
62 _roll_out_node = node->getChild("indicated-roll-deg", 0, true);
66 AttitudeIndicator::bind ()
68 std::ostringstream temp;
71 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
73 fgTie((branch + "/serviceable").c_str(),
74 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
75 fgTie((branch + "/spin").c_str(),
76 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
80 AttitudeIndicator::unbind ()
82 std::ostringstream temp;
85 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
87 fgUntie((branch + "/serviceable").c_str());
88 fgUntie((branch + "/spin").c_str());
92 AttitudeIndicator::update (double dt)
94 // If it's caged, it doesn't indicate
95 if (_caged_node->getBoolValue()) {
96 _roll_int_node->setDoubleValue(0.0);
97 _pitch_int_node->setDoubleValue(0.0);
101 // Get the spin from the gyro
102 _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
104 double spin = _gyro.get_spin_norm();
106 // Calculate the responsiveness
107 double responsiveness = spin * spin * spin * spin * spin * spin;
109 // Get the indicated roll and pitch
110 double roll = _roll_in_node->getDoubleValue();
111 double pitch = _pitch_in_node->getDoubleValue();
113 // Calculate the tumble for the
115 if (_tumble_flag_node->getBoolValue()) {
116 double tumble = _tumble_node->getDoubleValue();
117 if (fabs(roll) > 45.0) {
118 double target = (fabs(roll) - 45.0) / 45.0;
119 target *= target; // exponential past +-45 degrees
123 if (fabs(target) > fabs(tumble))
128 else if (tumble < -1.0)
131 // Reerect in 5 minutes
132 double step = dt/300.0;
135 else if (tumble > step)
139 _tumble_node->setDoubleValue(tumble);
142 roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll,
144 pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch,
147 // Assign the new values
148 _roll_int_node->setDoubleValue(roll);
149 _pitch_int_node->setDoubleValue(pitch);
151 // add in a gyro underspin "error" if gyro is spinning too slowly
154 if ( spin <= spin_thresh ) {
155 double roll_error_factor = (spin_thresh - spin) / spin_thresh;
156 double pitch_error_factor = (spin_thresh - spin) / spin_thresh;
157 roll_error = roll_error_factor * roll_error_factor * max_roll_error;
158 pitch_error = pitch_error_factor * pitch_error_factor * max_pitch_error;
164 _roll_out_node->setDoubleValue(roll + roll_error);
165 _pitch_out_node->setDoubleValue(pitch + pitch_error);
168 // end of attitude_indicator.cxx