| 1 | #ifdef HAVE_CONFIG_H |
| 2 | # include <config.h> |
| 3 | #endif |
| 4 | |
| 5 | #include <simgear/compiler.h> |
| 6 | |
| 7 | #include <iostream> |
| 8 | |
| 9 | #include <simgear/constants.h> |
| 10 | #include <simgear/io/iostreams/sgstream.hxx> |
| 11 | #include <simgear/misc/strutils.hxx> |
| 12 | #include <simgear/misc/sg_path.hxx> |
| 13 | |
| 14 | #include "GPSsmooth.hxx" |
| 15 | |
| 16 | using std::cout; |
| 17 | using std::endl; |
| 18 | |
| 19 | |
| 20 | GPSTrack::GPSTrack() {}; |
| 21 | GPSTrack::~GPSTrack() {}; |
| 22 | |
| 23 | |
| 24 | // load the specified file, return the number of records loaded |
| 25 | int GPSTrack::load( const std::string &file ) { |
| 26 | int count = 0; |
| 27 | |
| 28 | data.clear(); |
| 29 | |
| 30 | // openg the file |
| 31 | sg_gzifstream in( SGPath::fromLocal8Bit(file.c_str()) ); |
| 32 | if ( !in.is_open() ) { |
| 33 | cout << "Cannot open file: " << file << endl; |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | std::vector <std::string> tokens; |
| 38 | GPSPoint p; |
| 39 | |
| 40 | while ( ! in.eof() ) { |
| 41 | char tmp[2049]; |
| 42 | |
| 43 | in.getline(tmp, 2048); |
| 44 | |
| 45 | tokens.clear(); |
| 46 | tokens = simgear::strutils::split(tmp, ","); |
| 47 | |
| 48 | int dd; |
| 49 | double raw, min; |
| 50 | |
| 51 | if ( tokens[0] == "$GPRMC" && tokens.size() == 13 ) { |
| 52 | double raw_time = atof(tokens[1].c_str()); |
| 53 | GPSTime gps_time = GPSTime( raw_time ); |
| 54 | if ( (gps_time.get_time() > p.gps_time.get_time()) && |
| 55 | (p.gps_time.get_time() > 1.0) ) |
| 56 | { |
| 57 | // new data cycle store last data before continuing |
| 58 | data.push_back( p ); |
| 59 | count++; |
| 60 | } |
| 61 | |
| 62 | p.gps_time = gps_time; |
| 63 | |
| 64 | raw = atof( tokens[3].c_str() ); |
| 65 | dd = (int)(raw / 100.00); |
| 66 | min = raw - dd * 100.0; |
| 67 | p.lat_deg = dd + min / 60.0; |
| 68 | if ( tokens[4] == "S" ) { |
| 69 | p.lat_deg = -p.lat_deg; |
| 70 | } |
| 71 | raw = atof( tokens[5].c_str() ); |
| 72 | dd = (int)(raw / 100.00); |
| 73 | min = raw - dd * 100.0; |
| 74 | p.lon_deg = dd + min / 60.0; |
| 75 | if ( tokens[6] == "W" ) { |
| 76 | p.lon_deg = -p.lon_deg; |
| 77 | } |
| 78 | |
| 79 | static double max_speed = 0.0; |
| 80 | p.speed_kts = atof( tokens[7].c_str() ); |
| 81 | if ( p.speed_kts > max_speed ) { |
| 82 | max_speed = p.speed_kts; |
| 83 | cout << "max speed = " << max_speed << endl; |
| 84 | } |
| 85 | p.course_true = atof( tokens[8].c_str() ) * SGD_DEGREES_TO_RADIANS; |
| 86 | |
| 87 | } else if ( tokens[0] == "$GPGGA" && tokens.size() == 15 ) { |
| 88 | double raw_time = atof(tokens[1].c_str()); |
| 89 | GPSTime gps_time = GPSTime( raw_time ); |
| 90 | if ( fabs(gps_time.get_time() - p.gps_time.get_time()) > 0.0001 && |
| 91 | (p.gps_time.get_time() > 1.0) ) { |
| 92 | // new data cycle store last data before continuing |
| 93 | data.push_back( p ); |
| 94 | count++; |
| 95 | } |
| 96 | |
| 97 | p.gps_time = gps_time; |
| 98 | |
| 99 | raw = atof( tokens[2].c_str() ); |
| 100 | dd = (int)(raw / 100.00); |
| 101 | min = raw - dd * 100.0; |
| 102 | p.lat_deg = dd + min / 60.0; |
| 103 | if ( tokens[3] == "S" ) { |
| 104 | p.lat_deg = -p.lat_deg; |
| 105 | } |
| 106 | raw = atof( tokens[4].c_str() ); |
| 107 | dd = (int)(raw / 100.00); |
| 108 | min = raw - dd * 100.0; |
| 109 | p.lon_deg = dd + min / 60.0; |
| 110 | if ( tokens[5] == "W" ) { |
| 111 | p.lon_deg = -p.lon_deg; |
| 112 | } |
| 113 | |
| 114 | p.fix_quality = atoi( tokens[6].c_str() ); |
| 115 | p.num_satellites = atoi( tokens[7].c_str() ); |
| 116 | p.hdop = atof( tokens[8].c_str() ); |
| 117 | |
| 118 | static double max_alt = 0.0; |
| 119 | double alt = atof( tokens[9].c_str() ); |
| 120 | if ( alt > max_alt ) { |
| 121 | max_alt = alt; |
| 122 | cout << "max alt = " << max_alt << endl; |
| 123 | } |
| 124 | |
| 125 | if ( tokens[10] == "F" || tokens[10] == "f" ) { |
| 126 | alt *= SG_FEET_TO_METER; |
| 127 | } |
| 128 | p.altitude_msl = alt; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return count; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | static double interp( double a, double b, double p, bool rotational = false ) { |
| 137 | double diff = b - a; |
| 138 | if ( rotational ) { |
| 139 | // special handling of rotational data |
| 140 | if ( diff > SGD_PI ) { |
| 141 | diff -= SGD_2PI; |
| 142 | } else if ( diff < -SGD_PI ) { |
| 143 | diff += SGD_2PI; |
| 144 | } |
| 145 | } |
| 146 | return a + diff * p; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | GPSPoint GPSInterpolate( const GPSPoint A, const GPSPoint B, |
| 151 | const double percent ) { |
| 152 | GPSPoint p; |
| 153 | p.gps_time = GPSTime((int)interp(A.gps_time.get_time(), |
| 154 | B.gps_time.get_time(), |
| 155 | percent)); |
| 156 | p.lat_deg = interp(A.lat_deg, B.lat_deg, percent); |
| 157 | p.lon_deg = interp(A.lon_deg, B.lon_deg, percent); |
| 158 | p.fix_quality = (int)interp(A.fix_quality, B.fix_quality, percent); |
| 159 | p.num_satellites = (int)interp(A.num_satellites, B.num_satellites, percent); |
| 160 | p.hdop = interp(A.hdop, B.hdop, percent); |
| 161 | p.altitude_msl = interp(A.altitude_msl, B.altitude_msl, percent); |
| 162 | p.speed_kts = interp(A.speed_kts, B.speed_kts, percent); |
| 163 | p.course_true = interp(A.course_true, B.course_true, percent, true); |
| 164 | |
| 165 | return p; |
| 166 | } |