]> git.mxchange.org Git - flightgear.git/blobdiff - src/Systems/electrical.cxx
Moved some of the low level scene graph construction code over to simgear.
[flightgear.git] / src / Systems / electrical.cxx
index f4eebbbac2125d9064a4c5db9bdf0b30ef1ea8c6..4f8329af5706e2675feae2a6782212bbbdb93ceb 100644 (file)
@@ -56,6 +56,7 @@ FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
     }
     volts = node->getDoubleValue("volts");
     amps = node->getDoubleValue("amps");
+    rpm_src = node->getStringValue("rpm-source");
 
     int i;
     for ( i = 0; i < node->nChildren(); ++i ) {
@@ -69,7 +70,7 @@ FGElectricalSupplier::FGElectricalSupplier ( SGPropertyNode *node ) {
         }
     }
 
-    _rpm_node = fgGetNode("/engines/engine[0]/rpm", true);
+    _rpm_node = fgGetNode( rpm_src.c_str(), true);
 }  
 
 
@@ -180,9 +181,33 @@ FGElectricalConnector::FGElectricalConnector ( SGPropertyNode *node,
             add_switch( fgGetNode( child->getStringValue(), true ) );
         }
     }
+
+    // do a 2nd pass to pick up starting switch value if specified
+    for ( i = 0; i < node->nChildren(); ++i ) {
+        SGPropertyNode *child = node->getChild(i);
+        string cname = child->getName();
+        string cval = child->getStringValue();
+        // cout << "  " << cname << " = " << cval << endl;
+        if ( cname == "initial-state" ) {
+            if ( cval == "off" ) {
+                set_switches( false );
+            } else {
+                set_switches( true );
+            }
+        }
+    }
 }  
 
 
+// set all switches to the specified state
+void FGElectricalConnector::set_switches( bool state ) {
+    // cout << "setting switch state to " << state << endl;
+    for ( unsigned int i = 0; i < switches.size(); ++i ) {
+        switches[i]->setBoolValue( state );
+    }
+}
+
+
 // return true if all switches are true, false otherwise.  A connector
 // could have multiple switches, but they all need to be true(closed)
 // for current to get through.
@@ -211,29 +236,36 @@ FGElectricalSystem::~FGElectricalSystem () {
 void FGElectricalSystem::init () {
     config_props = new SGPropertyNode;
 
-    SGPath config( globals->get_fg_root() );
-    config.append( fgGetString("/sim/systems/electrical/path") );
+    SGPropertyNode *path_n = fgGetNode("/sim/systems/electrical/path");
 
-    SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
-            << config.str() );
-    try {
-        readProperties( config.str(), config_props );
+    if (path_n) {
+        SGPath config( globals->get_fg_root() );
+        config.append( path_n->getStringValue() );
 
-        if ( build() ) {
-            enabled = true;
-        } else {
-            SG_LOG( SG_ALL, SG_ALERT,
-                    "Detected an internal inconsistancy in the electrical" );
-            SG_LOG( SG_ALL, SG_ALERT,
-                    " system specification file.  See earlier errors for" );
-            SG_LOG( SG_ALL, SG_ALERT,
-                    " details.");
-            exit(-1);
-        }        
-    } catch (const sg_exception& exc) {
-        SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
+        SG_LOG( SG_ALL, SG_ALERT, "Reading electrical system model from "
                 << config.str() );
-    }
+        try {
+            readProperties( config.str(), config_props );
+
+            if ( build() ) {
+                enabled = true;
+            } else {
+                SG_LOG( SG_ALL, SG_ALERT,
+                        "Detected an internal inconsistancy in the electrical");
+                SG_LOG( SG_ALL, SG_ALERT,
+                        " system specification file.  See earlier errors for" );
+                SG_LOG( SG_ALL, SG_ALERT,
+                        " details.");
+                exit(-1);
+            }        
+        } catch (const sg_exception& exc) {
+            SG_LOG( SG_ALL, SG_ALERT, "Failed to load electrical system model: "
+                    << config.str() );
+        }
+
+    } else
+        SG_LOG( SG_ALL, SG_ALERT,
+                "No electrical model specified for this model!");
 
     delete config_props;
 }
@@ -270,10 +302,10 @@ void FGElectricalSystem::update (double dt) {
         connectors[i]->set_value( 0.0 );
     }
 
-    // for each supplier, propogate the electrical current
+    // for each supplier, propagate the electrical current
     for ( i = 0; i < suppliers.size(); ++i ) {
         // cout << " Updating: " << suppliers[i]->get_name() << endl;
-        propogate( suppliers[i], 0.0, " " );
+        propagate( suppliers[i], 0.0, " " );
     }
 
 }
@@ -315,14 +347,16 @@ bool FGElectricalSystem::build () {
 }
 
 
-// propogate the electrical current through the network
-void FGElectricalSystem::propogate( FGElectricalComponent *node, double val,
+// propagate the electrical current through the network
+void FGElectricalSystem::propagate( FGElectricalComponent *node, double val,
                                     string s ) {
     s += " ";
 
     // determine the current to carry forward
     double current = 0.0;
-    if ( node->get_kind() == FG_SUPPLIER ) {
+    if ( !fgGetBool("/systems/electrical/serviceable") ) {
+        current = 0;
+    } else if ( node->get_kind() == FG_SUPPLIER ) {
         // cout << s << " is a supplier" << endl;
         current = ((FGElectricalSupplier *)node)->get_output();
     } else if ( node->get_kind() == FG_BUS ) {
@@ -355,9 +389,9 @@ void FGElectricalSystem::propogate( FGElectricalComponent *node, double val,
     }
     // cout << s << node->get_name() << " -> " << node->get_value() << endl;
 
-    // propogate to all children
+    // propagate to all children
     for ( i = 0; i < node->get_num_outputs(); ++i ) {
-        propogate( node->get_output(i), current, s );
+        propagate( node->get_output(i), current, s );
     }
 }