| 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/raw_socket.hxx> |
| 21 | #include <simgear/serial/serial.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 "UGear.hxx" |
| 29 | #include "UGear_command.hxx" |
| 30 | #include "UGear_opengc.hxx" |
| 31 | #include "UGear_telnet.hxx" |
| 32 | |
| 33 | |
| 34 | using std::cout; |
| 35 | using std::endl; |
| 36 | using std::string; |
| 37 | |
| 38 | |
| 39 | // Network channels |
| 40 | static simgear::Socket fdm_sock, ctrls_sock, opengc_sock; |
| 41 | |
| 42 | // ugear data |
| 43 | UGTrack track; |
| 44 | |
| 45 | // Default ports |
| 46 | static int fdm_port = 5505; |
| 47 | static int ctrls_port = 5506; |
| 48 | static int opengc_port = 6000; |
| 49 | |
| 50 | // Default path |
| 51 | static string infile = ""; |
| 52 | static string flight_dir = ""; |
| 53 | static string serialdev = ""; |
| 54 | static string outfile = ""; |
| 55 | |
| 56 | // Master time counter |
| 57 | float sim_time = 0.0f; |
| 58 | double frame_us = 0.0f; |
| 59 | |
| 60 | // sim control |
| 61 | SGTimeStamp last_time_stamp; |
| 62 | SGTimeStamp current_time_stamp; |
| 63 | |
| 64 | // altitude offset |
| 65 | double alt_offset = 0.0; |
| 66 | |
| 67 | // skip initial seconds |
| 68 | double skip = 0.0; |
| 69 | |
| 70 | // for speed estimate |
| 71 | double last_lat = 0.0, last_lon = 0.0; |
| 72 | double kts_filter = 0.0; |
| 73 | |
| 74 | bool inited = false; |
| 75 | |
| 76 | bool run_real_time = true; |
| 77 | |
| 78 | bool ignore_checksum = false; |
| 79 | |
| 80 | bool sg_swap = false; |
| 81 | |
| 82 | bool use_ground_track_hdg = false; |
| 83 | bool use_ground_speed = false; |
| 84 | |
| 85 | bool est_controls = false; |
| 86 | |
| 87 | float gps_status = -1.0; |
| 88 | |
| 89 | |
| 90 | // The function htond is defined this way due to the way some |
| 91 | // processors and OSes treat floating point values. Some will raise |
| 92 | // an exception whenever a "bad" floating point value is loaded into a |
| 93 | // floating point register. Solaris is notorious for this, but then |
| 94 | // so is LynxOS on the PowerPC. By translating the data in place, |
| 95 | // there is no need to load a FP register with the "corruped" floating |
| 96 | // point value. By doing the BIG_ENDIAN test, I can optimize the |
| 97 | // routine for big-endian processors so it can be as efficient as |
| 98 | // possible |
| 99 | static void htond (double &x) |
| 100 | { |
| 101 | if ( sgIsLittleEndian() ) { |
| 102 | int *Double_Overlay; |
| 103 | int Holding_Buffer; |
| 104 | |
| 105 | Double_Overlay = (int *) &x; |
| 106 | Holding_Buffer = Double_Overlay [0]; |
| 107 | |
| 108 | Double_Overlay [0] = htonl (Double_Overlay [1]); |
| 109 | Double_Overlay [1] = htonl (Holding_Buffer); |
| 110 | } else { |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Float version |
| 116 | static void htonf (float &x) |
| 117 | { |
| 118 | if ( sgIsLittleEndian() ) { |
| 119 | int *Float_Overlay; |
| 120 | int Holding_Buffer; |
| 121 | |
| 122 | Float_Overlay = (int *) &x; |
| 123 | Holding_Buffer = Float_Overlay [0]; |
| 124 | |
| 125 | Float_Overlay [0] = htonl (Holding_Buffer); |
| 126 | } else { |
| 127 | return; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | static void ugear2fg( gps *gpspacket, imu *imupacket, nav *navpacket, |
| 133 | servo *servopacket, health *healthpacket, |
| 134 | FGNetFDM *fdm, FGNetCtrls *ctrls ) |
| 135 | { |
| 136 | unsigned int i; |
| 137 | |
| 138 | // Version sanity checking |
| 139 | fdm->version = FG_NET_FDM_VERSION; |
| 140 | |
| 141 | // Aero parameters |
| 142 | fdm->longitude = navpacket->lon * SG_DEGREES_TO_RADIANS; |
| 143 | fdm->latitude = navpacket->lat * SG_DEGREES_TO_RADIANS; |
| 144 | fdm->altitude = navpacket->alt + alt_offset; |
| 145 | fdm->agl = -9999.0; |
| 146 | fdm->psi = imupacket->psi; // heading |
| 147 | fdm->phi = imupacket->phi; // roll |
| 148 | fdm->theta = imupacket->the; // pitch; |
| 149 | |
| 150 | fdm->phidot = 0.0; |
| 151 | fdm->thetadot = 0.0; |
| 152 | fdm->psidot = 0.0; |
| 153 | |
| 154 | // estimate speed |
| 155 | // double az1, az2, dist; |
| 156 | // geo_inverse_wgs_84( fdm->altitude, last_lat, last_lon, |
| 157 | // fdm->latitude, fdm->longitude, &az1, &az2, &dist ); |
| 158 | // last_lat = fdm->latitude; |
| 159 | // last_lon = fdm->longitude; |
| 160 | // double v_ms = dist / (frame_us / 1000000); |
| 161 | // double v_kts = v_ms * SG_METER_TO_NM * 3600; |
| 162 | // kts_filter = (0.9 * kts_filter) + (0.1 * v_kts); |
| 163 | // printf("dist = %.5f kts est = %.2f\n", dist, kts_filter); |
| 164 | |
| 165 | double vn = navpacket->vn; |
| 166 | double ve = navpacket->ve; |
| 167 | double vd = navpacket->vd; |
| 168 | |
| 169 | if ( use_ground_track_hdg ) { |
| 170 | fdm->psi = SGD_PI * 0.5 - atan2(vn, ve); // heading |
| 171 | } |
| 172 | |
| 173 | double mps = 0.0; |
| 174 | if ( use_ground_speed ) { |
| 175 | mps = sqrt( vn*vn + ve*ve + vd*vd ); |
| 176 | } else { |
| 177 | mps = imupacket->Pt; |
| 178 | } |
| 179 | // double mph = mps * 3600 / 1609.3440; |
| 180 | double kts = mps * SG_MPS_TO_KT; |
| 181 | fdm->vcas = kts; |
| 182 | // printf("speed = %.2f mph %.2f kts\n", mph, kts ); |
| 183 | static double Ps = 0.0, Ps_last = 0.0, t_last = 0.0; |
| 184 | Ps_last = Ps; |
| 185 | Ps = 0.92 * Ps + 0.08 * imupacket->Ps; |
| 186 | double climb = (Ps - Ps_last) / (imupacket->time - t_last); |
| 187 | t_last = imupacket->time; |
| 188 | static double climbf = 0.0; |
| 189 | climbf = 0.994 * climbf + 0.006 * climb; |
| 190 | fdm->climb_rate = climbf; // fps |
| 191 | |
| 192 | static double Ps_error = 0.0; |
| 193 | static double Ps_count = 0; |
| 194 | const double span = 10000.0; |
| 195 | Ps_count += 1.0; if (Ps_count > (span-1.0)) { Ps_count = (span-1.0); } |
| 196 | double error = navpacket->alt - Ps; |
| 197 | Ps_error = (Ps_count/span) * Ps_error + ((span-Ps_count)/span) * error; |
| 198 | fdm->altitude = Ps + Ps_error; |
| 199 | |
| 200 | /* printf("%.3f, %.3f, %.3f, %.3f, %.8f, %.8f, %.3f, %.3f, %.3f, %.3f, %.3f\n", |
| 201 | imupacket->time, imupacket->the, -navpacket->vd, climbf, |
| 202 | navpacket->lat, navpacket->lon, gpspacket->alt, navpacket->alt, |
| 203 | imupacket->Ps, Ps, Ps + Ps_error); */ |
| 204 | |
| 205 | // cout << "climb rate = " << aero->hdota << endl; |
| 206 | fdm->v_north = 0.0; |
| 207 | fdm->v_east = 0.0; |
| 208 | fdm->v_down = 0.0; |
| 209 | fdm->v_body_u = 0.0; |
| 210 | fdm->v_body_v = 0.0; |
| 211 | fdm->v_body_w = 0.0; |
| 212 | fdm->stall_warning = 0.0; |
| 213 | |
| 214 | fdm->A_X_pilot = 0.0; |
| 215 | fdm->A_Y_pilot = 0.0; |
| 216 | fdm->A_Z_pilot = 0.0 /* (should be -G) */; |
| 217 | |
| 218 | // Engine parameters |
| 219 | fdm->num_engines = 1; |
| 220 | fdm->eng_state[0] = 2; |
| 221 | // cout << "state = " << fdm->eng_state[0] << endl; |
| 222 | double rpm = 5000.0 - ((double)servopacket->chn[2] / 65536.0)*3500.0; |
| 223 | if ( rpm < 0.0 ) { rpm = 0.0; } |
| 224 | if ( rpm > 5000.0 ) { rpm = 5000.0; } |
| 225 | fdm->rpm[0] = rpm; |
| 226 | |
| 227 | fdm->fuel_flow[0] = 0.0; |
| 228 | fdm->egt[0] = 0.0; |
| 229 | // cout << "egt = " << aero->EGT << endl; |
| 230 | fdm->oil_temp[0] = 0.0; |
| 231 | fdm->oil_px[0] = 0.0; |
| 232 | |
| 233 | // Consumables |
| 234 | fdm->num_tanks = 2; |
| 235 | fdm->fuel_quantity[0] = 0.0; |
| 236 | fdm->fuel_quantity[1] = 0.0; |
| 237 | |
| 238 | // Gear and flaps |
| 239 | fdm->num_wheels = 3; |
| 240 | fdm->wow[0] = 0; |
| 241 | fdm->wow[1] = 0; |
| 242 | fdm->wow[2] = 0; |
| 243 | |
| 244 | // the following really aren't used in this context |
| 245 | fdm->cur_time = 0; |
| 246 | fdm->warp = 0; |
| 247 | fdm->visibility = 0; |
| 248 | |
| 249 | // cout << "Flap deflection = " << aero->dflap << endl; |
| 250 | fdm->left_flap = 0.0; |
| 251 | fdm->right_flap = 0.0; |
| 252 | |
| 253 | if ( est_controls ) { |
| 254 | static float est_elev = 0.0; |
| 255 | static float est_aileron = 0.0; |
| 256 | static float est_rudder = 0.0; |
| 257 | est_elev = 0.99 * est_elev + 0.01 * (imupacket->q * 4); |
| 258 | est_aileron = 0.95 * est_aileron + 0.05 * (imupacket->p * 5); |
| 259 | est_rudder = 0.95 * est_rudder + 0.05 * (imupacket->r * 2); |
| 260 | ctrls->elevator = fdm->elevator = -est_elev; |
| 261 | ctrls->aileron = fdm->left_aileron = est_aileron; |
| 262 | fdm->right_aileron = -est_aileron; |
| 263 | ctrls->rudder = fdm->rudder = est_rudder; |
| 264 | } else { |
| 265 | ctrls->elevator = fdm->elevator = 1.0 - ((double)servopacket->chn[1] / 32768.0); |
| 266 | ctrls->aileron = fdm->left_aileron = 1.0 - ((double)servopacket->chn[0] / 32768.0); |
| 267 | fdm->right_aileron = ((double)servopacket->chn[0] / 32768.0) - 1.0; |
| 268 | ctrls->rudder = fdm->rudder = 1.0 - ((double)servopacket->chn[3] / 32768.0); |
| 269 | ctrls->elevator *= 3.0; |
| 270 | ctrls->aileron *= 3.0; |
| 271 | } |
| 272 | fdm->elevator_trim_tab = 0.0; |
| 273 | fdm->left_flap = 0.0; |
| 274 | fdm->right_flap = 0.0; |
| 275 | fdm->nose_wheel = 0.0; |
| 276 | fdm->speedbrake = 0.0; |
| 277 | fdm->spoilers = 0.0; |
| 278 | |
| 279 | // Convert the net buffer to network format |
| 280 | fdm->version = htonl(fdm->version); |
| 281 | |
| 282 | htond(fdm->longitude); |
| 283 | htond(fdm->latitude); |
| 284 | htond(fdm->altitude); |
| 285 | htonf(fdm->agl); |
| 286 | htonf(fdm->phi); |
| 287 | htonf(fdm->theta); |
| 288 | htonf(fdm->psi); |
| 289 | htonf(fdm->alpha); |
| 290 | htonf(fdm->beta); |
| 291 | |
| 292 | htonf(fdm->phidot); |
| 293 | htonf(fdm->thetadot); |
| 294 | htonf(fdm->psidot); |
| 295 | htonf(fdm->vcas); |
| 296 | htonf(fdm->climb_rate); |
| 297 | htonf(fdm->v_north); |
| 298 | htonf(fdm->v_east); |
| 299 | htonf(fdm->v_down); |
| 300 | htonf(fdm->v_body_u); |
| 301 | htonf(fdm->v_body_v); |
| 302 | htonf(fdm->v_body_w); |
| 303 | |
| 304 | htonf(fdm->A_X_pilot); |
| 305 | htonf(fdm->A_Y_pilot); |
| 306 | htonf(fdm->A_Z_pilot); |
| 307 | |
| 308 | htonf(fdm->stall_warning); |
| 309 | htonf(fdm->slip_deg); |
| 310 | |
| 311 | for ( i = 0; i < fdm->num_engines; ++i ) { |
| 312 | fdm->eng_state[i] = htonl(fdm->eng_state[i]); |
| 313 | htonf(fdm->rpm[i]); |
| 314 | htonf(fdm->fuel_flow[i]); |
| 315 | htonf(fdm->egt[i]); |
| 316 | htonf(fdm->cht[i]); |
| 317 | htonf(fdm->mp_osi[i]); |
| 318 | htonf(fdm->tit[i]); |
| 319 | htonf(fdm->oil_temp[i]); |
| 320 | htonf(fdm->oil_px[i]); |
| 321 | } |
| 322 | fdm->num_engines = htonl(fdm->num_engines); |
| 323 | |
| 324 | for ( i = 0; i < fdm->num_tanks; ++i ) { |
| 325 | htonf(fdm->fuel_quantity[i]); |
| 326 | } |
| 327 | fdm->num_tanks = htonl(fdm->num_tanks); |
| 328 | |
| 329 | for ( i = 0; i < fdm->num_wheels; ++i ) { |
| 330 | fdm->wow[i] = htonl(fdm->wow[i]); |
| 331 | htonf(fdm->gear_pos[i]); |
| 332 | htonf(fdm->gear_steer[i]); |
| 333 | htonf(fdm->gear_compression[i]); |
| 334 | } |
| 335 | fdm->num_wheels = htonl(fdm->num_wheels); |
| 336 | |
| 337 | fdm->cur_time = htonl( fdm->cur_time ); |
| 338 | fdm->warp = htonl( fdm->warp ); |
| 339 | htonf(fdm->visibility); |
| 340 | |
| 341 | htonf(fdm->elevator); |
| 342 | htonf(fdm->elevator_trim_tab); |
| 343 | htonf(fdm->left_flap); |
| 344 | htonf(fdm->right_flap); |
| 345 | htonf(fdm->left_aileron); |
| 346 | htonf(fdm->right_aileron); |
| 347 | htonf(fdm->rudder); |
| 348 | htonf(fdm->nose_wheel); |
| 349 | htonf(fdm->speedbrake); |
| 350 | htonf(fdm->spoilers); |
| 351 | |
| 352 | #if 0 |
| 353 | ctrls->version = FG_NET_CTRLS_VERSION; |
| 354 | ctrls->elevator_trim = 0.0; |
| 355 | ctrls->flaps = 0.0; |
| 356 | |
| 357 | htonl(ctrls->version); |
| 358 | htond(ctrls->aileron); |
| 359 | htond(ctrls->rudder); |
| 360 | htond(ctrls->elevator); |
| 361 | htond(ctrls->elevator_trim); |
| 362 | htond(ctrls->flaps); |
| 363 | #endif |
| 364 | } |
| 365 | |
| 366 | |
| 367 | static void ugear2opengc( gps *gpspacket, imu *imupacket, nav *navpacket, |
| 368 | servo *servopacket, health *healthpacket, |
| 369 | ogcFGData *ogc ) |
| 370 | { |
| 371 | // Version sanity checking |
| 372 | ogc->version_id = OGC_VERSION; |
| 373 | |
| 374 | // Aero parameters |
| 375 | ogc->longitude = navpacket->lon; |
| 376 | ogc->latitude = navpacket->lat; |
| 377 | ogc->heading = imupacket->psi * SG_RADIANS_TO_DEGREES; // heading |
| 378 | ogc->bank = imupacket->phi * SG_RADIANS_TO_DEGREES; // roll |
| 379 | ogc->pitch = imupacket->the * SG_RADIANS_TO_DEGREES; // pitch; |
| 380 | |
| 381 | ogc->phi_dot = 0.0; |
| 382 | ogc->theta_dot = 0.0; |
| 383 | ogc->psi_dot = 0.0; |
| 384 | |
| 385 | ogc->alpha = 0.0; |
| 386 | ogc->beta = 0.0; |
| 387 | ogc->alpha_dot = 0.0; |
| 388 | ogc->beta_dot = 0.0; |
| 389 | |
| 390 | ogc->left_aileron = 1.0 - ((double)servopacket->chn[0] / 32768.0); |
| 391 | ogc->right_aileron = ((double)servopacket->chn[0] / 32768.0) - 1.0; |
| 392 | ogc->elevator = 1.0 - ((double)servopacket->chn[1] / 32768.0); |
| 393 | ogc->elevator_trim = 0.0; |
| 394 | ogc->rudder = 1.0 - ((double)servopacket->chn[3] / 32768.0); |
| 395 | ogc->flaps = 0.0; |
| 396 | ogc->flaps_cmd = 0.0; |
| 397 | |
| 398 | ogc->wind = 0.0; |
| 399 | ogc->wind_dir = 0.0; |
| 400 | |
| 401 | // estimate speed |
| 402 | // double az1, az2, dist; |
| 403 | // geo_inverse_wgs_84( fdm->altitude, last_lat, last_lon, |
| 404 | // fdm->latitude, fdm->longitude, &az1, &az2, &dist ); |
| 405 | // last_lat = fdm->latitude; |
| 406 | // last_lon = fdm->longitude; |
| 407 | // double v_ms = dist / (frame_us / 1000000); |
| 408 | // double v_kts = v_ms * SG_METER_TO_NM * 3600; |
| 409 | // kts_filter = (0.9 * kts_filter) + (0.1 * v_kts); |
| 410 | // printf("dist = %.5f kts est = %.2f\n", dist, kts_filter); |
| 411 | |
| 412 | double vn = navpacket->vn; |
| 413 | double ve = navpacket->ve; |
| 414 | double vd = navpacket->vd; |
| 415 | |
| 416 | if ( use_ground_track_hdg ) { |
| 417 | ogc->heading = (SGD_PI * 0.5 - atan2(vn, ve)) * SG_RADIANS_TO_DEGREES; |
| 418 | } |
| 419 | |
| 420 | if ( ogc->heading < 0 ) { ogc->heading += 360.0; } |
| 421 | |
| 422 | double mps = 0.0; |
| 423 | if ( use_ground_speed ) { |
| 424 | mps = sqrt( vn*vn + ve*ve + vd*vd ); |
| 425 | } else { |
| 426 | mps = imupacket->Pt; |
| 427 | } |
| 428 | // double mph = mps * 3600 / 1609.3440; |
| 429 | double kts = mps * SG_MPS_TO_KT; |
| 430 | ogc->v_kcas = kts; |
| 431 | // printf("speed = %.2f mph %.2f kts\n", mph, kts ); |
| 432 | static double Ps = 0.0, Ps_last = 0.0, t_last = 0.0; |
| 433 | Ps_last = Ps; |
| 434 | Ps = 0.92 * Ps + 0.08 * imupacket->Ps; |
| 435 | double climb = (Ps - Ps_last) / (imupacket->time - t_last); |
| 436 | t_last = imupacket->time; |
| 437 | static double climbf = 0.0; |
| 438 | climbf = 0.994 * climbf + 0.006 * climb; |
| 439 | ogc->vvi = climbf; // fps |
| 440 | |
| 441 | // uncomment one of the following schemes for setting elevation: |
| 442 | |
| 443 | // use the navigation (inertially augmented gps estimate) |
| 444 | // ogc->altitude = ogc->elevation |
| 445 | // = (navpacket->alt + alt_offset * SG_METER_TO_FEET); |
| 446 | |
| 447 | // use estimate error between pressure sensor and gps altitude over time |
| 448 | // use pressure sensor + error average for altitude estimate. |
| 449 | static double Ps_error = 0.0; |
| 450 | static double Ps_count = 0; |
| 451 | const double span = 10000.0; |
| 452 | Ps_count += 1.0; if (Ps_count > (span-1.0)) { Ps_count = (span-1.0); } |
| 453 | double error = navpacket->alt - Ps; |
| 454 | Ps_error = (Ps_count/span) * Ps_error + ((span-Ps_count)/span) * error; |
| 455 | ogc->elevation = (Ps + Ps_error) * SG_METER_TO_FEET; |
| 456 | |
| 457 | /* printf("%.3f, %.3f, %.3f, %.3f, %.8f, %.8f, %.3f, %.3f, %.3f, %.3f, %.3f\n", |
| 458 | imupacket->time, imupacket->the, -navpacket->vd, climbf, |
| 459 | navpacket->lat, navpacket->lon, gpspacket->alt, navpacket->alt, |
| 460 | imupacket->Ps, Ps, Ps + Ps_error); */ |
| 461 | |
| 462 | if ( est_controls ) { |
| 463 | static float est_elev = 0.0; |
| 464 | static float est_aileron = 0.0; |
| 465 | static float est_rudder = 0.0; |
| 466 | est_elev = 0.99 * est_elev + 0.01 * (imupacket->q * 4); |
| 467 | est_aileron = 0.95 * est_aileron + 0.05 * (imupacket->p * 5); |
| 468 | est_rudder = 0.95 * est_rudder + 0.05 * (imupacket->r * 2); |
| 469 | ogc->elevator = -est_elev; |
| 470 | ogc->left_aileron = est_aileron; |
| 471 | ogc->right_aileron = -est_aileron; |
| 472 | ogc->rudder = est_rudder; |
| 473 | } else { |
| 474 | ogc->elevator = 1.0 - ((double)servopacket->chn[1] / 32768.0); |
| 475 | ogc->left_aileron = 1.0 - ((double)servopacket->chn[0] / 32768.0); |
| 476 | ogc->right_aileron = ((double)servopacket->chn[0] / 32768.0) - 1.0; |
| 477 | ogc->rudder = 1.0 - ((double)servopacket->chn[3] / 32768.0); |
| 478 | } |
| 479 | ogc->elevator *= 4.0; |
| 480 | ogc->left_aileron *= 4.0; |
| 481 | ogc->right_aileron *= 4.0; |
| 482 | ogc->rudder *= 4.0; |
| 483 | |
| 484 | // additional "abused" data fields |
| 485 | ogc->egt[0] = ogc->bank - healthpacket->target_roll_deg; // flight director target roll |
| 486 | ogc->egt[1] = -ogc->pitch + healthpacket->target_pitch_deg; // flight director target pitch |
| 487 | ogc->egt[2] = healthpacket->target_heading_deg; // target heading bug |
| 488 | ogc->egt[3] = healthpacket->target_climb_fps; // target VVI bug |
| 489 | ogc->epr[0] = healthpacket->target_altitude_ft; // target altitude bug |
| 490 | ogc->epr[1] = 30.0; // target speed bug |
| 491 | ogc->epr[2] = gps_status; // gps status box |
| 492 | } |
| 493 | |
| 494 | |
| 495 | static void send_data_udp( gps *gpspacket, imu *imupacket, nav *navpacket, |
| 496 | servo *servopacket, health *healthpacket ) |
| 497 | { |
| 498 | int ogcsize = sizeof( ogcFGData ); |
| 499 | int fdmsize = sizeof( FGNetFDM ); |
| 500 | // int ctrlsize = sizeof( FGNetCtrls ); |
| 501 | |
| 502 | // cout << "Running main loop" << endl; |
| 503 | |
| 504 | ogcFGData fgogc; |
| 505 | FGNetFDM fgfdm; |
| 506 | FGNetCtrls fgctrls; |
| 507 | |
| 508 | ugear2fg( gpspacket, imupacket, navpacket, servopacket, healthpacket, |
| 509 | &fgfdm, &fgctrls ); |
| 510 | ugear2opengc( gpspacket, imupacket, navpacket, servopacket, healthpacket, |
| 511 | &fgogc ); |
| 512 | opengc_sock.send(&fgogc, ogcsize, 0); |
| 513 | fdm_sock.send(&fgfdm, fdmsize, 0); |
| 514 | // len = ctrls_sock.send(&fgctrls, ctrlsize, 0); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | void usage( const string &argv0 ) { |
| 519 | cout << "Usage: " << argv0 << endl; |
| 520 | cout << "\t[ --help ]" << endl; |
| 521 | cout << "\t[ --infile <infile_name>" << endl; |
| 522 | cout << "\t[ --flight <flight_dir>" << endl; |
| 523 | cout << "\t[ --serial <dev_name>" << endl; |
| 524 | cout << "\t[ --outfile <outfile_name> (capture the data to a file)" << endl; |
| 525 | cout << "\t[ --hertz <hertz> ]" << endl; |
| 526 | cout << "\t[ --host <hostname> ]" << endl; |
| 527 | cout << "\t[ --broadcast ]" << endl; |
| 528 | cout << "\t[ --opengc-port <opengc output port #> ]" << endl; |
| 529 | cout << "\t[ --fdm-port <fdm output port #> ]" << endl; |
| 530 | cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl; |
| 531 | cout << "\t[ --groundtrack-heading ]" << endl; |
| 532 | cout << "\t[ --ground-speed ]" << endl; |
| 533 | cout << "\t[ --estimate-control-deflections ]" << endl; |
| 534 | cout << "\t[ --altitude-offset <meters> ]" << endl; |
| 535 | cout << "\t[ --skip-seconds <seconds> ]" << endl; |
| 536 | cout << "\t[ --no-real-time ]" << endl; |
| 537 | cout << "\t[ --ignore-checksum ]" << endl; |
| 538 | cout << "\t[ --sg-swap ]" << endl; |
| 539 | } |
| 540 | |
| 541 | |
| 542 | int main( int argc, char **argv ) { |
| 543 | double hertz = 60.0; |
| 544 | string out_host = "localhost"; |
| 545 | bool do_broadcast = false; |
| 546 | |
| 547 | // process command line arguments |
| 548 | for ( int i = 1; i < argc; ++i ) { |
| 549 | if ( strcmp( argv[i], "--help" ) == 0 ) { |
| 550 | usage( argv[0] ); |
| 551 | exit( 0 ); |
| 552 | } else if ( strcmp( argv[i], "--hertz" ) == 0 ) { |
| 553 | ++i; |
| 554 | if ( i < argc ) { |
| 555 | hertz = atof( argv[i] ); |
| 556 | } else { |
| 557 | usage( argv[0] ); |
| 558 | exit( -1 ); |
| 559 | } |
| 560 | } else if ( strcmp( argv[i], "--infile" ) == 0 ) { |
| 561 | ++i; |
| 562 | if ( i < argc ) { |
| 563 | infile = argv[i]; |
| 564 | } else { |
| 565 | usage( argv[0] ); |
| 566 | exit( -1 ); |
| 567 | } |
| 568 | } else if ( strcmp( argv[i], "--flight" ) == 0 ) { |
| 569 | ++i; |
| 570 | if ( i < argc ) { |
| 571 | flight_dir = argv[i]; |
| 572 | } else { |
| 573 | usage( argv[0] ); |
| 574 | exit( -1 ); |
| 575 | } |
| 576 | } else if ( strcmp( argv[i], "--outfile" ) == 0 ) { |
| 577 | ++i; |
| 578 | if ( i < argc ) { |
| 579 | outfile = argv[i]; |
| 580 | } else { |
| 581 | usage( argv[0] ); |
| 582 | exit( -1 ); |
| 583 | } |
| 584 | } else if ( strcmp( argv[i], "--serial" ) == 0 ) { |
| 585 | ++i; |
| 586 | if ( i < argc ) { |
| 587 | serialdev = argv[i]; |
| 588 | } else { |
| 589 | usage( argv[0] ); |
| 590 | exit( -1 ); |
| 591 | } |
| 592 | } else if ( strcmp( argv[i], "--host" ) == 0 ) { |
| 593 | ++i; |
| 594 | if ( i < argc ) { |
| 595 | out_host = argv[i]; |
| 596 | } else { |
| 597 | usage( argv[0] ); |
| 598 | exit( -1 ); |
| 599 | } |
| 600 | } else if ( strcmp( argv[i], "--broadcast" ) == 0 ) { |
| 601 | do_broadcast = true; |
| 602 | } else if ( strcmp( argv[i], "--opengc-port" ) == 0 ) { |
| 603 | ++i; |
| 604 | if ( i < argc ) { |
| 605 | opengc_port = atoi( argv[i] ); |
| 606 | } else { |
| 607 | usage( argv[0] ); |
| 608 | exit( -1 ); |
| 609 | } |
| 610 | } else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) { |
| 611 | ++i; |
| 612 | if ( i < argc ) { |
| 613 | fdm_port = atoi( argv[i] ); |
| 614 | } else { |
| 615 | usage( argv[0] ); |
| 616 | exit( -1 ); |
| 617 | } |
| 618 | } else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) { |
| 619 | ++i; |
| 620 | if ( i < argc ) { |
| 621 | ctrls_port = atoi( argv[i] ); |
| 622 | } else { |
| 623 | usage( argv[0] ); |
| 624 | exit( -1 ); |
| 625 | } |
| 626 | } else if ( strcmp (argv[i], "--groundtrack-heading" ) == 0 ) { |
| 627 | use_ground_track_hdg = true; |
| 628 | } else if ( strcmp (argv[i], "--ground-speed" ) == 0 ) { |
| 629 | use_ground_speed = true; |
| 630 | } else if (strcmp (argv[i], "--estimate-control-deflections" ) == 0) { |
| 631 | est_controls = true; |
| 632 | } else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) { |
| 633 | ++i; |
| 634 | if ( i < argc ) { |
| 635 | alt_offset = atof( argv[i] ); |
| 636 | } else { |
| 637 | usage( argv[0] ); |
| 638 | exit( -1 ); |
| 639 | } |
| 640 | } else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) { |
| 641 | ++i; |
| 642 | if ( i < argc ) { |
| 643 | skip = atof( argv[i] ); |
| 644 | } else { |
| 645 | usage( argv[0] ); |
| 646 | exit( -1 ); |
| 647 | } |
| 648 | } else if ( strcmp( argv[i], "--no-real-time" ) == 0 ) { |
| 649 | run_real_time = false; |
| 650 | } else if ( strcmp( argv[i], "--ignore-checksum" ) == 0 ) { |
| 651 | ignore_checksum = true; |
| 652 | } else if ( strcmp( argv[i], "--sg-swap" ) == 0 ) { |
| 653 | sg_swap = true; |
| 654 | } else { |
| 655 | usage( argv[0] ); |
| 656 | exit( -1 ); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | // Setup up outgoing network connections |
| 661 | |
| 662 | simgear::Socket::initSockets(); // We must call this before any other net stuff |
| 663 | |
| 664 | if ( ! opengc_sock.open( false ) ) { // open a UDP socket |
| 665 | cout << "error opening opengc output socket" << endl; |
| 666 | return -1; |
| 667 | } |
| 668 | if ( ! fdm_sock.open( false ) ) { // open a UDP socket |
| 669 | cout << "error opening fdm output socket" << endl; |
| 670 | return -1; |
| 671 | } |
| 672 | if ( ! ctrls_sock.open( false ) ) { // open a UDP socket |
| 673 | cout << "error opening ctrls output socket" << endl; |
| 674 | return -1; |
| 675 | } |
| 676 | cout << "open net channels" << endl; |
| 677 | |
| 678 | opengc_sock.setBlocking( false ); |
| 679 | fdm_sock.setBlocking( false ); |
| 680 | ctrls_sock.setBlocking( false ); |
| 681 | cout << "blocking false" << endl; |
| 682 | |
| 683 | if ( do_broadcast ) { |
| 684 | opengc_sock.setBroadcast( true ); |
| 685 | fdm_sock.setBroadcast( true ); |
| 686 | ctrls_sock.setBroadcast( true ); |
| 687 | } |
| 688 | |
| 689 | if ( opengc_sock.connect( out_host.c_str(), opengc_port ) == -1 ) { |
| 690 | perror("connect"); |
| 691 | cout << "error connecting to outgoing opengc port: " << out_host |
| 692 | << ":" << opengc_port << endl; |
| 693 | return -1; |
| 694 | } |
| 695 | cout << "connected outgoing opengc socket" << endl; |
| 696 | |
| 697 | if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) { |
| 698 | perror("connect"); |
| 699 | cout << "error connecting to outgoing fdm port: " << out_host |
| 700 | << ":" << fdm_port << endl; |
| 701 | return -1; |
| 702 | } |
| 703 | cout << "connected outgoing fdm socket" << endl; |
| 704 | |
| 705 | if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) { |
| 706 | perror("connect"); |
| 707 | cout << "error connecting to outgoing ctrls port: " << out_host |
| 708 | << ":" << ctrls_port << endl; |
| 709 | return -1; |
| 710 | } |
| 711 | cout << "connected outgoing ctrls socket" << endl; |
| 712 | |
| 713 | if ( sg_swap ) { |
| 714 | track.set_stargate_swap_mode(); |
| 715 | } |
| 716 | |
| 717 | UGTelnet telnet( 5402 ); |
| 718 | telnet.open(); |
| 719 | |
| 720 | if ( infile.length() || flight_dir.length() ) { |
| 721 | if ( infile.length() ) { |
| 722 | // Load data from a stream log data file |
| 723 | track.load_stream( infile, ignore_checksum ); |
| 724 | } else if ( flight_dir.length() ) { |
| 725 | // Load data from a flight directory |
| 726 | track.load_flight( flight_dir ); |
| 727 | } |
| 728 | cout << "Loaded " << track.gps_size() << " gps records." << endl; |
| 729 | cout << "Loaded " << track.imu_size() << " imu records." << endl; |
| 730 | cout << "Loaded " << track.nav_size() << " nav records." << endl; |
| 731 | cout << "Loaded " << track.servo_size() << " servo records." << endl; |
| 732 | cout << "Loaded " << track.health_size() << " health records." << endl; |
| 733 | |
| 734 | int size = track.imu_size(); |
| 735 | |
| 736 | double current_time = track.get_imupt(0).time; |
| 737 | cout << "Track begin time is " << current_time << endl; |
| 738 | double end_time = track.get_imupt(size-1).time; |
| 739 | cout << "Track end time is " << end_time << endl; |
| 740 | cout << "Duration = " << end_time - current_time << endl; |
| 741 | |
| 742 | if ( track.gps_size() > 0 ) { |
| 743 | double tmp = track.get_gpspt(track.gps_size()-1).ITOW; |
| 744 | int days = (int)(tmp / (24 * 60 * 60)); |
| 745 | tmp -= days * 24 * 60 * 60; |
| 746 | int hours = (int)(tmp / (60 * 60)); |
| 747 | tmp -= hours * 60 * 60; |
| 748 | int min = (int)(tmp / 60); |
| 749 | tmp -= min * 60; |
| 750 | double sec = tmp; |
| 751 | printf("[GPS ]:ITOW= %.3f[sec] %dd %02d:%02d:%06.3f\n", |
| 752 | tmp, days, hours, min, sec); |
| 753 | } |
| 754 | |
| 755 | |
| 756 | // advance skip seconds forward |
| 757 | current_time += skip; |
| 758 | |
| 759 | frame_us = 1000000.0 / hertz; |
| 760 | if ( frame_us < 0.0 ) { |
| 761 | frame_us = 0.0; |
| 762 | } |
| 763 | |
| 764 | SGTimeStamp start_time; |
| 765 | start_time.stamp(); |
| 766 | int gps_count = 0; |
| 767 | int imu_count = 0; |
| 768 | int nav_count = 0; |
| 769 | int servo_count = 0; |
| 770 | int health_count = 0; |
| 771 | |
| 772 | gps gps0, gps1; |
| 773 | gps0 = gps1 = track.get_gpspt( 0 ); |
| 774 | |
| 775 | imu imu0, imu1; |
| 776 | imu0 = imu1 = track.get_imupt( 0 ); |
| 777 | |
| 778 | nav nav0, nav1; |
| 779 | nav0 = nav1 = track.get_navpt( 0 ); |
| 780 | |
| 781 | servo servo0, servo1; |
| 782 | servo0 = servo1 = track.get_servopt( 0 ); |
| 783 | |
| 784 | health health0, health1; |
| 785 | health0 = health1 = track.get_healthpt( 0 ); |
| 786 | |
| 787 | double last_lat = -999.9, last_lon = -999.9; |
| 788 | |
| 789 | printf("<gpx>\n"); |
| 790 | printf(" <trk>\n"); |
| 791 | printf(" <trkseg>\n"); |
| 792 | while ( current_time < end_time ) { |
| 793 | // cout << "current_time = " << current_time << " end_time = " |
| 794 | // << end_time << endl; |
| 795 | |
| 796 | // Advance gps pointer |
| 797 | while ( current_time > gps1.time |
| 798 | && gps_count < track.gps_size() - 1 ) |
| 799 | { |
| 800 | gps0 = gps1; |
| 801 | ++gps_count; |
| 802 | // cout << "count = " << count << endl; |
| 803 | gps1 = track.get_gpspt( gps_count ); |
| 804 | } |
| 805 | // cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time() |
| 806 | // << endl; |
| 807 | |
| 808 | // Advance imu pointer |
| 809 | while ( current_time > imu1.time |
| 810 | && imu_count < track.imu_size() - 1 ) |
| 811 | { |
| 812 | imu0 = imu1; |
| 813 | ++imu_count; |
| 814 | // cout << "count = " << count << endl; |
| 815 | imu1 = track.get_imupt( imu_count ); |
| 816 | } |
| 817 | // cout << "pos0 = " << pos0.get_seconds() |
| 818 | // << " pos1 = " << pos1.get_seconds() << endl; |
| 819 | |
| 820 | // Advance nav pointer |
| 821 | while ( current_time > nav1.time |
| 822 | && nav_count < track.nav_size() - 1 ) |
| 823 | { |
| 824 | nav0 = nav1; |
| 825 | ++nav_count; |
| 826 | // cout << "nav count = " << nav_count << endl; |
| 827 | nav1 = track.get_navpt( nav_count ); |
| 828 | } |
| 829 | // cout << "pos0 = " << pos0.get_seconds() |
| 830 | // << " pos1 = " << pos1.get_seconds() << endl; |
| 831 | |
| 832 | // Advance servo pointer |
| 833 | while ( current_time > servo1.time |
| 834 | && servo_count < track.servo_size() - 1 ) |
| 835 | { |
| 836 | servo0 = servo1; |
| 837 | ++servo_count; |
| 838 | // cout << "count = " << count << endl; |
| 839 | servo1 = track.get_servopt( servo_count ); |
| 840 | } |
| 841 | // cout << "pos0 = " << pos0.get_seconds() |
| 842 | // << " pos1 = " << pos1.get_seconds() << endl; |
| 843 | |
| 844 | // Advance health pointer |
| 845 | while ( current_time > health1.time |
| 846 | && health_count < track.health_size() - 1 ) |
| 847 | { |
| 848 | health0 = health1; |
| 849 | ++health_count; |
| 850 | // cout << "count = " << count << endl; |
| 851 | health1 = track.get_healthpt( health_count ); |
| 852 | } |
| 853 | // cout << "pos0 = " << pos0.get_seconds() |
| 854 | // << " pos1 = " << pos1.get_seconds() << endl; |
| 855 | |
| 856 | double gps_percent; |
| 857 | if ( fabs(gps1.time - gps0.time) < 0.00001 ) { |
| 858 | gps_percent = 0.0; |
| 859 | } else { |
| 860 | gps_percent = |
| 861 | (current_time - gps0.time) / |
| 862 | (gps1.time - gps0.time); |
| 863 | } |
| 864 | // cout << "Percent = " << percent << endl; |
| 865 | |
| 866 | double imu_percent; |
| 867 | if ( fabs(imu1.time - imu0.time) < 0.00001 ) { |
| 868 | imu_percent = 0.0; |
| 869 | } else { |
| 870 | imu_percent = |
| 871 | (current_time - imu0.time) / |
| 872 | (imu1.time - imu0.time); |
| 873 | } |
| 874 | // cout << "Percent = " << percent << endl; |
| 875 | |
| 876 | double nav_percent; |
| 877 | if ( fabs(nav1.time - nav0.time) < 0.00001 ) { |
| 878 | nav_percent = 0.0; |
| 879 | } else { |
| 880 | nav_percent = |
| 881 | (current_time - nav0.time) / |
| 882 | (nav1.time - nav0.time); |
| 883 | } |
| 884 | // cout << "Percent = " << percent << endl; |
| 885 | |
| 886 | double servo_percent; |
| 887 | if ( fabs(servo1.time - servo0.time) < 0.00001 ) { |
| 888 | servo_percent = 0.0; |
| 889 | } else { |
| 890 | servo_percent = |
| 891 | (current_time - servo0.time) / |
| 892 | (servo1.time - servo0.time); |
| 893 | } |
| 894 | // cout << "Percent = " << percent << endl; |
| 895 | |
| 896 | double health_percent; |
| 897 | if ( fabs(health1.time - health0.time) < 0.00001 ) { |
| 898 | health_percent = 0.0; |
| 899 | } else { |
| 900 | health_percent = |
| 901 | (current_time - health0.time) / |
| 902 | (health1.time - health0.time); |
| 903 | } |
| 904 | // cout << "Percent = " << percent << endl; |
| 905 | |
| 906 | gps gpspacket = UGEARInterpGPS( gps0, gps1, gps_percent ); |
| 907 | imu imupacket = UGEARInterpIMU( imu0, imu1, imu_percent ); |
| 908 | nav navpacket = UGEARInterpNAV( nav0, nav1, nav_percent ); |
| 909 | servo servopacket = UGEARInterpSERVO( servo0, servo1, |
| 910 | servo_percent ); |
| 911 | health healthpacket = UGEARInterpHEALTH( health0, health1, |
| 912 | health_percent ); |
| 913 | |
| 914 | // cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg |
| 915 | // << endl; |
| 916 | // cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg |
| 917 | // << endl; |
| 918 | // cout << (double)current_time << " " << pos.lat_deg << ", " |
| 919 | // << pos.lon_deg << " " << att.yaw_deg << endl; |
| 920 | if ( gpspacket.lat > -500 ) { |
| 921 | // printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n", |
| 922 | // current_time, |
| 923 | // navpacket.lat, navpacket.lon, navpacket.alt, |
| 924 | // imupacket.psi, imupacket.the, imupacket.phi ); |
| 925 | double dlat = last_lat - navpacket.lat; |
| 926 | double dlon = last_lon - navpacket.lon; |
| 927 | double dist = sqrt( dlat*dlat + dlon*dlon ); |
| 928 | if ( dist > 0.01 ) { |
| 929 | printf(" <trkpt lat=\"%.8f\" lon=\"%.8f\"></trkpt>\n", |
| 930 | navpacket.lat, navpacket.lon ); |
| 931 | // printf(" </wpt>\n"); |
| 932 | last_lat = navpacket.lat; |
| 933 | last_lon = navpacket.lon; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | if ( (fabs(gpspacket.lat) < 0.0001 && |
| 938 | fabs(gpspacket.lon) < 0.0001 && |
| 939 | fabs(gpspacket.alt) < 0.0001) ) |
| 940 | { |
| 941 | printf("WARNING: LOST GPS!!!\n"); |
| 942 | gps_status = -1.0; |
| 943 | } else { |
| 944 | gps_status = 1.0; |
| 945 | } |
| 946 | |
| 947 | send_data_udp( &gpspacket, &imupacket, &navpacket, &servopacket, |
| 948 | &healthpacket ); |
| 949 | |
| 950 | if ( run_real_time ) { |
| 951 | // Update the elapsed time. |
| 952 | static bool first_time = true; |
| 953 | if ( first_time ) { |
| 954 | last_time_stamp.stamp(); |
| 955 | first_time = false; |
| 956 | } |
| 957 | |
| 958 | current_time_stamp.stamp(); |
| 959 | /* Convert to ms */ |
| 960 | double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs(); |
| 961 | if ( elapsed_us < (frame_us - 2000) ) { |
| 962 | double requested_us = (frame_us - elapsed_us) - 2000 ; |
| 963 | std::this_thread::sleep_for(std::chrono::milliseconds((int) (requested_us / 1000.0))); |
| 964 | } |
| 965 | current_time_stamp.stamp(); |
| 966 | while ( (current_time_stamp - last_time_stamp).toUSecs() < frame_us ) { |
| 967 | current_time_stamp.stamp(); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | current_time += (frame_us / 1000000.0); |
| 972 | last_time_stamp = current_time_stamp; |
| 973 | } |
| 974 | |
| 975 | printf(" <trkpt lat=\"%.8f\" lon=\"%.8f\"></trkpt>\n", |
| 976 | nav1.lat, nav1.lon ); |
| 977 | |
| 978 | printf(" </trkseg>\n"); |
| 979 | printf(" </trk>\n"); |
| 980 | nav0 = track.get_navpt( 0 ); |
| 981 | nav1 = track.get_navpt( track.nav_size() - 1 ); |
| 982 | printf(" <wpt lat=\"%.8f\" lon=\"%.8f\"></wpt>\n", |
| 983 | nav0.lat, nav0.lon ); |
| 984 | printf(" <wpt lat=\"%.8f\" lon=\"%.8f\"></wpt>\n", |
| 985 | nav1.lat, nav1.lon ); |
| 986 | printf("<gpx>\n"); |
| 987 | |
| 988 | cout << "Processed " << imu_count << " entries in " |
| 989 | << current_time_stamp - start_time << " seconds." |
| 990 | << endl; |
| 991 | } else if ( serialdev.length() ) { |
| 992 | // process incoming data from the serial port |
| 993 | |
| 994 | double current_time = 0.0; |
| 995 | double last_time = 0.0; |
| 996 | |
| 997 | gps gpspacket; memset( &gpspacket, 0, sizeof gpspacket ); |
| 998 | imu imupacket; memset( &imupacket, 0, sizeof imupacket ); |
| 999 | nav navpacket; memset( &navpacket, 0, sizeof navpacket ); |
| 1000 | servo servopacket; memset( &servopacket, 0, sizeof servopacket ); |
| 1001 | health healthpacket; memset( &healthpacket, 0, sizeof healthpacket ); |
| 1002 | |
| 1003 | double gps_time = 0.0; |
| 1004 | double imu_time = 0.0; |
| 1005 | double nav_time = 0.0; |
| 1006 | double servo_time = 0.0; |
| 1007 | double health_time = 0.0; |
| 1008 | double command_time = 0.0; |
| 1009 | double command_heartbeat = 0.0; |
| 1010 | |
| 1011 | // open the serial port device |
| 1012 | SGSerialPort uavcom( serialdev, 115200 ); |
| 1013 | if ( !uavcom.is_enabled() ) { |
| 1014 | cout << "Cannot open: " << serialdev << endl; |
| 1015 | return EXIT_FAILURE; |
| 1016 | } |
| 1017 | |
| 1018 | // open up the data log file if requested |
| 1019 | if ( !outfile.length() ) { |
| 1020 | cout << "no --outfile <name> specified, cannot capture data!" |
| 1021 | << endl; |
| 1022 | return EXIT_FAILURE; |
| 1023 | } |
| 1024 | SGFile log( outfile ); |
| 1025 | if ( !log.open( SG_IO_OUT ) ) { |
| 1026 | cout << "Cannot open: " << outfile << endl; |
| 1027 | return EXIT_FAILURE; |
| 1028 | } |
| 1029 | |
| 1030 | // add some test commands |
| 1031 | //command_mgr.add("ap,alt,1000"); |
| 1032 | //command_mgr.add("home,158.0,32.5"); |
| 1033 | //command_mgr.add("go,home"); |
| 1034 | //command_mgr.add("go,route"); |
| 1035 | |
| 1036 | while ( uavcom.is_enabled() ) { |
| 1037 | // cout << "looking for next message ..." << endl; |
| 1038 | int id = track.next_message( &uavcom, &log, &gpspacket, |
| 1039 | &imupacket, &navpacket, &servopacket, |
| 1040 | &healthpacket, ignore_checksum ); |
| 1041 | // cout << "message id = " << id << endl; |
| 1042 | |
| 1043 | telnet.process(); |
| 1044 | |
| 1045 | if ( id == GPS_PACKET ) { |
| 1046 | if ( gpspacket.time > gps_time ) { |
| 1047 | gps_time = gpspacket.time; |
| 1048 | current_time = gps_time; |
| 1049 | } else { |
| 1050 | cout << "oops gps back in time: " << gpspacket.time << " " << gps_time << endl; |
| 1051 | } |
| 1052 | } else if ( id == IMU_PACKET ) { |
| 1053 | if ( imupacket.time > imu_time ) { |
| 1054 | imu_time = imupacket.time; |
| 1055 | current_time = imu_time; |
| 1056 | } else { |
| 1057 | cout << "oops imu back in time: " << imupacket.time << " " << imu_time << endl; |
| 1058 | } |
| 1059 | } else if ( id == NAV_PACKET ) { |
| 1060 | if ( navpacket.time > nav_time ) { |
| 1061 | nav_time = navpacket.time; |
| 1062 | current_time = nav_time; |
| 1063 | } else { |
| 1064 | cout << "oops nav back in time: " << navpacket.time << " " << nav_time << endl; |
| 1065 | } |
| 1066 | } else if ( id == SERVO_PACKET ) { |
| 1067 | if ( servopacket.time > servo_time ) { |
| 1068 | servo_time = servopacket.time; |
| 1069 | current_time = servo_time; |
| 1070 | } else { |
| 1071 | cout << "oops servo back in time: " << servopacket.time << " " << servo_time << endl; |
| 1072 | } |
| 1073 | } else if ( id == HEALTH_PACKET ) { |
| 1074 | if ( healthpacket.time > health_time ) { |
| 1075 | health_time = healthpacket.time; |
| 1076 | current_time = health_time; |
| 1077 | printf("Received a health packet, sequence: %d\n", |
| 1078 | (int)healthpacket.command_sequence); |
| 1079 | command_mgr.update_cmd_sequence(healthpacket.command_sequence); |
| 1080 | } else { |
| 1081 | cout << "oops health back in time: " << healthpacket.time << " " << health_time << endl; |
| 1082 | } |
| 1083 | |
| 1084 | } |
| 1085 | |
| 1086 | if ( (current_time > gps_time + 2) || |
| 1087 | (fabs(gpspacket.lat) < 0.0001 && |
| 1088 | fabs(gpspacket.lon) < 0.0001 && |
| 1089 | fabs(gpspacket.alt) < 0.0001) ) |
| 1090 | { |
| 1091 | printf("WARNING: LOST GPS!!!\n"); |
| 1092 | gps_status = -1.0; |
| 1093 | } else { |
| 1094 | gps_status = 1.0; |
| 1095 | } |
| 1096 | |
| 1097 | // Generate a ground station heart beat every 4 seconds |
| 1098 | if ( current_time >= command_heartbeat + 4 ) { |
| 1099 | command_mgr.add("hb"); |
| 1100 | command_heartbeat = current_time; |
| 1101 | } |
| 1102 | |
| 1103 | // Command update @ 1hz |
| 1104 | if ( current_time >= command_time + 1 ) { |
| 1105 | command_mgr.update(&uavcom); |
| 1106 | command_time = current_time; |
| 1107 | } |
| 1108 | |
| 1109 | // Relay data on to FlightGear and LFSTech Glass |
| 1110 | if ( current_time >= last_time + (1/hertz) ) { |
| 1111 | // if ( gpspacket.lat > -500 ) { |
| 1112 | int londeg = (int)navpacket.lon; |
| 1113 | // double lonmin = fabs(navpacket.lon - londeg); |
| 1114 | int latdeg = (int)navpacket.lat; |
| 1115 | // double latmin = fabs(navpacket.lat - latdeg); |
| 1116 | londeg = abs(londeg); |
| 1117 | latdeg = abs(latdeg); |
| 1118 | /*printf( "%.2f %c%02d:%.4f %c%03d:%.4f %.1f %.2f %.2f %.2f\n", |
| 1119 | current_time, |
| 1120 | latdir, latdeg, latmin, londir, londeg, lonmin, |
| 1121 | navpacket.alt, |
| 1122 | imupacket.phi*SGD_RADIANS_TO_DEGREES, |
| 1123 | imupacket.the*SGD_RADIANS_TO_DEGREES, |
| 1124 | imupacket.psi*SGD_RADIANS_TO_DEGREES ); */ |
| 1125 | // } |
| 1126 | |
| 1127 | last_time = current_time; |
| 1128 | send_data_udp( &gpspacket, &imupacket, &navpacket, |
| 1129 | &servopacket, &healthpacket ); |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |