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"))
30 AttitudeIndicator::~AttitudeIndicator ()
35 AttitudeIndicator::init ()
38 branch = "/instrumentation/" + _name;
40 SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
42 _pitch_in_node = fgGetNode("/orientation/pitch-deg", true);
43 _roll_in_node = fgGetNode("/orientation/roll-deg", true);
44 _suction_node = fgGetNode(_suction.c_str(), true);
45 SGPropertyNode *cnode = node->getChild("config", 0, true);
46 _tumble_flag_node = cnode->getChild("tumble-flag", 0, true);
47 _caged_node = node->getChild("caged-flag", 0, true);
48 _tumble_node = node->getChild("tumble-norm", 0, true);
49 _pitch_int_node = node->getChild("internal-pitch-deg", 0, true);
50 _roll_int_node = node->getChild("internal-roll-deg", 0, true);
51 _pitch_out_node = node->getChild("indicated-pitch-deg", 0, true);
52 _roll_out_node = node->getChild("indicated-roll-deg", 0, true);
56 AttitudeIndicator::bind ()
58 std::ostringstream temp;
61 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
63 fgTie((branch + "/serviceable").c_str(),
64 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
65 fgTie((branch + "/spin").c_str(),
66 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
70 AttitudeIndicator::unbind ()
72 std::ostringstream temp;
75 branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
77 fgUntie((branch + "/serviceable").c_str());
78 fgUntie((branch + "/spin").c_str());
82 AttitudeIndicator::update (double dt)
84 // If it's caged, it doesn't indicate
85 if (_caged_node->getBoolValue()) {
86 _roll_int_node->setDoubleValue(0.0);
87 _pitch_int_node->setDoubleValue(0.0);
91 // Get the spin from the gyro
92 _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
94 double spin = _gyro.get_spin_norm();
96 // Calculate the responsiveness
97 double responsiveness = spin * spin * spin * spin * spin * spin;
99 // Get the indicated roll and pitch
100 double roll = _roll_in_node->getDoubleValue();
101 double pitch = _pitch_in_node->getDoubleValue();
103 // Calculate the tumble for the
105 if (_tumble_flag_node->getBoolValue()) {
106 double tumble = _tumble_node->getDoubleValue();
107 if (fabs(roll) > 45.0) {
108 double target = (fabs(roll) - 45.0) / 45.0;
109 target *= target; // exponential past +-45 degrees
113 if (fabs(target) > fabs(tumble))
118 else if (tumble < -1.0)
121 // Reerect in 5 minutes
122 double step = dt/300.0;
125 else if (tumble > step)
129 _tumble_node->setDoubleValue(tumble);
132 roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll,
134 pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch,
137 // Assign the new values
138 _roll_int_node->setDoubleValue(roll);
139 _pitch_int_node->setDoubleValue(pitch);
141 // add in a gyro underspin "error" if gyro is spinning too slowly
142 const double spin_thresh = 0.8;
143 const double max_roll_error = 40.0;
144 const double max_pitch_error = 12.0;
147 if ( spin <= spin_thresh ) {
148 double roll_error_factor = (spin_thresh - spin) / spin_thresh;
149 double pitch_error_factor = (spin_thresh - spin) / spin_thresh;
150 roll_error = roll_error_factor * roll_error_factor * max_roll_error;
151 pitch_error = pitch_error_factor * pitch_error_factor * max_pitch_error;
157 _roll_out_node->setDoubleValue(roll + roll_error);
158 _pitch_out_node->setDoubleValue(pitch + pitch_error);
161 // end of attitude_indicator.cxx