8 # include <netinet/in.h> // htonl() ntohl()
12 # include <strings.h> // for bzero()
14 # define bzero(a,b) memset(a,0,b)
21 #include <simgear/constants.h>
22 #include <simgear/io/lowlevel.hxx> // endian tests
23 #include <simgear/io/sg_file.hxx>
24 #include <simgear/io/raw_socket.hxx>
25 #include <simgear/serial/serial.hxx>
26 #include <simgear/math/sg_geodesy.hxx>
27 #include <simgear/timing/timestamp.hxx>
29 #include <Network/net_ctrls.hxx>
30 #include <Network/net_fdm.hxx>
33 #include "UGear_command.hxx"
34 #include "UGear_opengc.hxx"
35 #include "UGear_telnet.hxx"
44 static simgear::Socket fdm_sock, ctrls_sock, opengc_sock;
50 static int fdm_port = 5505;
51 static int ctrls_port = 5506;
52 static int opengc_port = 6000;
55 static string infile = "";
56 static string flight_dir = "";
57 static string serialdev = "";
58 static string outfile = "";
60 // Master time counter
61 float sim_time = 0.0f;
62 double frame_us = 0.0f;
65 SGTimeStamp last_time_stamp;
66 SGTimeStamp current_time_stamp;
69 double alt_offset = 0.0;
71 // skip initial seconds
75 double last_lat = 0.0, last_lon = 0.0;
76 double kts_filter = 0.0;
80 bool run_real_time = true;
82 bool ignore_checksum = false;
86 bool use_ground_track_hdg = false;
87 bool use_ground_speed = false;
89 bool est_controls = false;
91 float gps_status = -1.0;
94 // The function htond is defined this way due to the way some
95 // processors and OSes treat floating point values. Some will raise
96 // an exception whenever a "bad" floating point value is loaded into a
97 // floating point register. Solaris is notorious for this, but then
98 // so is LynxOS on the PowerPC. By translating the data in place,
99 // there is no need to load a FP register with the "corruped" floating
100 // point value. By doing the BIG_ENDIAN test, I can optimize the
101 // routine for big-endian processors so it can be as efficient as
103 static void htond (double &x)
105 if ( sgIsLittleEndian() ) {
109 Double_Overlay = (int *) &x;
110 Holding_Buffer = Double_Overlay [0];
112 Double_Overlay [0] = htonl (Double_Overlay [1]);
113 Double_Overlay [1] = htonl (Holding_Buffer);
120 static void htonf (float &x)
122 if ( sgIsLittleEndian() ) {
126 Float_Overlay = (int *) &x;
127 Holding_Buffer = Float_Overlay [0];
129 Float_Overlay [0] = htonl (Holding_Buffer);
136 static void ugear2fg( gps *gpspacket, imu *imupacket, nav *navpacket,
137 servo *servopacket, health *healthpacket,
138 FGNetFDM *fdm, FGNetCtrls *ctrls )
142 // Version sanity checking
143 fdm->version = FG_NET_FDM_VERSION;
146 fdm->longitude = navpacket->lon * SG_DEGREES_TO_RADIANS;
147 fdm->latitude = navpacket->lat * SG_DEGREES_TO_RADIANS;
148 fdm->altitude = navpacket->alt + alt_offset;
150 fdm->psi = imupacket->psi; // heading
151 fdm->phi = imupacket->phi; // roll
152 fdm->theta = imupacket->the; // pitch;
159 // double az1, az2, dist;
160 // geo_inverse_wgs_84( fdm->altitude, last_lat, last_lon,
161 // fdm->latitude, fdm->longitude, &az1, &az2, &dist );
162 // last_lat = fdm->latitude;
163 // last_lon = fdm->longitude;
164 // double v_ms = dist / (frame_us / 1000000);
165 // double v_kts = v_ms * SG_METER_TO_NM * 3600;
166 // kts_filter = (0.9 * kts_filter) + (0.1 * v_kts);
167 // printf("dist = %.5f kts est = %.2f\n", dist, kts_filter);
169 double vn = navpacket->vn;
170 double ve = navpacket->ve;
171 double vd = navpacket->vd;
173 if ( use_ground_track_hdg ) {
174 fdm->psi = SGD_PI * 0.5 - atan2(vn, ve); // heading
178 if ( use_ground_speed ) {
179 mps = sqrt( vn*vn + ve*ve + vd*vd );
183 // double mph = mps * 3600 / 1609.3440;
184 double kts = mps * SG_MPS_TO_KT;
186 // printf("speed = %.2f mph %.2f kts\n", mph, kts );
187 static double Ps = 0.0, Ps_last = 0.0, t_last = 0.0;
189 Ps = 0.92 * Ps + 0.08 * imupacket->Ps;
190 double climb = (Ps - Ps_last) / (imupacket->time - t_last);
191 t_last = imupacket->time;
192 static double climbf = 0.0;
193 climbf = 0.994 * climbf + 0.006 * climb;
194 fdm->climb_rate = climbf; // fps
196 static double Ps_error = 0.0;
197 static double Ps_count = 0;
198 const double span = 10000.0;
199 Ps_count += 1.0; if (Ps_count > (span-1.0)) { Ps_count = (span-1.0); }
200 double error = navpacket->alt - Ps;
201 Ps_error = (Ps_count/span) * Ps_error + ((span-Ps_count)/span) * error;
202 fdm->altitude = Ps + Ps_error;
204 /* printf("%.3f, %.3f, %.3f, %.3f, %.8f, %.8f, %.3f, %.3f, %.3f, %.3f, %.3f\n",
205 imupacket->time, imupacket->the, -navpacket->vd, climbf,
206 navpacket->lat, navpacket->lon, gpspacket->alt, navpacket->alt,
207 imupacket->Ps, Ps, Ps + Ps_error); */
209 // cout << "climb rate = " << aero->hdota << endl;
213 fdm->v_wind_body_north = 0.0;
214 fdm->v_wind_body_east = 0.0;
215 fdm->v_wind_body_down = 0.0;
216 fdm->stall_warning = 0.0;
218 fdm->A_X_pilot = 0.0;
219 fdm->A_Y_pilot = 0.0;
220 fdm->A_Z_pilot = 0.0 /* (should be -G) */;
223 fdm->num_engines = 1;
224 fdm->eng_state[0] = 2;
225 // cout << "state = " << fdm->eng_state[0] << endl;
226 double rpm = 5000.0 - ((double)servopacket->chn[2] / 65536.0)*3500.0;
227 if ( rpm < 0.0 ) { rpm = 0.0; }
228 if ( rpm > 5000.0 ) { rpm = 5000.0; }
231 fdm->fuel_flow[0] = 0.0;
233 // cout << "egt = " << aero->EGT << endl;
234 fdm->oil_temp[0] = 0.0;
235 fdm->oil_px[0] = 0.0;
239 fdm->fuel_quantity[0] = 0.0;
240 fdm->fuel_quantity[1] = 0.0;
248 // the following really aren't used in this context
253 // cout << "Flap deflection = " << aero->dflap << endl;
254 fdm->left_flap = 0.0;
255 fdm->right_flap = 0.0;
257 if ( est_controls ) {
258 static float est_elev = 0.0;
259 static float est_aileron = 0.0;
260 static float est_rudder = 0.0;
261 est_elev = 0.99 * est_elev + 0.01 * (imupacket->q * 4);
262 est_aileron = 0.95 * est_aileron + 0.05 * (imupacket->p * 5);
263 est_rudder = 0.95 * est_rudder + 0.05 * (imupacket->r * 2);
264 ctrls->elevator = fdm->elevator = -est_elev;
265 ctrls->aileron = fdm->left_aileron = est_aileron;
266 fdm->right_aileron = -est_aileron;
267 ctrls->rudder = fdm->rudder = est_rudder;
269 ctrls->elevator = fdm->elevator = 1.0 - ((double)servopacket->chn[1] / 32768.0);
270 ctrls->aileron = fdm->left_aileron = 1.0 - ((double)servopacket->chn[0] / 32768.0);
271 fdm->right_aileron = ((double)servopacket->chn[0] / 32768.0) - 1.0;
272 ctrls->rudder = fdm->rudder = 1.0 - ((double)servopacket->chn[3] / 32768.0);
273 ctrls->elevator *= 3.0;
274 ctrls->aileron *= 3.0;
276 fdm->elevator_trim_tab = 0.0;
277 fdm->left_flap = 0.0;
278 fdm->right_flap = 0.0;
279 fdm->nose_wheel = 0.0;
280 fdm->speedbrake = 0.0;
283 // Convert the net buffer to network format
284 fdm->version = htonl(fdm->version);
286 htond(fdm->longitude);
287 htond(fdm->latitude);
288 htond(fdm->altitude);
297 htonf(fdm->thetadot);
300 htonf(fdm->climb_rate);
304 htonf(fdm->v_wind_body_north);
305 htonf(fdm->v_wind_body_east);
306 htonf(fdm->v_wind_body_down);
308 htonf(fdm->A_X_pilot);
309 htonf(fdm->A_Y_pilot);
310 htonf(fdm->A_Z_pilot);
312 htonf(fdm->stall_warning);
313 htonf(fdm->slip_deg);
315 for ( i = 0; i < fdm->num_engines; ++i ) {
316 fdm->eng_state[i] = htonl(fdm->eng_state[i]);
318 htonf(fdm->fuel_flow[i]);
321 htonf(fdm->mp_osi[i]);
323 htonf(fdm->oil_temp[i]);
324 htonf(fdm->oil_px[i]);
326 fdm->num_engines = htonl(fdm->num_engines);
328 for ( i = 0; i < fdm->num_tanks; ++i ) {
329 htonf(fdm->fuel_quantity[i]);
331 fdm->num_tanks = htonl(fdm->num_tanks);
333 for ( i = 0; i < fdm->num_wheels; ++i ) {
334 fdm->wow[i] = htonl(fdm->wow[i]);
335 htonf(fdm->gear_pos[i]);
336 htonf(fdm->gear_steer[i]);
337 htonf(fdm->gear_compression[i]);
339 fdm->num_wheels = htonl(fdm->num_wheels);
341 fdm->cur_time = htonl( fdm->cur_time );
342 fdm->warp = htonl( fdm->warp );
343 htonf(fdm->visibility);
345 htonf(fdm->elevator);
346 htonf(fdm->elevator_trim_tab);
347 htonf(fdm->left_flap);
348 htonf(fdm->right_flap);
349 htonf(fdm->left_aileron);
350 htonf(fdm->right_aileron);
352 htonf(fdm->nose_wheel);
353 htonf(fdm->speedbrake);
354 htonf(fdm->spoilers);
357 ctrls->version = FG_NET_CTRLS_VERSION;
358 ctrls->elevator_trim = 0.0;
361 htonl(ctrls->version);
362 htond(ctrls->aileron);
363 htond(ctrls->rudder);
364 htond(ctrls->elevator);
365 htond(ctrls->elevator_trim);
371 static void ugear2opengc( gps *gpspacket, imu *imupacket, nav *navpacket,
372 servo *servopacket, health *healthpacket,
375 // Version sanity checking
376 ogc->version_id = OGC_VERSION;
379 ogc->longitude = navpacket->lon;
380 ogc->latitude = navpacket->lat;
381 ogc->heading = imupacket->psi * SG_RADIANS_TO_DEGREES; // heading
382 ogc->bank = imupacket->phi * SG_RADIANS_TO_DEGREES; // roll
383 ogc->pitch = imupacket->the * SG_RADIANS_TO_DEGREES; // pitch;
386 ogc->theta_dot = 0.0;
391 ogc->alpha_dot = 0.0;
394 ogc->left_aileron = 1.0 - ((double)servopacket->chn[0] / 32768.0);
395 ogc->right_aileron = ((double)servopacket->chn[0] / 32768.0) - 1.0;
396 ogc->elevator = 1.0 - ((double)servopacket->chn[1] / 32768.0);
397 ogc->elevator_trim = 0.0;
398 ogc->rudder = 1.0 - ((double)servopacket->chn[3] / 32768.0);
400 ogc->flaps_cmd = 0.0;
406 // double az1, az2, dist;
407 // geo_inverse_wgs_84( fdm->altitude, last_lat, last_lon,
408 // fdm->latitude, fdm->longitude, &az1, &az2, &dist );
409 // last_lat = fdm->latitude;
410 // last_lon = fdm->longitude;
411 // double v_ms = dist / (frame_us / 1000000);
412 // double v_kts = v_ms * SG_METER_TO_NM * 3600;
413 // kts_filter = (0.9 * kts_filter) + (0.1 * v_kts);
414 // printf("dist = %.5f kts est = %.2f\n", dist, kts_filter);
416 double vn = navpacket->vn;
417 double ve = navpacket->ve;
418 double vd = navpacket->vd;
420 if ( use_ground_track_hdg ) {
421 ogc->heading = (SGD_PI * 0.5 - atan2(vn, ve)) * SG_RADIANS_TO_DEGREES;
424 if ( ogc->heading < 0 ) { ogc->heading += 360.0; }
427 if ( use_ground_speed ) {
428 mps = sqrt( vn*vn + ve*ve + vd*vd );
432 // double mph = mps * 3600 / 1609.3440;
433 double kts = mps * SG_MPS_TO_KT;
435 // printf("speed = %.2f mph %.2f kts\n", mph, kts );
436 static double Ps = 0.0, Ps_last = 0.0, t_last = 0.0;
438 Ps = 0.92 * Ps + 0.08 * imupacket->Ps;
439 double climb = (Ps - Ps_last) / (imupacket->time - t_last);
440 t_last = imupacket->time;
441 static double climbf = 0.0;
442 climbf = 0.994 * climbf + 0.006 * climb;
443 ogc->vvi = climbf; // fps
445 // uncomment one of the following schemes for setting elevation:
447 // use the navigation (inertially augmented gps estimate)
448 // ogc->altitude = ogc->elevation
449 // = (navpacket->alt + alt_offset * SG_METER_TO_FEET);
451 // use estimate error between pressure sensor and gps altitude over time
452 // use pressure sensor + error average for altitude estimate.
453 static double Ps_error = 0.0;
454 static double Ps_count = 0;
455 const double span = 10000.0;
456 Ps_count += 1.0; if (Ps_count > (span-1.0)) { Ps_count = (span-1.0); }
457 double error = navpacket->alt - Ps;
458 Ps_error = (Ps_count/span) * Ps_error + ((span-Ps_count)/span) * error;
459 ogc->elevation = (Ps + Ps_error) * SG_METER_TO_FEET;
461 /* printf("%.3f, %.3f, %.3f, %.3f, %.8f, %.8f, %.3f, %.3f, %.3f, %.3f, %.3f\n",
462 imupacket->time, imupacket->the, -navpacket->vd, climbf,
463 navpacket->lat, navpacket->lon, gpspacket->alt, navpacket->alt,
464 imupacket->Ps, Ps, Ps + Ps_error); */
466 if ( est_controls ) {
467 static float est_elev = 0.0;
468 static float est_aileron = 0.0;
469 static float est_rudder = 0.0;
470 est_elev = 0.99 * est_elev + 0.01 * (imupacket->q * 4);
471 est_aileron = 0.95 * est_aileron + 0.05 * (imupacket->p * 5);
472 est_rudder = 0.95 * est_rudder + 0.05 * (imupacket->r * 2);
473 ogc->elevator = -est_elev;
474 ogc->left_aileron = est_aileron;
475 ogc->right_aileron = -est_aileron;
476 ogc->rudder = est_rudder;
478 ogc->elevator = 1.0 - ((double)servopacket->chn[1] / 32768.0);
479 ogc->left_aileron = 1.0 - ((double)servopacket->chn[0] / 32768.0);
480 ogc->right_aileron = ((double)servopacket->chn[0] / 32768.0) - 1.0;
481 ogc->rudder = 1.0 - ((double)servopacket->chn[3] / 32768.0);
483 ogc->elevator *= 4.0;
484 ogc->left_aileron *= 4.0;
485 ogc->right_aileron *= 4.0;
488 // additional "abused" data fields
489 ogc->egt[0] = ogc->bank - healthpacket->target_roll_deg; // flight director target roll
490 ogc->egt[1] = -ogc->pitch + healthpacket->target_pitch_deg; // flight director target pitch
491 ogc->egt[2] = healthpacket->target_heading_deg; // target heading bug
492 ogc->egt[3] = healthpacket->target_climb_fps; // target VVI bug
493 ogc->epr[0] = healthpacket->target_altitude_ft; // target altitude bug
494 ogc->epr[1] = 30.0; // target speed bug
495 ogc->epr[2] = gps_status; // gps status box
499 static void send_data_udp( gps *gpspacket, imu *imupacket, nav *navpacket,
500 servo *servopacket, health *healthpacket )
503 int ogcsize = sizeof( ogcFGData );
504 int fdmsize = sizeof( FGNetFDM );
505 // int ctrlsize = sizeof( FGNetCtrls );
507 // cout << "Running main loop" << endl;
513 ugear2fg( gpspacket, imupacket, navpacket, servopacket, healthpacket,
515 ugear2opengc( gpspacket, imupacket, navpacket, servopacket, healthpacket,
517 len = opengc_sock.send(&fgogc, ogcsize, 0);
518 len = fdm_sock.send(&fgfdm, fdmsize, 0);
519 // len = ctrls_sock.send(&fgctrls, ctrlsize, 0);
523 void usage( const string &argv0 ) {
524 cout << "Usage: " << argv0 << endl;
525 cout << "\t[ --help ]" << endl;
526 cout << "\t[ --infile <infile_name>" << endl;
527 cout << "\t[ --flight <flight_dir>" << endl;
528 cout << "\t[ --serial <dev_name>" << endl;
529 cout << "\t[ --outfile <outfile_name> (capture the data to a file)" << endl;
530 cout << "\t[ --hertz <hertz> ]" << endl;
531 cout << "\t[ --host <hostname> ]" << endl;
532 cout << "\t[ --broadcast ]" << endl;
533 cout << "\t[ --opengc-port <opengc output port #> ]" << endl;
534 cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
535 cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
536 cout << "\t[ --groundtrack-heading ]" << endl;
537 cout << "\t[ --ground-speed ]" << endl;
538 cout << "\t[ --estimate-control-deflections ]" << endl;
539 cout << "\t[ --altitude-offset <meters> ]" << endl;
540 cout << "\t[ --skip-seconds <seconds> ]" << endl;
541 cout << "\t[ --no-real-time ]" << endl;
542 cout << "\t[ --ignore-checksum ]" << endl;
543 cout << "\t[ --sg-swap ]" << endl;
547 int main( int argc, char **argv ) {
549 string out_host = "localhost";
550 bool do_broadcast = false;
552 // process command line arguments
553 for ( int i = 1; i < argc; ++i ) {
554 if ( strcmp( argv[i], "--help" ) == 0 ) {
557 } else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
560 hertz = atof( argv[i] );
565 } else if ( strcmp( argv[i], "--infile" ) == 0 ) {
573 } else if ( strcmp( argv[i], "--flight" ) == 0 ) {
576 flight_dir = argv[i];
581 } else if ( strcmp( argv[i], "--outfile" ) == 0 ) {
589 } else if ( strcmp( argv[i], "--serial" ) == 0 ) {
597 } else if ( strcmp( argv[i], "--host" ) == 0 ) {
605 } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
607 } else if ( strcmp( argv[i], "--opengc-port" ) == 0 ) {
610 opengc_port = atoi( argv[i] );
615 } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
618 fdm_port = atoi( argv[i] );
623 } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
626 ctrls_port = atoi( argv[i] );
631 } else if ( strcmp (argv[i], "--groundtrack-heading" ) == 0 ) {
632 use_ground_track_hdg = true;
633 } else if ( strcmp (argv[i], "--ground-speed" ) == 0 ) {
634 use_ground_speed = true;
635 } else if (strcmp (argv[i], "--estimate-control-deflections" ) == 0) {
637 } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
640 alt_offset = atof( argv[i] );
645 } else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) {
648 skip = atof( argv[i] );
653 } else if ( strcmp( argv[i], "--no-real-time" ) == 0 ) {
654 run_real_time = false;
655 } else if ( strcmp( argv[i], "--ignore-checksum" ) == 0 ) {
656 ignore_checksum = true;
657 } else if ( strcmp( argv[i], "--sg-swap" ) == 0 ) {
665 // Setup up outgoing network connections
667 simgear::Socket::initSockets(); // We must call this before any other net stuff
669 if ( ! opengc_sock.open( false ) ) { // open a UDP socket
670 cout << "error opening opengc output socket" << endl;
673 if ( ! fdm_sock.open( false ) ) { // open a UDP socket
674 cout << "error opening fdm output socket" << endl;
677 if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
678 cout << "error opening ctrls output socket" << endl;
681 cout << "open net channels" << endl;
683 opengc_sock.setBlocking( false );
684 fdm_sock.setBlocking( false );
685 ctrls_sock.setBlocking( false );
686 cout << "blocking false" << endl;
688 if ( do_broadcast ) {
689 opengc_sock.setBroadcast( true );
690 fdm_sock.setBroadcast( true );
691 ctrls_sock.setBroadcast( true );
694 if ( opengc_sock.connect( out_host.c_str(), opengc_port ) == -1 ) {
696 cout << "error connecting to outgoing opengc port: " << out_host
697 << ":" << opengc_port << endl;
700 cout << "connected outgoing opengc socket" << endl;
702 if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
704 cout << "error connecting to outgoing fdm port: " << out_host
705 << ":" << fdm_port << endl;
708 cout << "connected outgoing fdm socket" << endl;
710 if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
712 cout << "error connecting to outgoing ctrls port: " << out_host
713 << ":" << ctrls_port << endl;
716 cout << "connected outgoing ctrls socket" << endl;
719 track.set_stargate_swap_mode();
722 UGTelnet telnet( 5402 );
725 if ( infile.length() || flight_dir.length() ) {
726 if ( infile.length() ) {
727 // Load data from a stream log data file
728 track.load_stream( infile, ignore_checksum );
729 } else if ( flight_dir.length() ) {
730 // Load data from a flight directory
731 track.load_flight( flight_dir );
733 cout << "Loaded " << track.gps_size() << " gps records." << endl;
734 cout << "Loaded " << track.imu_size() << " imu records." << endl;
735 cout << "Loaded " << track.nav_size() << " nav records." << endl;
736 cout << "Loaded " << track.servo_size() << " servo records." << endl;
737 cout << "Loaded " << track.health_size() << " health records." << endl;
739 int size = track.imu_size();
741 double current_time = track.get_imupt(0).time;
742 cout << "Track begin time is " << current_time << endl;
743 double end_time = track.get_imupt(size-1).time;
744 cout << "Track end time is " << end_time << endl;
745 cout << "Duration = " << end_time - current_time << endl;
747 if ( track.gps_size() > 0 ) {
748 double tmp = track.get_gpspt(track.gps_size()-1).ITOW;
749 int days = (int)(tmp / (24 * 60 * 60));
750 tmp -= days * 24 * 60 * 60;
751 int hours = (int)(tmp / (60 * 60));
752 tmp -= hours * 60 * 60;
753 int min = (int)(tmp / 60);
756 printf("[GPS ]:ITOW= %.3f[sec] %dd %02d:%02d:%06.3f\n",
757 tmp, days, hours, min, sec);
761 // advance skip seconds forward
762 current_time += skip;
764 frame_us = 1000000.0 / hertz;
765 if ( frame_us < 0.0 ) {
769 SGTimeStamp start_time;
775 int health_count = 0;
778 gps0 = gps1 = track.get_gpspt( 0 );
781 imu0 = imu1 = track.get_imupt( 0 );
784 nav0 = nav1 = track.get_navpt( 0 );
786 servo servo0, servo1;
787 servo0 = servo1 = track.get_servopt( 0 );
789 health health0, health1;
790 health0 = health1 = track.get_healthpt( 0 );
792 double last_lat = -999.9, last_lon = -999.9;
796 printf(" <trkseg>\n");
797 while ( current_time < end_time ) {
798 // cout << "current_time = " << current_time << " end_time = "
799 // << end_time << endl;
801 // Advance gps pointer
802 while ( current_time > gps1.time
803 && gps_count < track.gps_size() - 1 )
807 // cout << "count = " << count << endl;
808 gps1 = track.get_gpspt( gps_count );
810 // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
813 // Advance imu pointer
814 while ( current_time > imu1.time
815 && imu_count < track.imu_size() - 1 )
819 // cout << "count = " << count << endl;
820 imu1 = track.get_imupt( imu_count );
822 // cout << "pos0 = " << pos0.get_seconds()
823 // << " pos1 = " << pos1.get_seconds() << endl;
825 // Advance nav pointer
826 while ( current_time > nav1.time
827 && nav_count < track.nav_size() - 1 )
831 // cout << "nav count = " << nav_count << endl;
832 nav1 = track.get_navpt( nav_count );
834 // cout << "pos0 = " << pos0.get_seconds()
835 // << " pos1 = " << pos1.get_seconds() << endl;
837 // Advance servo pointer
838 while ( current_time > servo1.time
839 && servo_count < track.servo_size() - 1 )
843 // cout << "count = " << count << endl;
844 servo1 = track.get_servopt( servo_count );
846 // cout << "pos0 = " << pos0.get_seconds()
847 // << " pos1 = " << pos1.get_seconds() << endl;
849 // Advance health pointer
850 while ( current_time > health1.time
851 && health_count < track.health_size() - 1 )
855 // cout << "count = " << count << endl;
856 health1 = track.get_healthpt( health_count );
858 // cout << "pos0 = " << pos0.get_seconds()
859 // << " pos1 = " << pos1.get_seconds() << endl;
862 if ( fabs(gps1.time - gps0.time) < 0.00001 ) {
866 (current_time - gps0.time) /
867 (gps1.time - gps0.time);
869 // cout << "Percent = " << percent << endl;
872 if ( fabs(imu1.time - imu0.time) < 0.00001 ) {
876 (current_time - imu0.time) /
877 (imu1.time - imu0.time);
879 // cout << "Percent = " << percent << endl;
882 if ( fabs(nav1.time - nav0.time) < 0.00001 ) {
886 (current_time - nav0.time) /
887 (nav1.time - nav0.time);
889 // cout << "Percent = " << percent << endl;
891 double servo_percent;
892 if ( fabs(servo1.time - servo0.time) < 0.00001 ) {
896 (current_time - servo0.time) /
897 (servo1.time - servo0.time);
899 // cout << "Percent = " << percent << endl;
901 double health_percent;
902 if ( fabs(health1.time - health0.time) < 0.00001 ) {
903 health_percent = 0.0;
906 (current_time - health0.time) /
907 (health1.time - health0.time);
909 // cout << "Percent = " << percent << endl;
911 gps gpspacket = UGEARInterpGPS( gps0, gps1, gps_percent );
912 imu imupacket = UGEARInterpIMU( imu0, imu1, imu_percent );
913 nav navpacket = UGEARInterpNAV( nav0, nav1, nav_percent );
914 servo servopacket = UGEARInterpSERVO( servo0, servo1,
916 health healthpacket = UGEARInterpHEALTH( health0, health1,
919 // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg
921 // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
923 // cout << (double)current_time << " " << pos.lat_deg << ", "
924 // << pos.lon_deg << " " << att.yaw_deg << endl;
925 if ( gpspacket.lat > -500 ) {
926 // printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
928 // navpacket.lat, navpacket.lon, navpacket.alt,
929 // imupacket.psi, imupacket.the, imupacket.phi );
930 double dlat = last_lat - navpacket.lat;
931 double dlon = last_lon - navpacket.lon;
932 double dist = sqrt( dlat*dlat + dlon*dlon );
934 printf(" <trkpt lat=\"%.8f\" lon=\"%.8f\"></trkpt>\n",
935 navpacket.lat, navpacket.lon );
936 // printf(" </wpt>\n");
937 last_lat = navpacket.lat;
938 last_lon = navpacket.lon;
942 if ( (fabs(gpspacket.lat) < 0.0001 &&
943 fabs(gpspacket.lon) < 0.0001 &&
944 fabs(gpspacket.alt) < 0.0001) )
946 printf("WARNING: LOST GPS!!!\n");
952 send_data_udp( &gpspacket, &imupacket, &navpacket, &servopacket,
955 if ( run_real_time ) {
956 // Update the elapsed time.
957 static bool first_time = true;
959 last_time_stamp.stamp();
963 current_time_stamp.stamp();
965 double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs();
966 if ( elapsed_us < (frame_us - 2000) ) {
967 double requested_us = (frame_us - elapsed_us) - 2000 ;
968 ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
970 current_time_stamp.stamp();
971 while ( (current_time_stamp - last_time_stamp).toUSecs() < frame_us ) {
972 current_time_stamp.stamp();
976 current_time += (frame_us / 1000000.0);
977 last_time_stamp = current_time_stamp;
980 printf(" <trkpt lat=\"%.8f\" lon=\"%.8f\"></trkpt>\n",
981 nav1.lat, nav1.lon );
983 printf(" </trkseg>\n");
985 nav0 = track.get_navpt( 0 );
986 nav1 = track.get_navpt( track.nav_size() - 1 );
987 printf(" <wpt lat=\"%.8f\" lon=\"%.8f\"></wpt>\n",
988 nav0.lat, nav0.lon );
989 printf(" <wpt lat=\"%.8f\" lon=\"%.8f\"></wpt>\n",
990 nav1.lat, nav1.lon );
993 cout << "Processed " << imu_count << " entries in "
994 << current_time_stamp - start_time << " seconds."
996 } else if ( serialdev.length() ) {
997 // process incoming data from the serial port
1000 double current_time = 0.0;
1001 double last_time = 0.0;
1003 gps gpspacket; bzero( &gpspacket, sizeof(gpspacket) );
1004 imu imupacket; bzero( &imupacket, sizeof(imupacket) );
1005 nav navpacket; bzero( &navpacket, sizeof(navpacket) );
1006 servo servopacket; bzero( &servopacket, sizeof(servopacket) );
1007 health healthpacket; bzero( &healthpacket, sizeof(healthpacket) );
1009 double gps_time = 0.0;
1010 double imu_time = 0.0;
1011 double nav_time = 0.0;
1012 double servo_time = 0.0;
1013 double health_time = 0.0;
1014 double command_time = 0.0;
1015 double command_heartbeat = 0.0;
1017 // open the serial port device
1018 SGSerialPort uavcom( serialdev, 115200 );
1019 if ( !uavcom.is_enabled() ) {
1020 cout << "Cannot open: " << serialdev << endl;
1024 // open up the data log file if requested
1025 if ( !outfile.length() ) {
1026 cout << "no --outfile <name> specified, cannot capture data!"
1030 SGFile log( outfile );
1031 if ( !log.open( SG_IO_OUT ) ) {
1032 cout << "Cannot open: " << outfile << endl;
1036 // add some test commands
1037 //command_mgr.add("ap,alt,1000");
1038 //command_mgr.add("home,158.0,32.5");
1039 //command_mgr.add("go,home");
1040 //command_mgr.add("go,route");
1042 while ( uavcom.is_enabled() ) {
1043 // cout << "looking for next message ..." << endl;
1044 int id = track.next_message( &uavcom, &log, &gpspacket,
1045 &imupacket, &navpacket, &servopacket,
1046 &healthpacket, ignore_checksum );
1047 // cout << "message id = " << id << endl;
1052 if ( id == GPS_PACKET ) {
1053 if ( gpspacket.time > gps_time ) {
1054 gps_time = gpspacket.time;
1055 current_time = gps_time;
1057 cout << "oops gps back in time: " << gpspacket.time << " " << gps_time << endl;
1059 } else if ( id == IMU_PACKET ) {
1060 if ( imupacket.time > imu_time ) {
1061 imu_time = imupacket.time;
1062 current_time = imu_time;
1064 cout << "oops imu back in time: " << imupacket.time << " " << imu_time << endl;
1066 } else if ( id == NAV_PACKET ) {
1067 if ( navpacket.time > nav_time ) {
1068 nav_time = navpacket.time;
1069 current_time = nav_time;
1071 cout << "oops nav back in time: " << navpacket.time << " " << nav_time << endl;
1073 } else if ( id == SERVO_PACKET ) {
1074 if ( servopacket.time > servo_time ) {
1075 servo_time = servopacket.time;
1076 current_time = servo_time;
1078 cout << "oops servo back in time: " << servopacket.time << " " << servo_time << endl;
1080 } else if ( id == HEALTH_PACKET ) {
1081 if ( healthpacket.time > health_time ) {
1082 health_time = healthpacket.time;
1083 current_time = health_time;
1084 printf("Received a health packet, sequence: %d\n",
1085 (int)healthpacket.command_sequence);
1086 command_mgr.update_cmd_sequence(healthpacket.command_sequence);
1088 cout << "oops health back in time: " << healthpacket.time << " " << health_time << endl;
1093 if ( (current_time > gps_time + 2) ||
1094 (fabs(gpspacket.lat) < 0.0001 &&
1095 fabs(gpspacket.lon) < 0.0001 &&
1096 fabs(gpspacket.alt) < 0.0001) )
1098 printf("WARNING: LOST GPS!!!\n");
1104 // Generate a ground station heart beat every 4 seconds
1105 if ( current_time >= command_heartbeat + 4 ) {
1106 command_mgr.add("hb");
1107 command_heartbeat = current_time;
1110 // Command update @ 1hz
1111 if ( current_time >= command_time + 1 ) {
1112 command_mgr.update(&uavcom);
1113 command_time = current_time;
1116 // Relay data on to FlightGear and LFSTech Glass
1117 if ( current_time >= last_time + (1/hertz) ) {
1118 // if ( gpspacket.lat > -500 ) {
1119 int londeg = (int)navpacket.lon;
1120 // double lonmin = fabs(navpacket.lon - londeg);
1121 int latdeg = (int)navpacket.lat;
1122 // double latmin = fabs(navpacket.lat - latdeg);
1123 char londir = 'E'; if ( londeg < 0 ) londir = 'W';
1124 char latdir = 'N'; if ( latdeg < 0 ) latdir = 'S';
1125 londeg = abs(londeg);
1126 latdeg = abs(latdeg);
1127 /*printf( "%.2f %c%02d:%.4f %c%03d:%.4f %.1f %.2f %.2f %.2f\n",
1129 latdir, latdeg, latmin, londir, londeg, lonmin,
1131 imupacket.phi*SGD_RADIANS_TO_DEGREES,
1132 imupacket.the*SGD_RADIANS_TO_DEGREES,
1133 imupacket.psi*SGD_RADIANS_TO_DEGREES ); */
1136 last_time = current_time;
1137 send_data_udp( &gpspacket, &imupacket, &navpacket,
1138 &servopacket, &healthpacket );