]> git.mxchange.org Git - flightgear.git/blob - utils/GPSsmooth/UGear_main.cxx
Maik: add ROTORELTARGET and ROTORENGINEMAXRELTORQUE input axes
[flightgear.git] / utils / GPSsmooth / UGear_main.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4
5 #ifndef _MSC_VER
6 #  include <strings.h>          // for bzero()
7 #else
8 #  define bzero(a,b) memset(a,0,b)
9 #endif
10 #include <iostream>
11 #include <string>
12
13 #include <plib/net.h>
14 #include <plib/sg.h>
15
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>
22
23 #include <Network/net_ctrls.hxx>
24 #include <Network/net_fdm.hxx>
25
26 #include "UGear.hxx"
27
28
29 SG_USING_STD(cout);
30 SG_USING_STD(endl);
31 SG_USING_STD(string);
32
33
34 // Network channels
35 static netSocket fdm_sock, ctrls_sock;
36
37 // ugear data
38 UGEARTrack track;
39
40 // Default ports
41 static int fdm_port = 5505;
42 static int ctrls_port = 5506;
43
44 // Default path
45 static string infile = "";
46 static string flight_dir = "";
47 static string serialdev = "";
48 static string outfile = "";
49
50 // Master time counter
51 float sim_time = 0.0f;
52 double frame_us = 0.0f;
53
54 // sim control
55 SGTimeStamp last_time_stamp;
56 SGTimeStamp current_time_stamp;
57
58 // altitude offset
59 double alt_offset = 0.0;
60
61 // skip initial seconds
62 double skip = 0.0;
63
64 // for speed estimate
65 // double last_lat = 0.0, last_lon = 0.0;
66 // double kts_filter = 0.0;
67
68 bool inited = false;
69
70 bool run_real_time = true;
71
72 bool ignore_checksum = false;
73
74 bool sg_swap = false;
75
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
84 // possible
85 static void htond (double &x)   
86 {
87     if ( sgIsLittleEndian() ) {
88         int    *Double_Overlay;
89         int     Holding_Buffer;
90     
91         Double_Overlay = (int *) &x;
92         Holding_Buffer = Double_Overlay [0];
93     
94         Double_Overlay [0] = htonl (Double_Overlay [1]);
95         Double_Overlay [1] = htonl (Holding_Buffer);
96     } else {
97         return;
98     }
99 }
100
101 // Float version
102 static void htonf (float &x)    
103 {
104     if ( sgIsLittleEndian() ) {
105         int    *Float_Overlay;
106         int     Holding_Buffer;
107     
108         Float_Overlay = (int *) &x;
109         Holding_Buffer = Float_Overlay [0];
110     
111         Float_Overlay [0] = htonl (Holding_Buffer);
112     } else {
113         return;
114     }
115 }
116
117
118 static void ugear2fg( gps *gpspacket, imu *imupacket, nav *navpacket,
119                       servo *servopacket, health *healthpacket,
120                       FGNetFDM *fdm, FGNetCtrls *ctrls )
121 {
122     unsigned int i;
123
124     // Version sanity checking
125     fdm->version = FG_NET_FDM_VERSION;
126
127     // Aero parameters
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;
131     fdm->agl = -9999.0;
132     fdm->psi = imupacket->psi; // heading
133     fdm->phi = imupacket->phi; // roll
134     fdm->theta = imupacket->the; // pitch;
135
136     fdm->phidot = 0.0;
137     fdm->thetadot = 0.0;
138     fdm->psidot = 0.0;
139
140     // estimate speed
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;
150
151     // enable to use ground track heading
152     fdm->psi = atan2(vn, ve); // heading
153
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;
158
159     fdm->climb_rate = 0; // fps
160     // cout << "climb rate = " << aero->hdota << endl;
161     fdm->v_north = 0.0;
162     fdm->v_east = 0.0;
163     fdm->v_down = 0.0;
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;
168
169     fdm->A_X_pilot = 0.0;
170     fdm->A_Y_pilot = 0.0;
171     fdm->A_Z_pilot = 0.0 /* (should be -G) */;
172
173     // Engine parameters
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; }
180     fdm->rpm[0] = rpm;
181
182     fdm->fuel_flow[0] = 0.0;
183     fdm->egt[0] = 0.0;
184     // cout << "egt = " << aero->EGT << endl;
185     fdm->oil_temp[0] = 0.0;
186     fdm->oil_px[0] = 0.0;
187
188     // Consumables
189     fdm->num_tanks = 2;
190     fdm->fuel_quantity[0] = 0.0;
191     fdm->fuel_quantity[1] = 0.0;
192
193     // Gear and flaps
194     fdm->num_wheels = 3;
195     fdm->wow[0] = 0;
196     fdm->wow[1] = 0;
197     fdm->wow[2] = 0;
198
199     // the following really aren't used in this context
200     fdm->cur_time = 0;
201     fdm->warp = 0;
202     fdm->visibility = 0;
203
204     // cout << "Flap deflection = " << aero->dflap << endl;
205     fdm->left_flap = 0.0;
206     fdm->right_flap = 0.0;
207
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;
217     fdm->spoilers = 0.0;
218
219     // Convert the net buffer to network format
220     fdm->version = htonl(fdm->version);
221
222     htond(fdm->longitude);
223     htond(fdm->latitude);
224     htond(fdm->altitude);
225     htonf(fdm->agl);
226     htonf(fdm->phi);
227     htonf(fdm->theta);
228     htonf(fdm->psi);
229     htonf(fdm->alpha);
230     htonf(fdm->beta);
231
232     htonf(fdm->phidot);
233     htonf(fdm->thetadot);
234     htonf(fdm->psidot);
235     htonf(fdm->vcas);
236     htonf(fdm->climb_rate);
237     htonf(fdm->v_north);
238     htonf(fdm->v_east);
239     htonf(fdm->v_down);
240     htonf(fdm->v_wind_body_north);
241     htonf(fdm->v_wind_body_east);
242     htonf(fdm->v_wind_body_down);
243
244     htonf(fdm->A_X_pilot);
245     htonf(fdm->A_Y_pilot);
246     htonf(fdm->A_Z_pilot);
247
248     htonf(fdm->stall_warning);
249     htonf(fdm->slip_deg);
250
251     for ( i = 0; i < fdm->num_engines; ++i ) {
252         fdm->eng_state[i] = htonl(fdm->eng_state[i]);
253         htonf(fdm->rpm[i]);
254         htonf(fdm->fuel_flow[i]);
255         htonf(fdm->egt[i]);
256         htonf(fdm->cht[i]);
257         htonf(fdm->mp_osi[i]);
258         htonf(fdm->tit[i]);
259         htonf(fdm->oil_temp[i]);
260         htonf(fdm->oil_px[i]);
261     }
262     fdm->num_engines = htonl(fdm->num_engines);
263
264     for ( i = 0; i < fdm->num_tanks; ++i ) {
265         htonf(fdm->fuel_quantity[i]);
266     }
267     fdm->num_tanks = htonl(fdm->num_tanks);
268
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]);
274     }
275     fdm->num_wheels = htonl(fdm->num_wheels);
276
277     fdm->cur_time = htonl( fdm->cur_time );
278     fdm->warp = htonl( fdm->warp );
279     htonf(fdm->visibility);
280
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);
287     htonf(fdm->rudder);
288     htonf(fdm->nose_wheel);
289     htonf(fdm->speedbrake);
290     htonf(fdm->spoilers);
291
292
293 }
294
295
296 static void send_data( gps *gpspacket, imu *imupacket, nav *navpacket,
297                        servo *servopacket, health *healthpacket ) {
298     int len;
299     int fdmsize = sizeof( FGNetFDM );
300
301     // cout << "Running main loop" << endl;
302
303     FGNetFDM fgfdm;
304     FGNetCtrls fgctrls;
305
306     ugear2fg( gpspacket, imupacket, navpacket, servopacket, healthpacket,
307               &fgfdm, &fgctrls );
308     len = fdm_sock.send(&fgfdm, fdmsize, 0);
309 }
310
311
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;
329 }
330
331
332 int main( int argc, char **argv ) {
333     double hertz = 60.0;
334     string out_host = "localhost";
335     bool do_broadcast = false;
336
337     // process command line arguments
338     for ( int i = 1; i < argc; ++i ) {
339         if ( strcmp( argv[i], "--help" ) == 0 ) {
340             usage( argv[0] );
341             exit( 0 );
342         } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
343             ++i;
344             if ( i < argc ) {
345                 hertz = atof( argv[i] );
346             } else {
347                 usage( argv[0] );
348                 exit( -1 );
349             }
350         } else if ( strcmp( argv[i], "--infile" ) == 0 ) {
351             ++i;
352             if ( i < argc ) {
353                 infile = argv[i];
354             } else {
355                 usage( argv[0] );
356                 exit( -1 );
357             }
358         } else if ( strcmp( argv[i], "--flight" ) == 0 ) {
359             ++i;
360             if ( i < argc ) {
361                 flight_dir = argv[i];
362             } else {
363                 usage( argv[0] );
364                 exit( -1 );
365             }
366         } else if ( strcmp( argv[i], "--outfile" ) == 0 ) {
367             ++i;
368             if ( i < argc ) {
369                 outfile = argv[i];
370             } else {
371                 usage( argv[0] );
372                 exit( -1 );
373             }
374         } else if ( strcmp( argv[i], "--serial" ) == 0 ) {
375             ++i;
376             if ( i < argc ) {
377                 serialdev = argv[i];
378             } else {
379                 usage( argv[0] );
380                 exit( -1 );
381             }
382         } else if ( strcmp( argv[i], "--host" ) == 0 ) {
383             ++i;
384             if ( i < argc ) {
385                 out_host = argv[i];
386             } else {
387                 usage( argv[0] );
388                 exit( -1 );
389             }
390         } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
391           do_broadcast = true;
392         } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
393             ++i;
394             if ( i < argc ) {
395                 fdm_port = atoi( argv[i] );
396             } else {
397                 usage( argv[0] );
398                 exit( -1 );
399             }
400         } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
401             ++i;
402             if ( i < argc ) {
403                 ctrls_port = atoi( argv[i] );
404             } else {
405                 usage( argv[0] );
406                 exit( -1 );
407             }
408         } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
409             ++i;
410             if ( i < argc ) {
411                 alt_offset = atof( argv[i] );
412             } else {
413                 usage( argv[0] );
414                 exit( -1 );
415             }
416         } else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) {
417             ++i;
418             if ( i < argc ) {
419                 skip = atof( argv[i] );
420             } else {
421                 usage( argv[0] );
422                 exit( -1 );
423             }
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 ) {
429             sg_swap = true;
430         } else {
431             usage( argv[0] );
432             exit( -1 );
433         }
434     }
435
436     // Setup up outgoing network connections
437
438     netInit( &argc,argv ); // We must call this before any other net stuff
439
440     if ( ! fdm_sock.open( false ) ) {  // open a UDP socket
441         cout << "error opening fdm output socket" << endl;
442         return -1;
443     }
444     if ( ! ctrls_sock.open( false ) ) {  // open a UDP socket
445         cout << "error opening ctrls output socket" << endl;
446         return -1;
447     }
448     cout << "open net channels" << endl;
449
450     fdm_sock.setBlocking( false );
451     ctrls_sock.setBlocking( false );
452     cout << "blocking false" << endl;
453
454     if ( do_broadcast ) {
455         fdm_sock.setBroadcast( true );
456         ctrls_sock.setBroadcast( true );
457     }
458
459     if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
460         perror("connect");
461         cout << "error connecting to outgoing fdm port: " << out_host
462              << ":" << fdm_port << endl;
463         return -1;
464     }
465     cout << "connected outgoing fdm socket" << endl;
466
467     if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
468         perror("connect");
469         cout << "error connecting to outgoing ctrls port: " << out_host
470              << ":" << ctrls_port << endl;
471         return -1;
472     }
473     cout << "connected outgoing ctrls socket" << endl;
474
475     if ( sg_swap ) {
476         track.set_stargate_swap_mode();
477     }
478
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 );
486         }
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;
492
493         int size = track.imu_size();
494
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;
500
501         // advance skip seconds forward
502         current_time += skip;
503
504         frame_us = 1000000.0 / hertz;
505         if ( frame_us < 0.0 ) {
506             frame_us = 0.0;
507         }
508
509         SGTimeStamp start_time;
510         start_time.stamp();
511         int gps_count = 0;
512         int imu_count = 0;
513         int nav_count = 0;
514         int servo_count = 0;
515         int health_count = 0;
516
517         gps gps0, gps1;
518         gps0 = gps1 = track.get_gpspt( 0 );
519     
520         imu imu0, imu1;
521         imu0 = imu1 = track.get_imupt( 0 );
522     
523         nav nav0, nav1;
524         nav0 = nav1 = track.get_navpt( 0 );
525     
526         servo servo0, servo1;
527         servo0 = servo1 = track.get_servopt( 0 );
528     
529         health health0, health1;
530         health0 = health1 = track.get_healthpt( 0 );
531
532         double last_lat = -999.9, last_lon = -999.9;
533
534         while ( current_time < end_time ) {
535             // cout << "current_time = " << current_time << " end_time = "
536             //      << end_time << endl;
537
538             // Advance gps pointer
539             while ( current_time > gps1.time
540                     && gps_count < track.gps_size() - 1 )
541             {
542                 gps0 = gps1;
543                 ++gps_count;
544                 // cout << "count = " << count << endl;
545                 gps1 = track.get_gpspt( gps_count );
546             }
547             // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
548             //      << endl;
549
550             // Advance imu pointer
551             while ( current_time > imu1.time
552                     && imu_count < track.imu_size() - 1 )
553             {
554                 imu0 = imu1;
555                 ++imu_count;
556                 // cout << "count = " << count << endl;
557                 imu1 = track.get_imupt( imu_count );
558             }
559             //  cout << "pos0 = " << pos0.get_seconds()
560             // << " pos1 = " << pos1.get_seconds() << endl;
561
562             // Advance nav pointer
563             while ( current_time > nav1.time
564                     && nav_count < track.nav_size() - 1 )
565             {
566                 nav0 = nav1;
567                 ++nav_count;
568                 // cout << "nav count = " << nav_count << endl;
569                 nav1 = track.get_navpt( nav_count );
570             }
571             //  cout << "pos0 = " << pos0.get_seconds()
572             // << " pos1 = " << pos1.get_seconds() << endl;
573
574             // Advance servo pointer
575             while ( current_time > servo1.time
576                     && servo_count < track.servo_size() - 1 )
577             {
578                 servo0 = servo1;
579                 ++servo_count;
580                 // cout << "count = " << count << endl;
581                 servo1 = track.get_servopt( servo_count );
582             }
583             //  cout << "pos0 = " << pos0.get_seconds()
584             // << " pos1 = " << pos1.get_seconds() << endl;
585
586             // Advance health pointer
587             while ( current_time > health1.time
588                     && health_count < track.health_size() - 1 )
589             {
590                 health0 = health1;
591                 ++health_count;
592                 // cout << "count = " << count << endl;
593                 health1 = track.get_healthpt( health_count );
594             }
595             //  cout << "pos0 = " << pos0.get_seconds()
596             // << " pos1 = " << pos1.get_seconds() << endl;
597
598             double gps_percent;
599             if ( fabs(gps1.time - gps0.time) < 0.00001 ) {
600                 gps_percent = 0.0;
601             } else {
602                 gps_percent =
603                     (current_time - gps0.time) /
604                     (gps1.time - gps0.time);
605             }
606             // cout << "Percent = " << percent << endl;
607
608             double imu_percent;
609             if ( fabs(imu1.time - imu0.time) < 0.00001 ) {
610                 imu_percent = 0.0;
611             } else {
612                 imu_percent =
613                     (current_time - imu0.time) /
614                     (imu1.time - imu0.time);
615             }
616             // cout << "Percent = " << percent << endl;
617
618             double nav_percent;
619             if ( fabs(nav1.time - nav0.time) < 0.00001 ) {
620                 nav_percent = 0.0;
621             } else {
622                 nav_percent =
623                     (current_time - nav0.time) /
624                     (nav1.time - nav0.time);
625             }
626             // cout << "Percent = " << percent << endl;
627
628             double servo_percent;
629             if ( fabs(servo1.time - servo0.time) < 0.00001 ) {
630                 servo_percent = 0.0;
631             } else {
632                 servo_percent =
633                     (current_time - servo0.time) /
634                     (servo1.time - servo0.time);
635             }
636             // cout << "Percent = " << percent << endl;
637
638             double health_percent;
639             if ( fabs(health1.time - health0.time) < 0.00001 ) {
640                 health_percent = 0.0;
641             } else {
642                 health_percent =
643                     (current_time - health0.time) /
644                     (health1.time - health0.time);
645             }
646             // cout << "Percent = " << percent << endl;
647
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,
652                                                   servo_percent );
653             health healthpacket = UGEARInterpHEALTH( health0, health1,
654                                                   health_percent );
655
656             // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg
657             //      << endl;
658             // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
659             //      << endl;
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",
664                 //         current_time,
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;
676                 }
677             }
678
679             send_data( &gpspacket, &imupacket, &navpacket, &servopacket,
680                        &healthpacket );
681
682             if ( run_real_time ) {
683                 // Update the elapsed time.
684                 static bool first_time = true;
685                 if ( first_time ) {
686                     last_time_stamp.stamp();
687                     first_time = false;
688                 }
689
690                 current_time_stamp.stamp();
691                 /* Convert to ms */
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) ) ;
696                 }
697                 current_time_stamp.stamp();
698                 while ( current_time_stamp - last_time_stamp < frame_us ) {
699                     current_time_stamp.stamp();
700                 }
701             }
702
703             current_time += (frame_us / 1000000.0);
704             last_time_stamp = current_time_stamp;
705         }
706
707         cout << "Processed " << imu_count << " entries in "
708              << (current_time_stamp - start_time) / 1000000 << " seconds."
709              << endl;
710     } else if ( serialdev.length() ) {
711         // process incoming data from the serial port
712
713         int count = 0;
714         double current_time = 0.0;
715         double last_time = 0.0;
716
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) );
722
723         double gps_time = 0;
724         double imu_time = 0;
725         double nav_time = 0;
726         double servo_time = 0;
727         double health_time = 0;
728
729         // open the serial port device
730         SGSerialPort input( serialdev, 115200 );
731         if ( !input.is_enabled() ) {
732             cout << "Cannot open: " << serialdev << endl;
733             return false;
734         }
735
736         // open up the data log file if requested
737         if ( !outfile.length() ) {
738             cout << "no --outfile <name> specified, cannot capture data!"
739                  << endl;
740             return false;
741         }
742         SGFile output( outfile );
743         if ( !output.open( SG_IO_OUT ) ) {
744             cout << "Cannot open: " << outfile << endl;
745             return false;
746         }
747
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;
754             count++;
755
756             if ( id == GPS_PACKET ) {
757                 if ( gpspacket.time > gps_time ) {
758                     gps_time = gpspacket.time;
759                     current_time = gps_time;
760                 } else {
761                   cout << "oops gps back in time: " << gpspacket.time << " " << gps_time << endl;
762                 }
763             } else if ( id == IMU_PACKET ) {
764                 if ( imupacket.time > imu_time ) {
765                     imu_time = imupacket.time;
766                     current_time = imu_time;
767                 } else {
768                     cout << "oops imu back in time: " << imupacket.time << " " << imu_time << endl;
769                 }
770             } else if ( id == NAV_PACKET ) {
771                 if ( navpacket.time > nav_time ) {
772                     nav_time = navpacket.time;
773                     current_time = nav_time;
774                 } else {
775                     cout << "oops nav back in time: " << navpacket.time << " " << nav_time << endl;
776                 }
777             } else if ( id == SERVO_PACKET ) {
778                 if ( servopacket.time > servo_time ) {
779                     servo_time = servopacket.time;
780                     current_time = servo_time;
781                 } else {
782                     cout << "oops servo back in time: " << servopacket.time << " " << servo_time << endl;
783                 }
784             } else if ( id == HEALTH_PACKET ) {
785                 if ( healthpacket.time > health_time ) {
786                     health_time = healthpacket.time;
787                     current_time = health_time;
788                 } else {
789                     cout << "oops health back in time: " << healthpacket.time << " " << health_time << endl;
790                 }
791             }
792
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",
796                         current_time,
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,
801                         healthpacket.volts,
802                         healthpacket.est_seconds);
803                 // }
804
805                 last_time = current_time;
806                 send_data( &gpspacket, &imupacket, &navpacket, &servopacket,
807                            &healthpacket );
808             }
809         }
810     }
811
812     return 0;
813 }