|
| 1 | +#include "LogAnalysis.h" |
| 2 | +//#include "dataconnection.h" |
| 3 | + |
| 4 | +#include <QFile> |
| 5 | +#include <QFileDialog> |
| 6 | +#include <QSqlError> |
| 7 | +#include <QDebug> |
| 8 | +#include <QSqlQuery> |
| 9 | +#include <QMessageBox> |
| 10 | + |
| 11 | +LogAnalysis::LogAnalysis(QWidget *parent) : |
| 12 | + QWidget(parent) |
| 13 | +{ |
| 14 | + _creatConnection(); |
| 15 | +} |
| 16 | + |
| 17 | +bool LogAnalysis::_creatConnection() |
| 18 | +{ |
| 19 | + db = QSqlDatabase::addDatabase("QSQLITE"); |
| 20 | + db.setDatabaseName("LogData.db"); |
| 21 | + |
| 22 | + if(!db.open()) { |
| 23 | + qDebug()<<db.lastError(); |
| 24 | + qFatal("Fail to connect"); |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + //db.transaction(); |
| 29 | + |
| 30 | + //create database table |
| 31 | + QSqlQuery query(db); |
| 32 | + |
| 33 | + query.exec("create table Mode (timestamp int primary key," |
| 34 | + "mode int)"); |
| 35 | + |
| 36 | + query.exec("create table FlightStatus (timestamp int primary key," |
| 37 | + "flightStatus int)"); |
| 38 | + |
| 39 | + query.exec("create table Baro (timestamp int primary key," |
| 40 | + "temperature float," |
| 41 | + "altitude float)"); |
| 42 | + |
| 43 | + query.exec("create table IMU (timestamp int primary key," |
| 44 | + "acc_x float," |
| 45 | + "acc_y float," |
| 46 | + "acc_z float," |
| 47 | + "gyro_x float," |
| 48 | + "gyro_y float," |
| 49 | + "gyro_z float," |
| 50 | + "mag_x float," |
| 51 | + "mag_y float," |
| 52 | + "mag_z float)"); |
| 53 | + |
| 54 | + query.exec("create table GPS (timestamp int primary key," |
| 55 | + "lat int," |
| 56 | + "lon int," |
| 57 | + "azm float," |
| 58 | + "vel float," |
| 59 | + "alt float," |
| 60 | + "sat int," |
| 61 | + "date int," |
| 62 | + "time int)"); |
| 63 | + |
| 64 | + query.exec("create table RC (timestamp int primary key," |
| 65 | + "ch1 float," |
| 66 | + "ch2 float," |
| 67 | + "ch3 float," |
| 68 | + "ch4 float," |
| 69 | + "ch5 float," |
| 70 | + "ch6 float," |
| 71 | + "ch7 float," |
| 72 | + "ch8 float," |
| 73 | + "ch9 float," |
| 74 | + "ch10 float," |
| 75 | + "ch11 float," |
| 76 | + "ch12 float," |
| 77 | + "ch13 float," |
| 78 | + "ch14 float," |
| 79 | + "ch15 float," |
| 80 | + "ch16 float)"); |
| 81 | + |
| 82 | + query.exec("create table Battery_Info (timestamp int primary key," |
| 83 | + "voltage float," |
| 84 | + "battery_remaining float)"); |
| 85 | + |
| 86 | + /*query.exec("create table Attitude_Setpoint (timestamp int primary key," |
| 87 | + "thrust float," |
| 88 | + "yaw_ff float)"); |
| 89 | + */ |
| 90 | + |
| 91 | + query.exec("create table Attitude (timestamp int primary key," |
| 92 | + "rate_x float," |
| 93 | + "rate_y float," |
| 94 | + "rate_z float," |
| 95 | + "euler_R float," |
| 96 | + "euler_P float," |
| 97 | + "euler_Y float)"); |
| 98 | + |
| 99 | + query.exec("create table Manual_Setpoint (timestamp int primary key," |
| 100 | + "throttle float," |
| 101 | + "euler_R float," |
| 102 | + "euler_P float," |
| 103 | + "euler_Y float)"); |
| 104 | + |
| 105 | + query.exec("create table PosCtrl_Setpoint (timestamp int primary key," |
| 106 | + "position_setpoint_x float," |
| 107 | + "position_setpoint_y float," |
| 108 | + "position_setpoint_z float," |
| 109 | + "vel_ff_x float," |
| 110 | + "vel_ff_y float," |
| 111 | + "vel_ff_z float," |
| 112 | + "acc_ff_x float," |
| 113 | + "acc_ff_y float," |
| 114 | + "acc_ff_z float," |
| 115 | + "yaw_setpoint float)"); |
| 116 | + |
| 117 | + query.exec("create table AltCtrl_Setpoint (timestamp int primary key," |
| 118 | + "pos_sp_z float," |
| 119 | + "vel_ff_z float," |
| 120 | + "euler_R float," |
| 121 | + "euler_P float," |
| 122 | + "euler_Y float)"); |
| 123 | + |
| 124 | + query.exec("create table Position (timestamp int primary key," |
| 125 | + "pos_x float," |
| 126 | + "pos_y float," |
| 127 | + "pos_z float," |
| 128 | + "vel_x float," |
| 129 | + "vel_y float," |
| 130 | + "vel_z float," |
| 131 | + "acc_x float," |
| 132 | + "acc_y float," |
| 133 | + "acc_z float)"); |
| 134 | + |
| 135 | + return true; |
| 136 | +} |
| 137 | + |
| 138 | +void LogAnalysis::logReadAndStore() |
| 139 | +{ |
| 140 | + /**/ |
| 141 | + db.transaction(); |
| 142 | + |
| 143 | + QStringList tables = db.tables(); |
| 144 | + QSqlQuery query(db); |
| 145 | + for(int i = 0;i<tables.count();i++) { |
| 146 | + QString truncateString = QString("truncate table %1").arg(tables.at(i)); |
| 147 | + //qDebug()<<truncateString; |
| 148 | + query.exec(truncateString); |
| 149 | + } |
| 150 | + |
| 151 | + QString fileName = QFileDialog::getOpenFileName(this,"Open File",".\\","texFile(*.txt *.bin)"); |
| 152 | + QFile file(fileName); |
| 153 | + |
| 154 | + if(fileName.isNull()) { |
| 155 | + QMessageBox::information(this,"Document","No document selected.",QMessageBox::Ok | QMessageBox::Cancel); |
| 156 | + emit openFileStatusChanged(false); |
| 157 | + } |
| 158 | + else { |
| 159 | + emit openFileStatusChanged(true); |
| 160 | + QByteArray b; |
| 161 | + QList<QByteArray> arrayList; |
| 162 | + if(!file.open(QIODevice::ReadOnly)) { |
| 163 | + qDebug()<<"can't open"; |
| 164 | + return; |
| 165 | + } |
| 166 | + else { |
| 167 | + // 用"\r\n"即0d0a分行,每一行存储一个数据结构,见sdfiles.h |
| 168 | + b = file.readAll(); |
| 169 | + QByteArray turn("\r\n",2); |
| 170 | + int len = turn.length(); |
| 171 | + int start = 0; |
| 172 | + int end = b.indexOf(turn,start); |
| 173 | + while(end != -1) { |
| 174 | + arrayList<<b.mid(start,(end-start)); |
| 175 | + start = end + len; |
| 176 | + end = b.indexOf(turn,start); |
| 177 | + } |
| 178 | + qDebug()<<arrayList.count(); |
| 179 | + } |
| 180 | + |
| 181 | + for(int i = 0;i<arrayList.size();i++) { |
| 182 | + //读取每一行的数据 |
| 183 | + const char* logData = arrayList.at(i).data(); |
| 184 | + //第一个字节为message_id |
| 185 | + // qDebug()<<QString::number(*logData); |
| 186 | + //QSqlQuery query(db); |
| 187 | + |
| 188 | + switch(*logData) { |
| 189 | + case 0: |
| 190 | + { |
| 191 | + mode_log_t* mode_log = (mode_log_t*)logData; |
| 192 | + mode_t mode =mode_log->mode_log_s; |
| 193 | + QString queryString = QString("insert into Mode values(%1,%2)").arg(mode_log->timestamp).arg(mode); |
| 194 | + query.exec(queryString); |
| 195 | + |
| 196 | + //mode_log_t* mode_log = (mode_log_t*)logData; |
| 197 | + //qDebug()<<"mode"<<mode_log->mode_log_s; |
| 198 | + } |
| 199 | + break; |
| 200 | + |
| 201 | + //0x01,flight status |
| 202 | + case 1: |
| 203 | + { |
| 204 | + status_log_t* status_log = (status_log_t*)logData; |
| 205 | + statusFlight_t status = status_log->status_log_s; |
| 206 | + QString queryString = QString("insert into FlightStatus values(%1,%2)").arg(status_log->timestamp).arg(status); |
| 207 | + query.exec(queryString); |
| 208 | + |
| 209 | + //qDebug()<<"status"<<status_log->status_log_s; |
| 210 | + } |
| 211 | + break; |
| 212 | + |
| 213 | + //0x10, baro |
| 214 | + case 16: |
| 215 | + { |
| 216 | + baro_log_t* baro_log = (baro_log_t*)logData; |
| 217 | + baro_t* baro = (baro_t*)&(baro_log->baro_log_s); |
| 218 | + |
| 219 | + QString queryString = QString("insert into Baro values(%1,%2,%3)").arg(baro->timestamp).arg(baro->temp).arg(baro->alt); |
| 220 | + query.exec(queryString); |
| 221 | + } |
| 222 | + break; |
| 223 | + |
| 224 | + |
| 225 | + //0x11,imu |
| 226 | + case 17: |
| 227 | + { |
| 228 | + imu_log_t* imu_log = (imu_log_t*)logData; |
| 229 | + //qDebug()<<"message_id"<<imu_log->message_id; |
| 230 | + marg_t* marg = (marg_t*)&(imu_log->marg_log_s); |
| 231 | + //qDebug()<<"x"<<marg->acc.x<<"y"<<marg->acc.y<<"z"<<marg->acc.z; |
| 232 | + QString queryString = QString("insert into IMU values(%1,%2,%3,%4,%5,%6,%7,%8,%9,%10)").arg(marg->timestamp) |
| 233 | + .arg(marg->acc.x).arg(marg->acc.y).arg(marg->acc.z) |
| 234 | + .arg(marg->gyr.x).arg(marg->gyr.y).arg(marg->gyr.z) |
| 235 | + .arg(marg->mag.x).arg(marg->mag.y).arg(marg->mag.z); |
| 236 | + query.exec(queryString); |
| 237 | + } |
| 238 | + break; |
| 239 | + |
| 240 | + //0x12,gps |
| 241 | + case 18: |
| 242 | + { |
| 243 | + gps_log_t* gps_log = (gps_log_t*)logData; |
| 244 | + //qDebug()<<"message_id"<<QString::number(gps_log->message_id); |
| 245 | + gpsRaw_t* gps = (gpsRaw_t*)&(gps_log->gpsRaw_log_s); |
| 246 | + qDebug()<<"gps date time"<<gps->date<<gps->time; |
| 247 | + QString queryString = QString("insert into GPS values(%1,%2,%3,%4,%5,%6,%7,%8,%9)").arg(gps->timestamp) |
| 248 | + .arg(gps->lat).arg(gps->lon).arg(gps->azm).arg(gps->vel).arg(gps->alt).arg(gps->sat).arg(gps->date).arg(gps->time); |
| 249 | + query.exec(queryString); |
| 250 | + } |
| 251 | + break; |
| 252 | + |
| 253 | + //0x13, rc |
| 254 | + case 19: |
| 255 | + { |
| 256 | + rc_log_t* rc_log = (rc_log_t*)logData; |
| 257 | + rc_t* rc = (rc_t*)&(rc_log->rc_log_s); |
| 258 | + //qDebug()<<"channel 3"<<rc->channels[2]; |
| 259 | + query.prepare("insert into RC values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); |
| 260 | + query.bindValue(0,rc->timestamp); |
| 261 | + for(int i =1;i<17;i++) { |
| 262 | + query.bindValue(i,rc->channels[i-1]); |
| 263 | + } |
| 264 | + query.exec(); |
| 265 | + } |
| 266 | + break; |
| 267 | + |
| 268 | + //0x14, power |
| 269 | + case 20: |
| 270 | + { |
| 271 | + bat_log_t* bat_log = (bat_log_t*)logData; |
| 272 | + battery_t* battery = (battery_t*)&(bat_log->bat_log_s); |
| 273 | + QString queryString = QString("insert into Battery_Info values(%1,%2,%3)").arg(battery->timestamp).arg(battery->voltage).arg(battery->battery_remaining); |
| 274 | + qDebug()<<"bat query"<<query.exec(queryString); |
| 275 | + |
| 276 | + //qDebug()<<"voltage: "<<battery->voltage<<"battery_remaining: "<<battery->battery_remaining; |
| 277 | + } |
| 278 | + break; |
| 279 | + |
| 280 | + //0x20, attsp |
| 281 | + /* case 32: |
| 282 | + { |
| 283 | + attsp_log_t* attsp_log = (attsp_log_t*)logData; |
| 284 | + attsp_t* attsp = (attsp_t*)&(attsp_log->attsp_log_s); |
| 285 | + QString queryString = QString("insert into Attitude_Setpoint values(%1,%2,%3)").arg(attsp->timestamp) |
| 286 | + .arg(attsp->thrust).arg(attsp->yaw_ff); |
| 287 | + query.exec(queryString); |
| 288 | + } |
| 289 | + break; |
| 290 | + */ |
| 291 | + |
| 292 | + //0x21, att |
| 293 | + case 33: |
| 294 | + { |
| 295 | + att_log_t* att_log = (att_log_t*)logData; |
| 296 | + att_t* att = (att_t*)&(att_log->att_log_s); |
| 297 | + QString queryString = QString("insert into Attitude values(%1,%2,%3,%4,%5,%6,%7)").arg(att->timestamp) |
| 298 | + .arg(att->rate.x).arg(att->rate.y).arg(att->rate.z).arg(att->Euler.R).arg(att->Euler.P).arg(att->Euler.Y); |
| 299 | + query.exec(queryString); |
| 300 | + } |
| 301 | + break; |
| 302 | + |
| 303 | + //0x22, output |
| 304 | + case 34: |
| 305 | + { |
| 306 | + output_t* output = (output_t*)logData; |
| 307 | + qDebug()<<"thrust"<<output->thrust; |
| 308 | + } |
| 309 | + break; |
| 310 | + |
| 311 | + //0x23, mansp |
| 312 | + case 35: |
| 313 | + { |
| 314 | + mansp_log_t* mansp_log = (mansp_log_t*)logData; |
| 315 | + manCtrlsp_t* manCtrlsp = (manCtrlsp_t*)&(mansp_log->mansp_log_s); |
| 316 | + QString queryString = QString("insert into Manual_Setpoint values(%1,%2,%3,%4)") |
| 317 | + .arg(manCtrlsp->throttle).arg(manCtrlsp->euler.R).arg(manCtrlsp->euler.P).arg(manCtrlsp->euler.Y); |
| 318 | + query.exec(queryString); |
| 319 | + } |
| 320 | + break; |
| 321 | + |
| 322 | + //0x24, altsp |
| 323 | + case 36: |
| 324 | + { |
| 325 | + altsp_log_t* altsp_log = (altsp_log_t*)logData; |
| 326 | + altCtrlsp_t* altCtrlsp = (altCtrlsp_t*)&(altsp_log->altsp_log_s); |
| 327 | + QString queryString = QString("insert into AltCtrl_Setpoint values(%1,%2,%3,%4,%5)") |
| 328 | + .arg(altCtrlsp->pos_sp_z).arg(altCtrlsp->euler.R).arg(altCtrlsp->euler.P).arg(altCtrlsp->euler.Y); |
| 329 | + query.exec(queryString); |
| 330 | + } |
| 331 | + break; |
| 332 | + |
| 333 | + //0x25, possp |
| 334 | + case 37: |
| 335 | + { |
| 336 | + possp_log_t* possp_log = (possp_log_t*)logData; |
| 337 | + posCtrlsp_t* posCtrlsp = (posCtrlsp_t*)&(possp_log->possp_log_s); |
| 338 | + QString queryString = QString("insert into PosCtrl_Setpoint values(%1,%2,%3,%4,%5,%6,%7,%8,%9,%10,%11)") |
| 339 | + .arg(posCtrlsp->timestamp).arg(posCtrlsp->pos_sp.x).arg(posCtrlsp->pos_sp.y).arg(posCtrlsp->pos_sp.z) |
| 340 | + .arg(posCtrlsp->vel_ff.x).arg(posCtrlsp->vel_ff.y).arg(posCtrlsp->vel_ff.z) |
| 341 | + .arg(posCtrlsp->acc_ff.x).arg(posCtrlsp->acc_ff.y).arg(posCtrlsp->acc_ff.z) |
| 342 | + .arg(posCtrlsp->yaw_sp); |
| 343 | + query.exec(queryString); |
| 344 | + } |
| 345 | + break; |
| 346 | + |
| 347 | + //0x26, pos |
| 348 | + case 38: |
| 349 | + { |
| 350 | + pos_log_t* pos_log = (pos_log_t*)logData; |
| 351 | + pos_t* pos = (pos_t*)&(pos_log->pos_log_s); |
| 352 | + QString queryString = QString("insert into Position values(%1,%2,%3,%4,%5,%6,%7,%8,%9,%10)") |
| 353 | + .arg(pos->timestamp).arg(pos->pos.x).arg(pos->pos.y).arg(pos->pos.z) |
| 354 | + .arg(pos->vel.x).arg(pos->vel.y).arg(pos->vel.z) |
| 355 | + .arg(pos->acc.x).arg(pos->acc.y).arg(pos->acc.z); |
| 356 | + query.exec(queryString); |
| 357 | + } |
| 358 | + break; |
| 359 | + |
| 360 | + default: |
| 361 | + break; |
| 362 | + } |
| 363 | + } |
| 364 | + db.commit(); |
| 365 | + } |
| 366 | +} |
0 commit comments