]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/mrg.cxx
Fix bug 191, uninitialised HUD color.
[flightgear.git] / src / Instrumentation / mrg.cxx
1 // MRG.cxx - an electrically powered master reference gyro.
2 // Written by Vivian Meazza based on work by David Megginson, started 2006.
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 #include <simgear/sg_inlines.h>
11
12
13 #include <iostream>
14 #include <string>
15 #include <sstream>
16 #include <math.h>       // fabs()
17
18 #include <Main/fg_props.hxx>
19 #include <Main/util.hxx>
20
21 #include "mrg.hxx"
22
23 const double MasterReferenceGyro::gravity = -32.1740485564;
24
25 MasterReferenceGyro::MasterReferenceGyro ( SGPropertyNode *node ) :
26 _name(node->getStringValue("name", "master-reference-gyro")),
27 _num(node->getIntValue("number", 0))
28 {
29 }
30
31 MasterReferenceGyro::~MasterReferenceGyro ()
32 {
33 }
34
35 void
36 MasterReferenceGyro::init ()
37 {
38     _last_hdg = 0;
39     _last_roll = 0;
40     _last_pitch = 0;
41     _indicated_hdg = 0;
42     _indicated_roll = 0;
43     _indicated_pitch = 0;
44     _indicated_hdg_rate = 0;
45     _indicated_roll_rate = 0;
46     _indicated_pitch_rate = 0;
47     _erect_time = 180;
48     _last_g = 1;
49     _g_error = 10;
50
51     string branch;
52     branch = "/instrumentation/" + _name;
53
54     _pitch_in_node = fgGetNode("/orientation/pitch-deg", true);
55     _roll_in_node = fgGetNode("/orientation/roll-deg", true);
56     _hdg_in_node = fgGetNode("/orientation/heading-deg", true);
57     _hdg_mag_in_node = fgGetNode("/orientation/heading-magnetic-deg", true);
58     _pitch_rate_node = fgGetNode("/orientation/pitch-rate-degps", true);
59     _roll_rate_node = fgGetNode("/orientation/roll-rate-degps", true);
60     _yaw_rate_node = fgGetNode("/orientation/yaw-rate-degps", true);
61     //_g_in_node =   fgGetNode("/accelerations/pilot/z-accel-fps_sec", true);
62     _g_in_node =   fgGetNode("/accelerations/pilot-g", true);
63     _electrical_node = fgGetNode("/systems/electrical/outputs/MRG", true);
64     _hdg_mag_in_node = fgGetNode("/orientation/heading-magnetic-deg", true);
65
66     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
67     _off_node = node->getChild("off-flag", 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     _hdg_out_node = node->getChild("indicated-hdg-deg", 0, true);
71     _hdg_mag_out_node = node->getChild("indicated-mag-hdg-deg", 0, true);
72     _pitch_rate_out_node = node->getChild("indicated-pitch-rate-degps", 0, true);
73     _roll_rate_out_node = node->getChild("indicated-roll-rate-degps", 0, true);
74     _hdg_rate_out_node = node->getChild("indicated-hdg-rate-degps", 0, true);
75     _responsiveness_node = node->getChild("responsiveness", 0, true);
76     _error_out_node = node->getChild("heading-bug-error-deg", 0, true);
77     _hdg_input_source_node = node->getChild("heading-source", 0, true);
78     _fast_erect_node = node->getChild("fast-erect", 0, true);
79
80     _electrical_node->setDoubleValue(0);
81     _responsiveness_node->setDoubleValue(0.75);
82     _off_node->setBoolValue(false);
83     _hdg_input_source_node->setBoolValue(false);
84     _fast_erect_node->setBoolValue(false);
85     _g_in_node->setDoubleValue(1);
86 }
87
88 void
89 MasterReferenceGyro::bind ()
90 {
91     std::ostringstream temp;
92     string branch;
93     temp << _num;
94     branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
95
96     fgTie((branch + "/serviceable").c_str(),
97         &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
98     fgTie((branch + "/spin").c_str(),
99         &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
100 }
101
102 void
103 MasterReferenceGyro::unbind ()
104 {
105     std::ostringstream temp;
106     string branch;
107     temp << _num;
108     branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
109
110     fgUntie((branch + "/serviceable").c_str());
111     fgUntie((branch + "/spin").c_str());
112 }
113
114 void
115 MasterReferenceGyro::update (double dt)
116 {
117     //sanity check
118     if (!fgGetBool("/sim/fdm-initialized", false)) {
119         return;
120     }
121
122     double indicated_roll = 0;
123     double indicated_pitch = 0;
124     double indicated_hdg = 0;
125     double indicated_roll_rate = 0;
126     double indicated_pitch_rate = 0;
127     double indicated_hdg_rate = 0;
128     double hdg = 0;
129     double erect_time_factor = 1;
130
131     const double erect_time = 180;
132     const double max_g_error = 10.0;
133
134     //Get the spin from the gyro
135     _gyro.set_power_norm( _electrical_node->getDoubleValue()/24 );
136     _gyro.update(dt);
137     double spin = _gyro.get_spin_norm();
138
139     // set the "off-flag"
140     if ( _electrical_node->getDoubleValue() > 0 && spin >= 0.25) {
141         _off_node->setBoolValue(false);
142     } else {
143         _off_node->setBoolValue(true);
144         return;
145     }
146
147     // Get the input values
148     if(_hdg_input_source_node->getBoolValue()){
149         hdg = _hdg_in_node->getDoubleValue();
150     } else {
151         hdg = _hdg_mag_in_node->getDoubleValue();
152     }
153
154     double roll = _roll_in_node->getDoubleValue();
155     double pitch = _pitch_in_node->getDoubleValue();
156     double g = _g_in_node->getDoubleValue()/* / gravity*/;
157
158     double roll_rate = _yaw_rate_node->getDoubleValue();
159     double pitch_rate = _pitch_rate_node->getDoubleValue();
160     double yaw_rate = _yaw_rate_node->getDoubleValue();
161
162     //modulate the input by the spin rate
163     double responsiveness = spin * spin * spin * spin * spin * spin;
164     roll = fgGetLowPass( _last_roll, roll, responsiveness );
165     pitch = fgGetLowPass( _last_pitch , pitch, responsiveness );
166
167     if ((hdg - _last_hdg) > 180)
168         _last_hdg += 360;
169     if ((hdg - _last_hdg) < -180)
170         _last_hdg -= 360;
171
172     hdg = fgGetLowPass( _last_hdg , hdg, responsiveness );
173
174     //but we need to filter the hdg and yaw_rate as well - yuk!
175     responsiveness = 0.1 / (spin * spin * spin * spin * spin * spin);
176     yaw_rate = fgGetLowPass( _last_yaw_rate , yaw_rate, responsiveness );
177     g = fgGetLowPass( _last_g , g, 1.5);
178
179
180     // store the new values
181     _last_roll = roll;
182     _last_pitch = pitch;
183     _last_hdg = hdg;
184     _last_roll_rate = roll_rate;
185     _last_pitch_rate = pitch_rate;
186     _last_yaw_rate = yaw_rate;
187     _last_g = g;
188
189     //the gyro only erects inside limits
190     if ( fabs ( yaw_rate ) <= 5
191         && g <= 1.5 && g >= -0.5){
192
193             if ( !_fast_erect_node->getBoolValue() ){
194                 erect_time_factor = 1;
195             } else {
196                 erect_time_factor = 2;
197             }
198
199         _g_error -= (max_g_error/(erect_time * 0.33)) * dt * erect_time_factor;
200     } else {
201         _g_error += (max_g_error /(erect_time * 0.33)) * dt * 2; 
202
203         //SG_LOG(SG_GENERAL, SG_ALERT,_num <<
204         //    " g input " << _g_in_node->getDoubleValue() * gravity
205         //    <<" _erect_time " << _erect_time 
206         //    << " yaw " <<   yaw_rate
207         //    << " pitch " << _pitch_rate_node->getDoubleValue()
208         //    << " roll " << _roll_rate_node->getDoubleValue());
209
210     }
211
212     //cout << "_g_error "<< _g_error << endl;
213     _g_error = SGMiscd::clip(_g_error, 0, 10);
214 //     cout << "_g_error clip "<< _g_error << endl;
215     indicated_roll = _last_roll + _g_error;
216     indicated_pitch = _last_pitch + _g_error;
217     indicated_hdg = _last_hdg + _g_error;
218     indicated_roll_rate = _last_roll_rate;
219     indicated_pitch_rate = _last_pitch_rate;
220     indicated_hdg_rate = _last_yaw_rate;
221     // calculate the difference between the indicated heading
222     // and the selected heading for use with an autopilot
223     static SGPropertyNode *bnode
224         = fgGetNode( "/autopilot/settings/heading-bug-deg", false );
225
226     if ( bnode ) {
227         double diff = bnode->getDoubleValue() - indicated_hdg;
228         if ( diff < -180.0 ) { diff += 360.0; }
229         if ( diff > 180.0 ) { diff -= 360.0; }
230         _error_out_node->setDoubleValue( diff );
231         //SG_LOG(SG_GENERAL, SG_ALERT,
232         //"autopilot input " << bnode->getDoubleValue() 
233         //<< " output " << _error_out_node->getDoubleValue()<<);
234     }
235
236     //smooth the output
237     double factor = _responsiveness_node->getDoubleValue() * dt;
238
239     indicated_roll = fgGetLowPass( _indicated_roll, indicated_roll, factor );
240     indicated_pitch = fgGetLowPass( _indicated_pitch , indicated_pitch, factor );
241     //indicated_hdg = fgGetLowPass( _indicated_hdg , indicated_hdg, factor );
242
243     indicated_roll_rate = fgGetLowPass( _indicated_roll_rate, indicated_roll_rate, factor );
244     indicated_pitch_rate = fgGetLowPass( _indicated_pitch_rate , indicated_pitch_rate, factor );
245     indicated_hdg_rate = fgGetLowPass( _indicated_hdg_rate , indicated_hdg_rate, factor );
246
247     // store the new values
248     _indicated_roll = indicated_roll;
249     _indicated_pitch = indicated_pitch;
250     _indicated_hdg = indicated_hdg;
251
252     _indicated_roll_rate = indicated_roll_rate;
253     _indicated_pitch_rate = indicated_pitch_rate;
254     _indicated_hdg_rate = indicated_hdg_rate;
255
256     // add in a gyro underspin "error" if gyro is spinning too slowly
257     const double spin_thresh = 0.8;
258     const double max_roll_error = 40.0;
259     const double max_pitch_error = 12.0;
260     const double max_hdg_error = 140.0;
261     double roll_error;
262     double pitch_error;
263     double hdg_error;
264
265     if ( spin <= spin_thresh ) {
266         double roll_error_factor        = ( spin_thresh - spin ) / spin_thresh;
267         double pitch_error_factor       = ( spin_thresh - spin ) / spin_thresh;
268         double hdg_error_factor         = ( spin_thresh - spin ) / spin_thresh;
269         roll_error      = roll_error_factor * roll_error_factor * max_roll_error;
270         pitch_error = pitch_error_factor * pitch_error_factor * max_pitch_error;
271         hdg_error       = hdg_error_factor * hdg_error_factor * max_hdg_error;
272     } else {
273         roll_error = 0.0;
274         pitch_error = 0.0;
275         hdg_error = 0.0;
276     }
277
278     _roll_out_node->setDoubleValue( _indicated_roll + roll_error );
279     _pitch_out_node->setDoubleValue( _indicated_pitch + pitch_error );
280     _hdg_out_node->setDoubleValue( _indicated_hdg + hdg_error );
281     _pitch_rate_out_node ->setDoubleValue( _indicated_pitch_rate );
282     _roll_rate_out_node ->setDoubleValue( _indicated_roll_rate );
283     _hdg_rate_out_node ->setDoubleValue( _indicated_hdg_rate );
284 }
285
286 // end of mrg.cxx