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