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>
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 ugear2fg( gps *gpspacket, imu *imupacket, nav *navpacket,
108 servo *servopacket, health *healthpacket,
109 FGNetFDM *fdm, FGNetCtrls *ctrls )
113 // Version sanity checking
114 fdm->version = FG_NET_FDM_VERSION;
117 fdm->longitude = gpspacket->lon * SG_DEGREES_TO_RADIANS;
118 fdm->latitude = gpspacket->lat * SG_DEGREES_TO_RADIANS;
119 fdm->altitude = gpspacket->alt;
121 fdm->psi = imupacket->psi; // heading
122 fdm->phi = imupacket->phi; // roll
123 fdm->theta = imupacket->the; // pitch;
130 // double az1, az2, dist;
131 // geo_inverse_wgs_84( pos.altitude_msl, last_lat, last_lon,
132 // pos.lat_deg, pos.lon_deg, &az1, &az2, &dist );
133 // double v_ms = dist / (frame_us / 1000000);
134 // double v_kts = v_ms * SG_METER_TO_NM * 3600;
135 // kts_filter = (0.99 * kts_filter) + (0.01 * v_kts);
136 double vn = gpspacket->vn;
137 double ve = gpspacket->ve;
138 double vd = gpspacket->vd;
140 fdm->vcas = sqrt( vn*vn + ve*ve + vd*vd );
141 // last_lat = pos.lat_deg;
142 // last_lon = pos.lon_deg;
143 // cout << "kts_filter = " << kts_filter << " vel = " << pos.speed_kts << endl;
145 fdm->climb_rate = 0; // fps
146 // cout << "climb rate = " << aero->hdota << endl;
150 fdm->v_wind_body_north = 0.0;
151 fdm->v_wind_body_east = 0.0;
152 fdm->v_wind_body_down = 0.0;
153 fdm->stall_warning = 0.0;
155 fdm->A_X_pilot = 0.0;
156 fdm->A_Y_pilot = 0.0;
157 fdm->A_Z_pilot = 0.0 /* (should be -G) */;
160 fdm->num_engines = 1;
161 fdm->eng_state[0] = 2;
162 // cout << "state = " << fdm->eng_state[0] << endl;
163 double rpm = ((fdm->vcas - 15.0) / 65.0) * 2000.0 + 500.0;
164 if ( rpm < 0.0 ) { rpm = 0.0; }
165 if ( rpm > 3000.0 ) { rpm = 3000.0; }
168 fdm->fuel_flow[0] = 0.0;
170 // cout << "egt = " << aero->EGT << endl;
171 fdm->oil_temp[0] = 0.0;
172 fdm->oil_px[0] = 0.0;
176 fdm->fuel_quantity[0] = 0.0;
177 fdm->fuel_quantity[1] = 0.0;
185 // the following really aren't used in this context
190 // cout << "Flap deflection = " << aero->dflap << endl;
191 fdm->left_flap = 0.0;
192 fdm->right_flap = 0.0;
194 fdm->elevator = -fdm->theta * 1.0;
195 fdm->elevator_trim_tab = 0.0;
196 fdm->left_flap = 0.0;
197 fdm->right_flap = 0.0;
198 fdm->left_aileron = fdm->phi * 1.0;
199 fdm->right_aileron = -fdm->phi * 1.0;
201 fdm->nose_wheel = 0.0;
202 fdm->speedbrake = 0.0;
205 // Convert the net buffer to network format
206 fdm->version = htonl(fdm->version);
208 htond(fdm->longitude);
209 htond(fdm->latitude);
210 htond(fdm->altitude);
219 htonf(fdm->thetadot);
222 htonf(fdm->climb_rate);
226 htonf(fdm->v_wind_body_north);
227 htonf(fdm->v_wind_body_east);
228 htonf(fdm->v_wind_body_down);
230 htonf(fdm->A_X_pilot);
231 htonf(fdm->A_Y_pilot);
232 htonf(fdm->A_Z_pilot);
234 htonf(fdm->stall_warning);
235 htonf(fdm->slip_deg);
237 for ( i = 0; i < fdm->num_engines; ++i ) {
238 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
240 htonf(fdm->fuel_flow[i]);
243 htonf(fdm->mp_osi[i]);
245 htonf(fdm->oil_temp[i]);
246 htonf(fdm->oil_px[i]);
248 fdm->num_engines = htonl(fdm->num_engines);
250 for ( i = 0; i < fdm->num_tanks; ++i ) {
251 htonf(fdm->fuel_quantity[i]);
253 fdm->num_tanks = htonl(fdm->num_tanks);
255 for ( i = 0; i < fdm->num_wheels; ++i ) {
256 fdm->wow[i] = htonl(fdm->wow[i]);
257 htonf(fdm->gear_pos[i]);
258 htonf(fdm->gear_steer[i]);
259 htonf(fdm->gear_compression[i]);
261 fdm->num_wheels = htonl(fdm->num_wheels);
263 fdm->cur_time = htonl( fdm->cur_time );
264 fdm->warp = htonl( fdm->warp );
265 htonf(fdm->visibility);
267 htonf(fdm->elevator);
268 htonf(fdm->elevator_trim_tab);
269 htonf(fdm->left_flap);
270 htonf(fdm->right_flap);
271 htonf(fdm->left_aileron);
272 htonf(fdm->right_aileron);
274 htonf(fdm->nose_wheel);
275 htonf(fdm->speedbrake);
276 htonf(fdm->spoilers);
280 static void send_data( gps *gpspacket, imu *imupacket, nav *navpacket,
281 servo *servopacket, health *healthpacket ) {
283 int fdmsize = sizeof( FGNetFDM );
285 // cout << "Running main loop" << endl;
290 ugear2fg( gpspacket, imupacket, navpacket, servopacket, healthpacket,
292 len = fdm_sock.send(&fgfdm, fdmsize, 0);
296 void usage( const string &argv0 ) {
297 cout << "Usage: " << argv0 << endl;
298 cout << "\t[ --help ]" << endl;
299 cout << "\t[ --infile <infile_name>" << endl;
300 cout << "\t[ --serial <dev_name>" << endl;
301 cout << "\t[ --outfile <outfile_name> (capture the data to a file)" << 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;
307 cout << "\t[ --altitude-offset <meters> ]" << endl;
308 cout << "\t[ --skip-seconds <seconds> ]" << endl;
312 int main( int argc, char **argv ) {
314 string out_host = "localhost";
315 bool do_broadcast = false;
317 // process command line arguments
318 for ( int i = 1; i < argc; ++i ) {
319 if ( strcmp( argv[i], "--help" ) == 0 ) {
322 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
325 hertz = atof( argv[i] );
330 } else if ( strcmp( argv[i], "--infile" ) == 0 ) {
338 } else if ( strcmp( argv[i], "--outfile" ) == 0 ) {
346 } else if ( strcmp( argv[i], "--serial" ) == 0 ) {
354 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
362 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
364 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
367 fdm_port = atoi( argv[i] );
372 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
375 ctrls_port = atoi( argv[i] );
380 } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
383 alt_offset = atof( argv[i] );
388 } else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) {
391 skip = atof( argv[i] );
402 // Setup up outgoing network connections
404 netInit( &argc,argv ); // We must call this before any other net stuff
406 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
407 cout << "error opening fdm output socket" << endl;
410 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
411 cout << "error opening ctrls output socket" << endl;
414 cout << "open net channels" << endl;
416 fdm_sock.setBlocking( false );
417 ctrls_sock.setBlocking( false );
418 cout << "blocking false" << endl;
420 if ( do_broadcast ) {
421 fdm_sock.setBroadcast( true );
422 ctrls_sock.setBroadcast( true );
425 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
427 cout << "error connecting to outgoing fdm port: " << out_host
428 << ":" << fdm_port << endl;
431 cout << "connected outgoing fdm socket" << endl;
433 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
435 cout << "error connecting to outgoing ctrls port: " << out_host
436 << ":" << ctrls_port << endl;
439 cout << "connected outgoing ctrls socket" << endl;
441 if ( infile.length() ) {
442 // Load data from a track data
443 track.load( infile );
444 cout << "Loaded " << track.gps_size() << " gps records." << endl;
445 cout << "Loaded " << track.imu_size() << " imu records." << endl;
446 cout << "Loaded " << track.nav_size() << " nav records." << endl;
447 cout << "Loaded " << track.servo_size() << " servo records." << endl;
448 cout << "Loaded " << track.health_size() << " health records." << endl;
450 int size = track.imu_size();
452 double current_time = track.get_imupt(0).time;
453 cout << "Track begin time is " << current_time << endl;
454 double end_time = track.get_imupt(size-1).time;
455 cout << "Track end time is " << end_time << endl;
456 cout << "Duration = " << end_time - current_time << endl;
458 // advance skip seconds forward
459 current_time += skip;
461 frame_us = 1000000.0 / hertz;
462 if ( frame_us < 0.0 ) {
466 SGTimeStamp start_time;
472 int health_count = 0;
475 gps0 = gps1 = track.get_gpspt( 0 );
478 imu0 = imu1 = track.get_imupt( 0 );
481 nav0 = nav1 = track.get_navpt( 0 );
483 servo servo0, servo1;
484 servo0 = servo1 = track.get_servopt( 0 );
486 health health0, health1;
487 health0 = health1 = track.get_healthpt( 0 );
489 while ( current_time < end_time ) {
490 // cout << "current_time = " << current_time << " end_time = "
491 // << end_time << endl;
493 // Advance gps pointer
494 while ( current_time > gps1.time
495 && gps_count < track.gps_size() )
499 // cout << "count = " << count << endl;
500 gps1 = track.get_gpspt( gps_count );
502 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
505 // Advance imu pointer
506 while ( current_time > imu1.time
507 && imu_count < track.imu_size() )
511 // cout << "count = " << count << endl;
512 imu1 = track.get_imupt( imu_count );
514 // cout << "pos0 = " << pos0.get_seconds()
515 // << " pos1 = " << pos1.get_seconds() << endl;
517 // Advance nav pointer
518 while ( current_time > nav1.time
519 && nav_count < track.nav_size() )
523 // cout << "count = " << count << endl;
524 nav1 = track.get_navpt( nav_count );
526 // cout << "pos0 = " << pos0.get_seconds()
527 // << " pos1 = " << pos1.get_seconds() << endl;
529 // Advance servo pointer
530 while ( current_time > servo1.time
531 && servo_count < track.servo_size() )
535 // cout << "count = " << count << endl;
536 servo1 = track.get_servopt( servo_count );
538 // cout << "pos0 = " << pos0.get_seconds()
539 // << " pos1 = " << pos1.get_seconds() << endl;
541 // Advance health pointer
542 while ( current_time > health1.time
543 && health_count < track.health_size() )
547 // cout << "count = " << count << endl;
548 health1 = track.get_healthpt( health_count );
550 // cout << "pos0 = " << pos0.get_seconds()
551 // << " pos1 = " << pos1.get_seconds() << endl;
554 if ( fabs(gps1.time - gps0.time) < 0.00001 ) {
558 (current_time - gps0.time) /
559 (gps1.time - gps0.time);
561 // cout << "Percent = " << percent << endl;
564 if ( fabs(imu1.time - imu0.time) < 0.00001 ) {
568 (current_time - imu0.time) /
569 (imu1.time - imu0.time);
571 // cout << "Percent = " << percent << endl;
574 if ( fabs(nav1.time - nav0.time) < 0.00001 ) {
578 (current_time - nav0.time) /
579 (nav1.time - nav0.time);
581 // cout << "Percent = " << percent << endl;
583 double servo_percent;
584 if ( fabs(servo1.time - servo0.time) < 0.00001 ) {
588 (current_time - servo0.time) /
589 (servo1.time - servo0.time);
591 // cout << "Percent = " << percent << endl;
593 double health_percent;
594 if ( fabs(health1.time - health0.time) < 0.00001 ) {
595 health_percent = 0.0;
598 (current_time - health0.time) /
599 (health1.time - health0.time);
601 // cout << "Percent = " << percent << endl;
603 gps gpspacket = UGEARInterpGPS( gps0, gps1, gps_percent );
604 imu imupacket = UGEARInterpIMU( imu0, imu1, imu_percent );
605 nav navpacket = UGEARInterpNAV( nav0, nav1, nav_percent );
606 servo servopacket = UGEARInterpSERVO( servo0, servo1,
608 health healthpacket = UGEARInterpHEALTH( health0, health1,
611 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg
613 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
615 // cout << (double)current_time << " " << pos.lat_deg << ", "
616 // << pos.lon_deg << " " << att.yaw_deg << endl;
617 if ( gpspacket.lat > -500 ) {
618 printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
620 gpspacket.lat, gpspacket.lon, gpspacket.alt,
621 imupacket.psi, imupacket.the, imupacket.phi );
624 send_data( &gpspacket, &imupacket, &navpacket, &servopacket,
627 // Update the elapsed time.
628 static bool first_time = true;
630 last_time_stamp.stamp();
634 current_time_stamp.stamp();
636 double elapsed_us = current_time_stamp - last_time_stamp;
637 if ( elapsed_us < (frame_us - 2000) ) {
638 double requested_us = (frame_us - elapsed_us) - 2000 ;
639 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
641 current_time_stamp.stamp();
642 while ( current_time_stamp - last_time_stamp < frame_us ) {
643 current_time_stamp.stamp();
646 current_time += (frame_us / 1000000.0);
647 last_time_stamp = current_time_stamp;
650 cout << "Processed " << imu_count << " entries in "
651 << (current_time_stamp - start_time) / 1000000 << " seconds."
653 } else if ( serialdev.length() ) {
654 // process incoming data from the serial port
657 double current_time = 0.0;
668 double servo_time = 0;
669 double health_time = 0;
671 // open the serial port device
672 SGSerialPort input( serialdev, 115200 );
673 if ( !input.is_enabled() ) {
674 cout << "Cannot open: " << serialdev << endl;
678 // open up the data log file if requested
679 if ( !outfile.length() ) {
680 cout << "no --outfile <name> specified, cannot capture data!"
684 SGFile output( outfile );
685 if ( !output.open( SG_IO_OUT ) ) {
686 cout << "Cannot open: " << outfile << endl;
690 while ( input.is_enabled() ) {
691 // cout << "looking for next message ..." << endl;
692 int id = track.next_message( &input, &output, &gpspacket,
693 &imupacket, &navpacket, &servopacket,
695 cout << "message id = " << id << endl;
698 if ( id == GPS_PACKET ) {
699 if ( gpspacket.time > gps_time ) {
700 gps_time = gpspacket.time;
701 current_time = gps_time;
703 cout << "oops gps back in time" << endl;
705 } else if ( id == IMU_PACKET ) {
706 if ( imupacket.time > imu_time ) {
707 imu_time = imupacket.time;
708 current_time = imu_time;
710 cout << "oops imu back in time" << endl;
712 } else if ( id == NAV_PACKET ) {
713 if ( navpacket.time > nav_time ) {
714 nav_time = navpacket.time;
715 current_time = nav_time;
717 cout << "oops nav back in time" << endl;
719 } else if ( id == SERVO_PACKET ) {
720 if ( servopacket.time > servo_time ) {
721 servo_time = servopacket.time;
722 current_time = servo_time;
724 cout << "oops servo back in time" << endl;
726 } else if ( id == HEALTH_PACKET ) {
727 if ( healthpacket.time > health_time ) {
728 health_time = healthpacket.time;
729 current_time = health_time;
731 cout << "oops health back in time" << endl;
735 // if ( gpspacket.lat > -500 ) {
736 printf( "%.2f %.6f %.6f %.1f %.2f %.2f %.2f\n",
738 navpacket.lat, navpacket.lon, navpacket.alt,
739 imupacket.phi*SGD_RADIANS_TO_DEGREES,
740 imupacket.the*SGD_RADIANS_TO_DEGREES,
741 imupacket.psi*SGD_RADIANS_TO_DEGREES );
744 send_data( &gpspacket, &imupacket, &navpacket, &servopacket,