7 #include <simgear/constants.h>
8 #include <simgear/io/sg_file.hxx>
9 #include <simgear/math/sg_geodesy.hxx>
10 #include <simgear/misc/sgstream.hxx>
11 #include <simgear/misc/strutils.hxx>
12 #include <simgear/misc/stdint.hxx>
14 #include "MIDG-II.hxx"
20 MIDGTrack::MIDGTrack() {};
21 MIDGTrack::~MIDGTrack() {};
26 static uint32_t read_swab( char *buf, size_t offset, size_t size ) {
29 char *ptr = buf + offset;
31 // MIDG data is big endian so swap if needed.
32 if ( sgIsLittleEndian() ) {
34 sgEndianSwap( (uint32_t *)ptr );
35 } else if ( size == 2 ) {
36 sgEndianSwap( (uint16_t *)ptr );
41 result = *(uint32_t *)ptr;
42 } else if ( size == 2 ) {
43 result = *(uint16_t *)ptr;
44 } else if ( size == 1 ) {
45 result = *(uint8_t *)ptr;
47 cout << "unknown size in read_swab()" << endl;
54 static bool validate_cksum( uint8_t id, uint8_t size, char *buf,
55 uint8_t cksum0, uint8_t cksum1 )
62 // cout << "c0 = " << (unsigned int)c0 << " c1 = " << (unsigned int)c1 << endl;
66 // cout << "c0 = " << (unsigned int)c0 << " c1 = " << (unsigned int)c1 << endl;
68 for ( uint8_t i = 0; i < size; i++ ) {
69 c0 += (uint8_t)buf[i];
71 // cout << "c0 = " << (unsigned int)c0 << " c1 = " << (unsigned int)c1
72 // << " [" << (unsigned int)buf[i] << "]" << endl;
75 // cout << "c0 = " << (unsigned int)c0 << " (" << (unsigned int)cksum0
76 // << ") c1 = " << (unsigned int)c1 << " (" << (unsigned int)cksum1
79 if ( c0 == cksum0 && c1 == cksum1 ) {
87 void MIDGTrack::parse_msg( const int id, char *buf, MIDGpos *pos, MIDGatt *att )
94 // cout << "message 1 =" << endl;
97 ts = (uint32_t)read_swab( buf, 0, 4 );
98 // cout << " time stamp = " << ts << endl;
101 status = (uint16_t)read_swab( buf, 4, 2 );
102 // cout << " status = " << status << endl;
105 temp = (int16_t)read_swab( buf, 6, 2 );
106 // cout << " temp = " << temp << endl;
108 } else if ( id == 2 ) {
115 // cout << "message 2 =" << endl;
118 ts = (uint32_t)read_swab( buf, 0, 4 );
119 // cout << " time stamp = " << ts << endl;
122 p = (int16_t)read_swab( buf, 4, 2 );
123 q = (int16_t)read_swab( buf, 6, 2 );
124 r = (int16_t)read_swab( buf, 8, 2 );
125 // cout << " pqr = " << p << "," << q << "," << r << endl;
128 ax = (int16_t)read_swab( buf, 10, 2 );
129 ay = (int16_t)read_swab( buf, 12, 2 );
130 az = (int16_t)read_swab( buf, 14, 2 );
131 // cout << " ax ay az = " << ax << "," << ay << "," << az << endl;
134 mx = (int16_t)read_swab( buf, 16, 2 );
135 my = (int16_t)read_swab( buf, 18, 2 );
136 mz = (int16_t)read_swab( buf, 20, 2 );
137 // cout << " mx my mz = " << mx << "," << my << "," << mz << endl;
140 flags = (uint8_t)read_swab( buf, 22, 1 );
141 // cout << " GPS 1PPS flag = " << (int)(flags & (1 << 6))
142 // << " Timestamp is gps = " << (int)(flags & (1 << 7)) << endl;
144 } else if ( id == 3 ) {
149 // cout << "message 3 =" << endl;
152 ts = (uint32_t)read_swab( buf, 0, 4 );
153 // cout << " time stamp = " << ts << endl;
156 mx = (int16_t)read_swab( buf, 4, 2 );
157 my = (int16_t)read_swab( buf, 6, 2 );
158 mz = (int16_t)read_swab( buf, 8, 2 );
159 // cout << " mx my mz = " << mx << "," << my << "," << mz << endl;
162 flags = (uint8_t)read_swab( buf, 10, 1 );
163 // cout << " GPS 1PPS flag = " << (int)(flags & (1 << 6)) << endl;
165 } else if ( id == 10 ) {
169 int16_t yaw, pitch, roll;
170 int32_t Qw, Qx, Qy, Qz;
173 // cout << "message 10 =" << endl;
176 ts = (uint32_t)read_swab( buf, 0, 4 );
177 // cout << " att time stamp = " << ts << endl;
178 att->midg_time = MIDGTime( ts );
181 p = (int16_t)read_swab( buf, 4, 2 );
182 q = (int16_t)read_swab( buf, 6, 2 );
183 r = (int16_t)read_swab( buf, 8, 2 );
184 // cout << " pqr = " << p << "," << q << "," << r << endl;
187 ax = (int16_t)read_swab( buf, 10, 2 );
188 ay = (int16_t)read_swab( buf, 12, 2 );
189 az = (int16_t)read_swab( buf, 14, 2 );
190 // cout << " ax ay az = " << ax << "," << ay << "," << az << endl;
193 yaw = (int16_t)read_swab( buf, 16, 2 );
194 pitch = (int16_t)read_swab( buf, 18, 2 );
195 roll = (int16_t)read_swab( buf, 20, 2 );
196 // cout << " yaw, pitch, roll = " << yaw << "," << pitch << ","
198 att->yaw_rad = ( (double)yaw / 100.0 ) * SG_PI / 180.0;
199 att->pitch_rad = ( (double)pitch / 100.0 ) * SG_PI / 180.0;
200 att->roll_rad = ( (double)roll / 100.0 ) * SG_PI / 180.0;
203 Qw = (int32_t)read_swab( buf, 22, 4 );
204 Qx = (int32_t)read_swab( buf, 26, 4 );
205 Qy = (int32_t)read_swab( buf, 30, 4 );
206 Qz = (int32_t)read_swab( buf, 34, 4 );
207 // cout << " Qw,Qx,Qy,Qz = " << Qw << "," << Qx << "," << Qy << ","
211 flags = (uint8_t)read_swab( buf, 38, 1 );
212 // cout << " External hdg measurement applied = "
213 // << (int)(flags & (1 << 3)) << endl
214 // << " Magnatometer measurement applied = "
215 // << (int)(flags & (1 << 4)) << endl
216 // << " DGPS = " << (int)(flags & (1 << 5)) << endl
217 // << " Timestamp is gps = " << (int)(flags & (1 << 6)) << endl
218 // << " INS mode = " << (int)(flags & (1 << 7))
221 } else if ( id == 12 ) {
223 int32_t posx, posy, posz;
224 int32_t velx, vely, velz;
227 // cout << "message 12 =" << endl;
230 ts = (uint32_t)read_swab( buf, 0, 4 );
231 // cout << " pos time stamp = " << ts << endl;
232 pos->midg_time = MIDGTime( ts );
235 posx = (int32_t)read_swab( buf, 4, 4 );
236 posy = (int32_t)read_swab( buf, 8, 4 );
237 posz = (int32_t)read_swab( buf, 12, 4 );
238 // cout << " pos = " << posx << "," << posy << "," << posz << endl;
241 xyz[0] = posx/100; xyz[1] = posy/100; xyz[2] = posz/100;
242 double lat, lon, alt;
243 sgCartToGeod(xyz, &lat, &lon, &alt);
244 pos->lat_deg = lat * 180.0 / SG_PI;
245 pos->lon_deg = lon * 180.0 / SG_PI;
246 pos->altitude_msl = alt;
247 // cout << " lon = " << pos->lon_deg << " lat = " << pos->lat_deg
248 // << " alt = " << pos->altitude_msl << endl;
251 velx = (int32_t)read_swab( buf, 16, 4 );
252 vely = (int32_t)read_swab( buf, 20, 4 );
253 velz = (int32_t)read_swab( buf, 24, 4 );
254 // cout << " vel = " << velx << "," << vely << "," << velz << endl;
255 double tmp1 = velx*velx + vely*vely + velz*velz;
256 double vel_cms = sqrt( tmp1 );
257 double vel_ms = vel_cms / 100.0;
258 pos->speed_kts = vel_ms * SG_METER_TO_NM * 3600;
261 flags = (uint8_t)read_swab( buf, 28, 1 );
262 // cout << " ENU pos rel to 1st fix = " << (int)(flags & (1 << 0)) << endl
263 // << " Velocity format = " << (int)(flags & (1 << 1)) << endl
264 // << " bit 2 = " << (int)(flags & (1 << 2)) << endl
265 // << " bit 3 = " << (int)(flags & (1 << 3)) << endl
266 // << " GPS pos/vel valid = " << (int)(flags & (1 << 4)) << endl
267 // << " DGPS = " << (int)(flags & (1 << 5)) << endl
268 // << " Timestamp is gps = " << (int)(flags & (1 << 6)) << endl
269 // << " Solution src (0=gps, 1=ins) = " << (int)(flags & (1 << 7))
272 } else if ( id == 20 ) {
273 uint32_t gps_ts, gps_week;
275 int32_t gps_posx, gps_posy, gps_posz;
276 int32_t gps_velx, gps_vely, gps_velz;
277 int16_t pdop, pacc, sacc;
279 // cout << "message 20 =" << endl;
281 // timestamp -- often slightly off from midg time stamp so
282 // let's not use gps ts to determine if we need to push the
283 // previous data or not, just roll it into the current data
284 // independent of time stamp.
285 gps_ts = (uint32_t)read_swab( buf, 0, 4 );
286 // pt->midg_time = MIDGTime( ts );
288 gps_week = (uint16_t)read_swab( buf, 4, 2 );
289 // cout << " gps time stamp = " << gps_ts << " week = " << gps_week
293 details = (uint16_t)read_swab( buf, 6, 2 );
294 // cout << " details = " << details << endl;
296 // gps_posx, gps_posy, gps_posz
297 gps_posx = (int32_t)read_swab( buf, 8, 4 );
298 gps_posy = (int32_t)read_swab( buf, 12, 4 );
299 gps_posz = (int32_t)read_swab( buf, 16, 4 );
300 // cout << " gps_pos = " << gps_posx << "," << gps_posy << ","
301 // << gps_posz << endl;
303 // gps_velx, gps_vely, gps_velz
304 gps_velx = (int32_t)read_swab( buf, 20, 4 );
305 gps_vely = (int32_t)read_swab( buf, 24, 4 );
306 gps_velz = (int32_t)read_swab( buf, 28, 4 );
307 // cout << " gps_vel = " << gps_velx << "," << gps_vely << ","
308 // << gps_velz << endl;
311 pdop = (uint16_t)read_swab( buf, 32, 2 );
312 // cout << " pdop = " << pdop << endl;
315 pacc = (uint16_t)read_swab( buf, 34, 2 );
316 // cout << " pacc = " << pacc << endl;
319 sacc = (uint16_t)read_swab( buf, 36, 2 );
320 // cout << " sacc = " << sacc << endl;
323 cout << "unknown id = " << id << endl;
328 // load the specified file, return the number of records loaded
329 bool MIDGTrack::load( const string &file ) {
335 uint32_t pos_time = 1;
336 uint32_t att_time = 1;
342 SGFile input( file );
343 if ( !input.open( SG_IO_IN ) ) {
344 cout << "Cannot open file: " << file << endl;
348 while ( ! input.eof() ) {
349 // cout << "looking for next message ..." << endl;
350 int id = next_message( &input, NULL, &pos, &att );
354 if ( att.get_msec() > att_time ) {
355 att_data.push_back( att );
356 att_time = att.get_msec();
358 cout << "oops att back in time" << endl;
360 } else if ( id == 12 ) {
361 if ( pos.get_msec() > pos_time ) {
362 pos_data.push_back( pos );
363 pos_time = pos.get_msec();
365 cout << "oops pos back in time" << endl;
370 cout << "processed " << count << " messages" << endl;
375 // attempt to work around some system dependent issues. Our read can
376 // return < data than we want.
377 int myread( SGIOChannel *ch, SGIOChannel *log, char *buf, int length ) {
380 while ( result != length && !myeof ) {
381 result = ch->read( buf, length );
382 if ( ch->get_type() == sgFileType ) {
383 myeof = ((SGFile *)ch)->eof();
387 if ( result > 0 && log != NULL ) {
388 log->write( buf, result );
394 // load the next message of a real time data stream
395 int MIDGTrack::next_message( SGIOChannel *ch, SGIOChannel *log,
396 MIDGpos *pos, MIDGatt *att )
401 // cout << "in next_message()" << endl;
405 // scan for sync characters
406 uint8_t sync0, sync1;
407 myread( ch, log, tmpbuf, 1 ); sync0 = (unsigned char)tmpbuf[0];
408 myread( ch, log, tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
409 while ( (sync0 != 129 || sync1 != 161) && !myeof ) {
411 myread( ch, log, tmpbuf, 1 ); sync1 = (unsigned char)tmpbuf[0];
412 // cout << "scanning for start of message, eof = " << ch->eof() << endl;
413 if ( ch->get_type() == sgFileType ) {
414 myeof = ((SGFile *)ch)->eof();
418 // cout << "found start of message ..." << endl;
420 // read message id and size
421 myread( ch, log, tmpbuf, 1 ); uint8_t id = (unsigned char)tmpbuf[0];
422 myread( ch, log, tmpbuf, 1 ); uint8_t size = (unsigned char)tmpbuf[0];
423 // cout << "message = " << (int)id << " size = " << (int)size << endl;
426 if ( ch->get_type() == sgFileType ) {
427 int count = myread( ch, log, savebuf, size );
428 if ( count != size ) {
429 cout << "ERROR: didn't read enough bytes!" << endl;
432 for ( int i = 0; i < size; ++i ) {
433 myread( ch, log, tmpbuf, 1 ); savebuf[i] = tmpbuf[0];
438 myread( ch, log, tmpbuf, 1 ); uint8_t cksum0 = (unsigned char)tmpbuf[0];
439 myread( ch, log, tmpbuf, 1 ); uint8_t cksum1 = (unsigned char)tmpbuf[0];
441 if ( validate_cksum( id, size, savebuf, cksum0, cksum1 ) ) {
442 parse_msg( id, savebuf, pos, att );
446 cout << "Check sum failure!" << endl;
451 static double interp( double a, double b, double p, bool rotational = false ) {
454 // special handling of rotational data
455 if ( diff > SGD_PI ) {
457 } else if ( diff < -SGD_PI ) {
465 MIDGpos MIDGInterpPos( const MIDGpos A, const MIDGpos B, const double percent )
468 p.midg_time = MIDGTime((uint32_t)interp(A.midg_time.get_msec(),
469 B.midg_time.get_msec(),
471 p.lat_deg = interp(A.lat_deg, B.lat_deg, percent);
472 p.lon_deg = interp(A.lon_deg, B.lon_deg, percent);
473 p.altitude_msl = interp(A.altitude_msl, B.altitude_msl, percent);
474 p.fix_quality = (int)interp(A.fix_quality, B.fix_quality, percent);
475 p.num_satellites = (int)interp(A.num_satellites, B.num_satellites, percent);
476 p.hdop = interp(A.hdop, B.hdop, percent);
477 p.speed_kts = interp(A.speed_kts, B.speed_kts, percent);
478 p.course_true = interp(A.course_true, B.course_true, percent, true);
483 MIDGatt MIDGInterpAtt( const MIDGatt A, const MIDGatt B, const double percent )
486 p.midg_time = MIDGTime((uint32_t)interp(A.midg_time.get_msec(),
487 B.midg_time.get_msec(),
489 p.yaw_rad = interp(A.yaw_rad, B.yaw_rad, percent, true);
490 p.pitch_rad = interp(A.pitch_rad, B.pitch_rad, percent, true);
491 p.roll_rad = interp(A.roll_rad, B.roll_rad, percent, true);