1 // ExternalNet.hxx -- an net interface to an external flight dynamics model
3 // Written by Curtis Olson, started November 2001.
5 // Copyright (C) 2001 Curtis L. Olson - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/io/lowlevel.hxx> // endian tests
30 #include <Main/fg_props.hxx>
31 #include <Network/native_ctrls.hxx>
32 #include <Network/native_fdm.hxx>
34 #include "ExternalNet.hxx"
37 FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
48 /////////////////////////////////////////////////////////
49 // Setup client udp connection (sends data to remote fdm)
51 if ( ! data_client.open( false ) ) {
52 SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client data channel" );
57 data_client.setBlocking( false );
59 if ( data_client.connect( fdm_host.c_str(), data_out_port ) == -1 ) {
60 printf("error connecting to %s:%d\n", fdm_host.c_str(), data_out_port);
64 /////////////////////////////////////////////////////////
65 // Setup server udp connection (for receiving data)
67 if ( ! data_server.open( false ) ) {
68 SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client server channel" );
73 data_server.setBlocking( false );
75 // allowed to read from a broadcast addr
76 // data_server.setBroadcast( true );
78 // if we bind to fdm_host = "" then we accept messages from
80 if ( data_server.bind( "", data_in_port ) == -1 ) {
81 printf("error binding to port %d\n", data_in_port);
87 FGExternalNet::~FGExternalNet() {
93 // Initialize the ExternalNet flight model, dt is the time increment
94 // for each subsequent iteration through the EOM
95 void FGExternalNet::init() {
96 // cout << "FGExternalNet::init()" << endl;
98 // Explicitly call the superclass's
102 double lon = fgGetDouble( "/sim/presets/longitude-deg" );
103 double lat = fgGetDouble( "/sim/presets/latitude-deg" );
104 double alt = fgGetDouble( "/sim/presets/altitude-ft" );
105 double ground = get_Runway_altitude_m();
106 double heading = fgGetDouble("/sim/presets/heading-deg");
107 double speed = fgGetDouble( "/sim/presets/airspeed-kt" );
112 sprintf( cmd, "/longitude-deg?value=%.8f", lon );
113 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
114 while ( !http->isDone(1000000) ) http->poll(0);
117 sprintf( cmd, "/latitude-deg?value=%.8f", lat );
118 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
119 while ( !http->isDone(1000000) ) http->poll(0);
122 sprintf( cmd, "/altitude-ft?value=%.8f", alt );
123 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
124 while ( !http->isDone(1000000) ) http->poll(0);
127 sprintf( cmd, "/ground-m?value=%.8f", ground );
128 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
129 while ( !http->isDone(1000000) ) http->poll(0);
132 sprintf( cmd, "/speed-kts?value=%.8f", speed );
133 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
134 while ( !http->isDone(1000000) ) http->poll(0);
137 sprintf( cmd, "/heading-deg?value=%.8f", heading );
138 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
139 while ( !http->isDone(1000000) ) http->poll(0);
142 SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
144 if( fgGetBool("/sim/presets/onground") ) {
145 sprintf( cmd, "/reset?value=ground" );
147 sprintf( cmd, "/reset?value=air" );
149 http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
150 while ( !http->isDone(1000000) ) http->poll(0);
153 SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
157 // Run an iteration of the EOM.
158 void FGExternalNet::update( double dt ) {
165 // Send control positions to remote fdm
166 length = sizeof(ctrls);
167 FGProps2NetCtrls( &ctrls, true, true );
168 if ( data_client.send( (char *)(& ctrls), length, 0 ) != length ) {
169 SG_LOG( SG_IO, SG_DEBUG, "Error writing data." );
171 SG_LOG( SG_IO, SG_DEBUG, "wrote control data." );
174 // Read next set of FDM data (blocking enabled to maintain 'sync')
175 length = sizeof(fdm);
176 while ( (result = data_server.recv( (char *)(& fdm), length, 0)) >= 0 ) {
177 SG_LOG( SG_IO, SG_DEBUG, "Success reading data." );
178 FGNetFDM2Props( &fdm );