修正线程占用CPU过大问题
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
//留出一个接口给界面去注册指令
|
||||
//收到ACK后发送一个信号
|
||||
|
||||
commandprocess::commandprocess(QObject *parent) : QObject(parent)
|
||||
commandprocess::commandprocess(QObject *parent) : ThreadTemplet(parent)
|
||||
{
|
||||
setRunFrq(10);
|
||||
|
||||
@@ -12,92 +12,33 @@ commandprocess::commandprocess(QObject *parent) : QObject(parent)
|
||||
|
||||
}
|
||||
|
||||
|
||||
void commandprocess::setRunFrq(uint32_t frq)
|
||||
commandprocess::~commandprocess()
|
||||
{
|
||||
if((frq != 0)||(frq <= 1000))
|
||||
{
|
||||
running_frq = frq;
|
||||
qDebug() << "set command thread running frquency:" <<frq <<"Hz";
|
||||
}
|
||||
}
|
||||
|
||||
void commandprocess::start()
|
||||
{
|
||||
if(thread == nullptr)
|
||||
{
|
||||
thread = new QThread();
|
||||
this->moveToThread(thread);
|
||||
connect(thread, &QThread::started, this, &commandprocess::process);
|
||||
}
|
||||
|
||||
if(!thread->isRunning())
|
||||
{
|
||||
running_flag = true;
|
||||
thread->start();
|
||||
qDebug() << "thread start" << running_flag;
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "thread has started";
|
||||
}
|
||||
}
|
||||
|
||||
void commandprocess::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";
|
||||
}
|
||||
qDebug() << "stop commmand" << QThread::currentThreadId();
|
||||
}
|
||||
|
||||
void commandprocess::process()//线程函数
|
||||
{
|
||||
uint8_t count = 0;
|
||||
|
||||
while (running_flag)
|
||||
qDebug() << "command" << QThread::currentThreadId();
|
||||
|
||||
while (true)
|
||||
{
|
||||
count ++;
|
||||
//QThread::msleep(1000.0/running_frq);
|
||||
|
||||
|
||||
switch(status.m_Mode)
|
||||
{
|
||||
default:
|
||||
case Nop_Mode : QThread::yieldCurrentThread();break;
|
||||
case Nop_Mode : QThread::msleep(1000.0/frq());break;
|
||||
case RecieveMode : ReadStateMachine();QApplication::processEvents();break;
|
||||
case TransmitMode : WriteStateMachine();QApplication::processEvents();break;
|
||||
}
|
||||
|
||||
if(isInterruptionRequested())//退出
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
//退出线程
|
||||
thread->quit();
|
||||
//thread->wait(200);//等待结束
|
||||
thread->deleteLater();
|
||||
thread = nullptr;
|
||||
}
|
||||
|
||||
void commandprocess::setID(int m_sysid,int m_compid)
|
||||
{
|
||||
sysid = (uint8_t)m_sysid;
|
||||
compid = (uint8_t)m_compid;
|
||||
}
|
||||
|
||||
void commandprocess::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);//使用信号和槽
|
||||
}
|
||||
|
||||
//发送函数
|
||||
@@ -130,7 +71,6 @@ void commandprocess::WriteCmd_int(uint8_t m_sysid, uint8_t m_compid ,
|
||||
status.transmit.type = 0;
|
||||
status.m_Mode = TransmitMode;//发送模式
|
||||
|
||||
start();//开启线程
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -161,7 +101,7 @@ void commandprocess::WriteCmd_long(float param1, float param2, float param3, fl
|
||||
//开启线程开始传输
|
||||
status.m_Mode = TransmitMode;//发送模式
|
||||
status.transmit.type = 1;
|
||||
start();//开启线程
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -187,7 +127,7 @@ void commandprocess::Parse(mavlink_message_t msg)
|
||||
case MAVLINK_MSG_ID_COMMAND_ACK:
|
||||
mavlink_msg_command_ack_decode(&msg,&command_ack);
|
||||
|
||||
if(command_ack.target_system != Current_sysID)
|
||||
if(command_ack.target_system != GCS_SysID)
|
||||
{
|
||||
qDebug() << command_ack.target_system << "this msg is'nt mine";
|
||||
//break;//如果目标系统不是自己,那么就抛弃该指令
|
||||
@@ -397,8 +337,8 @@ void commandprocess::_int(float param1,
|
||||
command_int.current = current;
|
||||
command_int.autocontinue = autocontinue;
|
||||
|
||||
mavlink_msg_command_int_encode(Current_sysID,Current_CompID, &msg,&command_int);
|
||||
SendMessage(msg);
|
||||
mavlink_msg_command_int_encode(GCS_SysID,GCS_CompID, &msg,&command_int);
|
||||
Send(msg);
|
||||
}
|
||||
|
||||
void commandprocess::_long(float param1,
|
||||
@@ -427,8 +367,8 @@ void commandprocess::_long(float param1,
|
||||
command_long.command = command;
|
||||
command_long.confirmation = confirmation;
|
||||
|
||||
mavlink_msg_command_long_encode(Current_sysID,Current_CompID, &msg,&command_long);
|
||||
SendMessage(msg);
|
||||
mavlink_msg_command_long_encode(GCS_SysID,GCS_CompID, &msg,&command_long);
|
||||
Send(msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -455,8 +395,8 @@ void commandprocess::ack(uint16_t command,uint8_t result,uint8_t progress,int32_
|
||||
command_ack.progress = progress;
|
||||
command_ack.result_param2 = result_param2;
|
||||
|
||||
mavlink_msg_command_ack_encode(Current_sysID,Current_CompID, &msg,&command_ack);
|
||||
SendMessage(msg);
|
||||
mavlink_msg_command_ack_encode(GCS_SysID,GCS_CompID, &msg,&command_ack);
|
||||
Send(msg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user