]> git.mxchange.org Git - flightgear.git/blob - src/FDM/ExternalNet/ExternalNet.cxx
Mathias Fröhlich:
[flightgear.git] / src / FDM / ExternalNet / ExternalNet.cxx
1 // ExternalNet.hxx -- an net interface to an external flight dynamics model
2 //
3 // Written by Curtis Olson, started November 2001.
4 //
5 // Copyright (C) 2001  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/io/lowlevel.hxx> // endian tests
29
30 #include <Main/fg_props.hxx>
31 #include <Network/native_ctrls.hxx>
32 #include <Network/native_fdm.hxx>
33
34 #include "ExternalNet.hxx"
35
36
37 FGExternalNet::FGExternalNet( double dt, string host, int dop, int dip, int cp )
38 {
39 //     set_delta_t( dt );
40
41     valid = true;
42
43     data_in_port = dip;
44     data_out_port = dop;
45     cmd_port = cp;
46     fdm_host = host;
47
48     /////////////////////////////////////////////////////////
49     // Setup client udp connection (sends data to remote fdm)
50
51     if ( ! data_client.open( false ) ) {
52         SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client data channel" );
53         valid = false;
54     }
55
56     // fire and forget
57     data_client.setBlocking( false );
58
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);
61         valid = false;
62     }
63
64     /////////////////////////////////////////////////////////
65     // Setup server udp connection (for receiving data)
66
67     if ( ! data_server.open( false ) ) {
68         SG_LOG( SG_FLIGHT, SG_ALERT, "Error opening client server channel" );
69         valid = false;
70     }
71
72     // disable blocking
73     data_server.setBlocking( false );
74
75     // allowed to read from a broadcast addr
76     // data_server.setBroadcast( true );
77
78     // if we bind to fdm_host = "" then we accept messages from
79     // anyone.
80     if ( data_server.bind( "", data_in_port ) == -1 ) {
81         printf("error binding to port %d\n", data_in_port);
82         valid = false;
83     }
84 }
85
86
87 FGExternalNet::~FGExternalNet() {
88     data_client.close();
89     data_server.close();
90 }
91
92
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;
97
98     // Explicitly call the superclass's
99     // init method first.
100     common_init();
101
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" );
108
109     char cmd[256];
110
111     HTTPClient *http;
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);
115     delete http;
116
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);
120     delete http;
121
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);
125     delete http;
126
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);
130     delete http;
131
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);
135     delete http;
136
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);
140     delete http;
141
142     SG_LOG( SG_IO, SG_INFO, "before sending reset command." );
143
144     if( fgGetBool("/sim/presets/onground") ) {
145       sprintf( cmd, "/reset?value=ground" );
146     } else {
147       sprintf( cmd, "/reset?value=air" );
148     }
149     http = new HTTPClient( fdm_host.c_str(), cmd_port, cmd );
150     while ( !http->isDone(1000000) ) http->poll(0);
151     delete http;
152
153     SG_LOG( SG_IO, SG_INFO, "Remote FDM init() finished." );
154 }
155
156
157 // Run an iteration of the EOM.
158 void FGExternalNet::update( double dt ) {
159     int length;
160     int result;
161
162     if (is_suspended())
163       return;
164
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." );
170     } else {
171         SG_LOG( SG_IO, SG_DEBUG, "wrote control data." );
172     }
173
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 );
179     }
180 }