]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/attitude_indicator.cxx
Improved tumbling behaviour -- the AI doesn't just freeze now, but
[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 ()
17 {
18 }
19
20 AttitudeIndicator::~AttitudeIndicator ()
21 {
22 }
23
24 void
25 AttitudeIndicator::init ()
26 {
27                                 // TODO: allow index of pump and AI
28                                 // to be configured.
29     _pitch_in_node = fgGetNode("/orientation/pitch-deg", true);
30     _roll_in_node = fgGetNode("/orientation/roll-deg", true);
31     _suction_node = fgGetNode("/systems/vacuum[0]/suction-inhg", true);
32     _tumble_flag_node =
33         fgGetNode("/instrumentation/attitude-indicator/config/tumble-flag",
34                   true);
35     _caged_node =
36         fgGetNode("/instrumentation/attitude-indicator/caged-flag", true);
37     _tumble_node =
38         fgGetNode("/instrumentation/attitude-indicator/tumble-norm", true);
39     _pitch_out_node =
40         fgGetNode("/instrumentation/attitude-indicator/indicated-pitch-deg",
41                   true);
42     _roll_out_node =
43         fgGetNode("/instrumentation/attitude-indicator/indicated-roll-deg",
44                   true);
45 }
46
47 void
48 AttitudeIndicator::bind ()
49 {
50     fgTie("/instrumentation/attitude-indicator/serviceable",
51           &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
52     fgTie("/instrumentation/attitude-indicator/spin",
53           &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
54 }
55
56 void
57 AttitudeIndicator::unbind ()
58 {
59     fgUntie("/instrumentation/attitude-indicator/serviceable");
60     fgUntie("/instrumentation/attitude-indicator/spin");
61 }
62
63 void
64 AttitudeIndicator::update (double dt)
65 {
66                                 // If it's caged, it doesn't indicate
67     if (_caged_node->getBoolValue()) {
68         _roll_out_node->setDoubleValue(0.0);
69         _pitch_out_node->setDoubleValue(0.0);
70         return;
71     }
72
73                                 // Get the spin from the gyro
74     _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
75     _gyro.update(dt);
76     double spin = _gyro.get_spin_norm();
77
78                                 // Calculate the responsiveness
79     double responsiveness = spin * spin * spin * spin * spin * spin;
80
81                                 // Get the indicated roll and pitch
82     double roll = _roll_in_node->getDoubleValue();
83     double pitch = _pitch_in_node->getDoubleValue();
84
85                                 // Calculate the tumble for the
86                                 // next pass.
87     if (_tumble_flag_node->getBoolValue()) {
88         double tumble = _tumble_node->getDoubleValue();
89         if (fabs(roll) > 45.0) {
90             double target = (fabs(roll) - 45.0) / 45.0;
91             target *= target;   // exponential past +-45 degrees
92             if (roll < 0)
93                 target = -target;
94
95             if (fabs(target) > fabs(tumble))
96                 tumble = target;
97
98             if (tumble > 1.0)
99                 tumble = 1.0;
100             else if (tumble < -1.0)
101                 tumble = -1.0;
102         }
103                                     // Reerect in 5 minutes
104         double step = dt/300.0;
105         if (tumble < -step)
106             tumble += step;
107         else if (tumble > step)
108             tumble -= step;
109
110         roll += tumble * 45;
111         _tumble_node->setDoubleValue(tumble);
112     }
113
114     roll = fgGetLowPass(_roll_out_node->getDoubleValue(), roll,
115                         responsiveness);
116     pitch = fgGetLowPass(_pitch_out_node->getDoubleValue(), pitch,
117                          responsiveness);
118
119                                 // Assign the new values
120     _roll_out_node->setDoubleValue(roll);
121     _pitch_out_node->setDoubleValue(pitch);
122 }
123
124 // end of attitude_indicator.cxx