1 // electrical.cxx - a flexible, generic electrical system model.
3 // Written by Curtis Olson, started September 2002.
5 // Copyright (C) 2002 Curtis L. Olson - curt@flightgear.org
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <simgear/misc/exception.hxx>
25 #include <simgear/misc/sg_path.hxx>
27 #include <Main/fg_props.hxx>
28 #include <Main/globals.hxx>
30 #include "electrical.hxx"
33 FGElectricalSupplier::FGElectricalSupplier ( string _name, string _model,
34 double _volts, double _amps )
39 if ( _model == "battery" ) {
41 } else if ( _model == "alternator" ) {
42 model = FG_ALTERNATOR;
43 } else if ( _model == "external" ) {
53 FGElectricalBus::FGElectricalBus ( string _name )
61 FGElectricalOutput::FGElectricalOutput ( string _name )
69 FGElectricalConnector::FGElectricalConnector ()
75 FGElectricalSystem::FGElectricalSystem () :
81 FGElectricalSystem::~FGElectricalSystem () {
85 void FGElectricalSystem::init () {
86 config_props = new SGPropertyNode;
88 SGPath config( globals->get_fg_root() );
89 config.append( fgGetString("/systems/electrical/path") );
91 SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
94 readProperties( config.str(), config_props );
99 SG_LOG( SG_ALL, SG_ALERT,
100 "Detected an internal inconsistancy in the electrical" );
101 SG_LOG( SG_ALL, SG_ALERT,
102 " system specification file. See earlier errors for" );
103 SG_LOG( SG_ALL, SG_ALERT,
107 } catch (const sg_exception& exc) {
108 SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
116 void FGElectricalSystem::bind () {
120 void FGElectricalSystem::unbind () {
124 void FGElectricalSystem::update (double dt) {
128 bool FGElectricalSystem::build () {
129 SGPropertyNode *node;
132 int count = config_props->nChildren();
133 for ( i = 0; i < count; ++i ) {
134 node = config_props->getChild(i);
135 string name = node->getName();
136 // cout << name << endl;
137 if ( name == "supplier" ) {
138 FGElectricalSupplier *s =
139 new FGElectricalSupplier( node->getStringValue("name"),
140 node->getStringValue("kind"),
141 node->getDoubleValue("volts"),
142 node->getDoubleValue("amps") );
143 suppliers.push_back( s );
144 } else if ( name == "bus" ) {
146 new FGElectricalBus( node->getStringValue("name") );
147 buses.push_back( b );
148 } else if ( name == "output" ) {
149 FGElectricalOutput *o =
150 new FGElectricalOutput( node->getStringValue("name") );
151 outputs.push_back( o );
152 } else if ( name == "connector" ) {
153 FGElectricalConnector *c =
154 new FGElectricalConnector();
155 connectors.push_back( c );
156 SGPropertyNode *child;
157 int ccount = node->nChildren();
158 for ( j = 0; j < ccount; ++j ) {
159 child = node->getChild(j);
160 string cname = child->getName();
161 string cval = child->getStringValue();
162 // cout << " " << cname << " = " << cval << endl;
163 if ( cname == "input" ) {
164 FGElectricalComponent *s = find( child->getStringValue() );
167 if ( s->get_kind() == FG_SUPPLIER ) {
168 ((FGElectricalSupplier *)s)->add_output( c );
169 } else if ( s->get_kind() == FG_BUS ) {
170 ((FGElectricalBus *)s)->add_output( c );
172 SG_LOG( SG_ALL, SG_ALERT,
173 "Attempt to connect to something that can't provide an output: "
174 << child->getStringValue() );
178 SG_LOG( SG_ALL, SG_ALERT,
179 "Can't find named source: "
180 << child->getStringValue() );
183 } else if ( cname == "output" ) {
184 FGElectricalComponent *s = find( child->getStringValue() );
187 if ( s->get_kind() == FG_BUS ) {
188 ((FGElectricalBus *)s)->add_input( c );
189 } else if ( s->get_kind() == FG_OUTPUT ) {
190 ((FGElectricalOutput *)s)->add_input( c );
192 SG_LOG( SG_ALL, SG_ALERT,
193 "Attempt to connect to something that can't provide an input: "
194 << child->getStringValue() );
198 SG_LOG( SG_ALL, SG_ALERT,
199 "Can't find named source: "
200 << child->getStringValue() );
203 } else if ( cname == "switch" ) {
204 c->add_switch( child->getStringValue() );
208 SG_LOG( SG_ALL, SG_ALERT, "Unknown component type specified: "
218 // search for the named component and return a pointer to it, NULL otherwise
219 FGElectricalComponent *FGElectricalSystem::find ( const string &name ) {
224 for ( i = 0; i < suppliers.size(); ++i ) {
225 s = ((FGElectricalSupplier *)suppliers[i])->get_name();
226 // cout << " " << s << endl;
233 for ( i = 0; i < buses.size(); ++i ) {
234 s = ((FGElectricalBus *)buses[i])->get_name();
235 // cout << " " << s << endl;
241 // then search outputs
242 for ( i = 0; i < outputs.size(); ++i ) {
243 s = ((FGElectricalOutput *)outputs[i])->get_name();
244 // cout << " " << s << endl;