]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/instrument_mgr.cxx
751f6c78c7fec517722aaddb5e02215dfdbb5097
[flightgear.git] / src / Instrumentation / instrument_mgr.cxx
1 // instrument_mgr.cxx - manage aircraft instruments.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 #ifdef HAVE_CONFIG_H
7 #  include <config.h>
8 #endif
9
10 #include <iostream>
11 #include <string>
12 #include <sstream>
13
14 #include <simgear/structure/exception.hxx>
15 #include <simgear/misc/sg_path.hxx>
16 #include <simgear/sg_inlines.h>
17 #include <simgear/props/props_io.hxx>
18
19 #include <Main/fg_props.hxx>
20 #include <Main/globals.hxx>
21 #include <Main/util.hxx>
22
23 #include "instrument_mgr.hxx"
24 #include "adf.hxx"
25 #include "airspeed_indicator.hxx"
26 #include "altimeter.hxx"
27 #include "attitude_indicator.hxx"
28 #include "clock.hxx"
29 #include "dme.hxx"
30 #include "gps.hxx"
31 #include "gsdi.hxx"
32 #include "heading_indicator.hxx"
33 #include "heading_indicator_fg.hxx"
34 #include "heading_indicator_dg.hxx"
35 #include "kr_87.hxx"
36 #include "mag_compass.hxx"
37 #include "marker_beacon.hxx"
38 #include "newnavradio.hxx"
39 #include "commradio.hxx"
40 #include "slip_skid_ball.hxx"
41 #include "transponder.hxx"
42 #include "turn_indicator.hxx"
43 #include "vertical_speed_indicator.hxx"
44 #include "inst_vertical_speed_indicator.hxx"
45 #include "tacan.hxx"
46 #include "mk_viii.hxx"
47 #include "mrg.hxx"
48 #include "rad_alt.hxx"
49 #include "tcas.hxx"
50
51 FGInstrumentMgr::FGInstrumentMgr () :
52   _explicitGps(false)
53 {    
54 }
55
56 FGInstrumentMgr::~FGInstrumentMgr ()
57 {
58 }
59
60 SGSubsystem::InitStatus FGInstrumentMgr::incrementalInit()
61 {
62   init();
63   return INIT_DONE;
64 }
65
66 void FGInstrumentMgr::init()
67 {
68   SGPropertyNode_ptr config_props = new SGPropertyNode;
69   SGPropertyNode* path_n = fgGetNode("/sim/instrumentation/path");
70   if (!path_n) {
71     SG_LOG(SG_COCKPIT, SG_WARN, "No instrumentation model specified for this model!");
72     return;
73   }
74
75   SGPath config = globals->resolve_aircraft_path(path_n->getStringValue());
76   SG_LOG( SG_COCKPIT, SG_INFO, "Reading instruments from " << config.str() );
77
78   try {
79     readProperties( config.str(), config_props );
80     if (!build(config_props)) {
81       throw sg_exception(
82                     "Detected an internal inconsistency in the instrumentation\n"
83                     "system specification file.  See earlier errors for details.");
84     }
85   } catch (const sg_exception& e) {
86     SG_LOG(SG_COCKPIT, SG_ALERT, "Failed to load instrumentation system model: "
87                     << config.str() << ":" << e.getFormattedMessage() );
88   }
89
90
91   if (!_explicitGps) {
92     SG_LOG(SG_INSTR, SG_INFO, "creating default GPS instrument");
93     SGPropertyNode_ptr nd(new SGPropertyNode);
94     nd->setStringValue("name", "gps");
95     nd->setIntValue("number", 0);
96     _instruments.push_back("gps[0]");
97     set_subsystem("gps[0]", new GPS(nd, true /* default GPS mode */));
98   }
99
100   // bind() created instruments before init.
101   for (unsigned int i=0; i<_instruments.size(); ++i) {
102     const std::string& nm(_instruments[i]);
103     SGSubsystem* instr = get_subsystem(nm);
104     instr->bind();
105   }
106
107   SGSubsystemGroup::init();
108 }
109
110 bool FGInstrumentMgr::build (SGPropertyNode* config_props)
111 {
112     for ( int i = 0; i < config_props->nChildren(); ++i ) {
113         SGPropertyNode *node = config_props->getChild(i);
114         std::string name = node->getName();
115
116         std::ostringstream subsystemname;
117         subsystemname << "instrument-" << i << '-'
118                 << node->getStringValue("name", name.c_str());
119         int index = node->getIntValue("number", 0);
120         if (index > 0)
121             subsystemname << '['<< index << ']';
122         std::string id = subsystemname.str();
123       
124         if ( name == "adf" ) {
125             set_subsystem( id, new ADF( node ), 0.15 );
126
127         } else if ( name == "airspeed-indicator" ) {
128             set_subsystem( id, new AirspeedIndicator( node ) );
129
130         } else if ( name == "altimeter" ) {
131             set_subsystem( id, new Altimeter( node, "altimeter" ) );
132
133         } else if ( name == "attitude-indicator" ) {
134             set_subsystem( id, new AttitudeIndicator( node ) );
135
136         } else if ( name == "clock" ) {
137             set_subsystem( id, new Clock( node ), 0.25 );
138
139         } else if ( name == "dme" ) {
140             set_subsystem( id, new DME( node ), 1.0 );
141
142         } else if ( name == "encoder" ) {
143             set_subsystem( id, new Altimeter( node, "encoder" ), 0.15 );
144
145         } else if ( name == "gps" ) {
146             // post 2.12.0, add a new name (distinct from 'gps'), so
147             // it is possible to create non-default GPS instruments.
148             // then authors of realistic GPS and FMSs can transition to using
149             // that name as they choose.
150             set_subsystem( id, new GPS( node, true /* default GPS mode */ ) );
151             _explicitGps = true;
152         } else if ( name == "gsdi" ) {
153             set_subsystem( id, new GSDI( node ) );
154
155         } else if ( name == "heading-indicator" ) {
156             set_subsystem( id, new HeadingIndicator( node ) );
157
158         } else if ( name == "heading-indicator-fg" ) {
159             set_subsystem( id, new HeadingIndicatorFG( node ) );
160
161         } else if ( name == "heading-indicator-dg" ) {
162             set_subsystem( id, new HeadingIndicatorDG( node ) );
163
164         } else if ( name == "KR-87" ) {
165             set_subsystem( id, new FGKR_87( node ) );
166
167         } else if ( name == "magnetic-compass" ) {
168             set_subsystem( id, new MagCompass( node ) );
169
170         } else if ( name == "marker-beacon" ) {
171             set_subsystem( id, new FGMarkerBeacon( node ), 0.2 );
172
173         } else if ( name == "comm-radio" ) {
174             set_subsystem( id, Instrumentation::CommRadio::createInstance( node ) );
175
176         } else if ( name == "nav-radio" ) {
177             set_subsystem( id, Instrumentation::NavRadio::createInstance( node ) );
178
179         } else if ( name == "slip-skid-ball" ) {
180             set_subsystem( id, new SlipSkidBall( node ), 0.03 );
181
182         } else if (( name == "transponder" ) || ( name == "KT-70" )) {
183             if  (name == "KT-70") {
184                 SG_LOG(SG_INSTR, SG_WARN, "KT-70 legacy instrument compatibility. "
185                        "Please update aircraft to use transponder directly");
186                 // force configuration into compatibility mode
187                 node->setBoolValue("kt70-compatibility", true);
188             }
189             set_subsystem( id, new Transponder( node ), 0.2 );
190
191         } else if ( name == "turn-indicator" ) {
192             set_subsystem( id, new TurnIndicator( node ) );
193
194         } else if ( name == "vertical-speed-indicator" ) {
195             set_subsystem( id, new VerticalSpeedIndicator( node ) );
196
197         } else if ( name == "inst-vertical-speed-indicator" ) {
198             set_subsystem( id, new InstVerticalSpeedIndicator( node ) );
199
200         } else if ( name == "tacan" ) {
201             set_subsystem( id, new TACAN( node ), 0.2 );
202
203         } else if ( name == "mk-viii" ) {
204             set_subsystem( id, new MK_VIII( node ), 0.2);
205
206         } else if ( name == "master-reference-gyro" ) {
207             set_subsystem( id, new MasterReferenceGyro( node ) );
208
209         } else if (( name == "groundradar" ) ||
210                    ( name == "radar" ) ||
211                    ( name == "air-ground-radar" ) ||
212                    ( name == "navigation-display" ))
213         {
214         // these instruments are handled by the CockpitDisplayManager
215         // catch them here so we can still warn about bogus names in
216         // the instruments file
217           continue;
218         } else if ( name == "radar-altimeter" ) {
219             set_subsystem( id, new RadarAltimeter( node ) );
220
221         } else if ( name == "tcas" ) {
222             set_subsystem( id, new TCAS( node ), 0.2);
223             
224         } else {
225             SG_LOG( SG_INSTR, SG_ALERT,
226                      "Unknown top level section in instrumentation: " << name );
227             continue;
228         }
229       
230       // only push to our array if we actually built an insturment
231         _instruments.push_back(id);
232     } // of instruments iteration
233     return true;
234 }
235
236 // end of instrument_manager.cxx