]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/instrument_mgr.cxx
ADF: code clean-up/documentation
[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 #include <Instrumentation/HUD/HUD.hxx>
23
24 #include "instrument_mgr.hxx"
25 #include "adf.hxx"
26 #include "airspeed_indicator.hxx"
27 #include "altimeter.hxx"
28 #include "attitude_indicator.hxx"
29 #include "clock.hxx"
30 #include "dme.hxx"
31 #include "gps.hxx"
32 #include "gsdi.hxx"
33 #include "heading_indicator.hxx"
34 #include "heading_indicator_fg.hxx"
35 #include "heading_indicator_dg.hxx"
36 #include "kr_87.hxx"
37 #include "kt_70.hxx"
38 #include "mag_compass.hxx"
39 #include "marker_beacon.hxx"
40 #include "newnavradio.hxx"
41 #include "slip_skid_ball.hxx"
42 #include "transponder.hxx"
43 #include "turn_indicator.hxx"
44 #include "vertical_speed_indicator.hxx"
45 #include "inst_vertical_speed_indicator.hxx"
46 #include "wxradar.hxx"
47 #include "tacan.hxx"
48 #include "mk_viii.hxx"
49 #include "mrg.hxx"
50 #include "groundradar.hxx"
51 #include "agradar.hxx"
52 #include "rad_alt.hxx"
53 #include "tcas.hxx"
54 #include "NavDisplay.hxx"
55
56 FGInstrumentMgr::FGInstrumentMgr () :
57   _explicitGps(false)
58 {    
59     globals->add_subsystem("hud", new HUD, SGSubsystemMgr::DISPLAY);
60 }
61
62 FGInstrumentMgr::~FGInstrumentMgr ()
63 {
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));
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 void FGInstrumentMgr::reinit()
111 {  
112 // delete all our instrument
113   for (unsigned int i=0; i<_instruments.size(); ++i) {
114     const std::string& nm(_instruments[i]);
115     SGSubsystem* instr = get_subsystem(nm);
116     instr->unbind();
117     remove_subsystem(nm);
118     delete instr;
119   }
120   
121   init();
122 }
123
124 bool FGInstrumentMgr::build (SGPropertyNode* config_props)
125 {
126     for ( int i = 0; i < config_props->nChildren(); ++i ) {
127         SGPropertyNode *node = config_props->getChild(i);
128         string name = node->getName();
129
130         std::ostringstream subsystemname;
131         subsystemname << "instrument-" << i << '-'
132                 << node->getStringValue("name", name.c_str());
133         int index = node->getIntValue("number", 0);
134         if (index > 0)
135             subsystemname << '['<< index << ']';
136         string id = subsystemname.str();
137         _instruments.push_back(id);
138       
139         if ( name == "adf" ) {
140             set_subsystem( id, new ADF( node ), 0.15 );
141
142         } else if ( name == "airspeed-indicator" ) {
143             set_subsystem( id, new AirspeedIndicator( node ) );
144
145         } else if ( name == "altimeter" ) {
146             set_subsystem( id, new Altimeter( node ) );
147
148         } else if ( name == "attitude-indicator" ) {
149             set_subsystem( id, new AttitudeIndicator( node ) );
150
151         } else if ( name == "clock" ) {
152             set_subsystem( id, new Clock( node ), 0.25 );
153
154         } else if ( name == "dme" ) {
155             set_subsystem( id, new DME( node ), 1.0 );
156
157         } else if ( name == "encoder" ) {
158             set_subsystem( id, new Altimeter( node ), 0.15 );
159
160         } else if ( name == "gps" ) {
161             set_subsystem( id, new GPS( node ) );
162             _explicitGps = true;
163         } else if ( name == "gsdi" ) {
164             set_subsystem( id, new GSDI( node ) );
165
166         } else if ( name == "heading-indicator" ) {
167             set_subsystem( id, new HeadingIndicator( node ) );
168
169         } else if ( name == "heading-indicator-fg" ) {
170             set_subsystem( id, new HeadingIndicatorFG( node ) );
171
172         } else if ( name == "heading-indicator-dg" ) {
173             set_subsystem( id, new HeadingIndicatorDG( node ) );
174
175         } else if ( name == "KR-87" ) {
176             set_subsystem( id, new FGKR_87( node ) );
177
178         } else if ( name == "KT-70" ) {
179             set_subsystem( id, new FGKT_70( node ) );
180
181         } else if ( name == "magnetic-compass" ) {
182             set_subsystem( id, new MagCompass( node ) );
183
184         } else if ( name == "marker-beacon" ) {
185             set_subsystem( id, new FGMarkerBeacon( node ), 0.2 );
186
187         } else if ( name == "nav-radio" ) {
188             set_subsystem( id, Instrumentation::NavRadio::createInstance( node ) );
189
190         } else if ( name == "slip-skid-ball" ) {
191             set_subsystem( id, new SlipSkidBall( node ), 0.03 );
192
193         } else if ( name == "transponder" ) {
194             set_subsystem( id, new Transponder( node ), 0.2 );
195
196         } else if ( name == "turn-indicator" ) {
197             set_subsystem( id, new TurnIndicator( node ) );
198
199         } else if ( name == "vertical-speed-indicator" ) {
200             set_subsystem( id, new VerticalSpeedIndicator( node ) );
201
202         } else if ( name == "radar" ) {
203             set_subsystem( id, new wxRadarBg ( node ), 1);
204
205         } else if ( name == "inst-vertical-speed-indicator" ) {
206             set_subsystem( id, new InstVerticalSpeedIndicator( node ) );
207
208         } else if ( name == "tacan" ) {
209             set_subsystem( id, new TACAN( node ), 0.2 );
210
211         } else if ( name == "mk-viii" ) {
212             set_subsystem( id, new MK_VIII( node ), 0.2);
213
214         } else if ( name == "master-reference-gyro" ) {
215             set_subsystem( id, new MasterReferenceGyro( node ) );
216
217         } else if ( name == "groundradar" ) {
218             set_subsystem( id, new GroundRadar( node ), 1 );
219
220         } else if ( name == "air-ground-radar" ) {
221             set_subsystem( id, new agRadar( node ),1);
222
223         } else if ( name == "radar-altimeter" ) {
224             set_subsystem( id, new radAlt( node ),1);
225
226         } else if ( name == "tcas" ) {
227             set_subsystem( id, new TCAS( node ), 0.2);
228         
229         } else if ( name == "navigation-display" ) {
230             set_subsystem( id, new NavDisplay( node ) );
231             
232         } else {
233             SG_LOG( SG_ALL, SG_ALERT, "Unknown top level section: "
234                     << name );
235             return false;
236         }
237     }
238     return true;
239 }
240
241 // end of instrument_manager.cxx