| 1 | #pragma once |
| 2 | |
| 3 | #include <simgear/compiler.h> |
| 4 | |
| 5 | #include <iostream> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
| 9 | #include <simgear/misc/stdint.hxx> |
| 10 | #include <simgear/io/iochannel.hxx> |
| 11 | #include <simgear/serial/serial.hxx> |
| 12 | |
| 13 | |
| 14 | // encapsulate a midg integer time (fixme, assumes all times in a track |
| 15 | // are from the same day, so we don't handle midnight roll over) |
| 16 | class MIDGTime { |
| 17 | |
| 18 | public: |
| 19 | |
| 20 | uint32_t msec; |
| 21 | double seconds; |
| 22 | |
| 23 | inline MIDGTime( const int dd, const int hh, const int mm, |
| 24 | const double ss ) |
| 25 | { |
| 26 | seconds = dd*86400.0 + hh*3600.0 + mm*60.0 + ss; |
| 27 | msec = (uint32_t)(seconds * 1000); |
| 28 | } |
| 29 | inline MIDGTime( const uint32_t midgtime_msec ) { |
| 30 | msec = midgtime_msec; |
| 31 | seconds = (double)midgtime_msec / 1000.0; |
| 32 | // std::cout << midgtime << " = " << seconds << std::endl; |
| 33 | } |
| 34 | inline ~MIDGTime() {} |
| 35 | |
| 36 | inline double get_seconds() const { return seconds; } |
| 37 | inline uint32_t get_msec() const { return msec; } |
| 38 | inline double diff_seconds( const MIDGTime t ) const { |
| 39 | return seconds - t.seconds; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | |
| 45 | // base class for MIDG data types |
| 46 | class MIDGpoint { |
| 47 | |
| 48 | public: |
| 49 | |
| 50 | MIDGTime midg_time; |
| 51 | |
| 52 | MIDGpoint() : |
| 53 | midg_time(MIDGTime(0)) |
| 54 | { } |
| 55 | |
| 56 | inline double get_seconds() const { return midg_time.get_seconds(); } |
| 57 | inline uint32_t get_msec() const { return midg_time.get_msec(); } |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | // encapsulate the interesting midg data for a moment in time |
| 62 | class MIDGpos : public MIDGpoint { |
| 63 | |
| 64 | public: |
| 65 | |
| 66 | double lat_deg; |
| 67 | double lon_deg; |
| 68 | double altitude_msl; |
| 69 | int fix_quality; |
| 70 | int num_satellites; |
| 71 | double hdop; |
| 72 | double speed_kts; |
| 73 | double course_true; |
| 74 | |
| 75 | MIDGpos() : |
| 76 | lat_deg(0.0), |
| 77 | lon_deg(0.0), |
| 78 | altitude_msl(0.0), |
| 79 | fix_quality(0), |
| 80 | num_satellites(0), |
| 81 | hdop(0.0), |
| 82 | speed_kts(0.0), |
| 83 | course_true(0.0) |
| 84 | { } |
| 85 | }; |
| 86 | |
| 87 | |
| 88 | // encapsulate the interesting midg data for a moment in time |
| 89 | class MIDGatt : public MIDGpoint { |
| 90 | |
| 91 | public: |
| 92 | |
| 93 | double yaw_rad; |
| 94 | double pitch_rad; |
| 95 | double roll_rad; |
| 96 | |
| 97 | MIDGatt() : |
| 98 | yaw_rad(0.0), |
| 99 | pitch_rad(0.0), |
| 100 | roll_rad(0.0) |
| 101 | { } |
| 102 | }; |
| 103 | |
| 104 | |
| 105 | // Manage a saved midg log (track file) |
| 106 | class MIDGTrack { |
| 107 | |
| 108 | private: |
| 109 | |
| 110 | std::vector <MIDGpos> pos_data; |
| 111 | std::vector <MIDGatt> att_data; |
| 112 | |
| 113 | // parse message and put current data into vector if message has a |
| 114 | // newer time stamp than existing data. |
| 115 | void parse_msg( const int id, char *buf, MIDGpos *pos, MIDGatt *att ); |
| 116 | |
| 117 | public: |
| 118 | |
| 119 | MIDGTrack(); |
| 120 | ~MIDGTrack(); |
| 121 | |
| 122 | // read/parse the next message from the specified data stream, |
| 123 | // returns id # if a valid message found. |
| 124 | int next_message( SGIOChannel *ch, SGIOChannel *log, |
| 125 | MIDGpos *pos, MIDGatt *att ); |
| 126 | int next_message( SGSerialPort *serial, SGIOChannel *log, |
| 127 | MIDGpos *pos, MIDGatt *att ); |
| 128 | |
| 129 | // load the named file into internal buffers |
| 130 | bool load( const std::string &file ); |
| 131 | |
| 132 | inline int pos_size() const { return pos_data.size(); } |
| 133 | inline int att_size() const { return att_data.size(); } |
| 134 | |
| 135 | inline MIDGpos get_pospt( const unsigned int i ) |
| 136 | { |
| 137 | if ( i < pos_data.size() ) { |
| 138 | return pos_data[i]; |
| 139 | } else { |
| 140 | return MIDGpos(); |
| 141 | } |
| 142 | } |
| 143 | inline MIDGatt get_attpt( const unsigned int i ) |
| 144 | { |
| 145 | if ( i < att_data.size() ) { |
| 146 | return att_data[i]; |
| 147 | } else { |
| 148 | return MIDGatt(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |
| 153 | }; |
| 154 | |
| 155 | |
| 156 | MIDGpos MIDGInterpPos( const MIDGpos A, const MIDGpos B, const double percent ); |
| 157 | MIDGatt MIDGInterpAtt( const MIDGatt A, const MIDGatt B, const double percent ); |