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