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