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("attitude-indicator"),
26 vacuum_system("/systems/vacuum")
29 for ( i = 0; i < node->nChildren(); ++i ) {
30 SGPropertyNode *child = node->getChild(i);
31 string cname = child->getName();
32 string cval = child->getStringValue();
33 if ( cname == "name" ) {
35 } else if ( cname == "number" ) {
36 num = (int) child->getDoubleValue();
37 } else if ( cname == "vacuum-system" ) {
40 SG_LOG( SG_INSTR, SG_WARN, "Error in attitude-indicator config logic" );
41 if ( name.length() ) {
42 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
48 AttitudeIndicator::AttitudeIndicator ()
52 AttitudeIndicator::~AttitudeIndicator ()
57 AttitudeIndicator::init ()
60 branch = "/instrumentation/" + name;
61 vacuum_system += "/suction-inhg";
63 SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
65 _pitch_in_node = fgGetNode("/orientation/pitch-deg", true);
66 _roll_in_node = fgGetNode("/orientation/roll-deg", true);
67 _suction_node = fgGetNode(vacuum_system.c_str(), true);
68 SGPropertyNode *cnode = node->getChild("config", 0, true);
69 _tumble_flag_node = cnode->getChild("tumble-flag", 0, true);
70 _caged_node = node->getChild("caged-flag", 0, true);
71 _tumble_node = node->getChild("tumble-norm", 0, true);
72 _pitch_int_node = node->getChild("internal-pitch-deg", 0, true);
73 _roll_int_node = node->getChild("internal-roll-deg", 0, true);
74 _pitch_out_node = node->getChild("indicated-pitch-deg", 0, true);
75 _roll_out_node = node->getChild("indicated-roll-deg", 0, true);
79 AttitudeIndicator::bind ()
81 std::ostringstream temp;
84 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
86 fgTie((branch + "/serviceable").c_str(),
87 &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
88 fgTie((branch + "/spin").c_str(),
89 &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
93 AttitudeIndicator::unbind ()
95 std::ostringstream temp;
98 branch = "/instrumentation/" + name + "[" + temp.str() + "]";
100 fgUntie((branch + "/serviceable").c_str());
101 fgUntie((branch + "/spin").c_str());
105 AttitudeIndicator::update (double dt)
107 // If it's caged, it doesn't indicate
108 if (_caged_node->getBoolValue()) {
109 _roll_int_node->setDoubleValue(0.0);
110 _pitch_int_node->setDoubleValue(0.0);
114 // Get the spin from the gyro
115 _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
117 double spin = _gyro.get_spin_norm();
119 // Calculate the responsiveness
120 double responsiveness = spin * spin * spin * spin * spin * spin;
122 // Get the indicated roll and pitch
123 double roll = _roll_in_node->getDoubleValue();
124 double pitch = _pitch_in_node->getDoubleValue();
126 // Calculate the tumble for the
128 if (_tumble_flag_node->getBoolValue()) {
129 double tumble = _tumble_node->getDoubleValue();
130 if (fabs(roll) > 45.0) {
131 double target = (fabs(roll) - 45.0) / 45.0;
132 target *= target; // exponential past +-45 degrees
136 if (fabs(target) > fabs(tumble))
141 else if (tumble < -1.0)
144 // Reerect in 5 minutes
145 double step = dt/300.0;
148 else if (tumble > step)
152 _tumble_node->setDoubleValue(tumble);
155 roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll,
157 pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch,
160 // Assign the new values
161 _roll_int_node->setDoubleValue(roll);
162 _pitch_int_node->setDoubleValue(pitch);
164 // add in a gyro underspin "error" if gyro is spinning too slowly
165 const double spin_thresh = 0.8;
166 const double max_roll_error = 40.0;
167 const double max_pitch_error = 12.0;
170 if ( spin <= spin_thresh ) {
171 double roll_error_factor = (spin_thresh - spin) / spin_thresh;
172 double pitch_error_factor = (spin_thresh - spin) / spin_thresh;
173 roll_error = roll_error_factor * roll_error_factor * max_roll_error;
174 pitch_error = pitch_error_factor * pitch_error_factor * max_pitch_error;
180 _roll_out_node->setDoubleValue(roll + roll_error);
181 _pitch_out_node->setDoubleValue(pitch + pitch_error);
184 // end of attitude_indicator.cxx