重改播放

This commit is contained in:
hm
2020-08-20 17:48:09 +08:00
parent 5c5ef643e1
commit 8ca8016d55
14 changed files with 199 additions and 220 deletions
+9 -5
View File
@@ -66,19 +66,23 @@ void Tools_Index2::ReplayComplete()
void Tools_Index2::on_horizontalSlider_sliderReleased()
{
}
void Tools_Index2::on_horizontalSlider_sliderPressed()
{
if(isMove == false)
{
//读取暂停和播放状态
PlayState = PlayState?false:true;
//暂停和播放
emit setPlay(PlayState);
}
}
void Tools_Index2::on_horizontalSlider_sliderPressed()
{
isMove = false;
}
void Tools_Index2::on_horizontalSlider_sliderMoved(int position)
{
isMove = true;
//设置比例
emit setPercentage((float)position/ui->horizontalSlider->maximum());
}
+1
View File
@@ -60,6 +60,7 @@ private:
Ui::Tools_Index2 *ui;
bool isMove = false;
bool PlayState = false;//未播放
+6 -6
View File
@@ -96,19 +96,19 @@ MainWindow::MainWindow(QWidget *parent)
//dlink ----- tools
connect(toolsui->index2,SIGNAL(setPlay(bool)),
dlink->play,SLOT(startReplay(bool)),Qt::DirectConnection);
dlink->mavlinknode->replay,SLOT(startReplay(bool)),Qt::DirectConnection);
connect(toolsui->index2,SIGNAL(setFileName(QString)),
dlink,SLOT(replay(QString)),Qt::DirectConnection);
dlink->mavlinknode,SLOT(setLogfile(QString)),Qt::DirectConnection);
connect(toolsui->index2,SIGNAL(setPercentage(float)),
dlink->play,SLOT(setPercentage(float)),Qt::DirectConnection);
dlink->mavlinknode->replay,SLOT(setPercentage(float)),Qt::DirectConnection);
connect(dlink->play,SIGNAL(currentPercentage(float)),
connect(dlink->mavlinknode->replay,SIGNAL(currentPercentage(float)),
toolsui->index2,SLOT(setCurrentPercentage(float)),Qt::DirectConnection);
connect(dlink->play,SIGNAL(replayComplete()),
toolsui->index2,SLOT(replayComplete()),Qt::DirectConnection);
connect(dlink->mavlinknode->replay,SIGNAL(replayComplete()),
toolsui->index2,SLOT(ReplayComplete()),Qt::DirectConnection);
//setting ----- map
connect(setting->index0->mapsetting,SIGNAL(getMapTypes()),
+4 -2
View File
@@ -59,13 +59,15 @@ HEADERS += \
mavlinknode.h \
parameterprocess.h \
missionprocess.h \
mavlinknodeglobal.h
mavlinknodeglobal.h \
replay.h
SOURCES += \
commandprocess.cpp \
mavlinknode.cpp \
parameterprocess.cpp \
missionprocess.cpp
missionprocess.cpp \
replay.cpp
INCLUDEPATH += $$PWD/../thirdpart/include
+91 -18
View File
@@ -9,11 +9,17 @@
MavLinkNode::MavLinkNode(QObject *parent) : QObject(parent)
{
//main thread
QDir *temp = new QDir;
if(!temp->exists("./Tlog"))
{
qDebug() << "make dir tlog";
temp->mkdir("./Tlog");//如果文件夹不存在就新建
}
running_flag = false;
Nodethread = new QThread();
this->moveToThread(Nodethread);
connect(Nodethread, &QThread::started, this, &MavLinkNode::process);
thread = new QThread();
this->moveToThread(thread);
connect(thread, &QThread::started, this, &MavLinkNode::process);
setRunFrq(10);//50
//初始化buff
initbuff();
@@ -22,6 +28,11 @@ MavLinkNode::MavLinkNode(QObject *parent) : QObject(parent)
int Current_sysID = 0xFB;
int Current_CompID = MAV_COMP_ID_MISSIONPLANNER;
replay = new Replay();
connect(replay,SIGNAL(readReady()),
this,SLOT(readPendingDatagramsReplay()),Qt::DirectConnection);
Mission = new MissionProcess();
Mission->setGCSID(Current_sysID,Current_CompID);
connect(Mission,SIGNAL(SendMessageTo(quint8,quint8*,quint16)),
@@ -54,12 +65,26 @@ MavLinkNode::MavLinkNode(QObject *parent) : QObject(parent)
showMessage(tr("解锁才能有航迹,需要修正,示波器显示时间不对"));
}
MavLinkNode::~MavLinkNode()
{
//关闭文件
if (mavLogFile)
{
mavLogFile->close();
delete mavLogFile;
mavLogFile = NULL;
}
//停止回放
if(replay)
{
replay->stop();
delete replay;
replay = nullptr;
}
//停止任务
if(Mission)
{
@@ -70,6 +95,7 @@ MavLinkNode::~MavLinkNode()
Mission = nullptr;
}
}
//停止参数
if(Parameter)
{
@@ -91,14 +117,14 @@ MavLinkNode::~MavLinkNode()
}
}
if(Nodethread->isRunning())
if(thread->isRunning())
{
Nodethread->quit();
Nodethread->wait();
thread->quit();
thread->wait();
}
delete Nodethread;
Nodethread = nullptr;
delete thread;
thread = nullptr;
}
@@ -114,16 +140,31 @@ void MavLinkNode::setRunFrq(uint32_t frq)
void MavLinkNode::start()
{
if(!Nodethread->isRunning())
if(!thread->isRunning())
{
if (mavLogFile)
{
mavLogFile->close();
delete mavLogFile;
}
QDateTime current = QDateTime::currentDateTime();
mavLogFile = new QFile(QString("./Tlog/%1.tlog").arg(current.toString("yyyyMMddHHmmss")));
mavLogFile->open(QIODevice::WriteOnly);
//初始化buff
initbuff();
running_flag = true;
Nodethread->start();
thread->start();
qDebug() << "MavLinkNode thread start" << running_flag;
if(replay)
{
replay->start();
}
if(Mission)
{
Mission->start();
@@ -148,8 +189,15 @@ void MavLinkNode::start()
void MavLinkNode::stop()
{
if(Nodethread->isRunning())
if(thread->isRunning())
{
if (mavLogFile)
{
mavLogFile->close();
delete mavLogFile;
mavLogFile = NULL;
}
running_flag = false;
qDebug() << "thread stop" << running_flag;
}
@@ -189,9 +237,11 @@ void MavLinkNode::process()//线程函数
}
}
running_flag = false;
//退出线程
Nodethread->quit();
Nodethread->wait();
disconnect(thread, nullptr, nullptr, nullptr);
thread->quit();
thread->wait();
}
void MavLinkNode::initbuff(void)
@@ -292,12 +342,19 @@ void MavLinkNode::Mavlinkparse(quint32 src,QByteArray datagram)
mavlink_message_t msg;
mavlink_status_t status;
//qDebug() << datagram;
for (QByteArray::const_iterator i = datagram.cbegin(); i != datagram.cend(); ++i)
{
if(MAVLINK_FRAMING_OK == mavlink_parse_char(src,*i,&msg,&status))
{
uint8_t buff[MAVLINK_MAX_PACKET_LEN+sizeof(quint64)];
quint64 currentTimestamp = ((quint64)QDateTime::currentMSecsSinceEpoch()) * 1000;
qToBigEndian(currentTimestamp, buff);
uint16_t len = mavlink_msg_to_send_buffer(buff+sizeof(quint64), &msg);
if (mavLogFile)
{
mavLogFile->write((const char *)buff, len+sizeof(quint64));
}
count++;
if(msg.sysid < 250) //过滤地面站发过来的数据
{
@@ -534,3 +591,19 @@ void MavLinkNode::setCurrentSelected(int sysid,int compid)
}
void MavLinkNode::readPendingDatagramsReplay(void)
{
if(replay)
{
QByteArray datagram = replay->readAll();
setbuff(SourceType::c_sock,datagram);
}
}
void MavLinkNode::setLogfile(QString file)
{
if(replay)
{
replay->setLogfile(file);
}
}
+12 -13
View File
@@ -3,10 +3,11 @@
#include <QObject>
#include "QThread"
#include "QFile"
#include "QDebug"
#include "mavlink.h"
#include "replay.h"
#include "missionprocess.h"
#include "parameterprocess.h"
#include "commandprocess.h"
@@ -52,23 +53,17 @@ class MavLinkNode : public QObject
mavlink_vfr_hud_t vfr_hud;
}_vehicle;
public:
explicit MavLinkNode(QObject *parent = nullptr);
~MavLinkNode();
_vehicle vehicle;
MissionProcess *Mission;
ParameterProcess *Parameter;
commandprocess *Commander;
//MAVLinkInspector *mavlinkinspector = nullptr;
Replay *replay = nullptr;
MissionProcess *Mission = nullptr;
ParameterProcess *Parameter = nullptr;
commandprocess *Commander = nullptr;
signals:
@@ -103,11 +98,12 @@ public slots:
return running_flag;
}
//缓存对外接口
void readPendingDatagramsReplay(void);
void setbuff(quint32 src, QByteArray data);
void setCurrentSelected(int sysid,int compid);
void setLogfile(QString file);
private slots:
//线程私有接口
@@ -146,7 +142,7 @@ protected:
bool running_flag = false;
quint32 running_frq = 200;//200Hz
QThread *Nodethread;
QThread *thread;
QThread *Parameterthread;
@@ -158,6 +154,9 @@ protected:
_buffdef serial_buff;
_buffdef client_buff;
QFile *mavLogFile = NULL;
};
#endif // MAVLINKNODE_H
+12 -11
View File
@@ -18,17 +18,17 @@ void MissionProcess::setRunFrq(uint32_t frq)
void MissionProcess::start()
{
if(Missionthread == nullptr)
if(thread == nullptr)
{
Missionthread = new QThread();
this->moveToThread(Missionthread);
connect(Missionthread, &QThread::started, this, &MissionProcess::process);
thread = new QThread();
this->moveToThread(thread);
connect(thread, &QThread::started, this, &MissionProcess::process);
}
if(!Missionthread->isRunning())
if(!thread->isRunning())
{
running_flag = true;
Missionthread->start();
thread->start();
qDebug() << "thread start" << running_flag;
}
else
@@ -39,7 +39,7 @@ void MissionProcess::start()
void MissionProcess::stop()
{
if(Missionthread->isRunning())
if(thread->isRunning())
{
running_flag = false;
qDebug() << "thread stop" << running_flag;
@@ -80,10 +80,11 @@ void MissionProcess::process()//线程函数
//qDebug() << count;
}
//退出线程
Missionthread->quit();
Missionthread->wait(200);//等待结束
Missionthread->deleteLater();
Missionthread = nullptr;
disconnect(thread, nullptr, nullptr, nullptr);
thread->quit();
thread->wait();//等待结束
thread->deleteLater();
thread = nullptr;
}
void MissionProcess::setID(int m_sysid,int m_compid)
+5 -1
View File
@@ -141,6 +141,10 @@ private slots:
void write_partial_list(void);
signals:
void readReady();
void showMessage(const QString &message,int TimeOut = 0);
void readError();
@@ -185,7 +189,7 @@ private:
bool running_flag = false;
quint32 running_frq = 200;//200Hz
QThread *Missionthread = nullptr;
QThread *thread = nullptr;
//本机的id
int Current_sysID = 0xF1;
+2 -1
View File
@@ -66,8 +66,9 @@ void ParameterProcess::process()//线程函数
}
//退出线程
disconnect(thread, nullptr, nullptr, nullptr);
thread->quit();
thread->wait(200);//等待结束
thread->wait();//等待结束
thread->deleteLater();
thread = nullptr;
}
+35 -9
View File
@@ -47,14 +47,17 @@ void Replay::start()
{
if(!file)
{
file = new QFile();
file->setFileName(logFile);
if(!file->open(QIODevice::ReadOnly))
{
if(!logFile.isEmpty())
{
QMessageBox::warning(nullptr,
tr("Error"),
tr("%1").arg(file->errorString()));
}
delete file;
file = nullptr;
}
@@ -64,6 +67,11 @@ void Replay::start()
thread->start();
qDebug() << "thread start" << running_flag;
}
}
else
{
qWarning() << "log file can't be fund";
}
}
else
@@ -88,24 +96,41 @@ void Replay::stop()
void Replay::process()//线程函数
{
quint64 lastTimestamp = 0;
while (running_flag)
{
if(isplaying)
{
if(!ispause)//不暂停
{
QThread::msleep(1000/running_frq);
QThread::msleep(5);
//读取数据放到缓存里面
if(file)//已经有文件,那么可以读取
{
if(position <= file->size())
{
buff.clear();
QByteArray raw;
file->seek(position);
buff = file->read(100);
position += 100;
raw = file->read(500);
QByteArray time = raw.mid(raw.indexOf(0xFD) - 8,8);
quint64 currentTimestamp = 0;
union {uint8_t B[8];uint64_t DW;}src;
src.B[0] = time.data()[7];
src.B[1] = time.data()[6];
src.B[2] = time.data()[5];
src.B[3] = time.data()[4];
src.B[4] = time.data()[3];
src.B[5] = time.data()[2];
src.B[6] = time.data()[1];
src.B[7] = time.data()[0];
currentTimestamp = src.DW/1000;
QThread::msleep(currentTimestamp - lastTimestamp);
}
else
{
@@ -113,7 +138,7 @@ void Replay::process()//线程函数
isplaying = false;//换成另外的标志
}
qDebug() << position << file->size() << percentage;
//qDebug() << position << file->size() << percentage;
//读取一帧数据
//将百分比增加,或者位置增加
percentage = (float)position / file->size();
@@ -135,8 +160,9 @@ void Replay::process()//线程函数
}
}
//退出线程
disconnect(thread, nullptr, nullptr, nullptr);
thread->quit();
//thread->wait();
thread->wait();
}
//类相关函数
+6 -14
View File
@@ -8,10 +8,13 @@
#include "QFile"
#include "replay.h"
#include "QDataStream"
#include "mavlink.h"
#include <QtEndian>
#include "QByteArray"
#ifdef QtDlink
#include <dlinkglobal.h>
class DLINKSHARED_EXPORT Replay : public QObject {
#ifdef QtMavlinkNode
#include <mavlinknodeglobal.h>
class MAVLINKNODESHARED_EXPORT Replay : public QObject {
#else
class Replay : public QObject
{
@@ -24,7 +27,6 @@ class Replay : public QObject
QByteArray buff[2];
}_buffdef;
public:
explicit Replay(QObject *parent = nullptr);
~Replay();
@@ -32,8 +34,6 @@ public:
QByteArray readAll(void);
signals:
void showMessage(const QString &message,int TimeOut = 0);
@@ -59,15 +59,10 @@ public slots:
void setPercentage(float value);
void startReplay(bool flag);
private slots:
//线程私有接口
void process();
protected:
bool running_flag = false;
@@ -78,9 +73,6 @@ protected:
QString logFile;
QFile *file = nullptr;
//QDataStream *stream = nullptr;
float percentage = 0.5;
int position = 0;
bool ispause = true;//控制暂停
+3 -113
View File
@@ -1,6 +1,5 @@
#include "dlink.h"
#include <string.h>
//#include "windows.h"
#include "QDateTime"
@@ -8,23 +7,6 @@ DLink::DLink(QObject *parent) : QObject(parent)
{
qDebug() << "Dlink " << QThread::currentThreadId();
QDir *temp = new QDir;
if(!temp->exists("./Tlog"))
{
qDebug() << "make dir tlog";
temp->mkdir("./Tlog");//如果文件夹不存在就新建
}
play = new Replay();
connect(play,&Replay::readReady,
this,&DLink::readPendingDatagramsReplay);
connect(this,SIGNAL(recieveMessage(quint32,QByteArray)),
this,SLOT(record(quint32,QByteArray)));
//采用插件导入的方式(plugin)
@@ -45,23 +27,10 @@ DLink::DLink(QObject *parent) : QObject(parent)
mavlinknode->start();
//其他协议节点。。。
//replay("./Tlog/20200818210716.tlog");
}
DLink::~DLink()
{
stopPort();
//查询所有的接口,然后删除
if (mavLogFile)
{
mavLogFile->close();
delete mavLogFile;
mavLogFile = NULL;
}
mavlinknode->stop();
delete mavlinknode;
@@ -137,18 +106,6 @@ bool DLink::setupPort(const QString port, qint32 baudrate, QSerialPort::Parity p
connect(serialPort, SIGNAL(readyRead()), this, SLOT(readPendingDatagramsSerialPort()));//本线程内调用
isSuccess = true;
if (mavLogFile)
{
mavLogFile->close();
delete mavLogFile;
}
QDateTime current = QDateTime::currentDateTime();
mavLogFile = new QFile(QString("./Tlog/%1.tlog").arg(current.toString("yyyyMMddHHmmss")));
mavLogFile->open(QIODevice::WriteOnly);
emit showMessage(tr("Serial Port Open Success"));
qWarning() << "Serial Port Open Success";
}
@@ -288,17 +245,6 @@ bool DLink::setupClient(const QHostAddress &local_addr,int local_port,const QHos
<< local_port
<< remote_port;
if (mavLogFile)
{
mavLogFile->close();
delete mavLogFile;
}
QDateTime current = QDateTime::currentDateTime();
mavLogFile = new QFile(QString("./Tlog/%1.tlog").arg(current.toString("yyyyMMddHHmmss")));
mavLogFile->open(QIODevice::WriteOnly);
isSuccess = true;
emit showMessage(tr("UdpSocket open"));
}
@@ -308,6 +254,7 @@ bool DLink::setupClient(const QHostAddress &local_addr,int local_port,const QHos
Clientsock->close();
isSuccess = false;
qWarning() << "sock not open";
delete Clientsock;
}
}
else
@@ -335,6 +282,8 @@ void DLink::readPendingDatagramsSerialPort(void)
void DLink::readPendingDatagramsClient(void)
{
//轮番查询
if(Clientsock)
{
while (Clientsock->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(Clientsock->pendingDatagramSize());
@@ -342,66 +291,7 @@ void DLink::readPendingDatagramsClient(void)
emit recieveMessage(SourceType::c_sock,datagram);
}
}
void DLink::readPendingDatagramsReplay(void)
{
if(play)
{
QByteArray datagram = play->readAll();
emit recieveMessage(SourceType::c_sock,datagram);
//qDebug() << "play" << datagram;
}
/*
if(play)
{
QByteArray datagram = play->readAll();
if(serialPort)
{
serialPort->write(datagram);
}
}
*/
}
void DLink::record(quint32 src,QByteArray data)
{
Q_UNUSED(src)
//加时间撮
if (mavLogFile)
{
char buff[sizeof(quint64)];
quint64 currentTimestamp = ((quint64)QDateTime::currentMSecsSinceEpoch()) * 1000;
qToBigEndian(currentTimestamp, buff);
data.append(buff);
//使用数据流的方式可能更好
mavLogFile->write((const char *)data,data.size());
//qDebug() << data;
}
}
void DLink::replay(QString file)
{
//得到文件的路
if(!play)
{
play = new Replay();
connect(play,&Replay::readReady,
this,&DLink::readPendingDatagramsReplay);
}
play->setLogfile(file);
//使用多线程进行读取数据
play->start();//开始线程
}
+4 -16
View File
@@ -8,7 +8,7 @@
#include "QDebug"
#include "mavlinknode.h"
#include "replay.h"
//#include "replay.h"
QT_BEGIN_NAMESPACE
@@ -47,7 +47,7 @@ public:
MavLinkNode *mavlinknode = nullptr;
Replay *play = nullptr;
signals:
void showMessage(const QString &message,int TimeOut = 0);
@@ -72,22 +72,16 @@ public slots:
void readPendingDatagramsClient(void);
void readPendingDatagramsReplay(void);
// void readPendingDatagramsReplay(void);
int SendMessageTo(quint8 ch, quint8 *msg, quint16 len);
void replay(QString file);
//void replay(QString file);
private slots:
void record(quint32 src,QByteArray data);
protected:
@@ -104,12 +98,6 @@ protected:
QList<Node> clientSockets;
QFile *mavLogFile = NULL;
};
+2 -4
View File
@@ -45,12 +45,10 @@ DEFINES += QT_DEPRECATED_WARNINGS
HEADERS += \
$$PWD/dlink.h \
$$PWD/dlinkglobal.h \
$$PWD/replay.h
$$PWD/dlinkglobal.h
SOURCES += \
$$PWD/dlink.cpp \
$$PWD/replay.cpp
$$PWD/dlink.cpp
#加入第三方库
INCLUDEPATH += $$PWD/../thirdpart/include