修正线程占用CPU过大问题
This commit is contained in:
@@ -3,84 +3,36 @@
|
||||
//留出一个接口给界面去注册指令
|
||||
//收到ACK后发送一个信号
|
||||
|
||||
statusprocess::statusprocess(QObject *parent) : QObject(parent)
|
||||
statusprocess::statusprocess(QObject *parent) : ThreadTemplet(parent)
|
||||
{
|
||||
setRunFrq(1);
|
||||
|
||||
status.m_Mode = _modetype::Nop_Mode;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void statusprocess::setRunFrq(uint32_t frq)
|
||||
statusprocess::~statusprocess()
|
||||
{
|
||||
if((frq != 0)||(frq <= 1000))
|
||||
{
|
||||
running_frq = frq;
|
||||
qDebug() << "set command thread running frquency:" <<frq <<"Hz";
|
||||
}
|
||||
qDebug() << "stop status" << QThread::currentThreadId();
|
||||
status.m_Mode = _modetype::Nop_Mode;
|
||||
|
||||
}
|
||||
|
||||
void statusprocess::start()
|
||||
{
|
||||
if(thread == nullptr)
|
||||
{
|
||||
thread = new QThread();
|
||||
this->moveToThread(thread);
|
||||
connect(thread, &QThread::started, this, &statusprocess::process);
|
||||
}
|
||||
|
||||
if(!thread->isRunning())
|
||||
{
|
||||
running_flag = true;
|
||||
thread->start();
|
||||
qDebug() << "thread start" << running_flag;
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "thread has started";
|
||||
}
|
||||
}
|
||||
|
||||
void statusprocess::stop()
|
||||
{
|
||||
if(thread->isRunning())
|
||||
{
|
||||
running_flag = false;
|
||||
qDebug() << "thread stop"
|
||||
<< QThread::currentThreadId()
|
||||
<< QThread::currentThread()
|
||||
<< "running state:"
|
||||
<< running_flag;
|
||||
|
||||
disconnect(thread, nullptr, nullptr, nullptr);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "thread is not running";
|
||||
}
|
||||
}
|
||||
|
||||
void statusprocess::process()//线程函数
|
||||
{
|
||||
uint8_t count = 0;
|
||||
|
||||
static int time = 0;
|
||||
|
||||
while (running_flag)
|
||||
while (true)
|
||||
{
|
||||
count ++;
|
||||
//QThread::msleep(1000.0/running_frq);
|
||||
|
||||
|
||||
switch(status.m_Mode)
|
||||
{
|
||||
default:
|
||||
case Nop_Mode : QThread::yieldCurrentThread();break;
|
||||
case RecieveMode : ReadStateMachine();QApplication::processEvents();break;
|
||||
case Nop_Mode : QThread::msleep(1000.0/frq());break;
|
||||
case RecieveMode : ReadStateMachine();break;
|
||||
case TransmitMode :
|
||||
{
|
||||
|
||||
if((QTime::currentTime().msecsSinceStartOfDay() - time) > (1000.0/running_frq))
|
||||
{
|
||||
heartbeat(m_heartbeat.custom_mode,
|
||||
@@ -90,36 +42,21 @@ void statusprocess::process()//线程函数
|
||||
m_heartbeat.system_status,
|
||||
m_heartbeat.mavlink_version);
|
||||
time = QTime::currentTime().msecsSinceStartOfDay();
|
||||
QApplication::processEvents();
|
||||
}
|
||||
else
|
||||
{
|
||||
QThread::yieldCurrentThread();
|
||||
QThread::msleep(1000.0/frq());
|
||||
//QThread::yieldCurrentThread();
|
||||
}
|
||||
|
||||
//qDebug() << "heartbeat";
|
||||
}
|
||||
}
|
||||
|
||||
if(isInterruptionRequested())//退出
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
//退出线程
|
||||
disconnect(thread, nullptr, nullptr, nullptr);
|
||||
thread->quit();
|
||||
//thread->wait(200);//等待结束
|
||||
thread->deleteLater();
|
||||
thread = nullptr;
|
||||
}
|
||||
|
||||
void statusprocess::setID(int m_sysid,int m_compid)
|
||||
{
|
||||
sysid = (uint8_t)m_sysid;
|
||||
compid = (uint8_t)m_compid;
|
||||
}
|
||||
|
||||
void statusprocess::SendMessage(mavlink_message_t msg)
|
||||
{
|
||||
uint8_t buff[256+20];
|
||||
uint16_t len = mavlink_msg_to_send_buffer(buff, &msg);
|
||||
emit SendMessageTo(0,buff, len);//使用信号和槽
|
||||
}
|
||||
|
||||
//发送函数
|
||||
@@ -127,8 +64,6 @@ void statusprocess::setHeartbeat(QVariant state,QVariant frq)
|
||||
{
|
||||
//给指令赋值
|
||||
|
||||
qDebug() << state << frq;
|
||||
|
||||
//开启线程开始传输
|
||||
status.transmit.type = 0;
|
||||
|
||||
@@ -138,13 +73,10 @@ void statusprocess::setHeartbeat(QVariant state,QVariant frq)
|
||||
}
|
||||
else
|
||||
{
|
||||
status.m_Mode = _modetype::Nop_Mode;//发送模式
|
||||
status.m_Mode = _modetype::Nop_Mode;//空闲模式
|
||||
}
|
||||
|
||||
setRunFrq(frq.toInt());
|
||||
|
||||
start();//开启线程
|
||||
|
||||
setRunFrq(frq.toFloat());
|
||||
}
|
||||
|
||||
|
||||
@@ -183,8 +115,8 @@ void statusprocess::heartbeat(uint32_t custom_mode,uint8_t type,uint8_t autopilo
|
||||
heartbeat.system_status = system_status;
|
||||
heartbeat.mavlink_version = mavlink_version;
|
||||
|
||||
mavlink_msg_heartbeat_encode(Current_sysID,Current_CompID, &msg,&heartbeat);
|
||||
SendMessage(msg);
|
||||
mavlink_msg_heartbeat_encode(GCS_SysID,GCS_CompID, &msg,&heartbeat);
|
||||
Send(msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user