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