]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/attitude_indicator.cxx
Roy Vegard Ovesen:
[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 <math.h>       // fabs()
10
11 #include "attitude_indicator.hxx"
12 #include <Main/fg_props.hxx>
13 #include <Main/util.hxx>
14
15
16 AttitudeIndicator::AttitudeIndicator ( SGPropertyNode *node )
17     :
18     name("attitude-indicator"),
19     num(0),
20     vacuum_system("/systems/vacuum")
21 {
22     int i;
23     for ( i = 0; i < node->nChildren(); ++i ) {
24         SGPropertyNode *child = node->getChild(i);
25         string cname = child->getName();
26         string cval = child->getStringValue();
27         if ( cname == "name" ) {
28             name = cval;
29         } else if ( cname == "number" ) {
30             num = (int) child->getDoubleValue();
31         } else if ( cname == "vacuum-system" ) {
32             vacuum_system = cval;
33         } else {
34             SG_LOG( SG_INSTR, SG_WARN, "Error in attitude-indicator config logic" );
35             if ( name.length() ) {
36                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
37             }
38         }
39     }
40 }
41
42 AttitudeIndicator::AttitudeIndicator ()
43 {
44 }
45
46 AttitudeIndicator::~AttitudeIndicator ()
47 {
48 }
49
50 void
51 AttitudeIndicator::init ()
52 {
53     string branch;
54     branch = "/instrumentation/" + name;
55     vacuum_system += "/suction-inhg";
56
57     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
58     
59     _pitch_in_node = fgGetNode("/orientation/pitch-deg", true);
60     _roll_in_node = fgGetNode("/orientation/roll-deg", true);
61     _suction_node = fgGetNode(vacuum_system.c_str(), true);
62     SGPropertyNode *cnode = node->getChild("config", 0, true);
63     _tumble_flag_node = cnode->getChild("tumble-flag", 0, true);
64     _caged_node = node->getChild("caged-flag", 0, true);
65     _tumble_node = node->getChild("tumble-norm", 0, true);
66     _pitch_int_node = node->getChild("internal-pitch-deg", 0, true);
67     _roll_int_node = node->getChild("internal-roll-deg", 0, true);
68     _pitch_out_node = node->getChild("indicated-pitch-deg", 0, true);
69     _roll_out_node = node->getChild("indicated-roll-deg", 0, true);
70
71     //_serviceable_node->setBoolValue(true);
72 }
73
74 void
75 AttitudeIndicator::bind ()
76 {
77     string branch;
78     branch = "/instrumentation/" + name + "/serviceable";
79
80     fgTie(branch.c_str(),
81           &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
82     branch = "/instrumentation/" + name + "/spin";
83     fgTie(branch.c_str(),
84           &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
85 }
86
87 void
88 AttitudeIndicator::unbind ()
89 {
90     string branch;
91     branch = "/instrumentation/" + name + "/serviceable";
92
93     fgUntie(branch.c_str());
94     branch = "/instrumentation/" + name + "/spin";
95     fgUntie(branch.c_str());
96 }
97
98 void
99 AttitudeIndicator::update (double dt)
100 {
101                                 // If it's caged, it doesn't indicate
102     if (_caged_node->getBoolValue()) {
103         _roll_int_node->setDoubleValue(0.0);
104         _pitch_int_node->setDoubleValue(0.0);
105         return;
106     }
107
108                                 // Get the spin from the gyro
109     _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
110     _gyro.update(dt);
111     double spin = _gyro.get_spin_norm();
112
113                                 // Calculate the responsiveness
114     double responsiveness = spin * spin * spin * spin * spin * spin;
115
116                                 // Get the indicated roll and pitch
117     double roll = _roll_in_node->getDoubleValue();
118     double pitch = _pitch_in_node->getDoubleValue();
119
120                                 // Calculate the tumble for the
121                                 // next pass.
122     if (_tumble_flag_node->getBoolValue()) {
123         double tumble = _tumble_node->getDoubleValue();
124         if (fabs(roll) > 45.0) {
125             double target = (fabs(roll) - 45.0) / 45.0;
126             target *= target;   // exponential past +-45 degrees
127             if (roll < 0)
128                 target = -target;
129
130             if (fabs(target) > fabs(tumble))
131                 tumble = target;
132
133             if (tumble > 1.0)
134                 tumble = 1.0;
135             else if (tumble < -1.0)
136                 tumble = -1.0;
137         }
138                                     // Reerect in 5 minutes
139         double step = dt/300.0;
140         if (tumble < -step)
141             tumble += step;
142         else if (tumble > step)
143             tumble -= step;
144
145         roll += tumble * 45;
146         _tumble_node->setDoubleValue(tumble);
147     }
148
149     roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll,
150                         responsiveness);
151     pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch,
152                          responsiveness);
153
154                                 // Assign the new values
155     _roll_int_node->setDoubleValue(roll);
156     _pitch_int_node->setDoubleValue(pitch);
157
158     // add in a gyro underspin "error" if gyro is spinning too slowly
159     const double spin_thresh = 0.8;
160     const double max_roll_error = 40.0;
161     const double max_pitch_error = 12.0;
162     double roll_error;
163     double pitch_error;
164     if ( spin <= spin_thresh ) {
165         double roll_error_factor = (spin_thresh - spin) / spin_thresh;
166         double pitch_error_factor = (spin_thresh - spin) / spin_thresh;
167         roll_error = roll_error_factor * roll_error_factor * max_roll_error;
168         pitch_error = pitch_error_factor * pitch_error_factor * max_pitch_error;
169     } else {
170         roll_error = 0.0;
171         pitch_error = 0.0;
172     }
173
174     _roll_out_node->setDoubleValue(roll + roll_error);
175     _pitch_out_node->setDoubleValue(pitch + pitch_error);
176 }
177
178 // end of attitude_indicator.cxx