11 #include <simgear/constants.h>
12 #include <simgear/io/lowlevel.hxx> // endian tests
13 #include <simgear/io/sg_file.hxx>
14 #include <simgear/io/sg_serial.hxx>
15 #include <simgear/math/sg_geodesy.hxx>
16 #include <simgear/timing/timestamp.hxx>
18 #include <Network/net_ctrls.hxx>
19 #include <Network/net_fdm.hxx>
21 #include "MIDG-II.hxx"
30 static netSocket fdm_sock, ctrls_sock;
36 static int fdm_port = 5505;
37 static int ctrls_port = 5506;
40 static string infile = "";
41 static string serialdev = "";
42 static string outfile = "";
44 // Master time counter
45 float sim_time = 0.0f;
46 double frame_us = 0.0f;
49 SGTimeStamp last_time_stamp;
50 SGTimeStamp current_time_stamp;
53 double alt_offset = 0.0;
55 // skip initial seconds
59 // double last_lat = 0.0, last_lon = 0.0;
60 // double kts_filter = 0.0;
65 // The function htond is defined this way due to the way some
66 // processors and OSes treat floating point values. Some will raise
67 // an exception whenever a "bad" floating point value is loaded into a
68 // floating point register. Solaris is notorious for this, but then
69 // so is LynxOS on the PowerPC. By translating the data in place,
70 // there is no need to load a FP register with the "corruped" floating
71 // point value. By doing the BIG_ENDIAN test, I can optimize the
72 // routine for big-endian processors so it can be as efficient as
74 static void htond (double &x)
76 if ( sgIsLittleEndian() ) {
80 Double_Overlay = (int *) &x;
81 Holding_Buffer = Double_Overlay [0];
83 Double_Overlay [0] = htonl (Double_Overlay [1]);
84 Double_Overlay [1] = htonl (Holding_Buffer);
91 static void htonf (float &x)
93 if ( sgIsLittleEndian() ) {
97 Float_Overlay = (int *) &x;
98 Holding_Buffer = Float_Overlay [0];
100 Float_Overlay [0] = htonl (Holding_Buffer);
107 static void midg2fg( const MIDGpos pos, const MIDGatt att,
108 FGNetFDM *fdm, FGNetCtrls *ctrls )
112 // Version sanity checking
113 fdm->version = FG_NET_FDM_VERSION;
116 fdm->longitude = pos.lon_deg * SGD_DEGREES_TO_RADIANS;
117 fdm->latitude = pos.lat_deg * SGD_DEGREES_TO_RADIANS;
118 fdm->altitude = pos.altitude_msl + alt_offset;
120 fdm->psi = att.yaw_rad; // heading
121 fdm->phi = att.roll_rad; // roll
122 fdm->theta = att.pitch_rad; // pitch;
129 // double az1, az2, dist;
130 // geo_inverse_wgs_84( pos.altitude_msl, last_lat, last_lon,
131 // pos.lat_deg, pos.lon_deg, &az1, &az2, &dist );
132 // double v_ms = dist / (frame_us / 1000000);
133 // double v_kts = v_ms * SG_METER_TO_NM * 3600;
134 // kts_filter = (0.99 * kts_filter) + (0.01 * v_kts);
135 fdm->vcas = pos.speed_kts;
136 // last_lat = pos.lat_deg;
137 // last_lon = pos.lon_deg;
138 // cout << "kts_filter = " << kts_filter << " vel = " << pos.speed_kts << endl;
140 fdm->climb_rate = 0; // fps
141 // cout << "climb rate = " << aero->hdota << endl;
145 fdm->v_wind_body_north = 0.0;
146 fdm->v_wind_body_east = 0.0;
147 fdm->v_wind_body_down = 0.0;
148 fdm->stall_warning = 0.0;
150 fdm->A_X_pilot = 0.0;
151 fdm->A_Y_pilot = 0.0;
152 fdm->A_Z_pilot = 0.0 /* (should be -G) */;
155 fdm->num_engines = 1;
156 fdm->eng_state[0] = 2;
157 // cout << "state = " << fdm->eng_state[0] << endl;
158 double rpm = ((pos.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0;
159 if ( rpm < 0.0 ) { rpm = 0.0; }
160 if ( rpm > 3000.0 ) { rpm = 3000.0; }
163 fdm->fuel_flow[0] = 0.0;
165 // cout << "egt = " << aero->EGT << endl;
166 fdm->oil_temp[0] = 0.0;
167 fdm->oil_px[0] = 0.0;
171 fdm->fuel_quantity[0] = 0.0;
172 fdm->fuel_quantity[1] = 0.0;
180 // the following really aren't used in this context
185 // cout << "Flap deflection = " << aero->dflap << endl;
186 fdm->left_flap = 0.0;
187 fdm->right_flap = 0.0;
189 fdm->elevator = -fdm->theta * 1.0;
190 fdm->elevator_trim_tab = 0.0;
191 fdm->left_flap = 0.0;
192 fdm->right_flap = 0.0;
193 fdm->left_aileron = fdm->phi * 1.0;
194 fdm->right_aileron = -fdm->phi * 1.0;
196 fdm->nose_wheel = 0.0;
197 fdm->speedbrake = 0.0;
200 // Convert the net buffer to network format
201 fdm->version = htonl(fdm->version);
203 htond(fdm->longitude);
204 htond(fdm->latitude);
205 htond(fdm->altitude);
214 htonf(fdm->thetadot);
217 htonf(fdm->climb_rate);
221 htonf(fdm->v_wind_body_north);
222 htonf(fdm->v_wind_body_east);
223 htonf(fdm->v_wind_body_down);
225 htonf(fdm->A_X_pilot);
226 htonf(fdm->A_Y_pilot);
227 htonf(fdm->A_Z_pilot);
229 htonf(fdm->stall_warning);
230 htonf(fdm->slip_deg);
232 for ( i = 0; i < fdm->num_engines; ++i ) {
233 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
235 htonf(fdm->fuel_flow[i]);
238 htonf(fdm->mp_osi[i]);
240 htonf(fdm->oil_temp[i]);
241 htonf(fdm->oil_px[i]);
243 fdm->num_engines = htonl(fdm->num_engines);
245 for ( i = 0; i < fdm->num_tanks; ++i ) {
246 htonf(fdm->fuel_quantity[i]);
248 fdm->num_tanks = htonl(fdm->num_tanks);
250 for ( i = 0; i < fdm->num_wheels; ++i ) {
251 fdm->wow[i] = htonl(fdm->wow[i]);
252 htonf(fdm->gear_pos[i]);
253 htonf(fdm->gear_steer[i]);
254 htonf(fdm->gear_compression[i]);
256 fdm->num_wheels = htonl(fdm->num_wheels);
258 fdm->cur_time = htonl( fdm->cur_time );
259 fdm->warp = htonl( fdm->warp );
260 htonf(fdm->visibility);
262 htonf(fdm->elevator);
263 htonf(fdm->elevator_trim_tab);
264 htonf(fdm->left_flap);
265 htonf(fdm->right_flap);
266 htonf(fdm->left_aileron);
267 htonf(fdm->right_aileron);
269 htonf(fdm->nose_wheel);
270 htonf(fdm->speedbrake);
271 htonf(fdm->spoilers);
275 static void send_data( const MIDGpos pos, const MIDGatt att ) {
277 int fdmsize = sizeof( FGNetFDM );
279 // cout << "Running main loop" << endl;
284 midg2fg( pos, att, &fgfdm, &fgctrls );
285 len = fdm_sock.send(&fgfdm, fdmsize, 0);
289 void usage( const string &argv0 ) {
290 cout << "Usage: " << argv0 << endl;
291 cout << "\t[ --help ]" << endl;
292 cout << "\t[ --infile <infile_name>" << endl;
293 cout << "\t[ --serial <dev_name>" << endl;
294 cout << "\t[ --outfile <outfile_name> (capture the data to a file)" << endl;
295 cout << "\t[ --hertz <hertz> ]" << endl;
296 cout << "\t[ --host <hostname> ]" << endl;
297 cout << "\t[ --broadcast ]" << endl;
298 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
299 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
300 cout << "\t[ --altitude-offset <meters> ]" << endl;
301 cout << "\t[ --skip-seconds <seconds> ]" << endl;
305 int main( int argc, char **argv ) {
307 string out_host = "localhost";
308 bool do_broadcast = false;
310 // process command line arguments
311 for ( int i = 1; i < argc; ++i ) {
312 if ( strcmp( argv[i], "--help" ) == 0 ) {
315 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
318 hertz = atof( argv[i] );
323 } else if ( strcmp( argv[i], "--infile" ) == 0 ) {
331 } else if ( strcmp( argv[i], "--outfile" ) == 0 ) {
339 } else if ( strcmp( argv[i], "--serial" ) == 0 ) {
347 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
355 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
357 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
360 fdm_port = atoi( argv[i] );
365 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
368 ctrls_port = atoi( argv[i] );
373 } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
376 alt_offset = atof( argv[i] );
381 } else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) {
384 skip = atof( argv[i] );
395 // Setup up outgoing network connections
397 netInit( &argc,argv ); // We must call this before any other net stuff
399 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
400 cout << "error opening fdm output socket" << endl;
403 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
404 cout << "error opening ctrls output socket" << endl;
407 cout << "open net channels" << endl;
409 fdm_sock.setBlocking( false );
410 ctrls_sock.setBlocking( false );
411 cout << "blocking false" << endl;
413 if ( do_broadcast ) {
414 fdm_sock.setBroadcast( true );
415 ctrls_sock.setBroadcast( true );
418 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
420 cout << "error connecting to outgoing fdm port: " << out_host
421 << ":" << fdm_port << endl;
424 cout << "connected outgoing fdm socket" << endl;
426 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
428 cout << "error connecting to outgoing ctrls port: " << out_host
429 << ":" << ctrls_port << endl;
432 cout << "connected outgoing ctrls socket" << endl;
434 if ( infile.length() ) {
435 // Load data from a track data
436 track.load( infile );
437 cout << "Loaded " << track.pos_size() << " position records." << endl;
438 cout << "Loaded " << track.att_size() << " attitude records." << endl;
440 int size = track.pos_size();
442 double current_time = track.get_pospt(0).get_seconds();
443 cout << "Track begin time is " << current_time << endl;
444 double end_time = track.get_pospt(size-1).get_seconds();
445 cout << "Track end time is " << end_time << endl;
446 cout << "Duration = " << end_time - current_time << endl;
448 // advance skip seconds forward
449 current_time += skip;
451 frame_us = 1000000.0 / hertz;
452 if ( frame_us < 0.0 ) {
456 SGTimeStamp start_time;
462 pos0 = pos1 = track.get_pospt( 0 );
465 att0 = att1 = track.get_attpt( 0 );
467 while ( current_time < end_time ) {
468 // cout << "current_time = " << current_time << " end_time = "
469 // << end_time << endl;
471 // Advance position pointer
472 while ( current_time > pos1.get_seconds()
473 && pos_count < track.pos_size() )
477 // cout << "count = " << count << endl;
478 pos1 = track.get_pospt( pos_count );
480 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
483 // Advance attitude pointer
484 while ( current_time > att1.get_seconds()
485 && att_count < track.att_size() )
489 // cout << "count = " << count << endl;
490 att1 = track.get_attpt( att_count );
492 // cout << "pos0 = " << pos0.get_seconds()
493 // << " pos1 = " << pos1.get_seconds() << endl;
496 if ( fabs(pos1.get_seconds() - pos0.get_seconds()) < 0.00001 ) {
500 (current_time - pos0.get_seconds()) /
501 (pos1.get_seconds() - pos0.get_seconds());
503 // cout << "Percent = " << percent << endl;
505 if ( fabs(att1.get_seconds() - att0.get_seconds()) < 0.00001 ) {
509 (current_time - att0.get_seconds()) /
510 (att1.get_seconds() - att0.get_seconds());
512 // cout << "Percent = " << percent << endl;
514 MIDGpos pos = MIDGInterpPos( pos0, pos1, pos_percent );
515 MIDGatt att = MIDGInterpAtt( att0, att1, att_percent );
516 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg
518 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
520 // cout << (double)current_time << " " << pos.lat_deg << ", "
521 // << pos.lon_deg << " " << att.yaw_deg << endl;
522 if ( pos.lat_deg > -500 ) {
523 printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
525 pos.lat_deg, pos.lon_deg, pos.altitude_msl,
526 att.yaw_rad * 180.0 / SG_PI,
527 att.pitch_rad * 180.0 / SG_PI,
528 att.roll_rad * 180.0 / SG_PI );
531 send_data( pos, att );
533 // Update the elapsed time.
534 static bool first_time = true;
536 last_time_stamp.stamp();
540 current_time_stamp.stamp();
542 double elapsed_us = current_time_stamp - last_time_stamp;
543 if ( elapsed_us < (frame_us - 2000) ) {
544 double requested_us = (frame_us - elapsed_us) - 2000 ;
545 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
547 current_time_stamp.stamp();
548 while ( current_time_stamp - last_time_stamp < frame_us ) {
549 current_time_stamp.stamp();
552 current_time += (frame_us / 1000000.0);
553 last_time_stamp = current_time_stamp;
556 cout << "Processed " << pos_count << " entries in "
557 << (current_time_stamp - start_time) / 1000000 << " seconds."
559 } else if ( serialdev.length() ) {
560 // process incoming data from the serial port
563 double current_time = 0.0;
568 uint32_t pos_time = 1;
569 uint32_t att_time = 1;
571 // open the serial port device
572 SGSerialPort input( serialdev, 115200 );
573 if ( !input.is_enabled() ) {
574 cout << "Cannot open: " << serialdev << endl;
578 // open up the data log file if requested
579 if ( !outfile.length() ) {
580 cout << "no --outfile <name> specified, cannot capture data!"
584 SGFile output( outfile );
585 if ( !output.open( SG_IO_OUT ) ) {
586 cout << "Cannot open: " << outfile << endl;
590 while ( input.is_enabled() ) {
591 // cout << "looking for next message ..." << endl;
592 int id = track.next_message( &input, &output, &pos, &att );
593 cout << "message id = " << id << endl;
597 if ( att.get_msec() > att_time ) {
598 att_time = att.get_msec();
599 current_time = att_time;
601 cout << "oops att back in time" << endl;
603 } else if ( id == 12 ) {
604 if ( pos.get_msec() > pos_time ) {
605 pos_time = pos.get_msec();
606 current_time = pos_time;
608 cout << "oops pos back in time" << endl;
612 if ( pos.lat_deg > -500 ) {
613 // printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
615 // pos.lat_deg, pos.lon_deg, pos.altitude_msl,
616 // att.yaw_rad * 180.0 / SG_PI,
617 // att.pitch_rad * 180.0 / SG_PI,
618 // att.roll_rad * 180.0 / SG_PI );
621 send_data( pos, att );