| 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 | enum ugPacketType { |
| 15 | GPS_PACKET = 0, |
| 16 | IMU_PACKET = 1, |
| 17 | NAV_PACKET = 2, |
| 18 | SERVO_PACKET = 3, |
| 19 | HEALTH_PACKET = 4 |
| 20 | }; |
| 21 | |
| 22 | struct imu { |
| 23 | double time; |
| 24 | double p,q,r; /* angular velocities */ |
| 25 | double ax,ay,az; /* acceleration */ |
| 26 | double hx,hy,hz; /* magnetic field */ |
| 27 | double Ps,Pt; /* static/pitot pressure */ |
| 28 | // double Tx,Ty,Tz; /* temperature */ |
| 29 | double phi,the,psi; /* attitudes */ |
| 30 | uint64_t err_type; /* error type */ |
| 31 | }; |
| 32 | |
| 33 | struct gps { |
| 34 | double time; |
| 35 | double lat,lon,alt; /* gps position */ |
| 36 | double ve,vn,vd; /* gps velocity */ |
| 37 | double ITOW; |
| 38 | uint64_t err_type; /* error type */ |
| 39 | }; |
| 40 | |
| 41 | struct nav { |
| 42 | double time; |
| 43 | double lat,lon,alt; |
| 44 | double ve,vn,vd; |
| 45 | // float t; |
| 46 | uint64_t err_type; |
| 47 | }; |
| 48 | |
| 49 | struct servo { |
| 50 | double time; |
| 51 | uint16_t chn[8]; |
| 52 | uint64_t status; |
| 53 | }; |
| 54 | |
| 55 | struct health { |
| 56 | double time; |
| 57 | double target_roll_deg; /* AP target roll angle */ |
| 58 | double target_heading_deg; /* AP target heading angle */ |
| 59 | double target_pitch_deg; /* AP target pitch angle */ |
| 60 | double target_climb_fps; /* AP target climb rate */ |
| 61 | double target_altitude_ft; /* AP target altitude */ |
| 62 | uint64_t command_sequence; /* highest received command sequence num */ |
| 63 | uint64_t target_waypoint; /* index of current waypoint target */ |
| 64 | uint64_t loadavg; /* system "1 minute" load average */ |
| 65 | uint64_t ahrs_hz; /* actual ahrs loop hz */ |
| 66 | uint64_t nav_hz; /* actual nav loop hz */ |
| 67 | }; |
| 68 | |
| 69 | // Manage a saved ugear log (track file) |
| 70 | class UGTrack { |
| 71 | |
| 72 | private: |
| 73 | |
| 74 | std::vector <gps> gps_data; |
| 75 | std::vector <imu> imu_data; |
| 76 | std::vector <nav> nav_data; |
| 77 | std::vector <servo> servo_data; |
| 78 | std::vector <health> health_data; |
| 79 | |
| 80 | // parse message and put current data into vector if message has a |
| 81 | // newer time stamp than existing data. |
| 82 | void parse_msg( const int id, char *buf, |
| 83 | gps *gpspacket, imu *imupacket, nav *navpacket, |
| 84 | servo *servopacket, health *healthpacket ); |
| 85 | |
| 86 | // activate special double swap logic for non-standard stargate |
| 87 | // double format |
| 88 | bool sg_swap; |
| 89 | |
| 90 | public: |
| 91 | |
| 92 | UGTrack(); |
| 93 | ~UGTrack(); |
| 94 | |
| 95 | // read/parse the next message from the specified data stream, |
| 96 | // returns id # if a valid message found. |
| 97 | int next_message( SGIOChannel *ch, SGIOChannel *log, |
| 98 | gps *gpspacket, imu *imupacket, nav *navpacket, |
| 99 | servo *servopacket, health * healthpacket, |
| 100 | bool ignore_checksum ); |
| 101 | int next_message( SGSerialPort *serial, SGIOChannel *log, |
| 102 | gps *gpspacket, imu *imupacket, nav *navpacket, |
| 103 | servo *servopacket, health *healthpacket, |
| 104 | bool ignore_checksum ); |
| 105 | |
| 106 | // load the named stream log file into internal buffers |
| 107 | bool load_stream( const std::string &file, bool ignore_checksum ); |
| 108 | |
| 109 | // load the named flight files into internal buffers |
| 110 | bool load_flight( const std::string &path ); |
| 111 | |
| 112 | inline int gps_size() const { return gps_data.size(); } |
| 113 | inline int imu_size() const { return imu_data.size(); } |
| 114 | inline int nav_size() const { return nav_data.size(); } |
| 115 | inline int servo_size() const { return servo_data.size(); } |
| 116 | inline int health_size() const { return health_data.size(); } |
| 117 | |
| 118 | inline gps get_gpspt( const unsigned int i ) |
| 119 | { |
| 120 | if ( i < gps_data.size() ) { |
| 121 | return gps_data[i]; |
| 122 | } else { |
| 123 | return gps(); |
| 124 | } |
| 125 | } |
| 126 | inline imu get_imupt( const unsigned int i ) |
| 127 | { |
| 128 | if ( i < imu_data.size() ) { |
| 129 | return imu_data[i]; |
| 130 | } else { |
| 131 | return imu(); |
| 132 | } |
| 133 | } |
| 134 | inline nav get_navpt( const unsigned int i ) |
| 135 | { |
| 136 | if ( i < nav_data.size() ) { |
| 137 | return nav_data[i]; |
| 138 | } else { |
| 139 | return nav(); |
| 140 | } |
| 141 | } |
| 142 | inline servo get_servopt( const unsigned int i ) |
| 143 | { |
| 144 | if ( i < servo_data.size() ) { |
| 145 | return servo_data[i]; |
| 146 | } else { |
| 147 | return servo(); |
| 148 | } |
| 149 | } |
| 150 | inline health get_healthpt( const unsigned int i ) |
| 151 | { |
| 152 | if ( i < health_data.size() ) { |
| 153 | return health_data[i]; |
| 154 | } else { |
| 155 | return health(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | // set stargate mode where we have to do an odd swapping of doubles to |
| 161 | // account for their non-standard formate |
| 162 | inline void set_stargate_swap_mode() { |
| 163 | sg_swap = true; |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | gps UGEARInterpGPS( const gps A, const gps B, const double percent ); |
| 169 | imu UGEARInterpIMU( const imu A, const imu B, const double percent ); |
| 170 | nav UGEARInterpNAV( const nav A, const nav B, const double percent ); |
| 171 | servo UGEARInterpSERVO( const servo A, const servo B, const double percent ); |
| 172 | health UGEARInterpHEALTH( const health A, const health B, |
| 173 | const double percent ); |