first commit

This commit is contained in:
Your Name
2022-10-20 20:29:11 +08:00
commit 4d531f8044
3238 changed files with 1387862 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
add_executable(GPSsmooth
GPSsmooth.cxx GPSsmooth.hxx
gps_main.cxx
)
add_executable(MIDGsmooth
MIDG-II.cxx MIDG-II.hxx
MIDG_main.cxx
)
add_executable(UGsmooth
UGear.cxx UGear.hxx
UGear_command.cxx UGear_command.hxx
UGear_telnet.cxx UGear_telnet.hxx
UGear_main.cxx
)
target_include_directories(GPSsmooth PRIVATE ${PLIB_INCLUDE_DIR})
target_include_directories(MIDGsmooth PRIVATE ${PLIB_INCLUDE_DIR})
target_include_directories(UGsmooth PRIVATE ${PLIB_INCLUDE_DIR})
target_link_libraries(GPSsmooth
SimGearCore
${PLIB_SG_LIBRARY}
${PLIB_UL_LIBRARY}
)
target_link_libraries(MIDGsmooth
SimGearCore
${PLIB_SG_LIBRARY}
${PLIB_UL_LIBRARY}
)
target_link_libraries(UGsmooth
SimGearCore
${PLIB_SG_LIBRARY}
${PLIB_UL_LIBRARY}
${ZLIB_LIBRARY}
)
install(TARGETS GPSsmooth MIDGsmooth UGsmooth RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@@ -0,0 +1,166 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/compiler.h>
#include <iostream>
#include <simgear/constants.h>
#include <simgear/io/iostreams/sgstream.hxx>
#include <simgear/misc/strutils.hxx>
#include <simgear/misc/sg_path.hxx>
#include "GPSsmooth.hxx"
using std::cout;
using std::endl;
GPSTrack::GPSTrack() {};
GPSTrack::~GPSTrack() {};
// load the specified file, return the number of records loaded
int GPSTrack::load( const std::string &file ) {
int count = 0;
data.clear();
// openg the file
sg_gzifstream in( SGPath::fromLocal8Bit(file.c_str()) );
if ( !in.is_open() ) {
cout << "Cannot open file: " << file << endl;
return 0;
}
std::vector <std::string> tokens;
GPSPoint p;
while ( ! in.eof() ) {
char tmp[2049];
in.getline(tmp, 2048);
tokens.clear();
tokens = simgear::strutils::split(tmp, ",");
int dd;
double raw, min;
if ( tokens[0] == "$GPRMC" && tokens.size() == 13 ) {
double raw_time = atof(tokens[1].c_str());
GPSTime gps_time = GPSTime( raw_time );
if ( (gps_time.get_time() > p.gps_time.get_time()) &&
(p.gps_time.get_time() > 1.0) )
{
// new data cycle store last data before continuing
data.push_back( p );
count++;
}
p.gps_time = gps_time;
raw = atof( tokens[3].c_str() );
dd = (int)(raw / 100.00);
min = raw - dd * 100.0;
p.lat_deg = dd + min / 60.0;
if ( tokens[4] == "S" ) {
p.lat_deg = -p.lat_deg;
}
raw = atof( tokens[5].c_str() );
dd = (int)(raw / 100.00);
min = raw - dd * 100.0;
p.lon_deg = dd + min / 60.0;
if ( tokens[6] == "W" ) {
p.lon_deg = -p.lon_deg;
}
static double max_speed = 0.0;
p.speed_kts = atof( tokens[7].c_str() );
if ( p.speed_kts > max_speed ) {
max_speed = p.speed_kts;
cout << "max speed = " << max_speed << endl;
}
p.course_true = atof( tokens[8].c_str() ) * SGD_DEGREES_TO_RADIANS;
} else if ( tokens[0] == "$GPGGA" && tokens.size() == 15 ) {
double raw_time = atof(tokens[1].c_str());
GPSTime gps_time = GPSTime( raw_time );
if ( fabs(gps_time.get_time() - p.gps_time.get_time()) > 0.0001 &&
(p.gps_time.get_time() > 1.0) ) {
// new data cycle store last data before continuing
data.push_back( p );
count++;
}
p.gps_time = gps_time;
raw = atof( tokens[2].c_str() );
dd = (int)(raw / 100.00);
min = raw - dd * 100.0;
p.lat_deg = dd + min / 60.0;
if ( tokens[3] == "S" ) {
p.lat_deg = -p.lat_deg;
}
raw = atof( tokens[4].c_str() );
dd = (int)(raw / 100.00);
min = raw - dd * 100.0;
p.lon_deg = dd + min / 60.0;
if ( tokens[5] == "W" ) {
p.lon_deg = -p.lon_deg;
}
p.fix_quality = atoi( tokens[6].c_str() );
p.num_satellites = atoi( tokens[7].c_str() );
p.hdop = atof( tokens[8].c_str() );
static double max_alt = 0.0;
double alt = atof( tokens[9].c_str() );
if ( alt > max_alt ) {
max_alt = alt;
cout << "max alt = " << max_alt << endl;
}
if ( tokens[10] == "F" || tokens[10] == "f" ) {
alt *= SG_FEET_TO_METER;
}
p.altitude_msl = alt;
}
}
return count;
}
static double interp( double a, double b, double p, bool rotational = false ) {
double diff = b - a;
if ( rotational ) {
// special handling of rotational data
if ( diff > SGD_PI ) {
diff -= SGD_2PI;
} else if ( diff < -SGD_PI ) {
diff += SGD_2PI;
}
}
return a + diff * p;
}
GPSPoint GPSInterpolate( const GPSPoint A, const GPSPoint B,
const double percent ) {
GPSPoint p;
p.gps_time = GPSTime((int)interp(A.gps_time.get_time(),
B.gps_time.get_time(),
percent));
p.lat_deg = interp(A.lat_deg, B.lat_deg, percent);
p.lon_deg = interp(A.lon_deg, B.lon_deg, percent);
p.fix_quality = (int)interp(A.fix_quality, B.fix_quality, percent);
p.num_satellites = (int)interp(A.num_satellites, B.num_satellites, percent);
p.hdop = interp(A.hdop, B.hdop, percent);
p.altitude_msl = interp(A.altitude_msl, B.altitude_msl, percent);
p.speed_kts = interp(A.speed_kts, B.speed_kts, percent);
p.course_true = interp(A.course_true, B.course_true, percent, true);
return p;
}

View File

@@ -0,0 +1,102 @@
#pragma once
#include <simgear/compiler.h>
#include <iostream>
#include <string>
#include <vector>
// encapsulate a gps integer time (fixme, assumes all times in a track
// are from the same day, so we don't handle midnight roll over)
class GPSTime {
public:
double seconds;
inline GPSTime( const int hh, const int mm, const double ss ) {
seconds = hh*3600 + mm*60 + ss;
}
inline GPSTime( const double gpstime ) {
double tmp = gpstime;
int hh = (int)(tmp / 10000);
tmp -= hh * 10000;
int mm = (int)(tmp / 100);
tmp -= mm * 100;
double ss = tmp;
seconds = hh*3600 + mm*60 + ss;
// std::cout << gpstime << " = " << seconds << std::endl;
}
inline ~GPSTime() {}
inline double get_time() const { return seconds; }
inline double diff_sec( const GPSTime t ) const {
return seconds - t.seconds;
}
};
// encapsulate the interesting gps data for a moment in time
class GPSPoint {
public:
GPSTime gps_time;
double lat_deg;
double lon_deg;
int fix_quality;
int num_satellites;
double hdop;
double altitude_msl;
double speed_kts;
double course_true;
GPSPoint() :
gps_time(GPSTime(0,0,0)),
lat_deg(0.0),
lon_deg(0.0),
fix_quality(0),
num_satellites(0),
hdop(0.0),
altitude_msl(0.0),
speed_kts(0.0),
course_true(0.0)
{ }
inline double get_time() const { return gps_time.get_time(); }
};
// Manage a saved gps log (track file)
class GPSTrack {
private:
std::vector <GPSPoint> data;
public:
GPSTrack();
~GPSTrack();
int load( const std::string &file );
inline int size() const { return data.size(); }
inline GPSPoint get_point( const unsigned int i )
{
if ( i < data.size() ) {
return data[i];
} else {
return GPSPoint();
}
}
};
GPSPoint GPSInterpolate( const GPSPoint A, const GPSPoint B,
const double percent );

586
utils/GPSsmooth/MIDG-II.cxx Normal file
View File

@@ -0,0 +1,586 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/compiler.h>
#include <iostream>
#include <simgear/constants.h>
#include <simgear/io/sg_file.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/io/iostreams/sgstream.hxx>
#include <simgear/misc/strutils.hxx>
#include <simgear/misc/stdint.hxx>
#include "MIDG-II.hxx"
using std::cout;
using std::endl;
MIDGTrack::MIDGTrack() {};
MIDGTrack::~MIDGTrack() {};
/*
* Unused function
*/
#if(0)
static uint32_t read_swab( char *buf, size_t offset, size_t size ) {
uint32_t result = 0;
char *ptr = buf + offset;
// MIDG data is big endian so swap if needed.
if ( sgIsLittleEndian() ) {
if ( size == 4 ) {
sgEndianSwap( (uint32_t *)ptr );
} else if ( size == 2 ) {
sgEndianSwap( (uint16_t *)ptr );
}
}
if ( size == 4 ) {
result = *(uint32_t *)ptr;
} else if ( size == 2 ) {
result = *(uint16_t *)ptr;
} else if ( size == 1 ) {
result = *(uint8_t *)ptr;
} else {
cout << "unknown size in read_swab()" << endl;
}
return result;
}
#endif
static bool validate_cksum( uint8_t id, uint8_t size, char *buf,
uint8_t cksum0, uint8_t cksum1 )
{
uint8_t c0 = 0;
uint8_t c1 = 0;
c0 += id;
c1 += c0;
// cout << "c0 = " << (unsigned int)c0 << " c1 = " << (unsigned int)c1 << endl;
c0 += size;
c1 += c0;
// cout << "c0 = " << (unsigned int)c0 << " c1 = " << (unsigned int)c1 << endl;
for ( uint8_t i = 0; i < size; i++ ) {
c0 += (uint8_t)buf[i];
c1 += c0;
// cout << "c0 = " << (unsigned int)c0 << " c1 = " << (unsigned int)c1
// << " [" << (unsigned int)buf[i] << "]" << endl;
}
// cout << "c0 = " << (unsigned int)c0 << " (" << (unsigned int)cksum0
// << ") c1 = " << (unsigned int)c1 << " (" << (unsigned int)cksum1
// << ")" << endl;
if ( c0 == cksum0 && c1 == cksum1 ) {
return true;
} else {
return false;
}
}
void MIDGTrack::parse_msg( const int id, char *buf, MIDGpos *pos, MIDGatt *att )
{
/*
* Completely unused parser results. Removed from compiling to remove the warnings
*/
#if(0)
if ( id == 1 ) {
uint32_t ts;
uint16_t status;
int16_t temp;
// cout << "message 1 =" << endl;
// timestamp
ts = (uint32_t)read_swab( buf, 0, 4 );
// cout << " time stamp = " << ts << endl;
// status
status = (uint16_t)read_swab( buf, 4, 2 );
// cout << " status = " << status << endl;
// temp
temp = (int16_t)read_swab( buf, 6, 2 );
// cout << " temp = " << temp << endl;
} else if ( id == 2 ) {
uint32_t ts;
int16_t p, q, r;
int16_t ax, ay, az;
int16_t mx, my, mz;
uint8_t flags;
// cout << "message 2 =" << endl;
// timestamp
ts = (uint32_t)read_swab( buf, 0, 4 );
// cout << " time stamp = " << ts << endl;
// p, q, r
p = (int16_t)read_swab( buf, 4, 2 );
q = (int16_t)read_swab( buf, 6, 2 );
r = (int16_t)read_swab( buf, 8, 2 );
// cout << " pqr = " << p << "," << q << "," << r << endl;
// ax, ay, az
ax = (int16_t)read_swab( buf, 10, 2 );
ay = (int16_t)read_swab( buf, 12, 2 );
az = (int16_t)read_swab( buf, 14, 2 );
// cout << " ax ay az = " << ax << "," << ay << "," << az << endl;
// mx, my, mz
mx = (int16_t)read_swab( buf, 16, 2 );
my = (int16_t)read_swab( buf, 18, 2 );
mz = (int16_t)read_swab( buf, 20, 2 );
// cout << " mx my mz = " << mx << "," << my << "," << mz << endl;
// flags
flags = (uint8_t)read_swab( buf, 22, 1 );
// cout << " GPS 1PPS flag = " << (int)(flags & (1 << 6))
// << " Timestamp is gps = " << (int)(flags & (1 << 7)) << endl;
} else if ( id == 3 ) {
uint32_t ts;
int16_t mx, my, mz;
uint8_t flags;
// cout << "message 3 =" << endl;
// timestamp
ts = (uint32_t)read_swab( buf, 0, 4 );
// cout << " time stamp = " << ts << endl;
// mx, my, mz
mx = (int16_t)read_swab( buf, 4, 2 );
my = (int16_t)read_swab( buf, 6, 2 );
mz = (int16_t)read_swab( buf, 8, 2 );
// cout << " mx my mz = " << mx << "," << my << "," << mz << endl;
// flags
flags = (uint8_t)read_swab( buf, 10, 1 );
// cout << " GPS 1PPS flag = " << (int)(flags & (1 << 6)) << endl;
} else if ( id == 10 ) {
uint32_t ts;
int16_t p, q, r;
int16_t ax, ay, az;
int16_t yaw, pitch, roll;
int32_t Qw, Qx, Qy, Qz;
uint8_t flags;
// cout << "message 10 =" << endl;
// timestamp
ts = (uint32_t)read_swab( buf, 0, 4 );
// cout << " att time stamp = " << ts << endl;
att->midg_time = MIDGTime( ts );
// p, q, r
p = (int16_t)read_swab( buf, 4, 2 );
q = (int16_t)read_swab( buf, 6, 2 );
r = (int16_t)read_swab( buf, 8, 2 );
// cout << " pqr = " << p << "," << q << "," << r << endl;
// ax, ay, az
ax = (int16_t)read_swab( buf, 10, 2 );
ay = (int16_t)read_swab( buf, 12, 2 );
az = (int16_t)read_swab( buf, 14, 2 );
// cout << " ax ay az = " << ax << "," << ay << "," << az << endl;
// yaw, pitch, roll
yaw = (int16_t)read_swab( buf, 16, 2 );
pitch = (int16_t)read_swab( buf, 18, 2 );
roll = (int16_t)read_swab( buf, 20, 2 );
// cout << " yaw, pitch, roll = " << yaw << "," << pitch << ","
// << roll << endl;
att->yaw_rad = ( (double)yaw / 100.0 ) * SG_PI / 180.0;
att->pitch_rad = ( (double)pitch / 100.0 ) * SG_PI / 180.0;
att->roll_rad = ( (double)roll / 100.0 ) * SG_PI / 180.0;
// Qw, Qx, Qy, Qz
Qw = (int32_t)read_swab( buf, 22, 4 );
Qx = (int32_t)read_swab( buf, 26, 4 );
Qy = (int32_t)read_swab( buf, 30, 4 );
Qz = (int32_t)read_swab( buf, 34, 4 );
// cout << " Qw,Qx,Qy,Qz = " << Qw << "," << Qx << "," << Qy << ","
// << Qz << endl;
// flags
flags = (uint8_t)read_swab( buf, 38, 1 );
// cout << " External hdg measurement applied = "
// << (int)(flags & (1 << 3)) << endl
// << " Magnatometer measurement applied = "
// << (int)(flags & (1 << 4)) << endl
// << " DGPS = " << (int)(flags & (1 << 5)) << endl
// << " Timestamp is gps = " << (int)(flags & (1 << 6)) << endl
// << " INS mode = " << (int)(flags & (1 << 7))
// << endl;
} else if ( id == 12 ) {
uint32_t ts;
int32_t posx, posy, posz;
int32_t velx, vely, velz;
uint8_t flags;
// cout << "message 12 =" << endl;
// timestamp
ts = (uint32_t)read_swab( buf, 0, 4 );
// cout << " pos time stamp = " << ts << endl;
pos->midg_time = MIDGTime( ts );
// posx, posy, posz
posx = (int32_t)read_swab( buf, 4, 4 );
posy = (int32_t)read_swab( buf, 8, 4 );
posz = (int32_t)read_swab( buf, 12, 4 );
// cout << " pos = " << posx << "," << posy << "," << posz << endl;
double xyz[3];
xyz[0] = (double)posx/100; xyz[1] = (double)posy/100; xyz[2] = (double)posz/100;
double lat, lon, alt;
sgCartToGeod(xyz, &lat, &lon, &alt);
pos->lat_deg = lat * 180.0 / SG_PI;
pos->lon_deg = lon * 180.0 / SG_PI;
pos->altitude_msl = alt;
// cout << " lon = " << pos->lon_deg << " lat = " << pos->lat_deg
// << " alt = " << pos->altitude_msl << endl;
// velx, vely, velz
velx = (int32_t)read_swab( buf, 16, 4 );
vely = (int32_t)read_swab( buf, 20, 4 );
velz = (int32_t)read_swab( buf, 24, 4 );
// cout << " vel = " << velx << "," << vely << "," << velz << endl;
double tmp1 = velx*velx + vely*vely + velz*velz;
double vel_cms = sqrt( tmp1 );
double vel_ms = vel_cms / 100.0;
pos->speed_kts = vel_ms * SG_METER_TO_NM * 3600;
// flags
flags = (uint8_t)read_swab( buf, 28, 1 );
// cout << " ENU pos rel to 1st fix = " << (int)(flags & (1 << 0)) << endl
// << " Velocity format = " << (int)(flags & (1 << 1)) << endl
// << " bit 2 = " << (int)(flags & (1 << 2)) << endl
// << " bit 3 = " << (int)(flags & (1 << 3)) << endl
// << " GPS pos/vel valid = " << (int)(flags & (1 << 4)) << endl
// << " DGPS = " << (int)(flags & (1 << 5)) << endl
// << " Timestamp is gps = " << (int)(flags & (1 << 6)) << endl
// << " Solution src (0=gps, 1=ins) = " << (int)(flags & (1 << 7))
// << endl;
} else if ( id == 20 ) {
uint32_t gps_ts, gps_week;
uint16_t details;
int32_t gps_posx, gps_posy, gps_posz;
int32_t gps_velx, gps_vely, gps_velz;
int16_t pdop, pacc, sacc;
// cout << "message 20 =" << endl;
// timestamp -- often slightly off from midg time stamp so
// let's not use gps ts to determine if we need to push the
// previous data or not, just roll it into the current data
// independent of time stamp.
gps_ts = (uint32_t)read_swab( buf, 0, 4 );
// pt->midg_time = MIDGTime( ts );
gps_week = (uint16_t)read_swab( buf, 4, 2 );
// cout << " gps time stamp = " << gps_ts << " week = " << gps_week
// << endl;
// details
details = (uint16_t)read_swab( buf, 6, 2 );
// cout << " details = " << details << endl;
// gps_posx, gps_posy, gps_posz
gps_posx = (int32_t)read_swab( buf, 8, 4 );
gps_posy = (int32_t)read_swab( buf, 12, 4 );
gps_posz = (int32_t)read_swab( buf, 16, 4 );
// cout << " gps_pos = " << gps_posx << "," << gps_posy << ","
// << gps_posz << endl;
// gps_velx, gps_vely, gps_velz
gps_velx = (int32_t)read_swab( buf, 20, 4 );
gps_vely = (int32_t)read_swab( buf, 24, 4 );
gps_velz = (int32_t)read_swab( buf, 28, 4 );
// cout << " gps_vel = " << gps_velx << "," << gps_vely << ","
// << gps_velz << endl;
// position dop
pdop = (uint16_t)read_swab( buf, 32, 2 );
// cout << " pdop = " << pdop << endl;
// position accuracy
pacc = (uint16_t)read_swab( buf, 34, 2 );
// cout << " pacc = " << pacc << endl;
// speed accuracy
sacc = (uint16_t)read_swab( buf, 36, 2 );
// cout << " sacc = " << sacc << endl;
} else {
cout << "unknown id = " << id << endl;
}
#endif
}
// load the specified file, return the number of records loaded
bool MIDGTrack::load( const string &file ) {
int count = 0;
MIDGpos pos;
MIDGatt att;
uint32_t pos_time = 1;
uint32_t att_time = 1;
pos_data.clear();
att_data.clear();
// open the file
SGFile input( file );
if ( !input.open( SG_IO_IN ) ) {
cout << "Cannot open file: " << file << endl;
return false;
}
while ( ! input.eof() ) {
// cout << "looking for next message ..." << endl;
int id = next_message( &input, NULL, &pos, &att );
count++;
if ( id == 10 ) {
if ( att.get_msec() > att_time ) {
att_data.push_back( att );
att_time = att.get_msec();
} else {
cout << "oops att back in time" << endl;
}
} else if ( id == 12 ) {
if ( pos.get_msec() > pos_time ) {
pos_data.push_back( pos );
pos_time = pos.get_msec();
} else {
cout << "oops pos back in time" << endl;
}
}
}
cout << "processed " << count << " messages" << endl;
return true;
}
// attempt to work around some system dependent issues. Our read can
// return < data than we want.
int myread( SGIOChannel *ch, SGIOChannel *log, char *buf, int length ) {
bool myeof = false;
int result = 0;
if ( !myeof ) {
result = ch->read( buf, length );
// cout << "wanted " << length << " read " << result << " bytes" << endl;
if ( ch->get_type() == sgFileType ) {
myeof = ((SGFile *)ch)->eof();
}
}
if ( result > 0 && log != NULL ) {
log->write( buf, result );
}
return result;
}
// attempt to work around some system dependent issues. Our read can
// return < data than we want.
int serial_read( SGSerialPort *serial, char *buf, int length ) {
int result = 0;
int bytes_read = 0;
char *tmp = buf;
while ( bytes_read < length ) {
result = serial->read_port( tmp, length - bytes_read );
bytes_read += result;
tmp += result;
// cout << " read " << bytes_read << " of " << length << endl;
}
return bytes_read;
}
// load the next message of a real time data stream
int MIDGTrack::next_message( SGIOChannel *ch, SGIOChannel *log,
MIDGpos *pos, MIDGatt *att )
{
char tmpbuf[256];
char savebuf[256];
// cout << "in next_message()" << endl;
bool myeof = false;
// scan for sync characters
uint8_t sync0, sync1;
myread( ch, log, tmpbuf, 1 ); sync0 = (unsigned char)tmpbuf[0];
myread( ch, log, tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
while ( (sync0 != 129 || sync1 != 161) && !myeof ) {
sync0 = sync1;
myread( ch, log, tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
// cout << "scanning for start of message "
// << (unsigned int)sync0 << " " << (unsigned int)sync1
// << ", eof = " << ch->eof() << endl;
if ( ch->get_type() == sgFileType ) {
myeof = ((SGFile *)ch)->eof();
}
}
// cout << "found start of message ..." << endl;
// read message id and size
myread( ch, log, tmpbuf, 1 ); uint8_t id = (unsigned char)tmpbuf[0];
myread( ch, log, tmpbuf, 1 ); uint8_t size = (unsigned char)tmpbuf[0];
// cout << "message = " << (int)id << " size = " << (int)size << endl;
// load message
if ( ch->get_type() == sgFileType ) {
int count = myread( ch, log, savebuf, size );
if ( count != size ) {
cout << "ERROR: didn't read enough bytes!" << endl;
}
} else {
#ifdef READ_ONE_BY_ONE
for ( int i = 0; i < size; ++i ) {
myread( ch, log, tmpbuf, 1 ); savebuf[i] = tmpbuf[0];
}
#else
myread( ch, log, savebuf, size );
#endif
}
// read checksum
myread( ch, log, tmpbuf, 1 ); uint8_t cksum0 = (unsigned char)tmpbuf[0];
myread( ch, log, tmpbuf, 1 ); uint8_t cksum1 = (unsigned char)tmpbuf[0];
if ( validate_cksum( id, size, savebuf, cksum0, cksum1 ) ) {
parse_msg( id, savebuf, pos, att );
return id;
}
cout << "Check sum failure!" << endl;
return -1;
}
// load the next message of a real time data stream
int MIDGTrack::next_message( SGSerialPort *serial, SGIOChannel *log,
MIDGpos *pos, MIDGatt *att )
{
char tmpbuf[256];
char savebuf[256];
cout << "in next_message()" << endl;
bool myeof = false;
// scan for sync characters
uint8_t sync0, sync1;
serial_read( serial, tmpbuf, 2 );
sync0 = (unsigned char)tmpbuf[0];
sync1 = (unsigned char)tmpbuf[1];
while ( (sync0 != 129 || sync1 != 161) && !myeof ) {
sync0 = sync1;
serial_read( serial, tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
cout << "scanning for start of message "
<< (unsigned int)sync0 << " " << (unsigned int)sync1
<< endl;
}
cout << "found start of message ..." << endl;
// read message id and size
serial_read( serial, tmpbuf, 2 );
uint8_t id = (unsigned char)tmpbuf[0];
uint8_t size = (unsigned char)tmpbuf[1];
// cout << "message = " << (int)id << " size = " << (int)size << endl;
// load message
serial_read( serial, savebuf, size );
// read checksum
serial_read( serial, tmpbuf, 2 );
uint8_t cksum0 = (unsigned char)tmpbuf[0];
uint8_t cksum1 = (unsigned char)tmpbuf[1];
if ( validate_cksum( id, size, savebuf, cksum0, cksum1 ) ) {
parse_msg( id, savebuf, pos, att );
//
// FIXME
// WRITE DATA TO LOG FILE
//
return id;
}
cout << "Check sum failure!" << endl;
return -1;
}
static double interp( double a, double b, double p, bool rotational = false ) {
double diff = b - a;
if ( rotational ) {
// special handling of rotational data
if ( diff > SGD_PI ) {
diff -= SGD_2PI;
} else if ( diff < -SGD_PI ) {
diff += SGD_2PI;
}
}
return a + diff * p;
}
MIDGpos MIDGInterpPos( const MIDGpos A, const MIDGpos B, const double percent )
{
MIDGpos p;
p.midg_time = MIDGTime((uint32_t)interp(A.midg_time.get_msec(),
B.midg_time.get_msec(),
percent));
p.lat_deg = interp(A.lat_deg, B.lat_deg, percent);
p.lon_deg = interp(A.lon_deg, B.lon_deg, percent);
p.altitude_msl = interp(A.altitude_msl, B.altitude_msl, percent);
p.fix_quality = (int)interp(A.fix_quality, B.fix_quality, percent);
p.num_satellites = (int)interp(A.num_satellites, B.num_satellites, percent);
p.hdop = interp(A.hdop, B.hdop, percent);
p.speed_kts = interp(A.speed_kts, B.speed_kts, percent);
p.course_true = interp(A.course_true, B.course_true, percent, true);
return p;
}
MIDGatt MIDGInterpAtt( const MIDGatt A, const MIDGatt B, const double percent )
{
MIDGatt p;
p.midg_time = MIDGTime((uint32_t)interp(A.midg_time.get_msec(),
B.midg_time.get_msec(),
percent));
p.yaw_rad = interp(A.yaw_rad, B.yaw_rad, percent, true);
p.pitch_rad = interp(A.pitch_rad, B.pitch_rad, percent, true);
p.roll_rad = interp(A.roll_rad, B.roll_rad, percent, true);
return p;
}

157
utils/GPSsmooth/MIDG-II.hxx Normal file
View File

@@ -0,0 +1,157 @@
#pragma once
#include <simgear/compiler.h>
#include <iostream>
#include <string>
#include <vector>
#include <simgear/misc/stdint.hxx>
#include <simgear/io/iochannel.hxx>
#include <simgear/serial/serial.hxx>
// encapsulate a midg integer time (fixme, assumes all times in a track
// are from the same day, so we don't handle midnight roll over)
class MIDGTime {
public:
uint32_t msec;
double seconds;
inline MIDGTime( const int dd, const int hh, const int mm,
const double ss )
{
seconds = dd*86400.0 + hh*3600.0 + mm*60.0 + ss;
msec = (uint32_t)(seconds * 1000);
}
inline MIDGTime( const uint32_t midgtime_msec ) {
msec = midgtime_msec;
seconds = (double)midgtime_msec / 1000.0;
// std::cout << midgtime << " = " << seconds << std::endl;
}
inline ~MIDGTime() {}
inline double get_seconds() const { return seconds; }
inline uint32_t get_msec() const { return msec; }
inline double diff_seconds( const MIDGTime t ) const {
return seconds - t.seconds;
}
};
// base class for MIDG data types
class MIDGpoint {
public:
MIDGTime midg_time;
MIDGpoint() :
midg_time(MIDGTime(0))
{ }
inline double get_seconds() const { return midg_time.get_seconds(); }
inline uint32_t get_msec() const { return midg_time.get_msec(); }
};
// encapsulate the interesting midg data for a moment in time
class MIDGpos : public MIDGpoint {
public:
double lat_deg;
double lon_deg;
double altitude_msl;
int fix_quality;
int num_satellites;
double hdop;
double speed_kts;
double course_true;
MIDGpos() :
lat_deg(0.0),
lon_deg(0.0),
altitude_msl(0.0),
fix_quality(0),
num_satellites(0),
hdop(0.0),
speed_kts(0.0),
course_true(0.0)
{ }
};
// encapsulate the interesting midg data for a moment in time
class MIDGatt : public MIDGpoint {
public:
double yaw_rad;
double pitch_rad;
double roll_rad;
MIDGatt() :
yaw_rad(0.0),
pitch_rad(0.0),
roll_rad(0.0)
{ }
};
// Manage a saved midg log (track file)
class MIDGTrack {
private:
std::vector <MIDGpos> pos_data;
std::vector <MIDGatt> att_data;
// parse message and put current data into vector if message has a
// newer time stamp than existing data.
void parse_msg( const int id, char *buf, MIDGpos *pos, MIDGatt *att );
public:
MIDGTrack();
~MIDGTrack();
// read/parse the next message from the specified data stream,
// returns id # if a valid message found.
int next_message( SGIOChannel *ch, SGIOChannel *log,
MIDGpos *pos, MIDGatt *att );
int next_message( SGSerialPort *serial, SGIOChannel *log,
MIDGpos *pos, MIDGatt *att );
// load the named file into internal buffers
bool load( const std::string &file );
inline int pos_size() const { return pos_data.size(); }
inline int att_size() const { return att_data.size(); }
inline MIDGpos get_pospt( const unsigned int i )
{
if ( i < pos_data.size() ) {
return pos_data[i];
} else {
return MIDGpos();
}
}
inline MIDGatt get_attpt( const unsigned int i )
{
if ( i < att_data.size() ) {
return att_data[i];
} else {
return MIDGatt();
}
}
};
MIDGpos MIDGInterpPos( const MIDGpos A, const MIDGpos B, const double percent );
MIDGatt MIDGInterpAtt( const MIDGatt A, const MIDGatt B, const double percent );

View File

@@ -0,0 +1,628 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef HAVE_WINDOWS_H
# include <windows.h>
#else
# include <netinet/in.h> // htonl() ntohl()
#endif
#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>
#include <simgear/io/sg_serial.hxx>
#include <simgear/io/raw_socket.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/timing/timestamp.hxx>
#include <Network/net_ctrls.hxx>
#include <Network/net_fdm.hxx>
#include "MIDG-II.hxx"
using std::cout;
using std::endl;
using std::string;
// Network channels
static simgear::Socket fdm_sock, ctrls_sock;
// midg data
MIDGTrack track;
// Default ports
static int fdm_port = 5505;
static int ctrls_port = 5506;
// Default path
static string infile = "";
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
// double last_lat = 0.0, last_lon = 0.0;
// double kts_filter = 0.0;
bool inited = false;
// 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
static void htond (double &x)
{
if ( sgIsLittleEndian() ) {
int *Double_Overlay;
int Holding_Buffer;
Double_Overlay = (int *) &x;
Holding_Buffer = Double_Overlay [0];
Double_Overlay [0] = htonl (Double_Overlay [1]);
Double_Overlay [1] = htonl (Holding_Buffer);
} else {
return;
}
}
// Float version
static void htonf (float &x)
{
if ( sgIsLittleEndian() ) {
int *Float_Overlay;
int Holding_Buffer;
Float_Overlay = (int *) &x;
Holding_Buffer = Float_Overlay [0];
Float_Overlay [0] = htonl (Holding_Buffer);
} else {
return;
}
}
static void midg2fg( const MIDGpos pos, const MIDGatt att,
FGNetFDM *fdm, FGNetCtrls *ctrls )
{
unsigned int i;
// Version sanity checking
fdm->version = FG_NET_FDM_VERSION;
// Aero parameters
fdm->longitude = pos.lon_deg * SGD_DEGREES_TO_RADIANS;
fdm->latitude = pos.lat_deg * SGD_DEGREES_TO_RADIANS;
fdm->altitude = pos.altitude_msl + alt_offset;
fdm->agl = -9999.0;
fdm->psi = att.yaw_rad; // heading
fdm->phi = att.roll_rad; // roll
fdm->theta = att.pitch_rad; // pitch;
fdm->phidot = 0.0;
fdm->thetadot = 0.0;
fdm->psidot = 0.0;
// estimate speed
// double az1, az2, dist;
// geo_inverse_wgs_84( pos.altitude_msl, last_lat, last_lon,
// pos.lat_deg, pos.lon_deg, &az1, &az2, &dist );
// double v_ms = dist / (frame_us / 1000000);
// double v_kts = v_ms * SG_METER_TO_NM * 3600;
// kts_filter = (0.99 * kts_filter) + (0.01 * v_kts);
fdm->vcas = pos.speed_kts;
// last_lat = pos.lat_deg;
// last_lon = pos.lon_deg;
// cout << "kts_filter = " << kts_filter << " vel = " << pos.speed_kts << endl;
fdm->climb_rate = 0; // fps
// cout << "climb rate = " << aero->hdota << endl;
fdm->v_north = 0.0;
fdm->v_east = 0.0;
fdm->v_down = 0.0;
fdm->v_body_u = 0.0;
fdm->v_body_v = 0.0;
fdm->v_body_w = 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;
double rpm = ((pos.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0;
if ( rpm < 0.0 ) { rpm = 0.0; }
if ( rpm > 3000.0 ) { rpm = 3000.0; }
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;
fdm->elevator = -fdm->theta * 1.0;
fdm->elevator_trim_tab = 0.0;
fdm->left_flap = 0.0;
fdm->right_flap = 0.0;
fdm->left_aileron = fdm->phi * 1.0;
fdm->right_aileron = -fdm->phi * 1.0;
fdm->rudder = 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_body_u);
htonf(fdm->v_body_v);
htonf(fdm->v_body_w);
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);
}
static void send_data( const MIDGpos pos, const MIDGatt att ) {
int fdmsize = sizeof( FGNetFDM );
FGNetFDM fgfdm;
FGNetCtrls fgctrls;
midg2fg( pos, att, &fgfdm, &fgctrls );
fdm_sock.send(&fgfdm, fdmsize, 0);
}
void usage( const string &argv0 ) {
cout << "Usage: " << argv0 << endl;
cout << "\t[ --help ]" << endl;
cout << "\t[ --infile <infile_name>" << endl;
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;
cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
cout << "\t[ --altitude-offset <meters> ]" << endl;
cout << "\t[ --skip-seconds <seconds> ]" << endl;
}
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 );
}
} 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;
} 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 );
}
} 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 );
}
} else {
usage( argv[0] );
exit( -1 );
}
}
// Setup up outgoing network connections
simgear::Socket::initSockets(); // We must call this before any other net stuff
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;
fdm_sock.setBlocking( false );
ctrls_sock.setBlocking( false );
cout << "blocking false" << endl;
if ( do_broadcast ) {
fdm_sock.setBroadcast( true );
ctrls_sock.setBroadcast( true );
}
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;
if ( infile.length() ) {
// Load data from a track data
track.load( infile );
cout << "Loaded " << track.pos_size() << " position records." << endl;
cout << "Loaded " << track.att_size() << " attitude records." << endl;
int size = track.pos_size();
double current_time = track.get_pospt(0).get_seconds();
cout << "Track begin time is " << current_time << endl;
double end_time = track.get_pospt(size-1).get_seconds();
cout << "Track end time is " << end_time << endl;
cout << "Duration = " << end_time - current_time << endl;
// 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 pos_count = 0;
int att_count = 0;
MIDGpos pos0, pos1;
pos0 = pos1 = track.get_pospt( 0 );
MIDGatt att0, att1;
att0 = att1 = track.get_attpt( 0 );
while ( current_time < end_time ) {
// cout << "current_time = " << current_time << " end_time = "
// << end_time << endl;
// Advance position pointer
while ( current_time > pos1.get_seconds()
&& pos_count < track.pos_size() )
{
pos0 = pos1;
++pos_count;
// cout << "count = " << count << endl;
pos1 = track.get_pospt( pos_count );
}
// cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
// << endl;
// Advance attitude pointer
while ( current_time > att1.get_seconds()
&& att_count < track.att_size() )
{
att0 = att1;
++att_count;
// cout << "count = " << count << endl;
att1 = track.get_attpt( att_count );
}
// cout << "pos0 = " << pos0.get_seconds()
// << " pos1 = " << pos1.get_seconds() << endl;
double pos_percent;
if ( fabs(pos1.get_seconds() - pos0.get_seconds()) < 0.00001 ) {
pos_percent = 0.0;
} else {
pos_percent =
(current_time - pos0.get_seconds()) /
(pos1.get_seconds() - pos0.get_seconds());
}
// cout << "Percent = " << percent << endl;
double att_percent;
if ( fabs(att1.get_seconds() - att0.get_seconds()) < 0.00001 ) {
att_percent = 0.0;
} else {
att_percent =
(current_time - att0.get_seconds()) /
(att1.get_seconds() - att0.get_seconds());
}
// cout << "Percent = " << percent << endl;
MIDGpos pos = MIDGInterpPos( pos0, pos1, pos_percent );
MIDGatt att = MIDGInterpAtt( att0, att1, att_percent );
// 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 ( pos.lat_deg > -500 ) {
printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
current_time,
pos.lat_deg, pos.lon_deg, pos.altitude_msl,
att.yaw_rad * 180.0 / SG_PI,
att.pitch_rad * 180.0 / SG_PI,
att.roll_rad * 180.0 / SG_PI );
}
send_data( pos, att );
// Update the elapsed time.
static bool first_time = true;
if ( first_time ) {
last_time_stamp.stamp();
first_time = false;
}
current_time_stamp.stamp();
/* Convert to ms */
double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs();
if ( elapsed_us < (frame_us - 2000) ) {
double requested_us = (frame_us - elapsed_us) - 2000 ;
ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
}
current_time_stamp.stamp();
while ( (current_time_stamp - last_time_stamp).toUSecs() < frame_us ) {
current_time_stamp.stamp();
}
current_time += (frame_us / 1000000.0);
last_time_stamp = current_time_stamp;
}
cout << "Processed " << pos_count << " entries in "
<< current_time_stamp - start_time << " seconds."
<< endl;
} else if ( serialdev.length() ) {
// process incoming data from the serial port
int count = 0;
MIDGpos pos;
MIDGatt att;
uint32_t pos_time = 1;
uint32_t att_time = 1;
// open the serial port device
SGSerialPort input( serialdev, 115200 );
if ( !input.is_enabled() ) {
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;
}
SGFile output( outfile );
if ( !output.open( SG_IO_OUT ) ) {
cout << "Cannot open: " << outfile << endl;
return false;
}
while ( input.is_enabled() ) {
// cout << "looking for next message ..." << endl;
int id = track.next_message( &input, &output, &pos, &att );
cout << "message id = " << id << endl;
count++;
if ( id == 10 ) {
if ( att.get_msec() > att_time ) {
att_time = att.get_msec();
//current_time = att_time;
} else {
cout << "oops att back in time" << endl;
}
} else if ( id == 12 ) {
if ( pos.get_msec() > pos_time ) {
pos_time = pos.get_msec();
//current_time = pos_time;
} else {
cout << "oops pos back in time" << endl;
}
}
if ( pos.lat_deg > -500 ) {
// printf( "%.3f %.4f %.4f %.1f %.2f %.2f %.2f\n",
// current_time,
// pos.lat_deg, pos.lon_deg, pos.altitude_msl,
// att.yaw_rad * 180.0 / SG_PI,
// att.pitch_rad * 180.0 / SG_PI,
// att.roll_rad * 180.0 / SG_PI );
}
send_data( pos, att );
}
}
return 0;
}

File diff suppressed because it is too large Load Diff

609
utils/GPSsmooth/UGear.cxx Normal file
View File

@@ -0,0 +1,609 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <iostream>
#include <cstdio>
#include <simgear/constants.h>
#include <simgear/io/sg_file.hxx>
#include <simgear/math/sg_geodesy.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/io/iostreams/sgstream.hxx>
#include <simgear/misc/strutils.hxx>
#include <simgear/misc/stdint.hxx>
#include "UGear.hxx"
using std::cout;
using std::endl;
#define START_OF_MSG0 147
#define START_OF_MSG1 224
UGTrack::UGTrack():
sg_swap(false)
{
};
UGTrack::~UGTrack() {};
// swap the 1st 4 bytes with the last 4 bytes of a stargate double so
// it matches the PC representation
static double sg_swap_double( uint8_t *buf, size_t offset ) {
double *result;
uint8_t tmpbuf[10];
for ( size_t i = 0; i < 4; ++i ) {
tmpbuf[i] = buf[offset + i + 4];
}
for ( size_t i = 0; i < 4; ++i ) {
tmpbuf[i + 4] = buf[offset + i];
}
// for ( size_t i = 0; i < 8; ++i ) {
// printf("%d ", tmpbuf[i]);
// }
// printf("\n");
result = (double *)tmpbuf;
return *result;
}
static bool validate_cksum( uint8_t id, uint8_t size, char *buf,
uint8_t cksum0, uint8_t cksum1,
bool ignore_checksum )
{
if ( ignore_checksum ) {
return true;
}
uint8_t c0 = 0;
uint8_t c1 = 0;
c0 += id;
c1 += c0;
// cout << "c0 = " << (unsigned int)c0 << " c1 = " << (unsigned int)c1 << endl;
c0 += size;
c1 += c0;
// cout << "c0 = " << (unsigned int)c0 << " c1 = " << (unsigned int)c1 << endl;
for ( uint8_t i = 0; i < size; i++ ) {
c0 += (uint8_t)buf[i];
c1 += c0;
// cout << "c0 = " << (unsigned int)c0 << " c1 = " << (unsigned int)c1
// << " [" << (unsigned int)(uint8_t)buf[i] << "]" << endl;
}
// cout << "c0 = " << (unsigned int)c0 << " (" << (unsigned int)cksum0
// << ") c1 = " << (unsigned int)c1 << " (" << (unsigned int)cksum1
// << ")" << endl;
if ( c0 == cksum0 && c1 == cksum1 ) {
return true;
} else {
return false;
}
}
void UGTrack::parse_msg( const int id, char *buf,
struct gps *gpspacket, imu *imupacket,
nav *navpacket, servo *servopacket,
health *healthpacket )
{
if ( id == GPS_PACKET ) {
*gpspacket = *(struct gps *)buf;
if ( sg_swap ) {
gpspacket->time = sg_swap_double( (uint8_t *)buf, 0 );
gpspacket->lat = sg_swap_double( (uint8_t *)buf, 8 );
gpspacket->lon = sg_swap_double( (uint8_t *)buf, 16 );
gpspacket->alt = sg_swap_double( (uint8_t *)buf, 24 );
gpspacket->vn = sg_swap_double( (uint8_t *)buf, 32 );
gpspacket->ve = sg_swap_double( (uint8_t *)buf, 40 );
gpspacket->vd = sg_swap_double( (uint8_t *)buf, 48 );
gpspacket->ITOW = sg_swap_double( (uint8_t *)buf, 56 );
}
} else if ( id == IMU_PACKET ) {
*imupacket = *(struct imu *)buf;
if ( sg_swap ) {
imupacket->time = sg_swap_double( (uint8_t *)buf, 0 );
imupacket->p = sg_swap_double( (uint8_t *)buf, 8 );
imupacket->q = sg_swap_double( (uint8_t *)buf, 16 );
imupacket->r = sg_swap_double( (uint8_t *)buf, 24 );
imupacket->ax = sg_swap_double( (uint8_t *)buf, 32 );
imupacket->ay = sg_swap_double( (uint8_t *)buf, 40 );
imupacket->az = sg_swap_double( (uint8_t *)buf, 48 );
imupacket->hx = sg_swap_double( (uint8_t *)buf, 56 );
imupacket->hy = sg_swap_double( (uint8_t *)buf, 64 );
imupacket->hz = sg_swap_double( (uint8_t *)buf, 72 );
imupacket->Ps = sg_swap_double( (uint8_t *)buf, 80 );
imupacket->Pt = sg_swap_double( (uint8_t *)buf, 88 );
imupacket->phi = sg_swap_double( (uint8_t *)buf, 96 );
imupacket->the = sg_swap_double( (uint8_t *)buf, 104 );
imupacket->psi = sg_swap_double( (uint8_t *)buf, 112 );
}
// printf("imu.time = %.4f size = %d\n", imupacket->time, sizeof(struct imu));
} else if ( id == NAV_PACKET ) {
*navpacket = *(struct nav *)buf;
if ( sg_swap ) {
navpacket->time = sg_swap_double( (uint8_t *)buf, 0 );
navpacket->lat = sg_swap_double( (uint8_t *)buf, 8 );
navpacket->lon = sg_swap_double( (uint8_t *)buf, 16 );
navpacket->alt = sg_swap_double( (uint8_t *)buf, 24 );
navpacket->vn = sg_swap_double( (uint8_t *)buf, 32 );
navpacket->ve = sg_swap_double( (uint8_t *)buf, 40 );
navpacket->vd = sg_swap_double( (uint8_t *)buf, 48 );
}
} else if ( id == SERVO_PACKET ) {
*servopacket = *(struct servo *)buf;
if ( sg_swap ) {
servopacket->time = sg_swap_double( (uint8_t *)buf, 0 );
}
// printf("servo time = %.3f %d %d\n", servopacket->time, servopacket->chn[0], servopacket->chn[1]);
} else if ( id == HEALTH_PACKET ) {
*healthpacket = *(struct health *)buf;
if ( sg_swap ) {
healthpacket->time = sg_swap_double( (uint8_t *)buf, 0 );
}
} else {
cout << "unknown id = " << id << endl;
}
}
// load the named stream log file into internal buffers
bool UGTrack::load_stream( const string &file, bool ignore_checksum ) {
int count = 0;
gps gpspacket;
imu imupacket;
nav navpacket;
servo servopacket;
health healthpacket;
double gps_time = 0;
double imu_time = 0;
double nav_time = 0;
double servo_time = 0;
double health_time = 0;
gps_data.clear();
imu_data.clear();
nav_data.clear();
servo_data.clear();
health_data.clear();
// open the file
SGFile input( file );
if ( !input.open( SG_IO_IN ) ) {
cout << "Cannot open file: " << file << endl;
return false;
}
while ( ! input.eof() ) {
// cout << "looking for next message ..." << endl;
int id = next_message( &input, NULL, &gpspacket, &imupacket,
&navpacket, &servopacket, &healthpacket,
ignore_checksum );
count++;
if ( id == GPS_PACKET ) {
if ( gpspacket.time > gps_time ) {
gps_data.push_back( gpspacket );
gps_time = gpspacket.time;
} else {
cout << "oops gps back in time: " << gpspacket.time << " " << gps_time << endl;
}
} else if ( id == IMU_PACKET ) {
if ( imupacket.time > imu_time ) {
imu_data.push_back( imupacket );
imu_time = imupacket.time;
} else {
cout << "oops imu back in time" << endl;
}
} else if ( id == NAV_PACKET ) {
if ( navpacket.time > nav_time ) {
nav_data.push_back( navpacket );
nav_time = navpacket.time;
} else {
cout << "oops nav back in time" << endl;
}
} else if ( id == SERVO_PACKET ) {
if ( servopacket.time > servo_time ) {
servo_data.push_back( servopacket );
servo_time = servopacket.time;
} else {
cout << "oops servo back in time" << endl;
}
} else if ( id == HEALTH_PACKET ) {
if ( healthpacket.time > health_time ) {
health_data.push_back( healthpacket );
health_time = healthpacket.time;
} else {
cout << "oops health back in time" << endl;
}
}
}
cout << "processed " << count << " messages" << endl;
return true;
}
// load the named stream log file into internal buffers
bool UGTrack::load_flight( const string &path ) {
gps gpspacket;
imu imupacket;
nav navpacket;
servo servopacket;
health healthpacket;
gps_data.clear();
imu_data.clear();
nav_data.clear();
servo_data.clear();
health_data.clear();
gzFile fgps = NULL;
gzFile fimu = NULL;
gzFile fnav = NULL;
gzFile fservo = NULL;
gzFile fhealth = NULL;
SGPath file;
int size;
// open the gps file
file = path; file.append( "gps.dat.gz" );
std::string fdata = file.local8BitStr();
if ( (fgps = gzopen( fdata.c_str(), "r" )) == NULL ) {
printf("Cannot open %s\n", fdata.c_str());
return false;
}
size = sizeof( struct gps );
printf("gps size = %d\n", size);
while ( gzread( fgps, &gpspacket, size ) == size ) {
gps_data.push_back( gpspacket );
}
// open the imu file
file = path; file.append( "imu.dat.gz" );
fdata = file.local8BitStr();
if ( (fimu = gzopen( fdata.c_str(), "r" )) == NULL ) {
printf("Cannot open %s\n", fdata.c_str());
return false;
}
size = sizeof( struct imu );
printf("imu size = %d\n", size);
while ( gzread( fimu, &imupacket, size ) == size ) {
imu_data.push_back( imupacket );
}
// open the nav file
file = path; file.append( "nav.dat.gz" );
fdata = file.local8BitStr();
if ( (fnav = gzopen( fdata.c_str(), "r" )) == NULL ) {
printf("Cannot open %s\n", fdata.c_str());
return false;
}
size = sizeof( struct nav );
printf("nav size = %d\n", size);
while ( gzread( fnav, &navpacket, size ) == size ) {
// printf("%.4f %.4f\n", navpacket.lat, navpacket.lon);
nav_data.push_back( navpacket );
}
// open the servo file
file = path; file.append( "servo.dat.gz" );
fdata = file.local8BitStr();
if ( (fservo = gzopen( fdata.c_str(), "r" )) == NULL ) {
printf("Cannot open %s\n", fdata.c_str());
return false;
}
size = sizeof( struct servo );
printf("servo size = %d\n", size);
while ( gzread( fservo, &servopacket, size ) == size ) {
servo_data.push_back( servopacket );
}
// open the health file
file = path; file.append( "health.dat.gz" );
fdata = file.local8BitStr();
if ( (fhealth = gzopen( fdata.c_str(), "r" )) == NULL ) {
printf("Cannot open %s\n", fdata.c_str());
return false;
}
size = sizeof( struct health );
printf("health size = %d\n", size);
while ( gzread( fhealth, &healthpacket, size ) == size ) {
health_data.push_back( healthpacket );
}
return true;
}
// attempt to work around some system dependent issues. Our read can
// return < data than we want.
int myread( SGIOChannel *ch, SGIOChannel *log, char *buf, int length ) {
bool myeof = false;
int result = 0;
if ( !myeof ) {
result = ch->read( buf, length );
// cout << "wanted " << length << " read " << result << " bytes" << endl;
if ( ch->get_type() == sgFileType ) {
myeof = ((SGFile *)ch)->eof();
}
}
if ( result > 0 && log != NULL ) {
log->write( buf, result );
}
return result;
}
// attempt to work around some system dependent issues. Our read can
// return < data than we want.
int serial_read( SGSerialPort *serial, SGIOChannel *log,
char *buf, int length )
{
int result = 0;
int bytes_read = 0;
char *tmp = buf;
while ( bytes_read < length ) {
result = serial->read_port( tmp, length - bytes_read );
bytes_read += result;
tmp += result;
// cout << " read " << bytes_read << " of " << length << endl;
}
if ( bytes_read > 0 && log != NULL ) {
log->write( buf, bytes_read );
}
return bytes_read;
}
// load the next message of a real time data stream
int UGTrack::next_message( SGIOChannel *ch, SGIOChannel *log,
gps *gpspacket, imu *imupacket, nav *navpacket,
servo *servopacket, health *healthpacket,
bool ignore_checksum )
{
char tmpbuf[256];
char savebuf[256];
// cout << "in next_message()" << endl;
bool myeof = false;
// scan for sync characters
uint8_t sync0, sync1;
myread( ch, log, tmpbuf, 2 );
sync0 = (unsigned char)tmpbuf[0];
sync1 = (unsigned char)tmpbuf[1];
while ( (sync0 != START_OF_MSG0 || sync1 != START_OF_MSG1) && !myeof ) {
sync0 = sync1;
myread( ch, log, tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
cout << "scanning for start of message "
<< (unsigned int)sync0 << " " << (unsigned int)sync1
<< ", eof = " << ch->eof() << endl;
if ( ch->get_type() == sgFileType ) {
myeof = ((SGFile *)ch)->eof();
}
}
cout << "found start of message ..." << endl;
// read message id and size
myread( ch, log, tmpbuf, 2 );
uint8_t id = (unsigned char)tmpbuf[0];
uint8_t size = (unsigned char)tmpbuf[1];
// cout << "message = " << (int)id << " size = " << (int)size << endl;
// load message
if ( ch->get_type() == sgFileType ) {
int count = myread( ch, log, savebuf, size );
if ( count != size ) {
cout << "ERROR: didn't read enough bytes!" << endl;
}
} else {
#ifdef READ_ONE_BY_ONE
for ( int i = 0; i < size; ++i ) {
myread( ch, log, tmpbuf, 1 ); savebuf[i] = tmpbuf[0];
}
#else
myread( ch, log, savebuf, size );
#endif
}
// read checksum
myread( ch, log, tmpbuf, 2 );
uint8_t cksum0 = (unsigned char)tmpbuf[0];
uint8_t cksum1 = (unsigned char)tmpbuf[1];
if ( validate_cksum( id, size, savebuf, cksum0, cksum1, ignore_checksum ) )
{
parse_msg( id, savebuf, gpspacket, imupacket, navpacket, servopacket,
healthpacket );
return id;
}
cout << "Check sum failure!" << endl;
return -1;
}
// load the next message of a real time data stream
int UGTrack::next_message( SGSerialPort *serial, SGIOChannel *log,
gps *gpspacket, imu *imupacket, nav *navpacket,
servo *servopacket, health *healthpacket,
bool ignore_checksum )
{
char tmpbuf[256];
char savebuf[256];
// cout << "in next_message()" << endl;
bool myeof = false;
// scan for sync characters
int scan_count = 0;
uint8_t sync0, sync1;
serial_read( serial, log, tmpbuf, 2 );
sync0 = (unsigned char)tmpbuf[0];
sync1 = (unsigned char)tmpbuf[1];
while ( (sync0 != START_OF_MSG0 || sync1 != START_OF_MSG1) && !myeof ) {
scan_count++;
sync0 = sync1;
serial_read( serial, log, tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
// cout << "scanning for start of message "
// << (unsigned int)sync0 << " " << (unsigned int)sync1
// << endl;
}
if ( scan_count > 0 ) {
cout << "found start of message after discarding " << scan_count
<< " bytes" << endl;
}
// cout << "found start of message ..." << endl;
// read message id and size
serial_read( serial, log, tmpbuf, 2 );
uint8_t id = (unsigned char)tmpbuf[0];
uint8_t size = (unsigned char)tmpbuf[1];
// cout << "message = " << (int)id << " size = " << (int)size << endl;
// load message
serial_read( serial, log, savebuf, size );
// read checksum
serial_read( serial, log, tmpbuf, 2 );
uint8_t cksum0 = (unsigned char)tmpbuf[0];
uint8_t cksum1 = (unsigned char)tmpbuf[1];
// cout << "cksum0 = " << (int)cksum0 << " cksum1 = " << (int)cksum1
// << endl;
if ( validate_cksum( id, size, savebuf, cksum0, cksum1, ignore_checksum ) )
{
parse_msg( id, savebuf, gpspacket, imupacket, navpacket, servopacket,
healthpacket );
return id;
}
cout << "Check sum failure!" << endl;
return -1;
}
static double interp( double a, double b, double p, bool rotational = false ) {
double diff = b - a;
if ( rotational ) {
// special handling of rotational data
if ( diff > SGD_PI ) {
diff -= SGD_2PI;
} else if ( diff < -SGD_PI ) {
diff += SGD_2PI;
}
}
return a + diff * p;
}
gps UGEARInterpGPS( const gps A, const gps B, const double percent )
{
gps p;
p.time = interp(A.time, B.time, percent);
p.lat = interp(A.lat, B.lat, percent);
p.lon = interp(A.lon, B.lon, percent);
p.alt = interp(A.alt, B.alt, percent);
p.ve = interp(A.ve, B.ve, percent);
p.vn = interp(A.vn, B.vn, percent);
p.vd = interp(A.vd, B.vd, percent);
p.ITOW = (int)interp(A.ITOW, B.ITOW, percent);
p.err_type = A.err_type;
return p;
}
imu UGEARInterpIMU( const imu A, const imu B, const double percent )
{
imu p;
p.time = interp(A.time, B.time, percent);
p.p = interp(A.p, B.p, percent);
p.q = interp(A.q, B.q, percent);
p.r = interp(A.r, B.r, percent);
p.ax = interp(A.ax, B.ax, percent);
p.ay = interp(A.ay, B.ay, percent);
p.az = interp(A.az, B.az, percent);
p.hx = interp(A.hx, B.hx, percent);
p.hy = interp(A.hy, B.hy, percent);
p.hz = interp(A.hz, B.hz, percent);
p.Ps = interp(A.Ps, B.Ps, percent);
p.Pt = interp(A.Pt, B.Pt, percent);
p.phi = interp(A.phi, B.phi, percent, true);
p.the = interp(A.the, B.the, percent, true);
p.psi = interp(A.psi, B.psi, percent, true);
p.err_type = A.err_type;
return p;
}
nav UGEARInterpNAV( const nav A, const nav B, const double percent )
{
nav p;
p.time = interp(A.time, B.time, percent);
p.lat = interp(A.lat, B.lat, percent);
p.lon = interp(A.lon, B.lon, percent);
p.alt = interp(A.alt, B.alt, percent);
p.ve = interp(A.ve, B.ve, percent);
p.vn = interp(A.vn, B.vn, percent);
p.vd = interp(A.vd, B.vd, percent);
p.err_type = A.err_type;
return p;
}
servo UGEARInterpSERVO( const servo A, const servo B, const double percent )
{
servo p;
for ( int i = 0; i < 8; ++i ) {
p.chn[i] = (uint16_t)interp(A.chn[i], B.chn[i], percent);
}
p.status = A.status;
return p;
}
health UGEARInterpHEALTH( const health A, const health B, const double percent )
{
health p;
p.command_sequence = B.command_sequence;
p.time = interp(A.time, B.time, percent);
return p;
}

173
utils/GPSsmooth/UGear.hxx Normal file
View File

@@ -0,0 +1,173 @@
#pragma once
#include <simgear/compiler.h>
#include <iostream>
#include <string>
#include <vector>
#include <simgear/misc/stdint.hxx>
#include <simgear/io/iochannel.hxx>
#include <simgear/serial/serial.hxx>
enum ugPacketType {
GPS_PACKET = 0,
IMU_PACKET = 1,
NAV_PACKET = 2,
SERVO_PACKET = 3,
HEALTH_PACKET = 4
};
struct imu {
double time;
double p,q,r; /* angular velocities */
double ax,ay,az; /* acceleration */
double hx,hy,hz; /* magnetic field */
double Ps,Pt; /* static/pitot pressure */
// double Tx,Ty,Tz; /* temperature */
double phi,the,psi; /* attitudes */
uint64_t err_type; /* error type */
};
struct gps {
double time;
double lat,lon,alt; /* gps position */
double ve,vn,vd; /* gps velocity */
double ITOW;
uint64_t err_type; /* error type */
};
struct nav {
double time;
double lat,lon,alt;
double ve,vn,vd;
// float t;
uint64_t err_type;
};
struct servo {
double time;
uint16_t chn[8];
uint64_t status;
};
struct health {
double time;
double target_roll_deg; /* AP target roll angle */
double target_heading_deg; /* AP target heading angle */
double target_pitch_deg; /* AP target pitch angle */
double target_climb_fps; /* AP target climb rate */
double target_altitude_ft; /* AP target altitude */
uint64_t command_sequence; /* highest received command sequence num */
uint64_t target_waypoint; /* index of current waypoint target */
uint64_t loadavg; /* system "1 minute" load average */
uint64_t ahrs_hz; /* actual ahrs loop hz */
uint64_t nav_hz; /* actual nav loop hz */
};
// Manage a saved ugear log (track file)
class UGTrack {
private:
std::vector <gps> gps_data;
std::vector <imu> imu_data;
std::vector <nav> nav_data;
std::vector <servo> servo_data;
std::vector <health> health_data;
// parse message and put current data into vector if message has a
// newer time stamp than existing data.
void parse_msg( const int id, char *buf,
gps *gpspacket, imu *imupacket, nav *navpacket,
servo *servopacket, health *healthpacket );
// activate special double swap logic for non-standard stargate
// double format
bool sg_swap;
public:
UGTrack();
~UGTrack();
// read/parse the next message from the specified data stream,
// returns id # if a valid message found.
int next_message( SGIOChannel *ch, SGIOChannel *log,
gps *gpspacket, imu *imupacket, nav *navpacket,
servo *servopacket, health * healthpacket,
bool ignore_checksum );
int next_message( SGSerialPort *serial, SGIOChannel *log,
gps *gpspacket, imu *imupacket, nav *navpacket,
servo *servopacket, health *healthpacket,
bool ignore_checksum );
// load the named stream log file into internal buffers
bool load_stream( const std::string &file, bool ignore_checksum );
// load the named flight files into internal buffers
bool load_flight( const std::string &path );
inline int gps_size() const { return gps_data.size(); }
inline int imu_size() const { return imu_data.size(); }
inline int nav_size() const { return nav_data.size(); }
inline int servo_size() const { return servo_data.size(); }
inline int health_size() const { return health_data.size(); }
inline gps get_gpspt( const unsigned int i )
{
if ( i < gps_data.size() ) {
return gps_data[i];
} else {
return gps();
}
}
inline imu get_imupt( const unsigned int i )
{
if ( i < imu_data.size() ) {
return imu_data[i];
} else {
return imu();
}
}
inline nav get_navpt( const unsigned int i )
{
if ( i < nav_data.size() ) {
return nav_data[i];
} else {
return nav();
}
}
inline servo get_servopt( const unsigned int i )
{
if ( i < servo_data.size() ) {
return servo_data[i];
} else {
return servo();
}
}
inline health get_healthpt( const unsigned int i )
{
if ( i < health_data.size() ) {
return health_data[i];
} else {
return health();
}
}
// set stargate mode where we have to do an odd swapping of doubles to
// account for their non-standard formate
inline void set_stargate_swap_mode() {
sg_swap = true;
}
};
gps UGEARInterpGPS( const gps A, const gps B, const double percent );
imu UGEARInterpIMU( const imu A, const imu B, const double percent );
nav UGEARInterpNAV( const nav A, const nav B, const double percent );
servo UGEARInterpSERVO( const servo A, const servo B, const double percent );
health UGEARInterpHEALTH( const health A, const health B,
const double percent );

View File

@@ -0,0 +1,106 @@
#include <cstring>
#include <cstdio>
#include "UGear_command.hxx"
UGCommand::UGCommand():
cmd_send_index(0),
cmd_recv_index(0),
prime_state(true)
{}
UGCommand::~UGCommand() {}
// calculate the nmea check sum
static char calc_nmea_cksum(const char *sentence) {
unsigned char sum = 0;
int i, len;
// cout << sentence << endl;
len = std::strlen(sentence);
sum = sentence[0];
for ( i = 1; i < len; i++ ) {
// cout << sentence[i];
sum ^= sentence[i];
}
// cout << endl;
// printf("sum = %02x\n", sum);
return sum;
}
// package and send the serial command
static int serial_send( SGSerialPort *serial, int sequence,
const string command )
{
char sequence_str[10];
snprintf( sequence_str, 9, "%d", sequence );
string package = sequence_str;
package += ",";
package += command;
char pkg_sum[10];
snprintf( pkg_sum, 3, "%02X", calc_nmea_cksum(package.c_str()) );
package += "*";
package += pkg_sum;
package += "\n";
unsigned int result = serial->write_port( package.c_str(),
package.length() );
if ( result != package.length() ) {
printf("ERROR: wrote %u of %u bytes to serial port!\n",
result, (unsigned)package.length() );
return 0;
}
return 1;
}
// send current command until acknowledged
int UGCommand::update( SGSerialPort *serial )
{
// if current command has been received, advance to next command
printf("sent = %d recv = %d\n", cmd_send_index, cmd_recv_index);
if ( cmd_recv_index >= cmd_send_index ) {
if ( ! cmd_queue.empty() ) {
if ( ! prime_state ) {
cmd_queue.pop();
cmd_send_index++;
} else {
prime_state = false;
}
}
}
// nothing to do if command queue empty
if ( cmd_queue.empty() ) {
prime_state = true;
return 0;
}
// send the command
string command = cmd_queue.front();
/*int result =*/ serial_send( serial, cmd_send_index, command );
return cmd_send_index;
}
void UGCommand::add( const string command )
{
printf("command queue: %s\n", command.c_str());
cmd_queue.push( command );
}
// create the global command channel manager
UGCommand command_mgr;

View File

@@ -0,0 +1,42 @@
#pragma once
#include <simgear/compiler.h>
#include <iostream>
#include <string>
#include <queue>
#include <simgear/misc/stdint.hxx>
#include <simgear/io/iochannel.hxx>
#include <simgear/serial/serial.hxx>
// Manage UGear Command Channel
class UGCommand {
private:
int cmd_send_index;
int cmd_recv_index;
bool prime_state;
std::queue <std::string> cmd_queue;
public:
UGCommand();
~UGCommand();
// send current command until acknowledged
int update( SGSerialPort *serial );
void add( const std::string command );
inline int cmd_queue_size() {
return cmd_queue.size();
}
inline void update_cmd_sequence( int sequence ) {
cmd_recv_index = sequence;
}
};
extern UGCommand command_mgr;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,141 @@
// opengc_data.hxx -- Define structure of OpenGC/FG uint32_terface parameters
//
// Version by J. Wojnaroski for uint32_terface to Open Glass Displays
//
// Modified 02/12/01 - Update engine structure for multi-engine models
// - Added data preamble to id msg types
//
// Modified 01/23/02 - Converted portions of the Engine and Gear accesssors to properties
// - Removed data from navigation functions. OpenGC provides own
//
// This file defines the class/structure of the UDP packet that sends
// the simulation data created by FlightGear to the glass displays. It
// is required to "sync" the data types contained in the packet
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#ifndef _OPENGC_DATA_HXX
#define _OPENGC_DATA_HXX
#ifndef __cplusplus
# error This library requires C++
#endif
const uint32_t OGC_VERSION = 4;
typedef unsigned int uint32_t;
class ogcFGData {
public:
// defines msg types and contents. The msg_content is used as a 'pouint32_ter' to
// a predefined set of msg strings
uint32_t version_id;
uint32_t msg_type;
uint32_t msg_content;
uint32_t reserved;
// position
double latitude;
double longitude;
double elevation;
double magvar;
// flight parameters
double pitch;
double bank;
double heading;
double altitude;
double altitude_agl; // this can also be the radar altimeter
double v_kcas;
double groundspeed;
double vvi;
double mach;
double v_keas; // equivalent airspeed in knots
// Data used by the FMC and autopilots
double phi_dot;
double theta_dot;
double psi_dot;
double alpha;
double alpha_dot;
double beta;
double beta_dot;
// Control surface positions
double left_aileron;
double right_aileron;
double aileron_trim;
double elevator;
double elevator_trim;
double rudder;
double rudder_trim;
double flaps;
double flaps_cmd;
// gear positions 0 = up and 1 = down The 747 has 5 wheel bogey assemblies
double gear_nose;
double gear_left;
double gear_right;
double gear_left_rear;
double gear_right_rear;
double parking_brake;
uint32_t wow_main; // logical and of main gear
uint32_t wow_nose;
// engine data
double rpm[4]; // this is for pistons, jets see below
double n1_turbine[4];
double epr[4];
double egt[4];
double n2_turbine[4];
double fuel_flow[4];
double man_pressure[4];
double oil_pressure[4];
double oil_temp[4];
double oil_quantity[4];
double hyd_pressure[4];
double throttle[4];
double mixture[4];
double prop_advance[4];
// fuel system
uint32_t num_tanks;
double fuel_tank[9];
// Pressures and temperatures
double static_temperature;
double total_temperature;
double static_pressure;
double total_pressure;
double dynamic_pressure;
// more environmental data
double wind;
double wind_dir;
double sea_level_pressure;
};
#endif // _OPENGC_HXX

View File

@@ -0,0 +1,235 @@
// \file props.cxx
// Property server class.
//
// Written by Curtis Olson, started September 2000.
// Modified by Bernie Bright, May 2002.
//
// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id$
#include <simgear/io/sg_netChat.hxx>
#include <simgear/structure/commands.hxx>
#include <simgear/misc/strutils.hxx>
#include <simgear/debug/logstream.hxx>
#include <cstdio>
#include <sstream>
#include "UGear_command.hxx"
#include "UGear_telnet.hxx"
using std::stringstream;
using std::ends;
/**
* Props connection class.
* This class represents a connection to props client.
*/
class PropsChannel : public simgear::NetChat
{
simgear::NetBuffer buffer;
/**
* Current property node name.
*/
string path;
enum Mode {
PROMPT,
DATA
};
Mode mode;
public:
/**
* Constructor.
*/
PropsChannel();
/**
* Append incoming data to our request buffer.
*
* @param s Character string to append to buffer
* @param n Number of characters to append.
*/
void collectIncomingData( const char* s, int n );
/**
* Process a complete request from the props client.
*/
void foundTerminator();
private:
/**
* Return a "Node no found" error message to the client.
*/
void node_not_found_error( const string& node_name );
};
/**
*
*/
PropsChannel::PropsChannel()
: buffer(512),
path("/"),
mode(PROMPT)
{
setTerminator( "\r\n" );
}
/**
*
*/
void
PropsChannel::collectIncomingData( const char* s, int n )
{
buffer.append( s, n );
}
/**
*
*/
void
PropsChannel::node_not_found_error( const string& node_name )
{
string error = "-ERR Node \"";
error += node_name;
error += "\" not found.";
push( error.c_str() );
push( getTerminator() );
}
/**
* We have a command.
*
*/
void
PropsChannel::foundTerminator()
{
const char* cmd = buffer.getData();
SG_LOG( SG_IO, SG_INFO, "processing command = \"" << cmd << "\"" );
std::vector<std::string> tokens = simgear::strutils::split( cmd );
if (!tokens.empty()) {
string command = tokens[0];
if ( command == "send" ) {
command_mgr.add( tokens[1] );
} else if ( command == "quit" ) {
close();
shouldDelete();
return;
} else if ( command == "data" ) {
mode = DATA;
} else if ( command == "prompt" ) {
mode = PROMPT;
} else {
const char* msg = "\
Valid commands are:\r\n\
\r\n\
data switch to raw data mode\r\n\
prompt switch to interactive mode (default)\r\n\
quit terminate connection\r\n\
send <command> send <command> to UAS\r\n";
push( msg );
}
}
if (mode == PROMPT) {
string prompt = "> ";
push( prompt.c_str() );
}
buffer.remove();
}
/**
*
*/
UGTelnet::UGTelnet( const int port_num ):
enabled(false)
{
port = port_num;
}
/**
*
*/
UGTelnet::~UGTelnet()
{
}
/**
*
*/
bool
UGTelnet::open()
{
if (enabled ) {
printf("This shouldn't happen, but the telnet channel is already in use, ignoring\n" );
return false;
}
simgear::NetChannel::open();
simgear::NetChannel::bind( "", port );
simgear::NetChannel::listen( 5 );
printf("Telnet server started on port %d\n", port );
enabled = true;
poller.addChannel(this);
return true;
}
/**
*
*/
bool
UGTelnet::close()
{
SG_LOG( SG_IO, SG_INFO, "closing UGTelnet" );
return true;
}
/**
*
*/
bool
UGTelnet::process()
{
poller.poll();
return true;
}
/**
*
*/
void
UGTelnet::handleAccept()
{
simgear::IPAddress addr;
int handle = simgear::NetChannel::accept( &addr );
printf("Telent server accepted connection from %s:%d\n",
addr.getHost(), addr.getPort() );
PropsChannel* channel = new PropsChannel();
channel->setHandle( handle );
poller.addChannel(channel);
}

View File

@@ -0,0 +1,85 @@
// \file UGear_telnet.hxx
// telnet server class.
//
// Adapted from FlightGear props.hxx/cxx code
// Written by Curtis Olson, started September 2000.
// Modified by Bernie Bright, May 2002.
//
// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id$
#pragma once
#include <string>
#include <vector>
#include <simgear/io/sg_netChannel.hxx>
/**
* Telent server class.
* This class provides a telnet-like server for remote access to
* FlightGear properties.
*/
class UGTelnet: simgear::NetChannel
{
private:
/**
* Server port to listen on.
*/
int port;
bool enabled;
simgear::NetChannelPoller poller;
public:
/**
* Create a new TCP server.
*
* @param tokens Tokenized configuration parameters
*/
UGTelnet( const int port_num );
/**
* Destructor.
*/
~UGTelnet();
/**
* Start the telnet server.
*/
bool open();
/**
* Process network activity.
*/
bool process();
/**
*
*/
bool close();
/**
* Accept a new client connection.
*/
void handleAccept();
};

View File

@@ -0,0 +1,491 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef HAVE_WINDOWS_H
# include <windows.h>
#else
# include <netinet/in.h> // htonl() ntohl()
#endif
#include <iostream>
#include <string>
#include <plib/sg.h>
#include <simgear/io/lowlevel.hxx> // endian tests
#include <simgear/io/raw_socket.hxx>
#include <simgear/timing/timestamp.hxx>
#include <Network/net_ctrls.hxx>
#include <Network/net_fdm.hxx>
#include "GPSsmooth.hxx"
using std::cout;
using std::endl;
using std::string;
// Network channels
static simgear::Socket fdm_sock, ctrls_sock;
// gps data
GPSTrack track;
// Default ports
static int fdm_port = 5505;
static int ctrls_port = 5506;
// Default path
static string file = "";
// Master time counter
float sim_time = 0.0f;
// sim control
SGTimeStamp last_time_stamp;
SGTimeStamp current_time_stamp;
bool inited = false;
// 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
static void htond (double &x)
{
if ( sgIsLittleEndian() ) {
int *Double_Overlay;
int Holding_Buffer;
Double_Overlay = (int *) &x;
Holding_Buffer = Double_Overlay [0];
Double_Overlay [0] = htonl (Double_Overlay [1]);
Double_Overlay [1] = htonl (Holding_Buffer);
} else {
return;
}
}
// Float version
static void htonf (float &x)
{
if ( sgIsLittleEndian() ) {
int *Float_Overlay;
int Holding_Buffer;
Float_Overlay = (int *) &x;
Holding_Buffer = Float_Overlay [0];
Float_Overlay [0] = htonl (Holding_Buffer);
} else {
return;
}
}
static void gps2fg( const GPSPoint p, FGNetFDM *fdm, FGNetCtrls *ctrls )
{
unsigned int i;
static double last_psi;
static double last_alt;
static double phi_filter = 0.0;
static double theta_filter = 0.0;
// Nan-be-gone
if ( phi_filter != phi_filter ) {
phi_filter = 0.0;
}
if ( theta_filter != theta_filter ) {
theta_filter = 0.0;
}
// Version sanity checking
fdm->version = FG_NET_FDM_VERSION;
// Aero parameters
fdm->longitude = p.lon_deg * SGD_DEGREES_TO_RADIANS;
fdm->latitude = p.lat_deg * SGD_DEGREES_TO_RADIANS;
fdm->altitude = p.altitude_msl;
fdm->agl = -9999.0;
fdm->psi = p.course_true; // heading
double diff = p.course_true - last_psi;
if ( diff < -SGD_PI ) { diff += 2.0*SGD_PI; }
if ( diff > SGD_PI ) { diff -= 2.0*SGD_PI; }
double phi = diff * 100.0;
if ( phi > 0.5*SGD_PI ) { phi = 0.5*SGD_PI; }
if ( phi < -0.5*SGD_PI ) { phi = -0.5*SGD_PI; }
phi_filter = 0.99*phi_filter + 0.01*phi;
fdm->phi = phi_filter;
last_psi = p.course_true;
// cout << p.course_true << endl;
diff = p.altitude_msl - last_alt;
if ( diff < -SGD_PI ) { diff += 2.0*SGD_PI; }
if ( diff > SGD_PI ) { diff -= 2.0*SGD_PI; }
double theta = diff * 2.0;
if ( theta > 0.5*SGD_PI ) { theta = 0.5*SGD_PI; }
if ( theta < -0.5*SGD_PI ) { theta = -0.5*SGD_PI; }
theta_filter = 0.99*theta_filter + 0.01*theta;
fdm->theta = theta_filter;
last_alt = p.altitude_msl;
fdm->phidot = 0.0;
fdm->thetadot = 0.0;
fdm->psidot = 0.0;
fdm->vcas = p.speed_kts;
fdm->climb_rate = 0; // fps
// cout << "climb rate = " << aero->hdota << endl;
fdm->v_north = 0.0;
fdm->v_east = 0.0;
fdm->v_down = 0.0;
fdm->v_body_u = 0.0;
fdm->v_body_v = 0.0;
fdm->v_body_w = 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;
double rpm = ((p.speed_kts - 15.0) / 65.0) * 2000.0 + 500.0;
if ( rpm < 0.0 ) { rpm = 0.0; }
if ( rpm > 3000.0 ) { rpm = 3000.0; }
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;
fdm->elevator = -theta_filter * 5.0;
fdm->elevator_trim_tab = 0.0;
fdm->left_flap = 0.0;
fdm->right_flap = 0.0;
fdm->left_aileron = phi_filter * 1.5;
fdm->right_aileron = phi_filter * 1.5;
fdm->rudder = 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_body_u);
htonf(fdm->v_body_v);
htonf(fdm->v_body_w);
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);
}
static void send_data( const GPSPoint p ) {
// int ctrlsize = sizeof( FGNetCtrls );
int fdmsize = sizeof( FGNetFDM );
// cout << "Running main loop" << endl;
FGNetFDM fgfdm;
FGNetCtrls fgctrls;
gps2fg( p, &fgfdm, &fgctrls );
fdm_sock.send(&fgfdm, fdmsize, 0);
}
void usage( const string &argv0 ) {
cout << "Usage: " << argv0 << endl;
cout << "\t[ --help ]" << endl;
cout << "\t[ --file <file_name>" << endl;
cout << "\t[ --hertz <hertz> ]" << endl;
cout << "\t[ --host <hostname> ]" << endl;
cout << "\t[ --broadcast ]" << endl;
cout << "\t[ --fdm-port <fdm output port #> ]" << endl;
cout << "\t[ --ctrls-port <ctrls output port #> ]" << endl;
}
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], "--file" ) == 0 ) {
++i;
if ( i < argc ) {
file = 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;
} 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 );
}
} else {
usage( argv[0] );
exit( -1 );
}
}
// Load the track data
if ( file == "" ) {
cout << "No track file specified" << endl;
exit(-1);
}
track.load( file );
cout << "Loaded " << track.size() << " records." << endl;
// Setup up outgoing network connections
simgear::Socket::initSockets(); // We must call this before any other net stuff
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;
fdm_sock.setBlocking( false );
ctrls_sock.setBlocking( false );
cout << "blocking false" << endl;
if ( do_broadcast ) {
fdm_sock.setBroadcast( true );
ctrls_sock.setBroadcast( true );
}
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;
int size = track.size();
double current_time = track.get_point(0).get_time();
cout << "Track begin time is " << current_time << endl;
double end_time = track.get_point(size-1).get_time();
cout << "Track end time is " << end_time << endl;
cout << "Duration = " << end_time - current_time << endl;
double frame_us = 1000000.0 / hertz;
if ( frame_us < 0.0 ) {
frame_us = 0.0;
}
SGTimeStamp start_time;
start_time.stamp();
int count = 0;
GPSPoint p, p0, p1;
p0 = p1 = track.get_point( 0 );
while ( current_time < end_time ) {
// cout << "current_time = " << current_time << " end_time = "
// << end_time << endl;
if ( current_time > p1.get_time() ) {
p0 = p1;
++count;
// cout << "count = " << count << endl;
p1 = track.get_point( count );
}
// cout << "p0 = " << p0.get_time() << " p1 = " << p1.get_time()
// << endl;
double percent;
if ( fabs(p1.get_time() - p0.get_time()) < 0.0001 ) {
percent = 0.0;
} else {
percent =
(current_time - p0.get_time()) /
(p1.get_time() - p0.get_time());
}
// cout << "Percent = " << percent << endl;
GPSPoint p = GPSInterpolate( p0, p1, percent );
// cout << current_time << " " << p0.lat_deg << ", " << p0.lon_deg << endl;
// cout << current_time << " " << p1.lat_deg << ", " << p1.lon_deg << endl;
cout << current_time << " " << p.lat_deg << ", " << p.lon_deg << endl;
send_data( p );
// Update the elapsed time.
static bool first_time = true;
if ( first_time ) {
last_time_stamp.stamp();
first_time = false;
}
current_time_stamp.stamp();
/* Convert to ms */
double elapsed_us = (current_time_stamp - last_time_stamp).toUSecs();
if ( elapsed_us < (frame_us - 2000) ) {
double requested_us = (frame_us - elapsed_us) - 2000 ;
ulMilliSecondSleep ( (int)(requested_us / 1000.0) ) ;
}
current_time_stamp.stamp();
while ( (current_time_stamp - last_time_stamp).toUSecs() < frame_us ) {
current_time_stamp.stamp();
}
current_time += (frame_us / 1000000.0);
last_time_stamp = current_time_stamp;
}
cout << "Processed " << count << " entries in "
<< current_time_stamp - start_time << " seconds." << endl;
return 0;
}

10613
utils/GPSsmooth/nmea_data.txt Normal file

File diff suppressed because it is too large Load Diff