]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/ExternalNet.cxx
Updates to the electrical system model. "Juice" is now propogated through
[flightgear.git] / src / FDM / ExternalNet.cxx
index 01d565e79abfbdc5fd893f26e8020d0bc72708d4..949af690bd8d4106026c215fd8a570ac87c04f07 100644 (file)
@@ -73,15 +73,38 @@ static void global2raw( FGRawCtrls *raw ) {
     raw->elevator_trim = node->getDoubleValue( "elevator-trim" );
     raw->rudder = node->getDoubleValue( "rudder" );
     raw->flaps = node->getDoubleValue( "flaps" );
+    raw->num_engines = FGRawCtrls::FG_MAX_ENGINES;
     for ( i = 0; i < FGRawCtrls::FG_MAX_ENGINES; ++i ) {
-       raw->throttle[i] = node->getDoubleValue( "throttle", i );
-       raw->mixture[i] = node->getDoubleValue( "mixture", i );
-       raw->prop_advance[i] = node->getDoubleValue( "propeller-pitch", i );
-       raw->magnetos[i] = node->getIntValue( "magnetos", i );
-       raw->starter[i] = node->getBoolValue( "starter", i );
+       raw->throttle[i] = node->getDoubleValue( "throttle", 0.0 );
+       raw->mixture[i] = node->getDoubleValue( "mixture", 0.0 );
+       raw->prop_advance[i] = node->getDoubleValue( "propeller-pitch", 0.0 );
+       raw->magnetos[i] = node->getIntValue( "magnetos", 0 );
+       if ( i == 0 ) {
+         // cout << "Magnetos -> " << node->getIntValue( "magnetos", 0 );
+       }
+       raw->starter[i] = node->getBoolValue( "starter", false );
+       if ( i == 0 ) {
+         // cout << " Starter -> " << node->getIntValue( "stater", false )
+         //      << endl;
+       }
+    }
+    raw->num_tanks = FGRawCtrls::FG_MAX_TANKS;
+    for ( i = 0; i < FGRawCtrls::FG_MAX_TANKS; ++i ) {
+        if ( node->getChild("fuel-selector", i) != 0 ) {
+            raw->fuel_selector[i]
+                = node->getChild("fuel-selector", i)->getDoubleValue();
+        } else {
+            raw->fuel_selector[i] = false;
+        }
     }
+    raw->num_wheels = FGRawCtrls::FG_MAX_WHEELS;
     for ( i = 0; i < FGRawCtrls::FG_MAX_WHEELS; ++i ) {
-       raw->brake[i] = node->getDoubleValue( "brakes", i );
+        if ( node->getChild("brakes", i) != 0 ) {
+            raw->brake[i]
+                = node->getChild("brakes", i)->getDoubleValue();
+        } else {
+            raw->brake[i] = 0.0;
+        }
     }
     raw->hground = fgGetDouble( "/environment/ground-elevation-m" );
     raw->magvar = fgGetDouble("/environment/magnetic-variation-deg");
@@ -98,12 +121,18 @@ static void global2raw( FGRawCtrls *raw ) {
        htond(raw->throttle[i]);
        htond(raw->mixture[i]);
        htond(raw->prop_advance[i]);
-       htonl(raw->magnetos[i]);
-       htonl(raw->starter[i]);
+       raw->magnetos[i] = htonl(raw->magnetos[i]);
+       raw->starter[i] = htonl(raw->starter[i]);
     }
+    raw->num_engines = htonl(raw->num_engines);
+    for ( i = 0; i < FGRawCtrls::FG_MAX_TANKS; ++i ) {
+        raw->fuel_selector[i] = htonl(raw->fuel_selector[i]);
+    }
+    raw->num_tanks = htonl(raw->num_tanks);
     for ( i = 0; i < FGRawCtrls::FG_MAX_WHEELS; ++i ) {
        htond(raw->brake[i]);
     }
+    raw->num_wheels = htonl(raw->num_wheels);
     htond(raw->hground);
     htond(raw->magvar);
     raw->speedup = htonl(raw->speedup);
@@ -149,9 +178,8 @@ static void net2global( FGNetFDM *net ) {
     }
 
     net->num_wheels = htonl(net->num_wheels);
-    for ( i = 0; i < net->num_wheels; ++i ) {
-       net->wow[i] = htonl(net->wow[i]);
-    }
+    // I don't need to convert the Wow flags, since they are one byte in size
+    htond(net->flap_deflection);
 
     net->cur_time = ntohl(net->cur_time);
     net->warp = ntohl(net->warp);
@@ -214,6 +242,17 @@ static void net2global( FGNetFDM *net ) {
            node->setDoubleValue("wow", net->wow[i] );
        }
 
+        fgSetDouble("/surface-positions/flap-pos-norm", net->flap_deflection);
+       SGPropertyNode * node = fgGetNode("/controls", true);
+        fgSetDouble("/surface-positions/elevator-pos-norm", 
+                   node->getDoubleValue( "elevator" ));
+        fgSetDouble("/surface-positions/rudder-pos-norm", 
+                   node->getDoubleValue( "rudder" ));
+        fgSetDouble("/surface-positions/left-aileron-pos-norm", 
+                   node->getDoubleValue( "aileron" ));
+        fgSetDouble("/surface-positions/right-aileron-pos-norm", 
+                   -node->getDoubleValue( "aileron" ));
+
        /* these are ignored for now  ... */
        /*
        if ( net->cur_time ) {
@@ -235,7 +274,7 @@ static void net2global( FGNetFDM *net ) {
 
 FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
 {
-    set_delta_t( dt );
+//     set_delta_t( dt );
 
     valid = true;
 
@@ -268,14 +307,15 @@ FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
        valid = false;
     }
 
-    // we want to block for incoming data in order to syncronize frame
-    // rates.
-    data_server.setBlocking( false /* don't block while testing */ );
-    // data_server.setBlocking( true /* don't block while testing */ );
+    // disable blocking
+    data_server.setBlocking( false );
+
+    // allowed to read from a broadcast addr
+    // data_server.setBroadcast( true );
 
     // if we bind to fdm_host = "" then we accept messages from
     // anyone.
-    if ( data_server.bind( fdm_host.c_str(), data_in_port ) == -1 ) {
+    if ( data_server.bind( "", data_in_port ) == -1 ) {
         printf("error binding to port %d\n", data_in_port);
        valid = false;
     }
@@ -322,29 +362,36 @@ void FGExternalNet::init() {
     new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
     netChannel::loop(0);
 
+    SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
+
     sprintf( cmd, "/reset?value=ground" );
     new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
     netChannel::loop(0);
+
+    SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
 }
 
 
-// Run an iteration of the EOM.  This is a NOP here because the flight
-// model values are getting filled in elsewhere (most likely from some
-// external source.)
-void FGExternalNet::update( int multiloop ) {
+// Run an iteration of the EOM.
+void FGExternalNet::update( double dt ) {
     int length;
     int result;
 
+    if (is_suspended())
+      return;
+
     // Send control positions to remote fdm
     length = sizeof(ctrls);
     global2raw( &ctrls );
     if ( data_client.send( (char *)(& ctrls), length, 0 ) != length ) {
-       SG_LOG( SG_IO, SG_ALERT, "Error writing data." );
+       SG_LOG( SG_IO, SG_DEBUG, "Error writing data." );
+    } else {
+       SG_LOG( SG_IO, SG_DEBUG, "wrote control data." );
     }
 
     // Read next set of FDM data (blocking enabled to maintain 'sync')
     length = sizeof(fdm);
-    if ( (result = data_server.recv( (char *)(& fdm), length, 0)) >= 0 ) {
+    while ( (result = data_server.recv( (char *)(& fdm), length, 0)) >= 0 ) {
        SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
        net2global( &fdm );
     }