2006-11-11 18:43:17 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2011-01-05 16:18:12 +00:00
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
# include <windows.h>
|
2011-01-05 16:38:15 +00:00
|
|
|
#else
|
|
|
|
# include <netinet/in.h> // htonl() ntohl()
|
2011-01-05 16:18:12 +00:00
|
|
|
#endif
|
|
|
|
|
2010-01-23 22:26:30 +00:00
|
|
|
#ifndef _WIN32
|
2007-01-11 22:06:33 +00:00
|
|
|
# include <strings.h> // for bzero()
|
|
|
|
#else
|
|
|
|
# define bzero(a,b) memset(a,0,b)
|
|
|
|
#endif
|
2006-11-11 18:43:17 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <plib/sg.h>
|
|
|
|
|
|
|
|
#include <simgear/constants.h>
|
|
|
|
#include <simgear/io/lowlevel.hxx> // endian tests
|
|
|
|
#include <simgear/io/sg_file.hxx>
|
2011-01-05 16:18:12 +00:00
|
|
|
#include <simgear/io/raw_socket.hxx>
|
2007-04-27 15:30:26 +00:00
|
|
|
#include <simgear/serial/serial.hxx>
|
2006-11-11 18:43:17 +00:00
|
|
|
#include <simgear/math/sg_geodesy.hxx>
|
|
|
|
#include <simgear/timing/timestamp.hxx>
|
|
|
|
|
|
|
|
#include <Network/net_ctrls.hxx>
|
|
|
|
#include <Network/net_fdm.hxx>
|
|
|
|
|
|
|
|
#include "UGear.hxx"
|
2008-03-30 05:34:40 +00:00
|
|
|
#include "UGear_command.hxx"
|
2008-03-28 22:55:42 +00:00
|
|
|
#include "UGear_opengc.hxx"
|
2008-04-04 22:34:19 +00:00
|
|
|
#include "UGear_telnet.hxx"
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
|
2008-07-29 08:27:48 +00:00
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
|
|
|
using std::string;
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Network channels
|
2011-01-05 16:18:12 +00:00
|
|
|
static simgear::Socket fdm_sock, ctrls_sock, opengc_sock;
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
// ugear data
|
2008-03-30 05:34:40 +00:00
|
|
|
UGTrack track;
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
// Default ports
|
|
|
|
static int fdm_port = 5505;
|
|
|
|
static int ctrls_port = 5506;
|
2008-03-28 22:55:42 +00:00
|
|
|
static int opengc_port = 6000;
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
// Default path
|
|
|
|
static string infile = "";
|
2007-04-27 15:30:26 +00:00
|
|
|
static string flight_dir = "";
|
2006-11-11 18:43:17 +00:00
|
|
|
static string serialdev = "";
|
|
|
|
static string outfile = "";
|
|
|
|
|
|
|
|
// Master time counter
|
|
|
|
float sim_time = 0.0f;
|
|
|
|
double frame_us = 0.0f;
|
|
|
|
|
|
|
|
// sim control
|
|
|
|
SGTimeStamp last_time_stamp;
|
|
|
|
SGTimeStamp current_time_stamp;
|
|
|
|
|
|
|
|
// altitude offset
|
|
|
|
double alt_offset = 0.0;
|
|
|
|
|
|
|
|
// skip initial seconds
|
|
|
|
double skip = 0.0;
|
|
|
|
|
|
|
|
// for speed estimate
|
2007-06-18 22:01:32 +00:00
|
|
|
double last_lat = 0.0, last_lon = 0.0;
|
|
|
|
double kts_filter = 0.0;
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
bool inited = false;
|
|
|
|
|
2007-04-27 15:30:26 +00:00
|
|
|
bool run_real_time = true;
|
|
|
|
|
2007-02-21 21:11:15 +00:00
|
|
|
bool ignore_checksum = false;
|
|
|
|
|
2007-04-27 15:30:26 +00:00
|
|
|
bool sg_swap = false;
|
2006-11-11 18:43:17 +00:00
|
|
|
|
2007-06-18 22:01:32 +00:00
|
|
|
bool use_ground_track_hdg = false;
|
2007-12-05 19:01:49 +00:00
|
|
|
bool use_ground_speed = false;
|
2007-06-18 22:01:32 +00:00
|
|
|
|
2007-06-11 19:13:08 +00:00
|
|
|
bool est_controls = false;
|
|
|
|
|
2008-03-28 22:55:42 +00:00
|
|
|
float gps_status = -1.0;
|
|
|
|
|
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
// The function htond is defined this way due to the way some
|
|
|
|
// processors and OSes treat floating point values. Some will raise
|
|
|
|
// an exception whenever a "bad" floating point value is loaded into a
|
|
|
|
// floating point register. Solaris is notorious for this, but then
|
|
|
|
// so is LynxOS on the PowerPC. By translating the data in place,
|
|
|
|
// there is no need to load a FP register with the "corruped" floating
|
|
|
|
// point value. By doing the BIG_ENDIAN test, I can optimize the
|
|
|
|
// routine for big-endian processors so it can be as efficient as
|
|
|
|
// possible
|
2012-06-17 10:14:35 +00:00
|
|
|
static void htond (double &x)
|
2006-11-11 18:43:17 +00:00
|
|
|
{
|
|
|
|
if ( sgIsLittleEndian() ) {
|
|
|
|
int *Double_Overlay;
|
|
|
|
int Holding_Buffer;
|
2012-06-17 10:14:35 +00:00
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
Double_Overlay = (int *) &x;
|
|
|
|
Holding_Buffer = Double_Overlay [0];
|
2012-06-17 10:14:35 +00:00
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
Double_Overlay [0] = htonl (Double_Overlay [1]);
|
|
|
|
Double_Overlay [1] = htonl (Holding_Buffer);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Float version
|
2012-06-17 10:14:35 +00:00
|
|
|
static void htonf (float &x)
|
2006-11-11 18:43:17 +00:00
|
|
|
{
|
|
|
|
if ( sgIsLittleEndian() ) {
|
|
|
|
int *Float_Overlay;
|
|
|
|
int Holding_Buffer;
|
2012-06-17 10:14:35 +00:00
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
Float_Overlay = (int *) &x;
|
|
|
|
Holding_Buffer = Float_Overlay [0];
|
2012-06-17 10:14:35 +00:00
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
Float_Overlay [0] = htonl (Holding_Buffer);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void ugear2fg( gps *gpspacket, imu *imupacket, nav *navpacket,
|
2006-11-22 04:16:07 +00:00
|
|
|
servo *servopacket, health *healthpacket,
|
2006-11-11 18:43:17 +00:00
|
|
|
FGNetFDM *fdm, FGNetCtrls *ctrls )
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
// Version sanity checking
|
|
|
|
fdm->version = FG_NET_FDM_VERSION;
|
|
|
|
|
|
|
|
// Aero parameters
|
2007-02-27 19:45:48 +00:00
|
|
|
fdm->longitude = navpacket->lon * SG_DEGREES_TO_RADIANS;
|
|
|
|
fdm->latitude = navpacket->lat * SG_DEGREES_TO_RADIANS;
|
|
|
|
fdm->altitude = navpacket->alt + alt_offset;
|
2006-11-11 18:43:17 +00:00
|
|
|
fdm->agl = -9999.0;
|
|
|
|
fdm->psi = imupacket->psi; // heading
|
|
|
|
fdm->phi = imupacket->phi; // roll
|
|
|
|
fdm->theta = imupacket->the; // pitch;
|
|
|
|
|
|
|
|
fdm->phidot = 0.0;
|
|
|
|
fdm->thetadot = 0.0;
|
|
|
|
fdm->psidot = 0.0;
|
|
|
|
|
|
|
|
// estimate speed
|
|
|
|
// double az1, az2, dist;
|
2007-06-18 22:01:32 +00:00
|
|
|
// geo_inverse_wgs_84( fdm->altitude, last_lat, last_lon,
|
|
|
|
// fdm->latitude, fdm->longitude, &az1, &az2, &dist );
|
|
|
|
// last_lat = fdm->latitude;
|
|
|
|
// last_lon = fdm->longitude;
|
2006-11-11 18:43:17 +00:00
|
|
|
// double v_ms = dist / (frame_us / 1000000);
|
|
|
|
// double v_kts = v_ms * SG_METER_TO_NM * 3600;
|
2007-06-18 22:01:32 +00:00
|
|
|
// kts_filter = (0.9 * kts_filter) + (0.1 * v_kts);
|
|
|
|
// printf("dist = %.5f kts est = %.2f\n", dist, kts_filter);
|
|
|
|
|
2007-02-27 19:45:48 +00:00
|
|
|
double vn = navpacket->vn;
|
|
|
|
double ve = navpacket->ve;
|
|
|
|
double vd = navpacket->vd;
|
|
|
|
|
2007-06-18 22:01:32 +00:00
|
|
|
if ( use_ground_track_hdg ) {
|
|
|
|
fdm->psi = SGD_PI * 0.5 - atan2(vn, ve); // heading
|
|
|
|
}
|
2006-11-11 18:43:17 +00:00
|
|
|
|
2007-12-05 19:01:49 +00:00
|
|
|
double mps = 0.0;
|
|
|
|
if ( use_ground_speed ) {
|
|
|
|
mps = sqrt( vn*vn + ve*ve + vd*vd );
|
|
|
|
} else {
|
|
|
|
mps = imupacket->Pt;
|
|
|
|
}
|
|
|
|
// double mph = mps * 3600 / 1609.3440;
|
2007-06-18 22:01:32 +00:00
|
|
|
double kts = mps * SG_MPS_TO_KT;
|
|
|
|
fdm->vcas = kts;
|
2007-12-05 19:01:49 +00:00
|
|
|
// printf("speed = %.2f mph %.2f kts\n", mph, kts );
|
|
|
|
static double Ps = 0.0, Ps_last = 0.0, t_last = 0.0;
|
|
|
|
Ps_last = Ps;
|
|
|
|
Ps = 0.92 * Ps + 0.08 * imupacket->Ps;
|
|
|
|
double climb = (Ps - Ps_last) / (imupacket->time - t_last);
|
|
|
|
t_last = imupacket->time;
|
|
|
|
static double climbf = 0.0;
|
|
|
|
climbf = 0.994 * climbf + 0.006 * climb;
|
|
|
|
fdm->climb_rate = climbf; // fps
|
|
|
|
|
|
|
|
static double Ps_error = 0.0;
|
|
|
|
static double Ps_count = 0;
|
|
|
|
const double span = 10000.0;
|
|
|
|
Ps_count += 1.0; if (Ps_count > (span-1.0)) { Ps_count = (span-1.0); }
|
|
|
|
double error = navpacket->alt - Ps;
|
|
|
|
Ps_error = (Ps_count/span) * Ps_error + ((span-Ps_count)/span) * error;
|
|
|
|
fdm->altitude = Ps + Ps_error;
|
|
|
|
|
2008-03-30 05:34:40 +00:00
|
|
|
/* printf("%.3f, %.3f, %.3f, %.3f, %.8f, %.8f, %.3f, %.3f, %.3f, %.3f, %.3f\n",
|
2007-12-05 19:01:49 +00:00
|
|
|
imupacket->time, imupacket->the, -navpacket->vd, climbf,
|
|
|
|
navpacket->lat, navpacket->lon, gpspacket->alt, navpacket->alt,
|
2008-03-30 05:34:40 +00:00
|
|
|
imupacket->Ps, Ps, Ps + Ps_error); */
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
// cout << "climb rate = " << aero->hdota << endl;
|
|
|
|
fdm->v_north = 0.0;
|
|
|
|
fdm->v_east = 0.0;
|
|
|
|
fdm->v_down = 0.0;
|
|
|
|
fdm->v_wind_body_north = 0.0;
|
|
|
|
fdm->v_wind_body_east = 0.0;
|
|
|
|
fdm->v_wind_body_down = 0.0;
|
|
|
|
fdm->stall_warning = 0.0;
|
|
|
|
|
|
|
|
fdm->A_X_pilot = 0.0;
|
|
|
|
fdm->A_Y_pilot = 0.0;
|
|
|
|
fdm->A_Z_pilot = 0.0 /* (should be -G) */;
|
|
|
|
|
|
|
|
// Engine parameters
|
|
|
|
fdm->num_engines = 1;
|
|
|
|
fdm->eng_state[0] = 2;
|
|
|
|
// cout << "state = " << fdm->eng_state[0] << endl;
|
2006-12-18 23:04:38 +00:00
|
|
|
double rpm = 5000.0 - ((double)servopacket->chn[2] / 65536.0)*3500.0;
|
2006-11-11 18:43:17 +00:00
|
|
|
if ( rpm < 0.0 ) { rpm = 0.0; }
|
2006-12-18 23:04:38 +00:00
|
|
|
if ( rpm > 5000.0 ) { rpm = 5000.0; }
|
2006-11-11 18:43:17 +00:00
|
|
|
fdm->rpm[0] = rpm;
|
|
|
|
|
|
|
|
fdm->fuel_flow[0] = 0.0;
|
|
|
|
fdm->egt[0] = 0.0;
|
|
|
|
// cout << "egt = " << aero->EGT << endl;
|
|
|
|
fdm->oil_temp[0] = 0.0;
|
|
|
|
fdm->oil_px[0] = 0.0;
|
|
|
|
|
|
|
|
// Consumables
|
|
|
|
fdm->num_tanks = 2;
|
|
|
|
fdm->fuel_quantity[0] = 0.0;
|
|
|
|
fdm->fuel_quantity[1] = 0.0;
|
|
|
|
|
|
|
|
// Gear and flaps
|
|
|
|
fdm->num_wheels = 3;
|
|
|
|
fdm->wow[0] = 0;
|
|
|
|
fdm->wow[1] = 0;
|
|
|
|
fdm->wow[2] = 0;
|
|
|
|
|
|
|
|
// the following really aren't used in this context
|
|
|
|
fdm->cur_time = 0;
|
|
|
|
fdm->warp = 0;
|
|
|
|
fdm->visibility = 0;
|
|
|
|
|
|
|
|
// cout << "Flap deflection = " << aero->dflap << endl;
|
|
|
|
fdm->left_flap = 0.0;
|
|
|
|
fdm->right_flap = 0.0;
|
|
|
|
|
2007-06-11 19:13:08 +00:00
|
|
|
if ( est_controls ) {
|
|
|
|
static float est_elev = 0.0;
|
|
|
|
static float est_aileron = 0.0;
|
|
|
|
static float est_rudder = 0.0;
|
2007-06-18 22:01:32 +00:00
|
|
|
est_elev = 0.99 * est_elev + 0.01 * (imupacket->q * 4);
|
2007-06-11 20:14:40 +00:00
|
|
|
est_aileron = 0.95 * est_aileron + 0.05 * (imupacket->p * 5);
|
2007-06-18 22:01:32 +00:00
|
|
|
est_rudder = 0.95 * est_rudder + 0.05 * (imupacket->r * 2);
|
2007-12-05 19:01:49 +00:00
|
|
|
ctrls->elevator = fdm->elevator = -est_elev;
|
|
|
|
ctrls->aileron = fdm->left_aileron = est_aileron;
|
2007-06-11 19:13:08 +00:00
|
|
|
fdm->right_aileron = -est_aileron;
|
2007-12-05 19:01:49 +00:00
|
|
|
ctrls->rudder = fdm->rudder = est_rudder;
|
2007-06-11 19:13:08 +00:00
|
|
|
} else {
|
2007-12-05 19:01:49 +00:00
|
|
|
ctrls->elevator = fdm->elevator = 1.0 - ((double)servopacket->chn[1] / 32768.0);
|
|
|
|
ctrls->aileron = fdm->left_aileron = 1.0 - ((double)servopacket->chn[0] / 32768.0);
|
|
|
|
fdm->right_aileron = ((double)servopacket->chn[0] / 32768.0) - 1.0;
|
|
|
|
ctrls->rudder = fdm->rudder = 1.0 - ((double)servopacket->chn[3] / 32768.0);
|
|
|
|
ctrls->elevator *= 3.0;
|
|
|
|
ctrls->aileron *= 3.0;
|
2007-06-11 19:13:08 +00:00
|
|
|
}
|
2006-11-11 18:43:17 +00:00
|
|
|
fdm->elevator_trim_tab = 0.0;
|
|
|
|
fdm->left_flap = 0.0;
|
|
|
|
fdm->right_flap = 0.0;
|
|
|
|
fdm->nose_wheel = 0.0;
|
|
|
|
fdm->speedbrake = 0.0;
|
|
|
|
fdm->spoilers = 0.0;
|
|
|
|
|
|
|
|
// Convert the net buffer to network format
|
|
|
|
fdm->version = htonl(fdm->version);
|
|
|
|
|
|
|
|
htond(fdm->longitude);
|
|
|
|
htond(fdm->latitude);
|
|
|
|
htond(fdm->altitude);
|
|
|
|
htonf(fdm->agl);
|
|
|
|
htonf(fdm->phi);
|
|
|
|
htonf(fdm->theta);
|
|
|
|
htonf(fdm->psi);
|
|
|
|
htonf(fdm->alpha);
|
|
|
|
htonf(fdm->beta);
|
|
|
|
|
|
|
|
htonf(fdm->phidot);
|
|
|
|
htonf(fdm->thetadot);
|
|
|
|
htonf(fdm->psidot);
|
|
|
|
htonf(fdm->vcas);
|
|
|
|
htonf(fdm->climb_rate);
|
|
|
|
htonf(fdm->v_north);
|
|
|
|
htonf(fdm->v_east);
|
|
|
|
htonf(fdm->v_down);
|
|
|
|
htonf(fdm->v_wind_body_north);
|
|
|
|
htonf(fdm->v_wind_body_east);
|
|
|
|
htonf(fdm->v_wind_body_down);
|
|
|
|
|
|
|
|
htonf(fdm->A_X_pilot);
|
|
|
|
htonf(fdm->A_Y_pilot);
|
|
|
|
htonf(fdm->A_Z_pilot);
|
|
|
|
|
|
|
|
htonf(fdm->stall_warning);
|
|
|
|
htonf(fdm->slip_deg);
|
|
|
|
|
|
|
|
for ( i = 0; i < fdm->num_engines; ++i ) {
|
|
|
|
fdm->eng_state[i] = htonl(fdm->eng_state[i]);
|
|
|
|
htonf(fdm->rpm[i]);
|
|
|
|
htonf(fdm->fuel_flow[i]);
|
|
|
|
htonf(fdm->egt[i]);
|
|
|
|
htonf(fdm->cht[i]);
|
|
|
|
htonf(fdm->mp_osi[i]);
|
|
|
|
htonf(fdm->tit[i]);
|
|
|
|
htonf(fdm->oil_temp[i]);
|
|
|
|
htonf(fdm->oil_px[i]);
|
|
|
|
}
|
|
|
|
fdm->num_engines = htonl(fdm->num_engines);
|
|
|
|
|
|
|
|
for ( i = 0; i < fdm->num_tanks; ++i ) {
|
|
|
|
htonf(fdm->fuel_quantity[i]);
|
|
|
|
}
|
|
|
|
fdm->num_tanks = htonl(fdm->num_tanks);
|
|
|
|
|
|
|
|
for ( i = 0; i < fdm->num_wheels; ++i ) {
|
|
|
|
fdm->wow[i] = htonl(fdm->wow[i]);
|
|
|
|
htonf(fdm->gear_pos[i]);
|
|
|
|
htonf(fdm->gear_steer[i]);
|
|
|
|
htonf(fdm->gear_compression[i]);
|
|
|
|
}
|
|
|
|
fdm->num_wheels = htonl(fdm->num_wheels);
|
|
|
|
|
|
|
|
fdm->cur_time = htonl( fdm->cur_time );
|
|
|
|
fdm->warp = htonl( fdm->warp );
|
|
|
|
htonf(fdm->visibility);
|
|
|
|
|
|
|
|
htonf(fdm->elevator);
|
|
|
|
htonf(fdm->elevator_trim_tab);
|
|
|
|
htonf(fdm->left_flap);
|
|
|
|
htonf(fdm->right_flap);
|
|
|
|
htonf(fdm->left_aileron);
|
|
|
|
htonf(fdm->right_aileron);
|
|
|
|
htonf(fdm->rudder);
|
|
|
|
htonf(fdm->nose_wheel);
|
|
|
|
htonf(fdm->speedbrake);
|
|
|
|
htonf(fdm->spoilers);
|
2006-12-18 23:04:38 +00:00
|
|
|
|
2012-06-17 10:14:35 +00:00
|
|
|
#if 0
|
2007-12-05 19:01:49 +00:00
|
|
|
ctrls->version = FG_NET_CTRLS_VERSION;
|
|
|
|
ctrls->elevator_trim = 0.0;
|
|
|
|
ctrls->flaps = 0.0;
|
|
|
|
|
|
|
|
htonl(ctrls->version);
|
|
|
|
htond(ctrls->aileron);
|
|
|
|
htond(ctrls->rudder);
|
|
|
|
htond(ctrls->elevator);
|
|
|
|
htond(ctrls->elevator_trim);
|
|
|
|
htond(ctrls->flaps);
|
|
|
|
#endif
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-28 22:55:42 +00:00
|
|
|
static void ugear2opengc( gps *gpspacket, imu *imupacket, nav *navpacket,
|
|
|
|
servo *servopacket, health *healthpacket,
|
|
|
|
ogcFGData *ogc )
|
|
|
|
{
|
|
|
|
// Version sanity checking
|
|
|
|
ogc->version_id = OGC_VERSION;
|
|
|
|
|
|
|
|
// Aero parameters
|
2008-03-30 05:34:40 +00:00
|
|
|
ogc->longitude = navpacket->lon;
|
|
|
|
ogc->latitude = navpacket->lat;
|
2008-03-28 22:55:42 +00:00
|
|
|
ogc->heading = imupacket->psi * SG_RADIANS_TO_DEGREES; // heading
|
|
|
|
ogc->bank = imupacket->phi * SG_RADIANS_TO_DEGREES; // roll
|
|
|
|
ogc->pitch = imupacket->the * SG_RADIANS_TO_DEGREES; // pitch;
|
|
|
|
|
|
|
|
ogc->phi_dot = 0.0;
|
|
|
|
ogc->theta_dot = 0.0;
|
|
|
|
ogc->psi_dot = 0.0;
|
|
|
|
|
|
|
|
ogc->alpha = 0.0;
|
|
|
|
ogc->beta = 0.0;
|
|
|
|
ogc->alpha_dot = 0.0;
|
|
|
|
ogc->beta_dot = 0.0;
|
|
|
|
|
|
|
|
ogc->left_aileron = 1.0 - ((double)servopacket->chn[0] / 32768.0);
|
|
|
|
ogc->right_aileron = ((double)servopacket->chn[0] / 32768.0) - 1.0;
|
|
|
|
ogc->elevator = 1.0 - ((double)servopacket->chn[1] / 32768.0);
|
|
|
|
ogc->elevator_trim = 0.0;
|
|
|
|
ogc->rudder = 1.0 - ((double)servopacket->chn[3] / 32768.0);
|
|
|
|
ogc->flaps = 0.0;
|
|
|
|
ogc->flaps_cmd = 0.0;
|
|
|
|
|
|
|
|
ogc->wind = 0.0;
|
|
|
|
ogc->wind_dir = 0.0;
|
|
|
|
|
|
|
|
// estimate speed
|
|
|
|
// double az1, az2, dist;
|
|
|
|
// geo_inverse_wgs_84( fdm->altitude, last_lat, last_lon,
|
|
|
|
// fdm->latitude, fdm->longitude, &az1, &az2, &dist );
|
|
|
|
// last_lat = fdm->latitude;
|
|
|
|
// last_lon = fdm->longitude;
|
|
|
|
// double v_ms = dist / (frame_us / 1000000);
|
|
|
|
// double v_kts = v_ms * SG_METER_TO_NM * 3600;
|
|
|
|
// kts_filter = (0.9 * kts_filter) + (0.1 * v_kts);
|
|
|
|
// printf("dist = %.5f kts est = %.2f\n", dist, kts_filter);
|
|
|
|
|
|
|
|
double vn = navpacket->vn;
|
|
|
|
double ve = navpacket->ve;
|
|
|
|
double vd = navpacket->vd;
|
|
|
|
|
|
|
|
if ( use_ground_track_hdg ) {
|
|
|
|
ogc->heading = (SGD_PI * 0.5 - atan2(vn, ve)) * SG_RADIANS_TO_DEGREES;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ogc->heading < 0 ) { ogc->heading += 360.0; }
|
|
|
|
|
|
|
|
double mps = 0.0;
|
|
|
|
if ( use_ground_speed ) {
|
|
|
|
mps = sqrt( vn*vn + ve*ve + vd*vd );
|
|
|
|
} else {
|
|
|
|
mps = imupacket->Pt;
|
|
|
|
}
|
|
|
|
// double mph = mps * 3600 / 1609.3440;
|
|
|
|
double kts = mps * SG_MPS_TO_KT;
|
|
|
|
ogc->v_kcas = kts;
|
|
|
|
// printf("speed = %.2f mph %.2f kts\n", mph, kts );
|
|
|
|
static double Ps = 0.0, Ps_last = 0.0, t_last = 0.0;
|
|
|
|
Ps_last = Ps;
|
|
|
|
Ps = 0.92 * Ps + 0.08 * imupacket->Ps;
|
|
|
|
double climb = (Ps - Ps_last) / (imupacket->time - t_last);
|
|
|
|
t_last = imupacket->time;
|
|
|
|
static double climbf = 0.0;
|
|
|
|
climbf = 0.994 * climbf + 0.006 * climb;
|
|
|
|
ogc->vvi = climbf; // fps
|
|
|
|
|
2008-03-30 05:34:40 +00:00
|
|
|
// uncomment one of the following schemes for setting elevation:
|
|
|
|
|
|
|
|
// use the navigation (inertially augmented gps estimate)
|
|
|
|
// ogc->altitude = ogc->elevation
|
|
|
|
// = (navpacket->alt + alt_offset * SG_METER_TO_FEET);
|
|
|
|
|
|
|
|
// use estimate error between pressure sensor and gps altitude over time
|
|
|
|
// use pressure sensor + error average for altitude estimate.
|
2008-03-28 22:55:42 +00:00
|
|
|
static double Ps_error = 0.0;
|
|
|
|
static double Ps_count = 0;
|
|
|
|
const double span = 10000.0;
|
|
|
|
Ps_count += 1.0; if (Ps_count > (span-1.0)) { Ps_count = (span-1.0); }
|
|
|
|
double error = navpacket->alt - Ps;
|
|
|
|
Ps_error = (Ps_count/span) * Ps_error + ((span-Ps_count)/span) * error;
|
2008-03-30 05:34:40 +00:00
|
|
|
ogc->elevation = (Ps + Ps_error) * SG_METER_TO_FEET;
|
2008-03-28 22:55:42 +00:00
|
|
|
|
2008-03-30 05:34:40 +00:00
|
|
|
/* printf("%.3f, %.3f, %.3f, %.3f, %.8f, %.8f, %.3f, %.3f, %.3f, %.3f, %.3f\n",
|
2008-03-28 22:55:42 +00:00
|
|
|
imupacket->time, imupacket->the, -navpacket->vd, climbf,
|
|
|
|
navpacket->lat, navpacket->lon, gpspacket->alt, navpacket->alt,
|
2008-03-30 05:34:40 +00:00
|
|
|
imupacket->Ps, Ps, Ps + Ps_error); */
|
2008-03-28 22:55:42 +00:00
|
|
|
|
|
|
|
if ( est_controls ) {
|
|
|
|
static float est_elev = 0.0;
|
|
|
|
static float est_aileron = 0.0;
|
|
|
|
static float est_rudder = 0.0;
|
|
|
|
est_elev = 0.99 * est_elev + 0.01 * (imupacket->q * 4);
|
|
|
|
est_aileron = 0.95 * est_aileron + 0.05 * (imupacket->p * 5);
|
|
|
|
est_rudder = 0.95 * est_rudder + 0.05 * (imupacket->r * 2);
|
|
|
|
ogc->elevator = -est_elev;
|
|
|
|
ogc->left_aileron = est_aileron;
|
|
|
|
ogc->right_aileron = -est_aileron;
|
|
|
|
ogc->rudder = est_rudder;
|
|
|
|
} else {
|
|
|
|
ogc->elevator = 1.0 - ((double)servopacket->chn[1] / 32768.0);
|
|
|
|
ogc->left_aileron = 1.0 - ((double)servopacket->chn[0] / 32768.0);
|
|
|
|
ogc->right_aileron = ((double)servopacket->chn[0] / 32768.0) - 1.0;
|
|
|
|
ogc->rudder = 1.0 - ((double)servopacket->chn[3] / 32768.0);
|
|
|
|
}
|
2008-04-04 06:15:05 +00:00
|
|
|
ogc->elevator *= 4.0;
|
|
|
|
ogc->left_aileron *= 4.0;
|
|
|
|
ogc->right_aileron *= 4.0;
|
|
|
|
ogc->rudder *= 4.0;
|
2008-03-28 22:55:42 +00:00
|
|
|
|
|
|
|
// additional "abused" data fields
|
2008-04-04 06:15:05 +00:00
|
|
|
ogc->egt[0] = ogc->bank - healthpacket->target_roll_deg; // flight director target roll
|
|
|
|
ogc->egt[1] = -ogc->pitch + healthpacket->target_pitch_deg; // flight director target pitch
|
|
|
|
ogc->egt[2] = healthpacket->target_heading_deg; // target heading bug
|
|
|
|
ogc->egt[3] = healthpacket->target_climb_fps; // target VVI bug
|
|
|
|
ogc->epr[0] = healthpacket->target_altitude_ft; // target altitude bug
|
|
|
|
ogc->epr[1] = 30.0; // target speed bug
|
2008-03-28 22:55:42 +00:00
|
|
|
ogc->epr[2] = gps_status; // gps status box
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-30 05:34:40 +00:00
|
|
|
static void send_data_udp( gps *gpspacket, imu *imupacket, nav *navpacket,
|
|
|
|
servo *servopacket, health *healthpacket )
|
|
|
|
{
|
2008-03-28 22:55:42 +00:00
|
|
|
int ogcsize = sizeof( ogcFGData );
|
2006-11-11 18:43:17 +00:00
|
|
|
int fdmsize = sizeof( FGNetFDM );
|
2008-03-30 05:34:40 +00:00
|
|
|
// int ctrlsize = sizeof( FGNetCtrls );
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
// cout << "Running main loop" << endl;
|
|
|
|
|
2008-03-28 22:55:42 +00:00
|
|
|
ogcFGData fgogc;
|
2006-11-11 18:43:17 +00:00
|
|
|
FGNetFDM fgfdm;
|
|
|
|
FGNetCtrls fgctrls;
|
|
|
|
|
2006-11-22 04:16:07 +00:00
|
|
|
ugear2fg( gpspacket, imupacket, navpacket, servopacket, healthpacket,
|
|
|
|
&fgfdm, &fgctrls );
|
2008-03-28 22:55:42 +00:00
|
|
|
ugear2opengc( gpspacket, imupacket, navpacket, servopacket, healthpacket,
|
|
|
|
&fgogc );
|
2012-06-17 10:14:35 +00:00
|
|
|
opengc_sock.send(&fgogc, ogcsize, 0);
|
|
|
|
fdm_sock.send(&fgfdm, fdmsize, 0);
|
2007-12-05 19:01:49 +00:00
|
|
|
// len = ctrls_sock.send(&fgctrls, ctrlsize, 0);
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void usage( const string &argv0 ) {
|
|
|
|
cout << "Usage: " << argv0 << endl;
|
|
|
|
cout << "\t[ --help ]" << endl;
|
|
|
|
cout << "\t[ --infile <infile_name>" << endl;
|
2007-04-27 15:30:26 +00:00
|
|
|
cout << "\t[ --flight <flight_dir>" << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
cout << "\t[ --serial <dev_name>" << endl;
|
|
|
|
cout << "\t[ --outfile <outfile_name> (capture the data to a file)" << endl;
|
|
|
|
cout << "\t[ --hertz <hertz> ]" << endl;
|
|
|
|
cout << "\t[ --host <hostname> ]" << endl;
|
|
|
|
cout << "\t[ --broadcast ]" << endl;
|
2008-03-28 22:55:42 +00:00
|
|
|
cout << "\t[ --opengc-port <opengc output port #> ]" << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
|
|
|
|
cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
|
2007-06-18 22:01:32 +00:00
|
|
|
cout << "\t[ --groundtrack-heading ]" << endl;
|
2007-12-05 19:01:49 +00:00
|
|
|
cout << "\t[ --ground-speed ]" << endl;
|
2007-06-11 19:13:08 +00:00
|
|
|
cout << "\t[ --estimate-control-deflections ]" << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
cout << "\t[ --altitude-offset <meters> ]" << endl;
|
|
|
|
cout << "\t[ --skip-seconds <seconds> ]" << endl;
|
2007-04-27 15:30:26 +00:00
|
|
|
cout << "\t[ --no-real-time ]" << endl;
|
2007-02-21 21:11:15 +00:00
|
|
|
cout << "\t[ --ignore-checksum ]" << endl;
|
2007-04-27 15:30:26 +00:00
|
|
|
cout << "\t[ --sg-swap ]" << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main( int argc, char **argv ) {
|
|
|
|
double hertz = 60.0;
|
|
|
|
string out_host = "localhost";
|
|
|
|
bool do_broadcast = false;
|
|
|
|
|
|
|
|
// process command line arguments
|
|
|
|
for ( int i = 1; i < argc; ++i ) {
|
|
|
|
if ( strcmp( argv[i], "--help" ) == 0 ) {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( 0 );
|
|
|
|
} else if ( strcmp( argv[i], "--hertz" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
hertz = atof( argv[i] );
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
} else if ( strcmp( argv[i], "--infile" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
infile = argv[i];
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2007-04-27 15:30:26 +00:00
|
|
|
} else if ( strcmp( argv[i], "--flight" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
flight_dir = argv[i];
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2006-11-11 18:43:17 +00:00
|
|
|
} else if ( strcmp( argv[i], "--outfile" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
outfile = argv[i];
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
} else if ( strcmp( argv[i], "--serial" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
serialdev = argv[i];
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
} else if ( strcmp( argv[i], "--host" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
out_host = argv[i];
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
} else if ( strcmp( argv[i], "--broadcast" ) == 0 ) {
|
|
|
|
do_broadcast = true;
|
2008-03-28 22:55:42 +00:00
|
|
|
} else if ( strcmp( argv[i], "--opengc-port" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
opengc_port = atoi( argv[i] );
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2006-11-11 18:43:17 +00:00
|
|
|
} else if ( strcmp( argv[i], "--fdm-port" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
fdm_port = atoi( argv[i] );
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
} else if ( strcmp( argv[i], "--ctrls-port" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
ctrls_port = atoi( argv[i] );
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2007-06-18 22:01:32 +00:00
|
|
|
} else if ( strcmp (argv[i], "--groundtrack-heading" ) == 0 ) {
|
|
|
|
use_ground_track_hdg = true;
|
2007-12-05 19:01:49 +00:00
|
|
|
} else if ( strcmp (argv[i], "--ground-speed" ) == 0 ) {
|
|
|
|
use_ground_speed = true;
|
2007-06-18 22:01:32 +00:00
|
|
|
} else if (strcmp (argv[i], "--estimate-control-deflections" ) == 0) {
|
2007-06-11 19:13:08 +00:00
|
|
|
est_controls = true;
|
2006-11-11 18:43:17 +00:00
|
|
|
} else if ( strcmp( argv[i], "--altitude-offset" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
alt_offset = atof( argv[i] );
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
} else if ( strcmp( argv[i], "--skip-seconds" ) == 0 ) {
|
|
|
|
++i;
|
|
|
|
if ( i < argc ) {
|
|
|
|
skip = atof( argv[i] );
|
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2007-04-27 15:30:26 +00:00
|
|
|
} else if ( strcmp( argv[i], "--no-real-time" ) == 0 ) {
|
|
|
|
run_real_time = false;
|
2007-02-21 21:11:15 +00:00
|
|
|
} else if ( strcmp( argv[i], "--ignore-checksum" ) == 0 ) {
|
2007-04-27 15:30:26 +00:00
|
|
|
ignore_checksum = true;
|
|
|
|
} else if ( strcmp( argv[i], "--sg-swap" ) == 0 ) {
|
|
|
|
sg_swap = true;
|
2006-11-11 18:43:17 +00:00
|
|
|
} else {
|
|
|
|
usage( argv[0] );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup up outgoing network connections
|
|
|
|
|
2011-01-05 16:18:12 +00:00
|
|
|
simgear::Socket::initSockets(); // We must call this before any other net stuff
|
2006-11-11 18:43:17 +00:00
|
|
|
|
2008-03-28 22:55:42 +00:00
|
|
|
if ( ! opengc_sock.open( false ) ) { // open a UDP socket
|
|
|
|
cout << "error opening opengc output socket" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2006-11-11 18:43:17 +00:00
|
|
|
if ( ! fdm_sock.open( false ) ) { // open a UDP socket
|
|
|
|
cout << "error opening fdm output socket" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ( ! ctrls_sock.open( false ) ) { // open a UDP socket
|
|
|
|
cout << "error opening ctrls output socket" << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cout << "open net channels" << endl;
|
|
|
|
|
2008-03-28 22:55:42 +00:00
|
|
|
opengc_sock.setBlocking( false );
|
2006-11-11 18:43:17 +00:00
|
|
|
fdm_sock.setBlocking( false );
|
|
|
|
ctrls_sock.setBlocking( false );
|
|
|
|
cout << "blocking false" << endl;
|
|
|
|
|
|
|
|
if ( do_broadcast ) {
|
2008-03-28 22:55:42 +00:00
|
|
|
opengc_sock.setBroadcast( true );
|
2006-11-11 18:43:17 +00:00
|
|
|
fdm_sock.setBroadcast( true );
|
|
|
|
ctrls_sock.setBroadcast( true );
|
|
|
|
}
|
|
|
|
|
2008-03-28 22:55:42 +00:00
|
|
|
if ( opengc_sock.connect( out_host.c_str(), opengc_port ) == -1 ) {
|
|
|
|
perror("connect");
|
|
|
|
cout << "error connecting to outgoing opengc port: " << out_host
|
|
|
|
<< ":" << opengc_port << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cout << "connected outgoing opengc socket" << endl;
|
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
if ( fdm_sock.connect( out_host.c_str(), fdm_port ) == -1 ) {
|
|
|
|
perror("connect");
|
|
|
|
cout << "error connecting to outgoing fdm port: " << out_host
|
|
|
|
<< ":" << fdm_port << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cout << "connected outgoing fdm socket" << endl;
|
|
|
|
|
|
|
|
if ( ctrls_sock.connect( out_host.c_str(), ctrls_port ) == -1 ) {
|
|
|
|
perror("connect");
|
|
|
|
cout << "error connecting to outgoing ctrls port: " << out_host
|
|
|
|
<< ":" << ctrls_port << endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cout << "connected outgoing ctrls socket" << endl;
|
|
|
|
|
2007-04-27 15:30:26 +00:00
|
|
|
if ( sg_swap ) {
|
|
|
|
track.set_stargate_swap_mode();
|
|
|
|
}
|
|
|
|
|
2008-04-04 22:34:19 +00:00
|
|
|
UGTelnet telnet( 5402 );
|
|
|
|
telnet.open();
|
|
|
|
|
2007-04-27 15:30:26 +00:00
|
|
|
if ( infile.length() || flight_dir.length() ) {
|
|
|
|
if ( infile.length() ) {
|
|
|
|
// Load data from a stream log data file
|
|
|
|
track.load_stream( infile, ignore_checksum );
|
|
|
|
} else if ( flight_dir.length() ) {
|
|
|
|
// Load data from a flight directory
|
|
|
|
track.load_flight( flight_dir );
|
|
|
|
}
|
2006-11-11 18:43:17 +00:00
|
|
|
cout << "Loaded " << track.gps_size() << " gps records." << endl;
|
|
|
|
cout << "Loaded " << track.imu_size() << " imu records." << endl;
|
|
|
|
cout << "Loaded " << track.nav_size() << " nav records." << endl;
|
|
|
|
cout << "Loaded " << track.servo_size() << " servo records." << endl;
|
2006-11-22 04:16:07 +00:00
|
|
|
cout << "Loaded " << track.health_size() << " health records." << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
int size = track.imu_size();
|
|
|
|
|
|
|
|
double current_time = track.get_imupt(0).time;
|
|
|
|
cout << "Track begin time is " << current_time << endl;
|
|
|
|
double end_time = track.get_imupt(size-1).time;
|
|
|
|
cout << "Track end time is " << end_time << endl;
|
|
|
|
cout << "Duration = " << end_time - current_time << endl;
|
|
|
|
|
2007-06-18 22:01:32 +00:00
|
|
|
if ( track.gps_size() > 0 ) {
|
|
|
|
double tmp = track.get_gpspt(track.gps_size()-1).ITOW;
|
2008-03-30 05:34:40 +00:00
|
|
|
int days = (int)(tmp / (24 * 60 * 60));
|
2007-06-18 22:01:32 +00:00
|
|
|
tmp -= days * 24 * 60 * 60;
|
2008-03-30 05:34:40 +00:00
|
|
|
int hours = (int)(tmp / (60 * 60));
|
2007-06-18 22:01:32 +00:00
|
|
|
tmp -= hours * 60 * 60;
|
2008-03-30 05:34:40 +00:00
|
|
|
int min = (int)(tmp / 60);
|
2007-06-18 22:01:32 +00:00
|
|
|
tmp -= min * 60;
|
|
|
|
double sec = tmp;
|
|
|
|
printf("[GPS ]:ITOW= %.3f[sec] %dd %02d:%02d:%06.3f\n",
|
|
|
|
tmp, days, hours, min, sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
// advance skip seconds forward
|
|
|
|
current_time += skip;
|
|
|
|
|
|
|
|
frame_us = 1000000.0 / hertz;
|
|
|
|
if ( frame_us < 0.0 ) {
|
|
|
|
frame_us = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SGTimeStamp start_time;
|
|
|
|
start_time.stamp();
|
|
|
|
int gps_count = 0;
|
|
|
|
int imu_count = 0;
|
|
|
|
int nav_count = 0;
|
|
|
|
int servo_count = 0;
|
2006-11-22 04:16:07 +00:00
|
|
|
int health_count = 0;
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
gps gps0, gps1;
|
|
|
|
gps0 = gps1 = track.get_gpspt( 0 );
|
2012-06-17 10:14:35 +00:00
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
imu imu0, imu1;
|
|
|
|
imu0 = imu1 = track.get_imupt( 0 );
|
2012-06-17 10:14:35 +00:00
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
nav nav0, nav1;
|
|
|
|
nav0 = nav1 = track.get_navpt( 0 );
|
2012-06-17 10:14:35 +00:00
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
servo servo0, servo1;
|
|
|
|
servo0 = servo1 = track.get_servopt( 0 );
|
2012-06-17 10:14:35 +00:00
|
|
|
|
2006-11-22 04:16:07 +00:00
|
|
|
health health0, health1;
|
|
|
|
health0 = health1 = track.get_healthpt( 0 );
|
2007-04-27 15:30:26 +00:00
|
|
|
|
|
|
|
double last_lat = -999.9, last_lon = -999.9;
|
|
|
|
|
2007-06-09 22:38:54 +00:00
|
|
|
printf("<gpx>\n");
|
|
|
|
printf(" <trk>\n");
|
|
|
|
printf(" <trkseg>\n");
|
2006-11-11 18:43:17 +00:00
|
|
|
while ( current_time < end_time ) {
|
|
|
|
// cout << "current_time = " << current_time << " end_time = "
|
|
|
|
// << end_time << endl;
|
|
|
|
|
|
|
|
// Advance gps pointer
|
|
|
|
while ( current_time > gps1.time
|
2007-04-27 15:30:26 +00:00
|
|
|
&& gps_count < track.gps_size() - 1 )
|
2006-11-11 18:43:17 +00:00
|
|
|
{
|
|
|
|
gps0 = gps1;
|
|
|
|
++gps_count;
|
|
|
|
// cout << "count = " << count << endl;
|
|
|
|
gps1 = track.get_gpspt( gps_count );
|
|
|
|
}
|
|
|
|
// cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
|
|
|
|
// << endl;
|
|
|
|
|
|
|
|
// Advance imu pointer
|
|
|
|
while ( current_time > imu1.time
|
2007-04-27 15:30:26 +00:00
|
|
|
&& imu_count < track.imu_size() - 1 )
|
2006-11-11 18:43:17 +00:00
|
|
|
{
|
|
|
|
imu0 = imu1;
|
|
|
|
++imu_count;
|
|
|
|
// cout << "count = " << count << endl;
|
|
|
|
imu1 = track.get_imupt( imu_count );
|
|
|
|
}
|
|
|
|
// cout << "pos0 = " << pos0.get_seconds()
|
|
|
|
// << " pos1 = " << pos1.get_seconds() << endl;
|
|
|
|
|
|
|
|
// Advance nav pointer
|
|
|
|
while ( current_time > nav1.time
|
2007-04-27 15:30:26 +00:00
|
|
|
&& nav_count < track.nav_size() - 1 )
|
2006-11-11 18:43:17 +00:00
|
|
|
{
|
|
|
|
nav0 = nav1;
|
|
|
|
++nav_count;
|
2007-04-27 15:30:26 +00:00
|
|
|
// cout << "nav count = " << nav_count << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
nav1 = track.get_navpt( nav_count );
|
|
|
|
}
|
|
|
|
// cout << "pos0 = " << pos0.get_seconds()
|
|
|
|
// << " pos1 = " << pos1.get_seconds() << endl;
|
|
|
|
|
|
|
|
// Advance servo pointer
|
|
|
|
while ( current_time > servo1.time
|
2007-04-27 15:30:26 +00:00
|
|
|
&& servo_count < track.servo_size() - 1 )
|
2006-11-11 18:43:17 +00:00
|
|
|
{
|
|
|
|
servo0 = servo1;
|
|
|
|
++servo_count;
|
|
|
|
// cout << "count = " << count << endl;
|
|
|
|
servo1 = track.get_servopt( servo_count );
|
|
|
|
}
|
|
|
|
// cout << "pos0 = " << pos0.get_seconds()
|
|
|
|
// << " pos1 = " << pos1.get_seconds() << endl;
|
|
|
|
|
2006-11-22 04:16:07 +00:00
|
|
|
// Advance health pointer
|
|
|
|
while ( current_time > health1.time
|
2007-04-27 15:30:26 +00:00
|
|
|
&& health_count < track.health_size() - 1 )
|
2006-11-22 04:16:07 +00:00
|
|
|
{
|
|
|
|
health0 = health1;
|
|
|
|
++health_count;
|
|
|
|
// cout << "count = " << count << endl;
|
|
|
|
health1 = track.get_healthpt( health_count );
|
|
|
|
}
|
|
|
|
// cout << "pos0 = " << pos0.get_seconds()
|
|
|
|
// << " pos1 = " << pos1.get_seconds() << endl;
|
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
double gps_percent;
|
|
|
|
if ( fabs(gps1.time - gps0.time) < 0.00001 ) {
|
|
|
|
gps_percent = 0.0;
|
|
|
|
} else {
|
|
|
|
gps_percent =
|
|
|
|
(current_time - gps0.time) /
|
|
|
|
(gps1.time - gps0.time);
|
|
|
|
}
|
|
|
|
// cout << "Percent = " << percent << endl;
|
|
|
|
|
|
|
|
double imu_percent;
|
|
|
|
if ( fabs(imu1.time - imu0.time) < 0.00001 ) {
|
|
|
|
imu_percent = 0.0;
|
|
|
|
} else {
|
|
|
|
imu_percent =
|
|
|
|
(current_time - imu0.time) /
|
|
|
|
(imu1.time - imu0.time);
|
|
|
|
}
|
|
|
|
// cout << "Percent = " << percent << endl;
|
|
|
|
|
|
|
|
double nav_percent;
|
|
|
|
if ( fabs(nav1.time - nav0.time) < 0.00001 ) {
|
|
|
|
nav_percent = 0.0;
|
|
|
|
} else {
|
|
|
|
nav_percent =
|
|
|
|
(current_time - nav0.time) /
|
|
|
|
(nav1.time - nav0.time);
|
|
|
|
}
|
|
|
|
// cout << "Percent = " << percent << endl;
|
|
|
|
|
|
|
|
double servo_percent;
|
|
|
|
if ( fabs(servo1.time - servo0.time) < 0.00001 ) {
|
|
|
|
servo_percent = 0.0;
|
|
|
|
} else {
|
|
|
|
servo_percent =
|
|
|
|
(current_time - servo0.time) /
|
|
|
|
(servo1.time - servo0.time);
|
|
|
|
}
|
|
|
|
// cout << "Percent = " << percent << endl;
|
|
|
|
|
2006-11-22 04:16:07 +00:00
|
|
|
double health_percent;
|
|
|
|
if ( fabs(health1.time - health0.time) < 0.00001 ) {
|
|
|
|
health_percent = 0.0;
|
|
|
|
} else {
|
|
|
|
health_percent =
|
|
|
|
(current_time - health0.time) /
|
|
|
|
(health1.time - health0.time);
|
|
|
|
}
|
|
|
|
// cout << "Percent = " << percent << endl;
|
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
gps gpspacket = UGEARInterpGPS( gps0, gps1, gps_percent );
|
|
|
|
imu imupacket = UGEARInterpIMU( imu0, imu1, imu_percent );
|
|
|
|
nav navpacket = UGEARInterpNAV( nav0, nav1, nav_percent );
|
|
|
|
servo servopacket = UGEARInterpSERVO( servo0, servo1,
|
|
|
|
servo_percent );
|
2006-11-22 04:16:07 +00:00
|
|
|
health healthpacket = UGEARInterpHEALTH( health0, health1,
|
|
|
|
health_percent );
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
// cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg
|
|
|
|
// << endl;
|
|
|
|
// cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg
|
|
|
|
// << endl;
|
|
|
|
// cout << (double)current_time << " " << pos.lat_deg << ", "
|
|
|
|
// << pos.lon_deg << " " << att.yaw_deg << endl;
|
|
|
|
if ( gpspacket.lat > -500 ) {
|
2007-04-27 15:30:26 +00:00
|
|
|
// printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
|
|
|
|
// current_time,
|
|
|
|
// navpacket.lat, navpacket.lon, navpacket.alt,
|
|
|
|
// imupacket.psi, imupacket.the, imupacket.phi );
|
|
|
|
double dlat = last_lat - navpacket.lat;
|
|
|
|
double dlon = last_lon - navpacket.lon;
|
|
|
|
double dist = sqrt( dlat*dlat + dlon*dlon );
|
2007-06-09 22:38:54 +00:00
|
|
|
if ( dist > 0.01 ) {
|
|
|
|
printf(" <trkpt lat=\"%.8f\" lon=\"%.8f\"></trkpt>\n",
|
2007-04-27 15:30:26 +00:00
|
|
|
navpacket.lat, navpacket.lon );
|
|
|
|
// printf(" </wpt>\n");
|
|
|
|
last_lat = navpacket.lat;
|
|
|
|
last_lon = navpacket.lon;
|
|
|
|
}
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-28 22:55:42 +00:00
|
|
|
if ( (fabs(gpspacket.lat) < 0.0001 &&
|
|
|
|
fabs(gpspacket.lon) < 0.0001 &&
|
|
|
|
fabs(gpspacket.alt) < 0.0001) )
|
|
|
|
{
|
|
|
|
printf("WARNING: LOST GPS!!!\n");
|
|
|
|
gps_status = -1.0;
|
|
|
|
} else {
|
|
|
|
gps_status = 1.0;
|
|
|
|
}
|
|
|
|
|
2008-03-30 05:34:40 +00:00
|
|
|
send_data_udp( &gpspacket, &imupacket, &navpacket, &servopacket,
|
|
|
|
&healthpacket );
|
2006-11-11 18:43:17 +00:00
|
|
|
|
2007-04-27 15:30:26 +00:00
|
|
|
if ( run_real_time ) {
|
|
|
|
// Update the elapsed time.
|
|
|
|
static bool first_time = true;
|
|
|
|
if ( first_time ) {
|
|
|
|
last_time_stamp.stamp();
|
|
|
|
first_time = false;
|
|
|
|
}
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
current_time_stamp.stamp();
|
2007-04-27 15:30:26 +00:00
|
|
|
/* Convert to ms */
|
2009-03-12 18:34:57 +00:00
|
|
|
double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs();
|
2007-04-27 15:30:26 +00:00
|
|
|
if ( elapsed_us < (frame_us - 2000) ) {
|
|
|
|
double requested_us = (frame_us - elapsed_us) - 2000 ;
|
|
|
|
ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
|
|
|
|
}
|
|
|
|
current_time_stamp.stamp();
|
2009-03-12 18:34:57 +00:00
|
|
|
while ( (current_time_stamp - last_time_stamp).toUSecs() < frame_us ) {
|
2007-04-27 15:30:26 +00:00
|
|
|
current_time_stamp.stamp();
|
|
|
|
}
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
current_time += (frame_us / 1000000.0);
|
|
|
|
last_time_stamp = current_time_stamp;
|
|
|
|
}
|
|
|
|
|
2007-06-09 22:38:54 +00:00
|
|
|
printf(" <trkpt lat=\"%.8f\" lon=\"%.8f\"></trkpt>\n",
|
|
|
|
nav1.lat, nav1.lon );
|
|
|
|
|
|
|
|
printf(" </trkseg>\n");
|
|
|
|
printf(" </trk>\n");
|
|
|
|
nav0 = track.get_navpt( 0 );
|
|
|
|
nav1 = track.get_navpt( track.nav_size() - 1 );
|
|
|
|
printf(" <wpt lat=\"%.8f\" lon=\"%.8f\"></wpt>\n",
|
|
|
|
nav0.lat, nav0.lon );
|
|
|
|
printf(" <wpt lat=\"%.8f\" lon=\"%.8f\"></wpt>\n",
|
|
|
|
nav1.lat, nav1.lon );
|
|
|
|
printf("<gpx>\n");
|
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
cout << "Processed " << imu_count << " entries in "
|
2009-03-12 18:34:57 +00:00
|
|
|
<< current_time_stamp - start_time << " seconds."
|
2006-11-11 18:43:17 +00:00
|
|
|
<< endl;
|
|
|
|
} else if ( serialdev.length() ) {
|
|
|
|
// process incoming data from the serial port
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
double current_time = 0.0;
|
2007-04-27 15:30:26 +00:00
|
|
|
double last_time = 0.0;
|
2006-11-11 18:43:17 +00:00
|
|
|
|
2006-12-18 23:04:38 +00:00
|
|
|
gps gpspacket; bzero( &gpspacket, sizeof(gpspacket) );
|
|
|
|
imu imupacket; bzero( &imupacket, sizeof(imupacket) );
|
|
|
|
nav navpacket; bzero( &navpacket, sizeof(navpacket) );
|
|
|
|
servo servopacket; bzero( &servopacket, sizeof(servopacket) );
|
|
|
|
health healthpacket; bzero( &healthpacket, sizeof(healthpacket) );
|
2006-11-11 18:43:17 +00:00
|
|
|
|
2008-03-30 05:34:40 +00:00
|
|
|
double gps_time = 0.0;
|
|
|
|
double imu_time = 0.0;
|
|
|
|
double nav_time = 0.0;
|
|
|
|
double servo_time = 0.0;
|
|
|
|
double health_time = 0.0;
|
|
|
|
double command_time = 0.0;
|
|
|
|
double command_heartbeat = 0.0;
|
2006-11-11 18:43:17 +00:00
|
|
|
|
|
|
|
// open the serial port device
|
2008-03-30 05:34:40 +00:00
|
|
|
SGSerialPort uavcom( serialdev, 115200 );
|
|
|
|
if ( !uavcom.is_enabled() ) {
|
2006-11-11 18:43:17 +00:00
|
|
|
cout << "Cannot open: " << serialdev << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// open up the data log file if requested
|
|
|
|
if ( !outfile.length() ) {
|
|
|
|
cout << "no --outfile <name> specified, cannot capture data!"
|
|
|
|
<< endl;
|
|
|
|
return false;
|
|
|
|
}
|
2008-03-30 05:34:40 +00:00
|
|
|
SGFile log( outfile );
|
|
|
|
if ( !log.open( SG_IO_OUT ) ) {
|
2006-11-11 18:43:17 +00:00
|
|
|
cout << "Cannot open: " << outfile << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-03-30 05:34:40 +00:00
|
|
|
// add some test commands
|
2008-04-04 22:34:19 +00:00
|
|
|
//command_mgr.add("ap,alt,1000");
|
|
|
|
//command_mgr.add("home,158.0,32.5");
|
|
|
|
//command_mgr.add("go,home");
|
|
|
|
//command_mgr.add("go,route");
|
2008-03-30 05:34:40 +00:00
|
|
|
|
|
|
|
while ( uavcom.is_enabled() ) {
|
2007-01-17 23:11:45 +00:00
|
|
|
// cout << "looking for next message ..." << endl;
|
2008-03-30 05:34:40 +00:00
|
|
|
int id = track.next_message( &uavcom, &log, &gpspacket,
|
2006-11-22 04:16:07 +00:00
|
|
|
&imupacket, &navpacket, &servopacket,
|
2007-02-21 21:11:15 +00:00
|
|
|
&healthpacket, ignore_checksum );
|
2007-01-17 23:11:45 +00:00
|
|
|
// cout << "message id = " << id << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
count++;
|
|
|
|
|
2008-04-04 22:34:19 +00:00
|
|
|
telnet.process();
|
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
if ( id == GPS_PACKET ) {
|
|
|
|
if ( gpspacket.time > gps_time ) {
|
|
|
|
gps_time = gpspacket.time;
|
|
|
|
current_time = gps_time;
|
|
|
|
} else {
|
2007-02-20 03:13:01 +00:00
|
|
|
cout << "oops gps back in time: " << gpspacket.time << " " << gps_time << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
|
|
|
} else if ( id == IMU_PACKET ) {
|
|
|
|
if ( imupacket.time > imu_time ) {
|
|
|
|
imu_time = imupacket.time;
|
|
|
|
current_time = imu_time;
|
|
|
|
} else {
|
2007-04-27 15:30:26 +00:00
|
|
|
cout << "oops imu back in time: " << imupacket.time << " " << imu_time << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
|
|
|
} else if ( id == NAV_PACKET ) {
|
|
|
|
if ( navpacket.time > nav_time ) {
|
|
|
|
nav_time = navpacket.time;
|
|
|
|
current_time = nav_time;
|
|
|
|
} else {
|
2007-04-27 15:30:26 +00:00
|
|
|
cout << "oops nav back in time: " << navpacket.time << " " << nav_time << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
|
|
|
} else if ( id == SERVO_PACKET ) {
|
|
|
|
if ( servopacket.time > servo_time ) {
|
|
|
|
servo_time = servopacket.time;
|
|
|
|
current_time = servo_time;
|
|
|
|
} else {
|
2007-04-27 15:30:26 +00:00
|
|
|
cout << "oops servo back in time: " << servopacket.time << " " << servo_time << endl;
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
2006-11-22 04:16:07 +00:00
|
|
|
} else if ( id == HEALTH_PACKET ) {
|
|
|
|
if ( healthpacket.time > health_time ) {
|
|
|
|
health_time = healthpacket.time;
|
|
|
|
current_time = health_time;
|
2008-03-30 05:34:40 +00:00
|
|
|
printf("Received a health packet, sequence: %d\n",
|
2008-07-21 19:57:56 +00:00
|
|
|
(int)healthpacket.command_sequence);
|
2008-04-04 22:34:19 +00:00
|
|
|
command_mgr.update_cmd_sequence(healthpacket.command_sequence);
|
2006-11-22 04:16:07 +00:00
|
|
|
} else {
|
2007-04-27 15:30:26 +00:00
|
|
|
cout << "oops health back in time: " << healthpacket.time << " " << health_time << endl;
|
2006-11-22 04:16:07 +00:00
|
|
|
}
|
2012-06-17 10:14:35 +00:00
|
|
|
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
|
|
|
|
2008-03-28 22:55:42 +00:00
|
|
|
if ( (current_time > gps_time + 2) ||
|
|
|
|
(fabs(gpspacket.lat) < 0.0001 &&
|
|
|
|
fabs(gpspacket.lon) < 0.0001 &&
|
|
|
|
fabs(gpspacket.alt) < 0.0001) )
|
|
|
|
{
|
|
|
|
printf("WARNING: LOST GPS!!!\n");
|
|
|
|
gps_status = -1.0;
|
2008-04-04 06:15:05 +00:00
|
|
|
} else {
|
|
|
|
gps_status = 1.0;
|
2008-03-28 22:55:42 +00:00
|
|
|
}
|
|
|
|
|
2008-04-04 22:34:19 +00:00
|
|
|
// Generate a ground station heart beat every 4 seconds
|
|
|
|
if ( current_time >= command_heartbeat + 4 ) {
|
|
|
|
command_mgr.add("hb");
|
2008-03-30 05:34:40 +00:00
|
|
|
command_heartbeat = current_time;
|
|
|
|
}
|
2012-06-17 10:14:35 +00:00
|
|
|
|
2008-03-30 05:34:40 +00:00
|
|
|
// Command update @ 1hz
|
|
|
|
if ( current_time >= command_time + 1 ) {
|
2008-04-04 22:34:19 +00:00
|
|
|
command_mgr.update(&uavcom);
|
2008-03-30 05:34:40 +00:00
|
|
|
command_time = current_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relay data on to FlightGear and LFSTech Glass
|
2007-04-27 15:30:26 +00:00
|
|
|
if ( current_time >= last_time + (1/hertz) ) {
|
|
|
|
// if ( gpspacket.lat > -500 ) {
|
2008-03-28 22:55:42 +00:00
|
|
|
int londeg = (int)navpacket.lon;
|
2008-07-21 19:57:56 +00:00
|
|
|
// double lonmin = fabs(navpacket.lon - londeg);
|
2008-03-28 22:55:42 +00:00
|
|
|
int latdeg = (int)navpacket.lat;
|
2008-07-21 19:57:56 +00:00
|
|
|
// double latmin = fabs(navpacket.lat - latdeg);
|
2008-03-28 22:55:42 +00:00
|
|
|
londeg = abs(londeg);
|
|
|
|
latdeg = abs(latdeg);
|
2008-03-30 05:34:40 +00:00
|
|
|
/*printf( "%.2f %c%02d:%.4f %c%03d:%.4f %.1f %.2f %.2f %.2f\n",
|
2007-04-27 15:30:26 +00:00
|
|
|
current_time,
|
2008-03-28 22:55:42 +00:00
|
|
|
latdir, latdeg, latmin, londir, londeg, lonmin,
|
|
|
|
navpacket.alt,
|
2007-04-27 15:30:26 +00:00
|
|
|
imupacket.phi*SGD_RADIANS_TO_DEGREES,
|
|
|
|
imupacket.the*SGD_RADIANS_TO_DEGREES,
|
2008-03-30 05:34:40 +00:00
|
|
|
imupacket.psi*SGD_RADIANS_TO_DEGREES ); */
|
2007-04-27 15:30:26 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
last_time = current_time;
|
2008-03-30 05:34:40 +00:00
|
|
|
send_data_udp( &gpspacket, &imupacket, &navpacket,
|
|
|
|
&servopacket, &healthpacket );
|
2007-04-27 15:30:26 +00:00
|
|
|
}
|
2006-11-11 18:43:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|