6 # include <strings.h> // for bzero()
8 # define bzero(a,b) memset(a,0,b)
16 #include <simgear/constants.h>
17 #include <simgear/io/lowlevel.hxx> // endian tests
18 #include <simgear/io/sg_file.hxx>
19 #include <simgear/serial/serial.hxx>
20 #include <simgear/math/sg_geodesy.hxx>
21 #include <simgear/timing/timestamp.hxx>
23 #include <Network/net_ctrls.hxx>
24 #include <Network/net_fdm.hxx>
35 static netSocket fdm_sock, ctrls_sock;
41 static int fdm_port = 5505;
42 static int ctrls_port = 5506;
45 static string infile = "";
46 static string flight_dir = "";
47 static string serialdev = "";
48 static string outfile = "";
50 // Master time counter
51 float sim_time = 0.0f;
52 double frame_us = 0.0f;
55 SGTimeStamp last_time_stamp;
56 SGTimeStamp current_time_stamp;
59 double alt_offset = 0.0;
61 // skip initial seconds
65 // double last_lat = 0.0, last_lon = 0.0;
66 // double kts_filter = 0.0;
70 bool run_real_time = true;
72 bool ignore_checksum = false;
76 // The function htond is defined this way due to the way some
77 // processors and OSes treat floating point values. Some will raise
78 // an exception whenever a "bad" floating point value is loaded into a
79 // floating point register. Solaris is notorious for this, but then
80 // so is LynxOS on the PowerPC. By translating the data in place,
81 // there is no need to load a FP register with the "corruped" floating
82 // point value. By doing the BIG_ENDIAN test, I can optimize the
83 // routine for big-endian processors so it can be as efficient as
85 static void htond (double &x)
87 if ( sgIsLittleEndian() ) {
91 Double_Overlay = (int *) &x;
92 Holding_Buffer = Double_Overlay [0];
94 Double_Overlay [0] = htonl (Double_Overlay [1]);
95 Double_Overlay [1] = htonl (Holding_Buffer);
102 static void htonf (float &x)
104 if ( sgIsLittleEndian() ) {
108 Float_Overlay = (int *) &x;
109 Holding_Buffer = Float_Overlay [0];
111 Float_Overlay [0] = htonl (Holding_Buffer);
118 static void ugear2fg( gps *gpspacket, imu *imupacket, nav *navpacket,
119 servo *servopacket, health *healthpacket,
120 FGNetFDM *fdm, FGNetCtrls *ctrls )
124 // Version sanity checking
125 fdm->version = FG_NET_FDM_VERSION;
128 fdm->longitude = navpacket->lon * SG_DEGREES_TO_RADIANS;
129 fdm->latitude = navpacket->lat * SG_DEGREES_TO_RADIANS;
130 fdm->altitude = navpacket->alt + alt_offset;
132 fdm->psi = imupacket->psi; // heading
133 fdm->phi = imupacket->phi; // roll
134 fdm->theta = imupacket->the; // pitch;
141 // double az1, az2, dist;
142 // geo_inverse_wgs_84( pos.altitude_msl, last_lat, last_lon,
143 // pos.lat_deg, pos.lon_deg, &az1, &az2, &dist );
144 // double v_ms = dist / (frame_us / 1000000);
145 // double v_kts = v_ms * SG_METER_TO_NM * 3600;
146 // kts_filter = (0.99 * kts_filter) + (0.01 * v_kts);
147 double vn = navpacket->vn;
148 double ve = navpacket->ve;
149 double vd = navpacket->vd;
151 // enable to use ground track heading
152 fdm->psi = atan2(vn, ve); // heading
154 fdm->vcas = sqrt( vn*vn + ve*ve + vd*vd );
155 // last_lat = pos.lat_deg;
156 // last_lon = pos.lon_deg;
157 // cout << "kts_filter = " << kts_filter << " vel = " << pos.speed_kts << endl;
159 fdm->climb_rate = 0; // fps
160 // cout << "climb rate = " << aero->hdota << endl;
164 fdm->v_wind_body_north = 0.0;
165 fdm->v_wind_body_east = 0.0;
166 fdm->v_wind_body_down = 0.0;
167 fdm->stall_warning = 0.0;
169 fdm->A_X_pilot = 0.0;
170 fdm->A_Y_pilot = 0.0;
171 fdm->A_Z_pilot = 0.0 /* (should be -G) */;
174 fdm->num_engines = 1;
175 fdm->eng_state[0] = 2;
176 // cout << "state = " << fdm->eng_state[0] << endl;
177 double rpm = 5000.0 - ((double)servopacket->chn[2] / 65536.0)*3500.0;
178 if ( rpm < 0.0 ) { rpm = 0.0; }
179 if ( rpm > 5000.0 ) { rpm = 5000.0; }
182 fdm->fuel_flow[0] = 0.0;
184 // cout << "egt = " << aero->EGT << endl;
185 fdm->oil_temp[0] = 0.0;
186 fdm->oil_px[0] = 0.0;
190 fdm->fuel_quantity[0] = 0.0;
191 fdm->fuel_quantity[1] = 0.0;
199 // the following really aren't used in this context
204 // cout << "Flap deflection = " << aero->dflap << endl;
205 fdm->left_flap = 0.0;
206 fdm->right_flap = 0.0;
208 fdm->elevator = ((double)servopacket->chn[1] / 32768.0) - 1.0;
209 fdm->elevator_trim_tab = 0.0;
210 fdm->left_flap = 0.0;
211 fdm->right_flap = 0.0;
212 fdm->left_aileron = ((double)servopacket->chn[0] / 32768.0) - 1.0;
213 fdm->right_aileron = 1.0 - ((double)servopacket->chn[0] / 32768.0);
214 fdm->rudder = 1.0 - ((double)servopacket->chn[3] / 32768.0);
215 fdm->nose_wheel = 0.0;
216 fdm->speedbrake = 0.0;
219 // Convert the net buffer to network format
220 fdm->version = htonl(fdm->version);
222 htond(fdm->longitude);
223 htond(fdm->latitude);
224 htond(fdm->altitude);
233 htonf(fdm->thetadot);
236 htonf(fdm->climb_rate);
240 htonf(fdm->v_wind_body_north);
241 htonf(fdm->v_wind_body_east);
242 htonf(fdm->v_wind_body_down);
244 htonf(fdm->A_X_pilot);
245 htonf(fdm->A_Y_pilot);
246 htonf(fdm->A_Z_pilot);
248 htonf(fdm->stall_warning);
249 htonf(fdm->slip_deg);
251 for ( i = 0; i < fdm->num_engines; ++i ) {
252 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
254 htonf(fdm->fuel_flow[i]);
257 htonf(fdm->mp_osi[i]);
259 htonf(fdm->oil_temp[i]);
260 htonf(fdm->oil_px[i]);
262 fdm->num_engines = htonl(fdm->num_engines);
264 for ( i = 0; i < fdm->num_tanks; ++i ) {
265 htonf(fdm->fuel_quantity[i]);
267 fdm->num_tanks = htonl(fdm->num_tanks);
269 for ( i = 0; i < fdm->num_wheels; ++i ) {
270 fdm->wow[i] = htonl(fdm->wow[i]);
271 htonf(fdm->gear_pos[i]);
272 htonf(fdm->gear_steer[i]);
273 htonf(fdm->gear_compression[i]);
275 fdm->num_wheels = htonl(fdm->num_wheels);
277 fdm->cur_time = htonl( fdm->cur_time );
278 fdm->warp = htonl( fdm->warp );
279 htonf(fdm->visibility);
281 htonf(fdm->elevator);
282 htonf(fdm->elevator_trim_tab);
283 htonf(fdm->left_flap);
284 htonf(fdm->right_flap);
285 htonf(fdm->left_aileron);
286 htonf(fdm->right_aileron);
288 htonf(fdm->nose_wheel);
289 htonf(fdm->speedbrake);
290 htonf(fdm->spoilers);
296 static void send_data( gps *gpspacket, imu *imupacket, nav *navpacket,
297 servo *servopacket, health *healthpacket ) {
299 int fdmsize = sizeof( FGNetFDM );
301 // cout << "Running main loop" << endl;
306 ugear2fg( gpspacket, imupacket, navpacket, servopacket, healthpacket,
308 len = fdm_sock.send(&fgfdm, fdmsize, 0);
312 void usage( const string &argv0 ) {
313 cout << "Usage: " << argv0 << endl;
314 cout << "\t[ --help ]" << endl;
315 cout << "\t[ --infile <infile_name>" << endl;
316 cout << "\t[ --flight <flight_dir>" << endl;
317 cout << "\t[ --serial <dev_name>" << endl;
318 cout << "\t[ --outfile <outfile_name> (capture the data to a file)" << endl;
319 cout << "\t[ --hertz <hertz> ]" << endl;
320 cout << "\t[ --host <hostname> ]" << endl;
321 cout << "\t[ --broadcast ]" << endl;
322 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
323 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
324 cout << "\t[ --altitude-offset <meters> ]" << endl;
325 cout << "\t[ --skip-seconds <seconds> ]" << endl;
326 cout << "\t[ --no-real-time ]" << endl;
327 cout << "\t[ --ignore-checksum ]" << endl;
328 cout << "\t[ --sg-swap ]" << endl;
332 int main( int argc, char **argv ) {
334 string out_host = "localhost";
335 bool do_broadcast = false;
337 // process command line arguments
338 for ( int i = 1; i < argc; ++i ) {
339 if ( strcmp( argv[i], "--help" ) == 0 ) {
342 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
345 hertz = atof( argv[i] );
350 } else if ( strcmp( argv[i], "--infile" ) == 0 ) {
358 } else if ( strcmp( argv[i], "--flight" ) == 0 ) {
361 flight_dir = argv[i];
366 } else if ( strcmp( argv[i], "--outfile" ) == 0 ) {
374 } else if ( strcmp( argv[i], "--serial" ) == 0 ) {
382 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
390 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
392 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
395 fdm_port = atoi( argv[i] );
400 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
403 ctrls_port = atoi( argv[i] );
408 } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
411 alt_offset = atof( argv[i] );
416 } else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) {
419 skip = atof( argv[i] );
424 } else if ( strcmp( argv[i], "--no-real-time" ) == 0 ) {
425 run_real_time = false;
426 } else if ( strcmp( argv[i], "--ignore-checksum" ) == 0 ) {
427 ignore_checksum = true;
428 } else if ( strcmp( argv[i], "--sg-swap" ) == 0 ) {
436 // Setup up outgoing network connections
438 netInit( &argc,argv ); // We must call this before any other net stuff
440 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
441 cout << "error opening fdm output socket" << endl;
444 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
445 cout << "error opening ctrls output socket" << endl;
448 cout << "open net channels" << endl;
450 fdm_sock.setBlocking( false );
451 ctrls_sock.setBlocking( false );
452 cout << "blocking false" << endl;
454 if ( do_broadcast ) {
455 fdm_sock.setBroadcast( true );
456 ctrls_sock.setBroadcast( true );
459 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
461 cout << "error connecting to outgoing fdm port: " << out_host
462 << ":" << fdm_port << endl;
465 cout << "connected outgoing fdm socket" << endl;
467 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
469 cout << "error connecting to outgoing ctrls port: " << out_host
470 << ":" << ctrls_port << endl;
473 cout << "connected outgoing ctrls socket" << endl;
476 track.set_stargate_swap_mode();
479 if ( infile.length() || flight_dir.length() ) {
480 if ( infile.length() ) {
481 // Load data from a stream log data file
482 track.load_stream( infile, ignore_checksum );
483 } else if ( flight_dir.length() ) {
484 // Load data from a flight directory
485 track.load_flight( flight_dir );
487 cout << "Loaded " << track.gps_size() << " gps records." << endl;
488 cout << "Loaded " << track.imu_size() << " imu records." << endl;
489 cout << "Loaded " << track.nav_size() << " nav records." << endl;
490 cout << "Loaded " << track.servo_size() << " servo records." << endl;
491 cout << "Loaded " << track.health_size() << " health records." << endl;
493 int size = track.imu_size();
495 double current_time = track.get_imupt(0).time;
496 cout << "Track begin time is " << current_time << endl;
497 double end_time = track.get_imupt(size-1).time;
498 cout << "Track end time is " << end_time << endl;
499 cout << "Duration = " << end_time - current_time << endl;
501 // advance skip seconds forward
502 current_time += skip;
504 frame_us = 1000000.0 / hertz;
505 if ( frame_us < 0.0 ) {
509 SGTimeStamp start_time;
515 int health_count = 0;
518 gps0 = gps1 = track.get_gpspt( 0 );
521 imu0 = imu1 = track.get_imupt( 0 );
524 nav0 = nav1 = track.get_navpt( 0 );
526 servo servo0, servo1;
527 servo0 = servo1 = track.get_servopt( 0 );
529 health health0, health1;
530 health0 = health1 = track.get_healthpt( 0 );
532 double last_lat = -999.9, last_lon = -999.9;
534 while ( current_time < end_time ) {
535 // cout << "current_time = " << current_time << " end_time = "
536 // << end_time << endl;
538 // Advance gps pointer
539 while ( current_time > gps1.time
540 && gps_count < track.gps_size() - 1 )
544 // cout << "count = " << count << endl;
545 gps1 = track.get_gpspt( gps_count );
547 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
550 // Advance imu pointer
551 while ( current_time > imu1.time
552 && imu_count < track.imu_size() - 1 )
556 // cout << "count = " << count << endl;
557 imu1 = track.get_imupt( imu_count );
559 // cout << "pos0 = " << pos0.get_seconds()
560 // << " pos1 = " << pos1.get_seconds() << endl;
562 // Advance nav pointer
563 while ( current_time > nav1.time
564 && nav_count < track.nav_size() - 1 )
568 // cout << "nav count = " << nav_count << endl;
569 nav1 = track.get_navpt( nav_count );
571 // cout << "pos0 = " << pos0.get_seconds()
572 // << " pos1 = " << pos1.get_seconds() << endl;
574 // Advance servo pointer
575 while ( current_time > servo1.time
576 && servo_count < track.servo_size() - 1 )
580 // cout << "count = " << count << endl;
581 servo1 = track.get_servopt( servo_count );
583 // cout << "pos0 = " << pos0.get_seconds()
584 // << " pos1 = " << pos1.get_seconds() << endl;
586 // Advance health pointer
587 while ( current_time > health1.time
588 && health_count < track.health_size() - 1 )
592 // cout << "count = " << count << endl;
593 health1 = track.get_healthpt( health_count );
595 // cout << "pos0 = " << pos0.get_seconds()
596 // << " pos1 = " << pos1.get_seconds() << endl;
599 if ( fabs(gps1.time - gps0.time) < 0.00001 ) {
603 (current_time - gps0.time) /
604 (gps1.time - gps0.time);
606 // cout << "Percent = " << percent << endl;
609 if ( fabs(imu1.time - imu0.time) < 0.00001 ) {
613 (current_time - imu0.time) /
614 (imu1.time - imu0.time);
616 // cout << "Percent = " << percent << endl;
619 if ( fabs(nav1.time - nav0.time) < 0.00001 ) {
623 (current_time - nav0.time) /
624 (nav1.time - nav0.time);
626 // cout << "Percent = " << percent << endl;
628 double servo_percent;
629 if ( fabs(servo1.time - servo0.time) < 0.00001 ) {
633 (current_time - servo0.time) /
634 (servo1.time - servo0.time);
636 // cout << "Percent = " << percent << endl;
638 double health_percent;
639 if ( fabs(health1.time - health0.time) < 0.00001 ) {
640 health_percent = 0.0;
643 (current_time - health0.time) /
644 (health1.time - health0.time);
646 // cout << "Percent = " << percent << endl;
648 gps gpspacket = UGEARInterpGPS( gps0, gps1, gps_percent );
649 imu imupacket = UGEARInterpIMU( imu0, imu1, imu_percent );
650 nav navpacket = UGEARInterpNAV( nav0, nav1, nav_percent );
651 servo servopacket = UGEARInterpSERVO( servo0, servo1,
653 health healthpacket = UGEARInterpHEALTH( health0, health1,
656 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg
658 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
660 // cout << (double)current_time << " " << pos.lat_deg << ", "
661 // << pos.lon_deg << " " << att.yaw_deg << endl;
662 if ( gpspacket.lat > -500 ) {
663 // printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
665 // navpacket.lat, navpacket.lon, navpacket.alt,
666 // imupacket.psi, imupacket.the, imupacket.phi );
667 double dlat = last_lat - navpacket.lat;
668 double dlon = last_lon - navpacket.lon;
669 double dist = sqrt( dlat*dlat + dlon*dlon );
670 if ( dist > 0.0015 ) {
671 printf(" <trkpt lat=\"%.8f\" lon=\"%.8f\"></trkpt>\n",
672 navpacket.lat, navpacket.lon );
673 // printf(" </wpt>\n");
674 last_lat = navpacket.lat;
675 last_lon = navpacket.lon;
679 send_data( &gpspacket, &imupacket, &navpacket, &servopacket,
682 if ( run_real_time ) {
683 // Update the elapsed time.
684 static bool first_time = true;
686 last_time_stamp.stamp();
690 current_time_stamp.stamp();
692 double elapsed_us = current_time_stamp - last_time_stamp;
693 if ( elapsed_us < (frame_us - 2000) ) {
694 double requested_us = (frame_us - elapsed_us) - 2000 ;
695 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
697 current_time_stamp.stamp();
698 while ( current_time_stamp - last_time_stamp < frame_us ) {
699 current_time_stamp.stamp();
703 current_time += (frame_us / 1000000.0);
704 last_time_stamp = current_time_stamp;
707 cout << "Processed " << imu_count << " entries in "
708 << (current_time_stamp - start_time) / 1000000 << " seconds."
710 } else if ( serialdev.length() ) {
711 // process incoming data from the serial port
714 double current_time = 0.0;
715 double last_time = 0.0;
717 gps gpspacket; bzero( &gpspacket, sizeof(gpspacket) );
718 imu imupacket; bzero( &imupacket, sizeof(imupacket) );
719 nav navpacket; bzero( &navpacket, sizeof(navpacket) );
720 servo servopacket; bzero( &servopacket, sizeof(servopacket) );
721 health healthpacket; bzero( &healthpacket, sizeof(healthpacket) );
726 double servo_time = 0;
727 double health_time = 0;
729 // open the serial port device
730 SGSerialPort input( serialdev, 115200 );
731 if ( !input.is_enabled() ) {
732 cout << "Cannot open: " << serialdev << endl;
736 // open up the data log file if requested
737 if ( !outfile.length() ) {
738 cout << "no --outfile <name> specified, cannot capture data!"
742 SGFile output( outfile );
743 if ( !output.open( SG_IO_OUT ) ) {
744 cout << "Cannot open: " << outfile << endl;
748 while ( input.is_enabled() ) {
749 // cout << "looking for next message ..." << endl;
750 int id = track.next_message( &input, &output, &gpspacket,
751 &imupacket, &navpacket, &servopacket,
752 &healthpacket, ignore_checksum );
753 // cout << "message id = " << id << endl;
756 if ( id == GPS_PACKET ) {
757 if ( gpspacket.time > gps_time ) {
758 gps_time = gpspacket.time;
759 current_time = gps_time;
761 cout << "oops gps back in time: " << gpspacket.time << " " << gps_time << endl;
763 } else if ( id == IMU_PACKET ) {
764 if ( imupacket.time > imu_time ) {
765 imu_time = imupacket.time;
766 current_time = imu_time;
768 cout << "oops imu back in time: " << imupacket.time << " " << imu_time << endl;
770 } else if ( id == NAV_PACKET ) {
771 if ( navpacket.time > nav_time ) {
772 nav_time = navpacket.time;
773 current_time = nav_time;
775 cout << "oops nav back in time: " << navpacket.time << " " << nav_time << endl;
777 } else if ( id == SERVO_PACKET ) {
778 if ( servopacket.time > servo_time ) {
779 servo_time = servopacket.time;
780 current_time = servo_time;
782 cout << "oops servo back in time: " << servopacket.time << " " << servo_time << endl;
784 } else if ( id == HEALTH_PACKET ) {
785 if ( healthpacket.time > health_time ) {
786 health_time = healthpacket.time;
787 current_time = health_time;
789 cout << "oops health back in time: " << healthpacket.time << " " << health_time << endl;
793 if ( current_time >= last_time + (1/hertz) ) {
794 // if ( gpspacket.lat > -500 ) {
795 printf( "%.2f %.6f %.6f %.1f %.2f %.2f %.2f %.2f %d\n",
797 navpacket.lat, navpacket.lon, navpacket.alt,
798 imupacket.phi*SGD_RADIANS_TO_DEGREES,
799 imupacket.the*SGD_RADIANS_TO_DEGREES,
800 imupacket.psi*SGD_RADIANS_TO_DEGREES,
802 healthpacket.est_seconds);
805 last_time = current_time;
806 send_data( &gpspacket, &imupacket, &navpacket, &servopacket,