8 # include <netinet/in.h> // htonl() ntohl()
16 #include <simgear/io/lowlevel.hxx> // endian tests
17 #include <simgear/io/raw_socket.hxx>
18 #include <simgear/timing/timestamp.hxx>
20 #include <Network/net_ctrls.hxx>
21 #include <Network/net_fdm.hxx>
32 static simgear::Socket fdm_sock, ctrls_sock;
38 static int fdm_port = 5505;
39 static int ctrls_port = 5506;
42 static string file = "";
44 // Master time counter
45 float sim_time = 0.0f;
48 SGTimeStamp last_time_stamp;
49 SGTimeStamp current_time_stamp;
54 // The function htond is defined this way due to the way some
55 // processors and OSes treat floating point values. Some will raise
56 // an exception whenever a "bad" floating point value is loaded into a
57 // floating point register. Solaris is notorious for this, but then
58 // so is LynxOS on the PowerPC. By translating the data in place,
59 // there is no need to load a FP register with the "corruped" floating
60 // point value. By doing the BIG_ENDIAN test, I can optimize the
61 // routine for big-endian processors so it can be as efficient as
63 static void htond (double &x)
65 if ( sgIsLittleEndian() ) {
69 Double_Overlay = (int *) &x;
70 Holding_Buffer = Double_Overlay [0];
72 Double_Overlay [0] = htonl (Double_Overlay [1]);
73 Double_Overlay [1] = htonl (Holding_Buffer);
80 static void htonf (float &x)
82 if ( sgIsLittleEndian() ) {
86 Float_Overlay = (int *) &x;
87 Holding_Buffer = Float_Overlay [0];
89 Float_Overlay [0] = htonl (Holding_Buffer);
96 static void gps2fg( const GPSPoint p, FGNetFDM *fdm, FGNetCtrls *ctrls )
100 static double last_psi;
101 static double last_alt;
102 static double phi_filter = 0.0;
103 static double theta_filter = 0.0;
106 if ( phi_filter != phi_filter ) {
109 if ( theta_filter != theta_filter ) {
113 // Version sanity checking
114 fdm->version = FG_NET_FDM_VERSION;
117 fdm->longitude = p.lon_deg * SGD_DEGREES_TO_RADIANS;
118 fdm->latitude = p.lat_deg * SGD_DEGREES_TO_RADIANS;
119 fdm->altitude = p.altitude_msl;
121 fdm->psi = p.course_true; // heading
123 double diff = p.course_true - last_psi;
124 if ( diff < -SGD_PI ) { diff += 2.0*SGD_PI; }
125 if ( diff > SGD_PI ) { diff -= 2.0*SGD_PI; }
126 double phi = diff * 100.0;
127 if ( phi > 0.5*SGD_PI ) { phi = 0.5*SGD_PI; }
128 if ( phi < -0.5*SGD_PI ) { phi = -0.5*SGD_PI; }
129 phi_filter = 0.99*phi_filter + 0.01*phi;
130 fdm->phi = phi_filter;
131 last_psi = p.course_true;
132 // cout << p.course_true << endl;
134 diff = p.altitude_msl - last_alt;
135 if ( diff < -SGD_PI ) { diff += 2.0*SGD_PI; }
136 if ( diff > SGD_PI ) { diff -= 2.0*SGD_PI; }
137 double theta = diff * 2.0;
138 if ( theta > 0.5*SGD_PI ) { theta = 0.5*SGD_PI; }
139 if ( theta < -0.5*SGD_PI ) { theta = -0.5*SGD_PI; }
140 theta_filter = 0.99*theta_filter + 0.01*theta;
141 fdm->theta = theta_filter;
142 last_alt = p.altitude_msl;
147 fdm->vcas = p.speed_kts;
148 fdm->climb_rate = 0; // fps
149 // cout << "climb rate = " << aero->hdota << endl;
153 fdm->v_wind_body_north = 0.0;
154 fdm->v_wind_body_east = 0.0;
155 fdm->v_wind_body_down = 0.0;
156 fdm->stall_warning = 0.0;
158 fdm->A_X_pilot = 0.0;
159 fdm->A_Y_pilot = 0.0;
160 fdm->A_Z_pilot = 0.0 /* (should be -G) */;
163 fdm->num_engines = 1;
164 fdm->eng_state[0] = 2;
165 // cout << "state = " << fdm->eng_state[0] << endl;
166 double rpm = ((p.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0;
167 if ( rpm < 0.0 ) { rpm = 0.0; }
168 if ( rpm > 3000.0 ) { rpm = 3000.0; }
171 fdm->fuel_flow[0] = 0.0;
173 // cout << "egt = " << aero->EGT << endl;
174 fdm->oil_temp[0] = 0.0;
175 fdm->oil_px[0] = 0.0;
179 fdm->fuel_quantity[0] = 0.0;
180 fdm->fuel_quantity[1] = 0.0;
188 // the following really aren't used in this context
193 // cout << "Flap deflection = " << aero->dflap << endl;
194 fdm->left_flap = 0.0;
195 fdm->right_flap = 0.0;
197 fdm->elevator = -theta_filter * 5.0;
198 fdm->elevator_trim_tab = 0.0;
199 fdm->left_flap = 0.0;
200 fdm->right_flap = 0.0;
201 fdm->left_aileron = phi_filter * 1.5;
202 fdm->right_aileron = phi_filter * 1.5;
204 fdm->nose_wheel = 0.0;
205 fdm->speedbrake = 0.0;
208 // Convert the net buffer to network format
209 fdm->version = htonl(fdm->version);
211 htond(fdm->longitude);
212 htond(fdm->latitude);
213 htond(fdm->altitude);
222 htonf(fdm->thetadot);
225 htonf(fdm->climb_rate);
229 htonf(fdm->v_wind_body_north);
230 htonf(fdm->v_wind_body_east);
231 htonf(fdm->v_wind_body_down);
233 htonf(fdm->A_X_pilot);
234 htonf(fdm->A_Y_pilot);
235 htonf(fdm->A_Z_pilot);
237 htonf(fdm->stall_warning);
238 htonf(fdm->slip_deg);
240 for ( i = 0; i < fdm->num_engines; ++i ) {
241 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
243 htonf(fdm->fuel_flow[i]);
246 htonf(fdm->mp_osi[i]);
248 htonf(fdm->oil_temp[i]);
249 htonf(fdm->oil_px[i]);
251 fdm->num_engines = htonl(fdm->num_engines);
253 for ( i = 0; i < fdm->num_tanks; ++i ) {
254 htonf(fdm->fuel_quantity[i]);
256 fdm->num_tanks = htonl(fdm->num_tanks);
258 for ( i = 0; i < fdm->num_wheels; ++i ) {
259 fdm->wow[i] = htonl(fdm->wow[i]);
260 htonf(fdm->gear_pos[i]);
261 htonf(fdm->gear_steer[i]);
262 htonf(fdm->gear_compression[i]);
264 fdm->num_wheels = htonl(fdm->num_wheels);
266 fdm->cur_time = htonl( fdm->cur_time );
267 fdm->warp = htonl( fdm->warp );
268 htonf(fdm->visibility);
270 htonf(fdm->elevator);
271 htonf(fdm->elevator_trim_tab);
272 htonf(fdm->left_flap);
273 htonf(fdm->right_flap);
274 htonf(fdm->left_aileron);
275 htonf(fdm->right_aileron);
277 htonf(fdm->nose_wheel);
278 htonf(fdm->speedbrake);
279 htonf(fdm->spoilers);
283 static void send_data( const GPSPoint p ) {
285 // int ctrlsize = sizeof( FGNetCtrls );
286 int fdmsize = sizeof( FGNetFDM );
288 // cout << "Running main loop" << endl;
293 gps2fg( p, &fgfdm, &fgctrls );
294 len = fdm_sock.send(&fgfdm, fdmsize, 0);
298 void usage( const string &argv0 ) {
299 cout << "Usage: " << argv0 << endl;
300 cout << "\t[ --help ]" << endl;
301 cout << "\t[ --file <file_name>" << endl;
302 cout << "\t[ --hertz <hertz> ]" << endl;
303 cout << "\t[ --host <hostname> ]" << endl;
304 cout << "\t[ --broadcast ]" << endl;
305 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
306 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
310 int main( int argc, char **argv ) {
312 string out_host = "localhost";
313 bool do_broadcast = false;
315 // process command line arguments
316 for ( int i = 1; i < argc; ++i ) {
317 if ( strcmp( argv[i], "--help" ) == 0 ) {
320 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
323 hertz = atof( argv[i] );
328 } else if ( strcmp( argv[i], "--file" ) == 0 ) {
336 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
344 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
346 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
349 fdm_port = atoi( argv[i] );
354 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
357 ctrls_port = atoi( argv[i] );
368 // Load the track data
370 cout << "No track file specified" << endl;
374 cout << "Loaded " << track.size() << " records." << endl;
376 // Setup up outgoing network connections
378 simgear::Socket::initSockets(); // We must call this before any other net stuff
380 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
381 cout << "error opening fdm output socket" << endl;
384 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
385 cout << "error opening ctrls output socket" << endl;
388 cout << "open net channels" << endl;
390 fdm_sock.setBlocking( false );
391 ctrls_sock.setBlocking( false );
392 cout << "blocking false" << endl;
394 if ( do_broadcast ) {
395 fdm_sock.setBroadcast( true );
396 ctrls_sock.setBroadcast( true );
399 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
401 cout << "error connecting to outgoing fdm port: " << out_host
402 << ":" << fdm_port << endl;
405 cout << "connected outgoing fdm socket" << endl;
407 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
409 cout << "error connecting to outgoing ctrls port: " << out_host
410 << ":" << ctrls_port << endl;
413 cout << "connected outgoing ctrls socket" << endl;
415 int size = track.size();
417 double current_time = track.get_point(0).get_time();
418 cout << "Track begin time is " << current_time << endl;
419 double end_time = track.get_point(size-1).get_time();
420 cout << "Track end time is " << end_time << endl;
421 cout << "Duration = " << end_time - current_time << endl;
423 double frame_us = 1000000.0 / hertz;
424 if ( frame_us < 0.0 ) {
428 SGTimeStamp start_time;
433 p0 = p1 = track.get_point( 0 );
435 while ( current_time < end_time ) {
436 // cout << "current_time = " << current_time << " end_time = "
437 // << end_time << endl;
439 if ( current_time > p1.get_time() ) {
442 // cout << "count = " << count << endl;
443 p1 = track.get_point( count );
445 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
449 if ( fabs(p1.get_time() - p0.get_time()) < 0.0001 ) {
453 (current_time - p0.get_time()) /
454 (p1.get_time() - p0.get_time());
456 // cout << "Percent = " << percent << endl;
458 GPSPoint p = GPSInterpolate( p0, p1, percent );
459 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg << endl;
460 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg << endl;
461 cout << current_time << " " << p.lat_deg << ", " << p.lon_deg << endl;
465 // Update the elapsed time.
466 static bool first_time = true;
468 last_time_stamp.stamp();
472 current_time_stamp.stamp();
474 double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs();
475 if ( elapsed_us < (frame_us - 2000) ) {
476 double requested_us = (frame_us - elapsed_us) - 2000 ;
477 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
479 current_time_stamp.stamp();
480 while ( (current_time_stamp - last_time_stamp).toUSecs() < frame_us ) {
481 current_time_stamp.stamp();
484 current_time += (frame_us / 1000000.0);
485 last_time_stamp = current_time_stamp;
488 cout << "Processed " << count << " entries in "
489 << current_time_stamp - start_time << " seconds." << endl;