]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/attitude_indicator.cxx
Changes by Roy Ovesen to begin migrating the avionics out of the Cockpit
[flightgear.git] / src / Instrumentation / attitude_indicator.cxx
1 // attitude_indicator.cxx - a vacuum-powered attitude indicator.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 // TODO:
7 // - better spin-up
8
9 #include <simgear/compiler.h>
10
11 #include STL_IOSTREAM
12 #include STL_STRING
13 #include <sstream>
14
15 #include <math.h>       // fabs()
16
17 #include "attitude_indicator.hxx"
18 #include <Main/fg_props.hxx>
19 #include <Main/util.hxx>
20
21
22 AttitudeIndicator::AttitudeIndicator ( SGPropertyNode *node )
23     :
24     name("attitude-indicator"),
25     num(0),
26     vacuum_system("/systems/vacuum")
27 {
28     int i;
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" ) {
34             name = cval;
35         } else if ( cname == "number" ) {
36             num = (int) child->getDoubleValue();
37         } else if ( cname == "vacuum-system" ) {
38             vacuum_system = cval;
39         } else {
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 );
43             }
44         }
45     }
46 }
47
48 AttitudeIndicator::AttitudeIndicator ()
49 {
50 }
51
52 AttitudeIndicator::~AttitudeIndicator ()
53 {
54 }
55
56 void
57 AttitudeIndicator::init ()
58 {
59     string branch;
60     branch = "/instrumentation/" + name;
61     vacuum_system += "/suction-inhg";
62
63     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
64     
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);
76 }
77
78 void
79 AttitudeIndicator::bind ()
80 {
81     std::ostringstream temp;
82     string branch;
83     temp << num;
84     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
85
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);
90 }
91
92 void
93 AttitudeIndicator::unbind ()
94 {
95     std::ostringstream temp;
96     string branch;
97     temp << num;
98     branch = "/instrumentation/" + name + "[" + temp.str() + "]";
99
100     fgUntie((branch + "/serviceable").c_str());
101     fgUntie((branch + "/spin").c_str());
102 }
103
104 void
105 AttitudeIndicator::update (double dt)
106 {
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);
111         return;
112     }
113
114                                 // Get the spin from the gyro
115     _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
116     _gyro.update(dt);
117     double spin = _gyro.get_spin_norm();
118
119                                 // Calculate the responsiveness
120     double responsiveness = spin * spin * spin * spin * spin * spin;
121
122                                 // Get the indicated roll and pitch
123     double roll = _roll_in_node->getDoubleValue();
124     double pitch = _pitch_in_node->getDoubleValue();
125
126                                 // Calculate the tumble for the
127                                 // next pass.
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
133             if (roll < 0)
134                 target = -target;
135
136             if (fabs(target) > fabs(tumble))
137                 tumble = target;
138
139             if (tumble > 1.0)
140                 tumble = 1.0;
141             else if (tumble < -1.0)
142                 tumble = -1.0;
143         }
144                                     // Reerect in 5 minutes
145         double step = dt/300.0;
146         if (tumble < -step)
147             tumble += step;
148         else if (tumble > step)
149             tumble -= step;
150
151         roll += tumble * 45;
152         _tumble_node->setDoubleValue(tumble);
153     }
154
155     roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll,
156                         responsiveness);
157     pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch,
158                          responsiveness);
159
160                                 // Assign the new values
161     _roll_int_node->setDoubleValue(roll);
162     _pitch_int_node->setDoubleValue(pitch);
163
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;
168     double roll_error;
169     double pitch_error;
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;
175     } else {
176         roll_error = 0.0;
177         pitch_error = 0.0;
178     }
179
180     _roll_out_node->setDoubleValue(roll + roll_error);
181     _pitch_out_node->setDoubleValue(pitch + pitch_error);
182 }
183
184 // end of attitude_indicator.cxx