Skip to content

Commit

Permalink
Merge pull request #616 from OpenHD/consti-dev
Browse files Browse the repository at this point in the history
Improve terminating (perhaps fix rpi terminate bug)
  • Loading branch information
Consti10 authored Nov 22, 2023
2 parents c859f6f + cffb1c1 commit b6a0445
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 14 deletions.
7 changes: 3 additions & 4 deletions app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,10 @@ int main(int argc, char *argv[]) {
MavlinkTelemetry::instance().start();

QRenderStats::instance().register_to_root_window(engine);

LogMessagesModel::instanceGround().addLogMessage("QOpenHD","running");
const int retval = app.exec();

// Terminating needs a bit of special care due to the singleton usage and threads
qDebug()<<"Terminating";
MavlinkTelemetry::instance().terminate();
return retval;


}
16 changes: 15 additions & 1 deletion app/telemetry/MavlinkTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,27 @@ void MavlinkTelemetry::start()
m_heartbeat_thread=std::make_unique<std::thread>(&MavlinkTelemetry::send_heartbeat_loop,this);
}

MavlinkTelemetry::~MavlinkTelemetry()
void MavlinkTelemetry::terminate()
{
// first stop any incoming telemetry
if(m_heartbeat_thread){
m_heartbeat_thread_run=false;
m_heartbeat_thread->join();
m_heartbeat_thread=nullptr;
}
m_udp_connection=nullptr;
m_tcp_connection=nullptr;
// Cleanup those 2 threads
CmdSender::instance().terminate();
XParam::instance().terminate();
qDebug()<<"MavlinkTelemetry::stopped";
}

MavlinkTelemetry::~MavlinkTelemetry()
{
qDebug()<<"MavlinkTelemetry::~() begin";
terminate();
qDebug()<<"MavlinkTelemetry::~() end";
}

MavlinkTelemetry &MavlinkTelemetry::instance()
Expand Down
3 changes: 3 additions & 0 deletions app/telemetry/MavlinkTelemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class MavlinkTelemetry : public QObject
MavlinkTelemetry(QObject *parent = nullptr);
~MavlinkTelemetry();
static MavlinkTelemetry& instance();
// start / terminate needs a bit of care due to singleton usage,
// not clean, but works
void start();
void terminate();
/**
* Send a message to the OHD ground unit. If no connection has been established (yet), this should return immediately.
* The message can be aimed at either the OHD ground unit, the OHD air unit (forwarded by OpenHD) or the FC connected to the
Expand Down
15 changes: 11 additions & 4 deletions app/telemetry/action/impl/cmdsender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ CmdSender::CmdSender()

CmdSender::~CmdSender()
{
m_timeout_thread_run=false;
m_timeout_thread->join();
m_timeout_thread=nullptr;
terminate();
}

void CmdSender::terminate()
{
if(m_timeout_thread){
m_timeout_thread_run=false;
m_timeout_thread->join();
m_timeout_thread=nullptr;
}
}

CmdSender &CmdSender::instance()
Expand Down Expand Up @@ -178,7 +185,7 @@ void CmdSender::send_mavlink_command_long(const mavlink_command_long_t &cmd)

void CmdSender::loop_timeout()
{
while(true){
while(m_timeout_thread_run){
std::this_thread::sleep_for(std::chrono::milliseconds(100));
handle_timeout();
}
Expand Down
1 change: 1 addition & 0 deletions app/telemetry/action/impl/cmdsender.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CmdSender
public:
CmdSender();
~CmdSender();
void terminate();
static CmdSender& instance();
/**
* returns true if this message has been consumed, false otherwise.
Expand Down
15 changes: 11 additions & 4 deletions app/telemetry/action/impl/xparam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ XParam::XParam()

XParam::~XParam()
{
m_timeout_thread_run=false;
m_timeout_thread->join();
m_timeout_thread=nullptr;
terminate();
}

void XParam::terminate()
{
if(m_timeout_thread){
m_timeout_thread_run=false;
m_timeout_thread->join();
m_timeout_thread=nullptr;
}
}

XParam &XParam::instance()
Expand Down Expand Up @@ -400,7 +407,7 @@ void XParam::update_progress_get_all(const RunningParamCmdGetAll &cmd)

void XParam::loop_timeout()
{
while(true){
while(m_timeout_thread_run){
std::this_thread::sleep_for(std::chrono::milliseconds(100));
check_timeout_param_get_all();
check_timeout_param_set();
Expand Down
1 change: 1 addition & 0 deletions app/telemetry/action/impl/xparam.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class XParam
public:
explicit XParam();
~XParam();
void terminate();
static XParam& instance();
/**
* returns true if this message has been consumed, false otherwise.
Expand Down
4 changes: 3 additions & 1 deletion app/videostreaming/avcodec/avcodec_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void AVCodecDecoder::terminate()
// Wait for everything to cleanup and stop
decode_thread->join();
}
qDebug()<<"Decoding terminated";
}

void AVCodecDecoder::timer_check_settings_changed_callback()
Expand Down Expand Up @@ -158,7 +159,8 @@ void AVCodecDecoder::constant_decode()
}
}
qDebug()<<"Decode stopped,restarting";
std::this_thread::sleep_for(std::chrono::seconds(1));
// wait a bit before starting again, to save CPU
if(!m_should_terminate)std::this_thread::sleep_for(std::chrono::seconds(1));
}
}

Expand Down

0 comments on commit b6a0445

Please sign in to comment.