Skip to content

Commit

Permalink
Use a timer instead of a loop for Mavlink states
Browse files Browse the repository at this point in the history
Loop causes problems at app exit
  • Loading branch information
steveatinfincia committed Jan 13, 2020
1 parent 592a7fe commit 073fe65
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 76 deletions.
1 change: 1 addition & 0 deletions inc/mavlinktelemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private slots:
qint64 last_heartbeat_timestamp;

QMutex stateLock;
QTimer m_stateLoopTimer;

bool m_loading = false;
bool m_saving = false;
Expand Down
135 changes: 59 additions & 76 deletions src/mavlinktelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ void MavlinkTelemetry::init() {
mavlinkSocket->bind(QHostAddress::Any, MAVLINK_LOCAL_PORT);
connect(mavlinkSocket, &QUdpSocket::readyRead, this, &MavlinkTelemetry::processMavlinkDatagrams);

QFuture<void> future = QtConcurrent::run(this, &MavlinkTelemetry::stateLoop);
connect(&m_stateLoopTimer, &QTimer::timeout, this, &MavlinkTelemetry::stateLoop);
resetParamVars();
m_stateLoopTimer.start(200);
}


Expand Down Expand Up @@ -119,87 +121,68 @@ void MavlinkTelemetry::resetParamVars() {


void MavlinkTelemetry::stateLoop() {

resetParamVars();

while (true) {
QThread::msleep(200);

QMutexLocker locker(&stateLock);

qint64 current_timestamp = QDateTime::currentMSecsSinceEpoch();
m_last_heartbeat_raw = current_timestamp - last_heartbeat_timestamp;
set_last_heartbeat(QString(tr("%1ms").arg(m_last_heartbeat_raw)));

switch (state) {
case MavlinkStateDisconnected: {
set_loading(false);
set_saving(false);
if (m_ground_available) {
state = MavlinkStateConnected;
}

continue;
QMutexLocker locker(&stateLock);

qint64 current_timestamp = QDateTime::currentMSecsSinceEpoch();
m_last_heartbeat_raw = current_timestamp - last_heartbeat_timestamp;
set_last_heartbeat(QString(tr("%1ms").arg(m_last_heartbeat_raw)));

switch (state) {
case MavlinkStateDisconnected: {
set_loading(false);
set_saving(false);
if (m_ground_available) {
state = MavlinkStateConnected;
}
case MavlinkStateConnected: {
if (initialConnectTimer == -1) {
initialConnectTimer = QDateTime::currentMSecsSinceEpoch();
} else if (current_timestamp - initialConnectTimer < 5000) {
state = MavlinkStateGetParameters;
resetParamVars();
fetchParameters();
LocalMessage::instance()->showMessage("Connecting to drone", 2);
}

continue;
break;
}
case MavlinkStateConnected: {
if (initialConnectTimer == -1) {
initialConnectTimer = QDateTime::currentMSecsSinceEpoch();
} else if (current_timestamp - initialConnectTimer < 5000) {
state = MavlinkStateGetParameters;
resetParamVars();
fetchParameters();
LocalMessage::instance()->showMessage("Connecting to drone", 2);
}
case MavlinkStateGetParameters: {
set_loading(true);
set_saving(false);
qint64 currentTime = QDateTime::currentMSecsSinceEpoch();

if (isConnectionLost()) {
resetParamVars();
m_ground_available = false;
state = MavlinkStateDisconnected;
LocalMessage::instance()->showMessage("Connection to drone lost (E1)", 4);

continue;
}

if ((parameterCount != 0) && parameterIndex == (parameterCount - 1)) {
emit allParametersChanged();
state = MavlinkStateIdle;

continue;
}


if (currentTime - parameterLastReceivedTime > 7000) {
resetParamVars();
m_ground_available = false;
state = MavlinkStateDisconnected;
LocalMessage::instance()->showMessage("Connection to drone lost (E2)", 4);

continue;
}

continue;
break;
}
case MavlinkStateGetParameters: {
set_loading(true);
set_saving(false);
qint64 currentTime = QDateTime::currentMSecsSinceEpoch();

if (isConnectionLost()) {
resetParamVars();
m_ground_available = false;
state = MavlinkStateDisconnected;
LocalMessage::instance()->showMessage("Connection to drone lost (E1)", 4);
}
case MavlinkStateIdle: {
set_loading(false);

if (isConnectionLost()) {
resetParamVars();
m_ground_available = false;
state = MavlinkStateDisconnected;
LocalMessage::instance()->showMessage("Connection to drone lost (E3)", 4);

continue;
}
if ((parameterCount != 0) && parameterIndex == (parameterCount - 1)) {
emit allParametersChanged();
state = MavlinkStateIdle;
}

break;
if (currentTime - parameterLastReceivedTime > 7000) {
resetParamVars();
m_ground_available = false;
state = MavlinkStateDisconnected;
LocalMessage::instance()->showMessage("Connection to drone lost (E2)", 4);
}
break;
}
case MavlinkStateIdle: {
set_loading(false);

if (isConnectionLost()) {
resetParamVars();
m_ground_available = false;
state = MavlinkStateDisconnected;
LocalMessage::instance()->showMessage("Connection to drone lost (E3)", 4);
}

break;
}
}
}
Expand Down

0 comments on commit 073fe65

Please sign in to comment.