| 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 | #include <config.h> |
| 7 | |
| 8 | #include <iostream> |
| 9 | #include <string> |
| 10 | #include <sstream> |
| 11 | |
| 12 | #include <simgear/structure/exception.hxx> |
| 13 | #include <simgear/misc/sg_path.hxx> |
| 14 | #include <simgear/sg_inlines.h> |
| 15 | #include <simgear/props/props_io.hxx> |
| 16 | #include <simgear/debug/ErrorReportingCallback.hxx> |
| 17 | |
| 18 | #include <Main/fg_props.hxx> |
| 19 | #include <Main/globals.hxx> |
| 20 | #include <Main/util.hxx> |
| 21 | |
| 22 | #include "instrument_mgr.hxx" |
| 23 | #include "adf.hxx" |
| 24 | #include "airspeed_indicator.hxx" |
| 25 | #include "altimeter.hxx" |
| 26 | #include "attitude_indicator.hxx" |
| 27 | #include "attitude_indicator_electric.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_DEV_WARN, "No instrumentation model specified for this model!"); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | SGPath config = globals->resolve_aircraft_path(path_n->getStringValue()); |
| 76 | if (!config.exists()) { |
| 77 | SG_LOG(SG_COCKPIT, SG_DEV_ALERT, "Missing instrumentation file at:" << config); |
| 78 | simgear::reportFailure(simgear::LoadFailure::NotFound, simgear::ErrorCode::AircraftSystems, |
| 79 | "FGInstrumentMgr: Missing instrumentation file", config); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | SG_LOG( SG_COCKPIT, SG_INFO, "Reading instruments from " << config ); |
| 84 | |
| 85 | try { |
| 86 | readProperties( config, config_props ); |
| 87 | if (!build(config_props, config)) { |
| 88 | throw sg_exception( |
| 89 | "Detected an internal inconsistency in the instrumentation\n" |
| 90 | "system specification file. See earlier errors for details."); |
| 91 | } |
| 92 | } catch (const sg_exception& e) { |
| 93 | simgear::reportFailure(simgear::LoadFailure::BadData, simgear::ErrorCode::AircraftSystems, |
| 94 | "Failed to load instrumentation system:" + e.getFormattedMessage(), |
| 95 | e.getLocation()); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | if (!_explicitGps) { |
| 100 | SG_LOG(SG_INSTR, SG_INFO, "creating default GPS instrument"); |
| 101 | SGPropertyNode_ptr nd(new SGPropertyNode); |
| 102 | nd->setStringValue("name", "gps"); |
| 103 | nd->setIntValue("number", 0); |
| 104 | _instruments.push_back("gps[0]"); |
| 105 | set_subsystem("gps[0]", new GPS(nd, true /* default GPS mode */)); |
| 106 | } |
| 107 | |
| 108 | SGSubsystemGroup::init(); |
| 109 | } |
| 110 | |
| 111 | bool FGInstrumentMgr::build (SGPropertyNode* config_props, const SGPath& path) |
| 112 | { |
| 113 | for ( int i = 0; i < config_props->nChildren(); ++i ) { |
| 114 | SGPropertyNode *node = config_props->getChild(i); |
| 115 | std::string name = node->getNameString(); |
| 116 | |
| 117 | std::ostringstream subsystemname; |
| 118 | subsystemname << "instrument-" << i << '-' |
| 119 | << node->getStringValue("name", name.c_str()); |
| 120 | int index = node->getIntValue("number", 0); |
| 121 | if (index > 0) |
| 122 | subsystemname << '['<< index << ']'; |
| 123 | std::string id = subsystemname.str(); |
| 124 | |
| 125 | if ( name == "adf" ) { |
| 126 | set_subsystem( id, new ADF( node ), 0.15 ); |
| 127 | |
| 128 | } else if ( name == "airspeed-indicator" ) { |
| 129 | set_subsystem( id, new AirspeedIndicator( node ) ); |
| 130 | |
| 131 | } else if ( name == "altimeter" ) { |
| 132 | set_subsystem( id, new Altimeter( node, "altimeter" ) ); |
| 133 | |
| 134 | } else if ( name == "attitude-indicator" ) { |
| 135 | set_subsystem( id, new AttitudeIndicator( node ) ); |
| 136 | |
| 137 | } else if ( name == "attitude-indicator-electric" ) { |
| 138 | set_subsystem( id, new AttitudeIndicatorElectric( node ) ); |
| 139 | |
| 140 | } else if ( name == "clock" ) { |
| 141 | set_subsystem( id, new Clock( node ), 0.25 ); |
| 142 | |
| 143 | } else if ( name == "dme" ) { |
| 144 | set_subsystem( id, new DME( node ), 1.0 ); |
| 145 | |
| 146 | } else if ( name == "encoder" ) { |
| 147 | set_subsystem( id, new Altimeter( node, "encoder" ), 0.15 ); |
| 148 | |
| 149 | } else if ( name == "gps" ) { |
| 150 | // post 2.12.0, add a new name (distinct from 'gps'), so |
| 151 | // it is possible to create non-default GPS instruments. |
| 152 | // then authors of realistic GPS and FMSs can transition to using |
| 153 | // that name as they choose. |
| 154 | set_subsystem( id, new GPS( node, true /* default GPS mode */ ) ); |
| 155 | _explicitGps = true; |
| 156 | } else if ( name == "gsdi" ) { |
| 157 | set_subsystem( id, new GSDI( node ) ); |
| 158 | |
| 159 | } else if ( name == "heading-indicator" ) { |
| 160 | set_subsystem( id, new HeadingIndicator( node ) ); |
| 161 | |
| 162 | } else if ( name == "heading-indicator-fg" ) { |
| 163 | set_subsystem( id, new HeadingIndicatorFG( node ) ); |
| 164 | |
| 165 | } else if ( name == "heading-indicator-dg" ) { |
| 166 | set_subsystem( id, new HeadingIndicatorDG( node ) ); |
| 167 | |
| 168 | } else if ( name == "KR-87" ) { |
| 169 | set_subsystem( id, new FGKR_87( 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)); |
| 176 | |
| 177 | } else if ( name == "comm-radio" ) { |
| 178 | set_subsystem( id, Instrumentation::CommRadio::createInstance( node ) ); |
| 179 | |
| 180 | } else if ( name == "nav-radio" ) { |
| 181 | set_subsystem( id, Instrumentation::NavRadio::createInstance( node ) ); |
| 182 | |
| 183 | } else if ( name == "slip-skid-ball" ) { |
| 184 | set_subsystem( id, new SlipSkidBall( node ), 0.03 ); |
| 185 | |
| 186 | } else if (( name == "transponder" ) || ( name == "KT-70" )) { |
| 187 | if (name == "KT-70") { |
| 188 | SG_LOG(SG_INSTR, SG_DEV_ALERT, "KT-70 legacy instrument compatibility. " |
| 189 | "Please update aircraft to use transponder directly"); |
| 190 | // force configuration into compatibility mode |
| 191 | node->setBoolValue("kt70-compatibility", true); |
| 192 | } |
| 193 | set_subsystem( id, new Transponder( node ), 0.2 ); |
| 194 | |
| 195 | } else if ( name == "turn-indicator" ) { |
| 196 | set_subsystem( id, new TurnIndicator( node ) ); |
| 197 | |
| 198 | } else if ( name == "vertical-speed-indicator" ) { |
| 199 | set_subsystem( id, new VerticalSpeedIndicator( node ) ); |
| 200 | |
| 201 | } else if ( name == "inst-vertical-speed-indicator" ) { |
| 202 | set_subsystem( id, new InstVerticalSpeedIndicator( node ) ); |
| 203 | |
| 204 | } else if ( name == "tacan" ) { |
| 205 | set_subsystem( id, new TACAN( node ), 0.2 ); |
| 206 | |
| 207 | } else if ( name == "mk-viii" ) { |
| 208 | set_subsystem( id, new MK_VIII( node ), 0.2); |
| 209 | |
| 210 | } else if ( name == "master-reference-gyro" ) { |
| 211 | set_subsystem( id, new MasterReferenceGyro( node ) ); |
| 212 | |
| 213 | } else if (( name == "groundradar" ) || |
| 214 | ( name == "radar" ) || |
| 215 | ( name == "air-ground-radar" ) || |
| 216 | ( name == "navigation-display" )) |
| 217 | { |
| 218 | // these instruments are handled by the CockpitDisplayManager |
| 219 | // catch them here so we can still warn about bogus names in |
| 220 | // the instruments file |
| 221 | continue; |
| 222 | } else if ( name == "radar-altimeter" ) { |
| 223 | set_subsystem( id, new RadarAltimeter( node ) ); |
| 224 | |
| 225 | } else if ( name == "tcas" ) { |
| 226 | set_subsystem( id, new TCAS( node ), 0.2); |
| 227 | |
| 228 | } else { |
| 229 | simgear::reportFailure(simgear::LoadFailure::Misconfigured, simgear::ErrorCode::AircraftSystems, |
| 230 | "Unknown top level section in instrumentation:" + name, |
| 231 | path); |
| 232 | continue; |
| 233 | } |
| 234 | |
| 235 | // only push to our array if we actually built an insturment |
| 236 | _instruments.push_back(id); |
| 237 | } // of instruments iteration |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | // Register the subsystem. |
| 243 | SGSubsystemMgr::Registrant<FGInstrumentMgr> registrantFGInstrumentMgr( |
| 244 | SGSubsystemMgr::FDM); |
| 245 | |
| 246 | // end of instrument_manager.cxx |