Skip to content
QGCMAVLinkLogPlayer.cc 6.84 KiB
Newer Older
#include <QtEndian>
#include "MainWindow.h"
Gus Grubba's avatar
Gus Grubba committed
#ifndef NO_SERIAL_LINK
#include "SerialLink.h"
dogmaphobic's avatar
dogmaphobic committed
#endif
#include "ui_QGCMAVLinkLogPlayer.h"
Don Gagne's avatar
Don Gagne committed
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
#include "SettingsManager.h"
#include "AppSettings.h"
Don Gagne's avatar
Don Gagne committed
#include "LinkManager.h"
#include "QGCMessageBox.h"
DonLakeFlyer's avatar
DonLakeFlyer committed
QGCMAVLinkLogPlayer::QGCMAVLinkLogPlayer(QWidget *parent)
    : QWidget           (parent)
    , _replayLink       (NULL)
    , _lastCurrentTime  (0)
    , _ui               (new Ui::QGCMAVLinkLogPlayer)
Gus Grubba's avatar
Gus Grubba committed
{
Don Gagne's avatar
Don Gagne committed
    _ui->setupUi(this);
    _ui->horizontalLayout->setAlignment(Qt::AlignTop);
Don Gagne's avatar
Don Gagne committed
    connect(_ui->selectFileButton,  &QPushButton::clicked,      this, &QGCMAVLinkLogPlayer::_selectLogFileForPlayback);
    connect(_ui->playButton,        &QPushButton::clicked,      this, &QGCMAVLinkLogPlayer::_playPauseToggle);
    connect(_ui->positionSlider,    &QSlider::valueChanged,     this, &QGCMAVLinkLogPlayer::_setPlayheadFromSlider);
    connect(_ui->positionSlider,    &QSlider::sliderPressed,    this, &QGCMAVLinkLogPlayer::_pause);
Gus Grubba's avatar
Gus Grubba committed

#if 0
    // Speed slider is removed from 3.0 release. Too broken to fix.
    connect(_ui->speedSlider, &QSlider::valueChanged, this, &QGCMAVLinkLogPlayer::_setAccelerationFromSlider);
    _ui->speedSlider->setMinimum(-100);
    _ui->speedSlider->setMaximum(100);
    _ui->speedSlider->setValue(0);
#endif

Don Gagne's avatar
Don Gagne committed
    _enablePlaybackControls(false);
Gus Grubba's avatar
Gus Grubba committed

Don Gagne's avatar
Don Gagne committed
    _ui->positionSlider->setMinimum(0);
    _ui->positionSlider->setMaximum(100);
Gus Grubba's avatar
Gus Grubba committed

Don Gagne's avatar
Don Gagne committed
QGCMAVLinkLogPlayer::~QGCMAVLinkLogPlayer()
Don Gagne's avatar
Don Gagne committed
    delete _ui;
Don Gagne's avatar
Don Gagne committed
void QGCMAVLinkLogPlayer::_playPauseToggle(void)
Don Gagne's avatar
Don Gagne committed
    if (_replayLink->isPlaying()) {
        _pause();
    } else {
        _replayLink->play();
Don Gagne's avatar
Don Gagne committed
void QGCMAVLinkLogPlayer::_pause(void)
Don Gagne's avatar
Don Gagne committed
    _replayLink->pause();
Don Gagne's avatar
Don Gagne committed
void QGCMAVLinkLogPlayer::_selectLogFileForPlayback(void)
Don Gagne's avatar
Don Gagne committed
    // Disallow replay when any links are connected
    if (qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()) {
        QGCMessageBox::information(tr("Log Replay"), tr("You must close all connections prior to replaying a log."));
Don Gagne's avatar
Don Gagne committed
        return;
    }
Gus Grubba's avatar
Gus Grubba committed

    QString logFilename = QGCQFileDialog::getOpenFileName(
Don Gagne's avatar
Don Gagne committed
        tr("Load Telemetry Log File"),
        qgcApp()->toolbox()->settingsManager()->appSettings()->telemetrySavePath(),
        tr("MAVLink Log Files (*.tlog);;All Files (*)"));
Don Gagne's avatar
Don Gagne committed
    if (logFilename.isEmpty()) {
        return;
Gus Grubba's avatar
Gus Grubba committed

Don Gagne's avatar
Don Gagne committed
    LogReplayLinkConfiguration* linkConfig = new LogReplayLinkConfiguration(QString("Log Replay"));
    linkConfig->setLogFilename(logFilename);
    linkConfig->setName(linkConfig->logFilenameShort());
    _ui->logFileNameLabel->setText(linkConfig->logFilenameShort());

    LinkManager* linkMgr = qgcApp()->toolbox()->linkManager();
    SharedLinkConfigurationPointer sharedConfig = linkMgr->addConfiguration(linkConfig);
    _replayLink = (LogReplayLink*)qgcApp()->toolbox()->linkManager()->createConnectedLink(sharedConfig);
Gus Grubba's avatar
Gus Grubba committed

DonLakeFlyer's avatar
DonLakeFlyer committed
    connect(_replayLink, &LogReplayLink::logFileStats,                      this, &QGCMAVLinkLogPlayer::_logFileStats);
    connect(_replayLink, &LogReplayLink::playbackStarted,                   this, &QGCMAVLinkLogPlayer::_playbackStarted);
    connect(_replayLink, &LogReplayLink::playbackPaused,                    this, &QGCMAVLinkLogPlayer::_playbackPaused);
    connect(_replayLink, &LogReplayLink::playbackPercentCompleteChanged,    this, &QGCMAVLinkLogPlayer::_playbackPercentCompleteChanged);
    connect(_replayLink, &LogReplayLink::currentLogTimeSecs,                this, &QGCMAVLinkLogPlayer::_setCurrentLogTime);
    connect(_replayLink, &LogReplayLink::disconnected,                      this, &QGCMAVLinkLogPlayer::_replayLinkDisconnected);
Gus Grubba's avatar
Gus Grubba committed

Don Gagne's avatar
Don Gagne committed
    _ui->positionSlider->setValue(0);
Don Gagne's avatar
Don Gagne committed
    _ui->speedSlider->setValue(0);
Don Gagne's avatar
Don Gagne committed
void QGCMAVLinkLogPlayer::_playbackError(void)
Don Gagne's avatar
Don Gagne committed
    _ui->logFileNameLabel->setText("Error");
    _enablePlaybackControls(false);
Don Gagne's avatar
Don Gagne committed
QString QGCMAVLinkLogPlayer::_secondsToHMS(int seconds)
Don Gagne's avatar
Don Gagne committed
    int secondsPart  = seconds;
    int minutesPart  = secondsPart / 60;
    int hoursPart    = minutesPart / 60;
    secondsPart -= 60 * minutesPart;
    minutesPart -= 60 * hoursPart;
Don Gagne's avatar
Don Gagne committed
    return QString("%1h:%2m:%3s").arg(hoursPart, 2).arg(minutesPart, 2).arg(secondsPart, 2);
Don Gagne's avatar
Don Gagne committed
/// Signalled from LogReplayLink once log file information is known
void QGCMAVLinkLogPlayer::_logFileStats(bool    logTimestamped,         ///< true: timestamped log
                                        int     logDurationSeconds,     ///< Log duration
                                        int     binaryBaudRate)         ///< Baud rate for non-timestamped log
Don Gagne's avatar
Don Gagne committed
    Q_UNUSED(logTimestamped);
    Q_UNUSED(binaryBaudRate);
Gus Grubba's avatar
Gus Grubba committed

    qDebug() << "_logFileStats" << logDurationSeconds;

Don Gagne's avatar
Don Gagne committed
    _logDurationSeconds = logDurationSeconds;
Gus Grubba's avatar
Gus Grubba committed

DonLakeFlyer's avatar
DonLakeFlyer committed
    _ui->logLengthTime->setText(_secondsToHMS(logDurationSeconds));
Don Gagne's avatar
Don Gagne committed
/// Signalled from LogReplayLink when replay starts
void QGCMAVLinkLogPlayer::_playbackStarted(void)
Don Gagne's avatar
Don Gagne committed
    _enablePlaybackControls(true);
    _ui->playButton->setChecked(true);
    _ui->playButton->setIcon(QIcon(":/res/Pause"));
Don Gagne's avatar
Don Gagne committed
    _ui->positionSlider->setEnabled(false);
Don Gagne's avatar
Don Gagne committed
/// Signalled from LogReplayLink when replay is paused
void QGCMAVLinkLogPlayer::_playbackPaused(void)
Don Gagne's avatar
Don Gagne committed
    _ui->playButton->setIcon(QIcon(":/res/Play"));
    _ui->playButton->setChecked(false);
Don Gagne's avatar
Don Gagne committed
    _ui->positionSlider->setEnabled(true);
Don Gagne's avatar
Don Gagne committed
void QGCMAVLinkLogPlayer::_playbackPercentCompleteChanged(int percentComplete)
Don Gagne's avatar
Don Gagne committed
    _ui->positionSlider->blockSignals(true);
    _ui->positionSlider->setValue(percentComplete);
    _ui->positionSlider->blockSignals(false);
Don Gagne's avatar
Don Gagne committed
void QGCMAVLinkLogPlayer::_setPlayheadFromSlider(int value)
Don Gagne's avatar
Don Gagne committed
    if (_replayLink) {
        _replayLink->movePlayhead(value);
Don Gagne's avatar
Don Gagne committed
void QGCMAVLinkLogPlayer::_enablePlaybackControls(bool enabled)
Don Gagne's avatar
Don Gagne committed
    _ui->playButton->setEnabled(enabled);
Don Gagne's avatar
Don Gagne committed
    _ui->speedSlider->setEnabled(enabled);
Don Gagne's avatar
Don Gagne committed
    _ui->positionSlider->setEnabled(enabled);
Don Gagne's avatar
Don Gagne committed
void QGCMAVLinkLogPlayer::_setAccelerationFromSlider(int value)
dogmaphobic's avatar
dogmaphobic committed
    //qDebug() << value;
Don Gagne's avatar
Don Gagne committed
    if (_replayLink) {
        _replayLink->setAccelerationFactor(value);
    }
Gus Grubba's avatar
Gus Grubba committed

Don Gagne's avatar
Don Gagne committed
    // Factor: -100: 0.01x, 0: 1.0x, 100: 100x
Gus Grubba's avatar
Gus Grubba committed

Don Gagne's avatar
Don Gagne committed
    float accelerationFactor;
    if (value < 0) {
        accelerationFactor = 0.01f;
        value -= -100;
        if (value > 0) {
            accelerationFactor *= (float)value;
        }
    } else if (value > 0) {
        accelerationFactor = 1.0f * (float)value;
    } else {
        accelerationFactor = 1.0f;
    }
Gus Grubba's avatar
Gus Grubba committed

Don Gagne's avatar
Don Gagne committed
    _ui->speedLabel->setText(QString("Speed: %1X").arg(accelerationFactor, 5, 'f', 2, '0'));
Don Gagne's avatar
Don Gagne committed
void QGCMAVLinkLogPlayer::_replayLinkDisconnected(void)
Don Gagne's avatar
Don Gagne committed
    _enablePlaybackControls(false);
    _replayLink = NULL;
Don Gagne's avatar
Don Gagne committed
}
DonLakeFlyer's avatar
DonLakeFlyer committed

void QGCMAVLinkLogPlayer::_setCurrentLogTime(int secs)
{
    if (secs != _lastCurrentTime) {
        _lastCurrentTime = secs;
        _ui->logCurrentTime->setText(_secondsToHMS(secs));
    }
}