| 1 | |
| 2 | #include <config.h> |
| 3 | |
| 4 | #ifdef HAVE_WINDOWS_H |
| 5 | # include <windows.h> |
| 6 | #else |
| 7 | # include <netinet/in.h> // htonl() ntohl() |
| 8 | #endif |
| 9 | |
| 10 | #include <cstdlib> |
| 11 | |
| 12 | #include <chrono> |
| 13 | #include <iostream> |
| 14 | #include <string> |
| 15 | #include <thread> |
| 16 | |
| 17 | #include <simgear/constants.h> |
| 18 | #include <simgear/io/lowlevel.hxx> // endian tests |
| 19 | #include <simgear/io/sg_file.hxx> |
| 20 | #include <simgear/io/sg_serial.hxx> |
| 21 | #include <simgear/io/raw_socket.hxx> |
| 22 | #include <simgear/math/sg_geodesy.hxx> |
| 23 | #include <simgear/timing/timestamp.hxx> |
| 24 | |
| 25 | #include <Network/net_ctrls.hxx> |
| 26 | #include <Network/net_fdm.hxx> |
| 27 | |
| 28 | #include "MIDG-II.hxx" |
| 29 | |
| 30 | |
| 31 | using std::cout; |
| 32 | using std::endl; |
| 33 | using std::string; |
| 34 | |
| 35 | |
| 36 | // Network channels |
| 37 | static simgear::Socket fdm_sock, ctrls_sock; |
| 38 | |
| 39 | // midg data |
| 40 | MIDGTrack track; |
| 41 | |
| 42 | // Default ports |
| 43 | static int fdm_port = 5505; |
| 44 | static int ctrls_port = 5506; |
| 45 | |
| 46 | // Default path |
| 47 | static string infile = ""; |
| 48 | static string serialdev = ""; |
| 49 | static string outfile = ""; |
| 50 | |
| 51 | // Master time counter |
| 52 | float sim_time = 0.0f; |
| 53 | double frame_us = 0.0f; |
| 54 | |
| 55 | // sim control |
| 56 | SGTimeStamp last_time_stamp; |
| 57 | SGTimeStamp current_time_stamp; |
| 58 | |
| 59 | // altitude offset |
| 60 | double alt_offset = 0.0; |
| 61 | |
| 62 | // skip initial seconds |
| 63 | double skip = 0.0; |
| 64 | |
| 65 | // for speed estimate |
| 66 | // double last_lat = 0.0, last_lon = 0.0; |
| 67 | // double kts_filter = 0.0; |
| 68 | |
| 69 | bool inited = false; |
| 70 | |
| 71 | |
| 72 | // The function htond is defined this way due to the way some |
| 73 | // processors and OSes treat floating point values. Some will raise |
| 74 | // an exception whenever a "bad" floating point value is loaded into a |
| 75 | // floating point register. Solaris is notorious for this, but then |
| 76 | // so is LynxOS on the PowerPC. By translating the data in place, |
| 77 | // there is no need to load a FP register with the "corrupted" floating |
| 78 | // point value. By doing the BIG_ENDIAN test, I can optimize the |
| 79 | // routine for big-endian processors so it can be as efficient as |
| 80 | // possible |
| 81 | static void htond (double &x) |
| 82 | { |
| 83 | if ( sgIsLittleEndian() ) { |
| 84 | int *Double_Overlay; |
| 85 | int Holding_Buffer; |
| 86 | |
| 87 | Double_Overlay = (int *) &x; |
| 88 | Holding_Buffer = Double_Overlay [0]; |
| 89 | |
| 90 | Double_Overlay [0] = htonl (Double_Overlay [1]); |
| 91 | Double_Overlay [1] = htonl (Holding_Buffer); |
| 92 | } else { |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Float version |
| 98 | static void htonf (float &x) |
| 99 | { |
| 100 | if ( sgIsLittleEndian() ) { |
| 101 | int *Float_Overlay; |
| 102 | int Holding_Buffer; |
| 103 | |
| 104 | Float_Overlay = (int *) &x; |
| 105 | Holding_Buffer = Float_Overlay [0]; |
| 106 | |
| 107 | Float_Overlay [0] = htonl (Holding_Buffer); |
| 108 | } else { |
| 109 | return; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static void midg2fg( const MIDGpos pos, const MIDGatt att, |
| 115 | FGNetFDM *fdm, FGNetCtrls *ctrls ) |
| 116 | { |
| 117 | unsigned int i; |
| 118 | |
| 119 | // Version sanity checking |
| 120 | fdm->version = FG_NET_FDM_VERSION; |
| 121 | |
| 122 | // Aero parameters |
| 123 | fdm->longitude = pos.lon_deg * SGD_DEGREES_TO_RADIANS; |
| 124 | fdm->latitude = pos.lat_deg * SGD_DEGREES_TO_RADIANS; |
| 125 | fdm->altitude = pos.altitude_msl + alt_offset; |
| 126 | fdm->agl = -9999.0; |
| 127 | fdm->psi = att.yaw_rad; // heading |
| 128 | fdm->phi = att.roll_rad; // roll |
| 129 | fdm->theta = att.pitch_rad; // pitch; |
| 130 | |
| 131 | fdm->phidot = 0.0; |
| 132 | fdm->thetadot = 0.0; |
| 133 | fdm->psidot = 0.0; |
| 134 | |
| 135 | // estimate speed |
| 136 | // double az1, az2, dist; |
| 137 | // geo_inverse_wgs_84( pos.altitude_msl, last_lat, last_lon, |
| 138 | // pos.lat_deg, pos.lon_deg, &az1, &az2, &dist ); |
| 139 | // double v_ms = dist / (frame_us / 1000000); |
| 140 | // double v_kts = v_ms * SG_METER_TO_NM * 3600; |
| 141 | // kts_filter = (0.99 * kts_filter) + (0.01 * v_kts); |
| 142 | fdm->vcas = pos.speed_kts; |
| 143 | // last_lat = pos.lat_deg; |
| 144 | // last_lon = pos.lon_deg; |
| 145 | // cout << "kts_filter = " << kts_filter << " vel = " << pos.speed_kts << endl; |
| 146 | |
| 147 | fdm->climb_rate = 0; // fps |
| 148 | // cout << "climb rate = " << aero->hdota << endl; |
| 149 | fdm->v_north = 0.0; |
| 150 | fdm->v_east = 0.0; |
| 151 | fdm->v_down = 0.0; |
| 152 | fdm->v_body_u = 0.0; |
| 153 | fdm->v_body_v = 0.0; |
| 154 | fdm->v_body_w = 0.0; |
| 155 | fdm->stall_warning = 0.0; |
| 156 | |
| 157 | fdm->A_X_pilot = 0.0; |
| 158 | fdm->A_Y_pilot = 0.0; |
| 159 | fdm->A_Z_pilot = 0.0 /* (should be -G) */; |
| 160 | |
| 161 | // Engine parameters |
| 162 | fdm->num_engines = 1; |
| 163 | fdm->eng_state[0] = 2; |
| 164 | // cout << "state = " << fdm->eng_state[0] << endl; |
| 165 | double rpm = ((pos.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0; |
| 166 | if ( rpm < 0.0 ) { rpm = 0.0; } |
| 167 | if ( rpm > 3000.0 ) { rpm = 3000.0; } |
| 168 | fdm->rpm[0] = rpm; |
| 169 | |
| 170 | fdm->fuel_flow[0] = 0.0; |
| 171 | fdm->egt[0] = 0.0; |
| 172 | // cout << "egt = " << aero->EGT << endl; |
| 173 | fdm->oil_temp[0] = 0.0; |
| 174 | fdm->oil_px[0] = 0.0; |
| 175 | |
| 176 | // Consumables |
| 177 | fdm->num_tanks = 2; |
| 178 | fdm->fuel_quantity[0] = 0.0; |
| 179 | fdm->fuel_quantity[1] = 0.0; |
| 180 | |
| 181 | // Gear and flaps |
| 182 | fdm->num_wheels = 3; |
| 183 | fdm->wow[0] = 0; |
| 184 | fdm->wow[1] = 0; |
| 185 | fdm->wow[2] = 0; |
| 186 | |
| 187 | // the following really aren't used in this context |
| 188 | fdm->cur_time = 0; |
| 189 | fdm->warp = 0; |
| 190 | fdm->visibility = 0; |
| 191 | |
| 192 | // cout << "Flap deflection = " << aero->dflap << endl; |
| 193 | fdm->left_flap = 0.0; |
| 194 | fdm->right_flap = 0.0; |
| 195 | |
| 196 | fdm->elevator = -fdm->theta * 1.0; |
| 197 | fdm->elevator_trim_tab = 0.0; |
| 198 | fdm->left_flap = 0.0; |
| 199 | fdm->right_flap = 0.0; |
| 200 | fdm->left_aileron = fdm->phi * 1.0; |
| 201 | fdm->right_aileron = -fdm->phi * 1.0; |
| 202 | fdm->rudder = 0.0; |
| 203 | fdm->nose_wheel = 0.0; |
| 204 | fdm->speedbrake = 0.0; |
| 205 | fdm->spoilers = 0.0; |
| 206 | |
| 207 | // Convert the net buffer to network format |
| 208 | fdm->version = htonl(fdm->version); |
| 209 | |
| 210 | htond(fdm->longitude); |
| 211 | htond(fdm->latitude); |
| 212 | htond(fdm->altitude); |
| 213 | htonf(fdm->agl); |
| 214 | htonf(fdm->phi); |
| 215 | htonf(fdm->theta); |
| 216 | htonf(fdm->psi); |
| 217 | htonf(fdm->alpha); |
| 218 | htonf(fdm->beta); |
| 219 | |
| 220 | htonf(fdm->phidot); |
| 221 | htonf(fdm->thetadot); |
| 222 | htonf(fdm->psidot); |
| 223 | htonf(fdm->vcas); |
| 224 | htonf(fdm->climb_rate); |
| 225 | htonf(fdm->v_north); |
| 226 | htonf(fdm->v_east); |
| 227 | htonf(fdm->v_down); |
| 228 | htonf(fdm->v_body_u); |
| 229 | htonf(fdm->v_body_v); |
| 230 | htonf(fdm->v_body_w); |
| 231 | |
| 232 | htonf(fdm->A_X_pilot); |
| 233 | htonf(fdm->A_Y_pilot); |
| 234 | htonf(fdm->A_Z_pilot); |
| 235 | |
| 236 | htonf(fdm->stall_warning); |
| 237 | htonf(fdm->slip_deg); |
| 238 | |
| 239 | for ( i = 0; i < fdm->num_engines; ++i ) { |
| 240 | fdm->eng_state[i] = htonl(fdm->eng_state[i]); |
| 241 | htonf(fdm->rpm[i]); |
| 242 | htonf(fdm->fuel_flow[i]); |
| 243 | htonf(fdm->egt[i]); |
| 244 | htonf(fdm->cht[i]); |
| 245 | htonf(fdm->mp_osi[i]); |
| 246 | htonf(fdm->tit[i]); |
| 247 | htonf(fdm->oil_temp[i]); |
| 248 | htonf(fdm->oil_px[i]); |
| 249 | } |
| 250 | fdm->num_engines = htonl(fdm->num_engines); |
| 251 | |
| 252 | for ( i = 0; i < fdm->num_tanks; ++i ) { |
| 253 | htonf(fdm->fuel_quantity[i]); |
| 254 | } |
| 255 | fdm->num_tanks = htonl(fdm->num_tanks); |
| 256 | |
| 257 | for ( i = 0; i < fdm->num_wheels; ++i ) { |
| 258 | fdm->wow[i] = htonl(fdm->wow[i]); |
| 259 | htonf(fdm->gear_pos[i]); |
| 260 | htonf(fdm->gear_steer[i]); |
| 261 | htonf(fdm->gear_compression[i]); |
| 262 | } |
| 263 | fdm->num_wheels = htonl(fdm->num_wheels); |
| 264 | |
| 265 | fdm->cur_time = htonl( fdm->cur_time ); |
| 266 | fdm->warp = htonl( fdm->warp ); |
| 267 | htonf(fdm->visibility); |
| 268 | |
| 269 | htonf(fdm->elevator); |
| 270 | htonf(fdm->elevator_trim_tab); |
| 271 | htonf(fdm->left_flap); |
| 272 | htonf(fdm->right_flap); |
| 273 | htonf(fdm->left_aileron); |
| 274 | htonf(fdm->right_aileron); |
| 275 | htonf(fdm->rudder); |
| 276 | htonf(fdm->nose_wheel); |
| 277 | htonf(fdm->speedbrake); |
| 278 | htonf(fdm->spoilers); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | static void send_data( const MIDGpos pos, const MIDGatt att ) { |
| 283 | int fdmsize = sizeof( FGNetFDM ); |
| 284 | |
| 285 | FGNetFDM fgfdm; |
| 286 | FGNetCtrls fgctrls; |
| 287 | |
| 288 | midg2fg( pos, att, &fgfdm, &fgctrls ); |
| 289 | fdm_sock.send(&fgfdm, fdmsize, 0); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | void usage( const string &argv0 ) { |
| 294 | cout << "Usage: " << argv0 << endl; |
| 295 | cout << "\t[ --help ]" << endl; |
| 296 | cout << "\t[ --infile <infile_name>" << endl; |
| 297 | cout << "\t[ --serial <dev_name>" << endl; |
| 298 | cout << "\t[ --outfile <outfile_name> (capture the data to a file)" << endl; |
| 299 | cout << "\t[ --hertz <hertz> ]" << endl; |
| 300 | cout << "\t[ --host <hostname> ]" << endl; |
| 301 | cout << "\t[ --broadcast ]" << endl; |
| 302 | cout << "\t[ --fdm-port <fdm output port #> ]" << endl; |
| 303 | cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl; |
| 304 | cout << "\t[ --altitude-offset <meters> ]" << endl; |
| 305 | cout << "\t[ --skip-seconds <seconds> ]" << endl; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | int main( int argc, char **argv ) { |
| 310 | double hertz = 60.0; |
| 311 | string out_host = "localhost"; |
| 312 | bool do_broadcast = false; |
| 313 | |
| 314 | // process command line arguments |
| 315 | for ( int i = 1; i < argc; ++i ) { |
| 316 | if ( strcmp( argv[i], "--help" ) == 0 ) { |
| 317 | usage( argv[0] ); |
| 318 | exit( 0 ); |
| 319 | } else if ( strcmp( argv[i], "--hertz" ) == 0 ) { |
| 320 | ++i; |
| 321 | if ( i < argc ) { |
| 322 | hertz = atof( argv[i] ); |
| 323 | } else { |
| 324 | usage( argv[0] ); |
| 325 | exit( -1 ); |
| 326 | } |
| 327 | } else if ( strcmp( argv[i], "--infile" ) == 0 ) { |
| 328 | ++i; |
| 329 | if ( i < argc ) { |
| 330 | infile = argv[i]; |
| 331 | } else { |
| 332 | usage( argv[0] ); |
| 333 | exit( -1 ); |
| 334 | } |
| 335 | } else if ( strcmp( argv[i], "--outfile" ) == 0 ) { |
| 336 | ++i; |
| 337 | if ( i < argc ) { |
| 338 | outfile = argv[i]; |
| 339 | } else { |
| 340 | usage( argv[0] ); |
| 341 | exit( -1 ); |
| 342 | } |
| 343 | } else if ( strcmp( argv[i], "--serial" ) == 0 ) { |
| 344 | ++i; |
| 345 | if ( i < argc ) { |
| 346 | serialdev = argv[i]; |
| 347 | } else { |
| 348 | usage( argv[0] ); |
| 349 | exit( -1 ); |
| 350 | } |
| 351 | } else if ( strcmp( argv[i], "--host" ) == 0 ) { |
| 352 | ++i; |
| 353 | if ( i < argc ) { |
| 354 | out_host = argv[i]; |
| 355 | } else { |
| 356 | usage( argv[0] ); |
| 357 | exit( -1 ); |
| 358 | } |
| 359 | } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) { |
| 360 | do_broadcast = true; |
| 361 | } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) { |
| 362 | ++i; |
| 363 | if ( i < argc ) { |
| 364 | fdm_port = atoi( argv[i] ); |
| 365 | } else { |
| 366 | usage( argv[0] ); |
| 367 | exit( -1 ); |
| 368 | } |
| 369 | } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) { |
| 370 | ++i; |
| 371 | if ( i < argc ) { |
| 372 | ctrls_port = atoi( argv[i] ); |
| 373 | } else { |
| 374 | usage( argv[0] ); |
| 375 | exit( -1 ); |
| 376 | } |
| 377 | } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) { |
| 378 | ++i; |
| 379 | if ( i < argc ) { |
| 380 | alt_offset = atof( argv[i] ); |
| 381 | } else { |
| 382 | usage( argv[0] ); |
| 383 | exit( -1 ); |
| 384 | } |
| 385 | } else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) { |
| 386 | ++i; |
| 387 | if ( i < argc ) { |
| 388 | skip = atof( argv[i] ); |
| 389 | } else { |
| 390 | usage( argv[0] ); |
| 391 | exit( -1 ); |
| 392 | } |
| 393 | } else { |
| 394 | usage( argv[0] ); |
| 395 | exit( -1 ); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Setup up outgoing network connections |
| 400 | |
| 401 | simgear::Socket::initSockets(); // We must call this before any other net stuff |
| 402 | |
| 403 | if ( ! fdm_sock.open( false ) ) { // open a UDP socket |
| 404 | cout << "error opening fdm output socket" << endl; |
| 405 | return -1; |
| 406 | } |
| 407 | if ( ! ctrls_sock.open( false ) ) { // open a UDP socket |
| 408 | cout << "error opening ctrls output socket" << endl; |
| 409 | return -1; |
| 410 | } |
| 411 | cout << "open net channels" << endl; |
| 412 | |
| 413 | fdm_sock.setBlocking( false ); |
| 414 | ctrls_sock.setBlocking( false ); |
| 415 | cout << "blocking false" << endl; |
| 416 | |
| 417 | if ( do_broadcast ) { |
| 418 | fdm_sock.setBroadcast( true ); |
| 419 | ctrls_sock.setBroadcast( true ); |
| 420 | } |
| 421 | |
| 422 | if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) { |
| 423 | perror("connect"); |
| 424 | cout << "error connecting to outgoing fdm port: " << out_host |
| 425 | << ":" << fdm_port << endl; |
| 426 | return -1; |
| 427 | } |
| 428 | cout << "connected outgoing fdm socket" << endl; |
| 429 | |
| 430 | if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) { |
| 431 | perror("connect"); |
| 432 | cout << "error connecting to outgoing ctrls port: " << out_host |
| 433 | << ":" << ctrls_port << endl; |
| 434 | return -1; |
| 435 | } |
| 436 | cout << "connected outgoing ctrls socket" << endl; |
| 437 | |
| 438 | if ( infile.length() ) { |
| 439 | // Load data from a track data |
| 440 | track.load( infile ); |
| 441 | cout << "Loaded " << track.pos_size() << " position records." << endl; |
| 442 | cout << "Loaded " << track.att_size() << " attitude records." << endl; |
| 443 | |
| 444 | int size = track.pos_size(); |
| 445 | |
| 446 | double current_time = track.get_pospt(0).get_seconds(); |
| 447 | cout << "Track begin time is " << current_time << endl; |
| 448 | double end_time = track.get_pospt(size-1).get_seconds(); |
| 449 | cout << "Track end time is " << end_time << endl; |
| 450 | cout << "Duration = " << end_time - current_time << endl; |
| 451 | |
| 452 | // advance skip seconds forward |
| 453 | current_time += skip; |
| 454 | |
| 455 | frame_us = 1000000.0 / hertz; |
| 456 | if ( frame_us < 0.0 ) { |
| 457 | frame_us = 0.0; |
| 458 | } |
| 459 | |
| 460 | SGTimeStamp start_time; |
| 461 | start_time.stamp(); |
| 462 | int pos_count = 0; |
| 463 | int att_count = 0; |
| 464 | |
| 465 | MIDGpos pos0, pos1; |
| 466 | pos0 = pos1 = track.get_pospt( 0 ); |
| 467 | |
| 468 | MIDGatt att0, att1; |
| 469 | att0 = att1 = track.get_attpt( 0 ); |
| 470 | |
| 471 | while ( current_time < end_time ) { |
| 472 | // cout << "current_time = " << current_time << " end_time = " |
| 473 | // << end_time << endl; |
| 474 | |
| 475 | // Advance position pointer |
| 476 | while ( current_time > pos1.get_seconds() |
| 477 | && pos_count < track.pos_size() ) |
| 478 | { |
| 479 | pos0 = pos1; |
| 480 | ++pos_count; |
| 481 | // cout << "count = " << count << endl; |
| 482 | pos1 = track.get_pospt( pos_count ); |
| 483 | } |
| 484 | // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time() |
| 485 | // << endl; |
| 486 | |
| 487 | // Advance attitude pointer |
| 488 | while ( current_time > att1.get_seconds() |
| 489 | && att_count < track.att_size() ) |
| 490 | { |
| 491 | att0 = att1; |
| 492 | ++att_count; |
| 493 | // cout << "count = " << count << endl; |
| 494 | att1 = track.get_attpt( att_count ); |
| 495 | } |
| 496 | // cout << "pos0 = " << pos0.get_seconds() |
| 497 | // << " pos1 = " << pos1.get_seconds() << endl; |
| 498 | |
| 499 | double pos_percent; |
| 500 | if ( fabs(pos1.get_seconds() - pos0.get_seconds()) < 0.00001 ) { |
| 501 | pos_percent = 0.0; |
| 502 | } else { |
| 503 | pos_percent = |
| 504 | (current_time - pos0.get_seconds()) / |
| 505 | (pos1.get_seconds() - pos0.get_seconds()); |
| 506 | } |
| 507 | // cout << "Percent = " << percent << endl; |
| 508 | double att_percent; |
| 509 | if ( fabs(att1.get_seconds() - att0.get_seconds()) < 0.00001 ) { |
| 510 | att_percent = 0.0; |
| 511 | } else { |
| 512 | att_percent = |
| 513 | (current_time - att0.get_seconds()) / |
| 514 | (att1.get_seconds() - att0.get_seconds()); |
| 515 | } |
| 516 | // cout << "Percent = " << percent << endl; |
| 517 | |
| 518 | MIDGpos pos = MIDGInterpPos( pos0, pos1, pos_percent ); |
| 519 | MIDGatt att = MIDGInterpAtt( att0, att1, att_percent ); |
| 520 | // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg |
| 521 | // << endl; |
| 522 | // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg |
| 523 | // << endl; |
| 524 | // cout << (double)current_time << " " << pos.lat_deg << ", " |
| 525 | // << pos.lon_deg << " " << att.yaw_deg << endl; |
| 526 | if ( pos.lat_deg > -500 ) { |
| 527 | printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n", |
| 528 | current_time, |
| 529 | pos.lat_deg, pos.lon_deg, pos.altitude_msl, |
| 530 | att.yaw_rad * 180.0 / SG_PI, |
| 531 | att.pitch_rad * 180.0 / SG_PI, |
| 532 | att.roll_rad * 180.0 / SG_PI ); |
| 533 | } |
| 534 | |
| 535 | send_data( pos, att ); |
| 536 | |
| 537 | // Update the elapsed time. |
| 538 | static bool first_time = true; |
| 539 | if ( first_time ) { |
| 540 | last_time_stamp.stamp(); |
| 541 | first_time = false; |
| 542 | } |
| 543 | |
| 544 | current_time_stamp.stamp(); |
| 545 | /* Convert to ms */ |
| 546 | double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs(); |
| 547 | if ( elapsed_us < (frame_us - 2000) ) { |
| 548 | double requested_us = (frame_us - elapsed_us) - 2000 ; |
| 549 | std::this_thread::sleep_for(std::chrono::milliseconds((int) (requested_us / 1000.0))); |
| 550 | } |
| 551 | current_time_stamp.stamp(); |
| 552 | while ( (current_time_stamp - last_time_stamp).toUSecs() < frame_us ) { |
| 553 | current_time_stamp.stamp(); |
| 554 | } |
| 555 | |
| 556 | current_time += (frame_us / 1000000.0); |
| 557 | last_time_stamp = current_time_stamp; |
| 558 | } |
| 559 | |
| 560 | cout << "Processed " << pos_count << " entries in " |
| 561 | << current_time_stamp - start_time << " seconds." |
| 562 | << endl; |
| 563 | } else if ( serialdev.length() ) { |
| 564 | // process incoming data from the serial port |
| 565 | |
| 566 | int count = 0; |
| 567 | |
| 568 | MIDGpos pos; |
| 569 | MIDGatt att; |
| 570 | |
| 571 | uint32_t pos_time = 1; |
| 572 | uint32_t att_time = 1; |
| 573 | |
| 574 | // open the serial port device |
| 575 | SGSerialPort input( serialdev, 115200 ); |
| 576 | if ( !input.is_enabled() ) { |
| 577 | cout << "Cannot open: " << serialdev << endl; |
| 578 | return EXIT_FAILURE; |
| 579 | } |
| 580 | |
| 581 | // open up the data log file if requested |
| 582 | if ( !outfile.length() ) { |
| 583 | cout << "no --outfile <name> specified, cannot capture data!" |
| 584 | << endl; |
| 585 | return EXIT_FAILURE; |
| 586 | } |
| 587 | SGFile output( outfile ); |
| 588 | if ( !output.open( SG_IO_OUT ) ) { |
| 589 | cout << "Cannot open: " << outfile << endl; |
| 590 | return EXIT_FAILURE; |
| 591 | } |
| 592 | |
| 593 | while ( input.is_enabled() ) { |
| 594 | // cout << "looking for next message ..." << endl; |
| 595 | int id = track.next_message( &input, &output, &pos, &att ); |
| 596 | cout << "message id = " << id << endl; |
| 597 | count++; |
| 598 | |
| 599 | if ( id == 10 ) { |
| 600 | if ( att.get_msec() > att_time ) { |
| 601 | att_time = att.get_msec(); |
| 602 | //current_time = att_time; |
| 603 | } else { |
| 604 | cout << "oops att back in time" << endl; |
| 605 | } |
| 606 | } else if ( id == 12 ) { |
| 607 | if ( pos.get_msec() > pos_time ) { |
| 608 | pos_time = pos.get_msec(); |
| 609 | //current_time = pos_time; |
| 610 | } else { |
| 611 | cout << "oops pos back in time" << endl; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | if ( pos.lat_deg > -500 ) { |
| 616 | // printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n", |
| 617 | // current_time, |
| 618 | // pos.lat_deg, pos.lon_deg, pos.altitude_msl, |
| 619 | // att.yaw_rad * 180.0 / SG_PI, |
| 620 | // att.pitch_rad * 180.0 / SG_PI, |
| 621 | // att.roll_rad * 180.0 / SG_PI ); |
| 622 | } |
| 623 | |
| 624 | send_data( pos, att ); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | return 0; |
| 629 | } |