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