]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/attitude_indicator.cxx
James Turner:
[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 <iostream>
12 #include <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(node->getStringValue("name", "attitude-indicator")),
25     _num(node->getIntValue("number", 0)),
26     _suction(node->getStringValue("suction", "/systems/vacuum/suction-inhg"))
27 {
28 }
29
30 AttitudeIndicator::~AttitudeIndicator ()
31 {
32 }
33
34 void
35 AttitudeIndicator::init ()
36 {
37     string branch;
38     branch = "/instrumentation/" + _name;
39
40     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
41     
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);
53 }
54
55 void
56 AttitudeIndicator::bind ()
57 {
58     std::ostringstream temp;
59     string branch;
60     temp << _num;
61     branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
62
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);
67 }
68
69 void
70 AttitudeIndicator::unbind ()
71 {
72     std::ostringstream temp;
73     string branch;
74     temp << _num;
75     branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
76
77     fgUntie((branch + "/serviceable").c_str());
78     fgUntie((branch + "/spin").c_str());
79 }
80
81 void
82 AttitudeIndicator::update (double dt)
83 {
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);
88         return;
89     }
90
91                                 // Get the spin from the gyro
92     _gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
93     _gyro.update(dt);
94     double spin = _gyro.get_spin_norm();
95
96                                 // Calculate the responsiveness
97     double responsiveness = spin * spin * spin * spin * spin * spin;
98
99                                 // Get the indicated roll and pitch
100     double roll = _roll_in_node->getDoubleValue();
101     double pitch = _pitch_in_node->getDoubleValue();
102
103                                 // Calculate the tumble for the
104                                 // next pass.
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
110             if (roll < 0)
111                 target = -target;
112
113             if (fabs(target) > fabs(tumble))
114                 tumble = target;
115
116             if (tumble > 1.0)
117                 tumble = 1.0;
118             else if (tumble < -1.0)
119                 tumble = -1.0;
120         }
121                                     // Reerect in 5 minutes
122         double step = dt/300.0;
123         if (tumble < -step)
124             tumble += step;
125         else if (tumble > step)
126             tumble -= step;
127
128         roll += tumble * 45;
129         _tumble_node->setDoubleValue(tumble);
130     }
131
132     roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll,
133                         responsiveness);
134     pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch,
135                          responsiveness);
136
137                                 // Assign the new values
138     _roll_int_node->setDoubleValue(roll);
139     _pitch_int_node->setDoubleValue(pitch);
140
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;
145     double roll_error;
146     double pitch_error;
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;
152     } else {
153         roll_error = 0.0;
154         pitch_error = 0.0;
155     }
156
157     _roll_out_node->setDoubleValue(roll + roll_error);
158     _pitch_out_node->setDoubleValue(pitch + pitch_error);
159 }
160
161 // end of attitude_indicator.cxx