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