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