QGCMAVLinkLogPlayer.cc 5.99 KB
Newer Older
1
#include <QStandardPaths>
2
#include <QtEndian>
3

4
#include "MainWindow.h"
dogmaphobic's avatar
dogmaphobic committed
5
#ifndef __ios__
6
#include "SerialLink.h"
dogmaphobic's avatar
dogmaphobic committed
7
#endif
8
#include "QGCMAVLinkLogPlayer.h"
9
#include "QGC.h"
10
#include "ui_QGCMAVLinkLogPlayer.h"
Don Gagne's avatar
Don Gagne committed
11
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
12
#include "LinkManager.h"
Don Gagne's avatar
Don Gagne committed
13
#include "QGCFileDialog.h"
14
#include "QGCMessageBox.h"
15 16

QGCMAVLinkLogPlayer::QGCMAVLinkLogPlayer(MAVLinkProtocol* mavlink, QWidget *parent) :
17
    QWidget(parent),
Don Gagne's avatar
Don Gagne committed
18 19
    _replayLink(NULL),
    _ui(new Ui::QGCMAVLinkLogPlayer)
20
{
Don Gagne's avatar
Don Gagne committed
21 22
    Q_ASSERT(mavlink);
    
Don Gagne's avatar
Don Gagne committed
23 24
    _ui->setupUi(this);
    _ui->horizontalLayout->setAlignment(Qt::AlignTop);
25

26
    // Setup buttons
Don Gagne's avatar
Don Gagne committed
27 28 29 30 31
    connect(_ui->selectFileButton, &QPushButton::clicked, this, &QGCMAVLinkLogPlayer::_selectLogFileForPlayback);
    connect(_ui->playButton, &QPushButton::clicked, this, &QGCMAVLinkLogPlayer::_playPauseToggle);
    connect(_ui->speedSlider, &QSlider::valueChanged, this, &QGCMAVLinkLogPlayer::_setAccelerationFromSlider);
    connect(_ui->positionSlider, &QSlider::valueChanged, this, &QGCMAVLinkLogPlayer::_setPlayheadFromSlider);
    connect(_ui->positionSlider, &QSlider::sliderPressed, this, &QGCMAVLinkLogPlayer::_pause);
Don Gagne's avatar
Don Gagne committed
32
    
Don Gagne's avatar
Don Gagne committed
33
    _enablePlaybackControls(false);
Don Gagne's avatar
Don Gagne committed
34
    
Don Gagne's avatar
Don Gagne committed
35 36
    _ui->positionSlider->setMinimum(0);
    _ui->positionSlider->setMaximum(100);
Don Gagne's avatar
Don Gagne committed
37
    
Don Gagne's avatar
Don Gagne committed
38 39 40
    _ui->speedSlider->setMinimum(-100);
    _ui->speedSlider->setMaximum(100);
    _ui->speedSlider->setValue(0);
41 42
}

Don Gagne's avatar
Don Gagne committed
43
QGCMAVLinkLogPlayer::~QGCMAVLinkLogPlayer()
44
{
Don Gagne's avatar
Don Gagne committed
45
    delete _ui;
46 47
}

Don Gagne's avatar
Don Gagne committed
48
void QGCMAVLinkLogPlayer::_playPauseToggle(void)
49
{
Don Gagne's avatar
Don Gagne committed
50 51 52 53
    if (_replayLink->isPlaying()) {
        _pause();
    } else {
        _replayLink->play();
54
    }
55 56
}

Don Gagne's avatar
Don Gagne committed
57
void QGCMAVLinkLogPlayer::_pause(void)
58
{
Don Gagne's avatar
Don Gagne committed
59
    _replayLink->pause();
60 61
}

Don Gagne's avatar
Don Gagne committed
62
void QGCMAVLinkLogPlayer::_selectLogFileForPlayback(void)
Lorenz Meier's avatar
Lorenz Meier committed
63
{
Don Gagne's avatar
Don Gagne committed
64
    // Disallow replay when any links are connected
65
    if (LinkManager::instance()->anyConnectedLinks()) {
66
        QGCMessageBox::information(tr("Log Replay"), tr("You must close all connections prior to replaying a log."));
Don Gagne's avatar
Don Gagne committed
67 68 69
        return;
    }
    
Don Gagne's avatar
Don Gagne committed
70
    QString logFilename = QGCFileDialog::getOpenFileName(
71 72 73
        this,
        tr("Load MAVLink Log File"),
        qgcApp()->mavlinkLogFilesLocation(),
74
        tr("MAVLink Log Files (*.mavlink);;All Files (*)"));
75

Don Gagne's avatar
Don Gagne committed
76 77
    if (logFilename.isEmpty()) {
        return;
78
    }
Don Gagne's avatar
Don Gagne committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    
    LinkInterface* createConnectedLink(LinkConfiguration* config);
    
    LogReplayLinkConfiguration* linkConfig = new LogReplayLinkConfiguration(QString("Log Replay"));
    linkConfig->setLogFilename(logFilename);
    linkConfig->setName(linkConfig->logFilenameShort());
    _ui->logFileNameLabel->setText(linkConfig->logFilenameShort());
    _replayLink = (LogReplayLink*)LinkManager::instance()->createConnectedLink(linkConfig);
    
    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::disconnected, this, &QGCMAVLinkLogPlayer::_replayLinkDisconnected);
    
    _ui->positionSlider->setValue(0);
    _ui->speedSlider->setValue(0);
96 97
}

Don Gagne's avatar
Don Gagne committed
98
void QGCMAVLinkLogPlayer::_playbackError(void)
99
{
Don Gagne's avatar
Don Gagne committed
100 101
    _ui->logFileNameLabel->setText("Error");
    _enablePlaybackControls(false);
102 103
}

Don Gagne's avatar
Don Gagne committed
104
QString QGCMAVLinkLogPlayer::_secondsToHMS(int seconds)
105
{
Don Gagne's avatar
Don Gagne committed
106 107 108 109 110
    int secondsPart  = seconds;
    int minutesPart  = secondsPart / 60;
    int hoursPart    = minutesPart / 60;
    secondsPart -= 60 * minutesPart;
    minutesPart -= 60 * hoursPart;
111

Don Gagne's avatar
Don Gagne committed
112
    return QString("%1h:%2m:%3s").arg(hoursPart, 2).arg(minutesPart, 2).arg(secondsPart, 2);
113 114
}

Don Gagne's avatar
Don Gagne committed
115 116 117 118
/// 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
119
{
Don Gagne's avatar
Don Gagne committed
120 121 122 123 124 125
    Q_UNUSED(logTimestamped);
    Q_UNUSED(binaryBaudRate);
    
    _logDurationSeconds = logDurationSeconds;
    
    _ui->logStatsLabel->setText(_secondsToHMS(logDurationSeconds));
126 127
}

Don Gagne's avatar
Don Gagne committed
128 129
/// Signalled from LogReplayLink when replay starts
void QGCMAVLinkLogPlayer::_playbackStarted(void)
130
{
Don Gagne's avatar
Don Gagne committed
131 132 133
    _enablePlaybackControls(true);
    _ui->playButton->setChecked(true);
    _ui->playButton->setIcon(QIcon(":/res/Pause"));
134
}
135

Don Gagne's avatar
Don Gagne committed
136 137
/// Signalled from LogReplayLink when replay is paused
void QGCMAVLinkLogPlayer::_playbackPaused(void)
138
{
Don Gagne's avatar
Don Gagne committed
139 140
    _ui->playButton->setIcon(QIcon(":/res/Play"));
    _ui->playButton->setChecked(false);
141
}
142

Don Gagne's avatar
Don Gagne committed
143
void QGCMAVLinkLogPlayer::_playbackPercentCompleteChanged(int percentComplete)
144
{
Don Gagne's avatar
Don Gagne committed
145 146 147
    _ui->positionSlider->blockSignals(true);
    _ui->positionSlider->setValue(percentComplete);
    _ui->positionSlider->blockSignals(false);
148 149
}

Don Gagne's avatar
Don Gagne committed
150
void QGCMAVLinkLogPlayer::_setPlayheadFromSlider(int value)
151
{
Don Gagne's avatar
Don Gagne committed
152 153
    if (_replayLink) {
        _replayLink->movePlayhead(value);
154 155
    }
}
156

Don Gagne's avatar
Don Gagne committed
157
void QGCMAVLinkLogPlayer::_enablePlaybackControls(bool enabled)
158
{
Don Gagne's avatar
Don Gagne committed
159 160 161
    _ui->playButton->setEnabled(enabled);
    _ui->speedSlider->setEnabled(enabled);
    _ui->positionSlider->setEnabled(enabled);
162
}
Don Gagne's avatar
Don Gagne committed
163

Don Gagne's avatar
Don Gagne committed
164
void QGCMAVLinkLogPlayer::_setAccelerationFromSlider(int value)
Don Gagne's avatar
Don Gagne committed
165
{
Don Gagne's avatar
Don Gagne committed
166 167 168 169
    qDebug() << value;
    if (_replayLink) {
        _replayLink->setAccelerationFactor(value);
    }
Don Gagne's avatar
Don Gagne committed
170
    
Don Gagne's avatar
Don Gagne committed
171
    // Factor: -100: 0.01x, 0: 1.0x, 100: 100x
Don Gagne's avatar
Don Gagne committed
172
    
Don Gagne's avatar
Don Gagne committed
173 174 175 176 177 178 179 180 181 182 183 184
    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;
    }
Don Gagne's avatar
Don Gagne committed
185
    
Don Gagne's avatar
Don Gagne committed
186
    _ui->speedLabel->setText(QString("Speed: %1X").arg(accelerationFactor, 5, 'f', 2, '0'));
Don Gagne's avatar
Don Gagne committed
187 188
}

Don Gagne's avatar
Don Gagne committed
189
void QGCMAVLinkLogPlayer::_replayLinkDisconnected(void)
Don Gagne's avatar
Don Gagne committed
190
{
Don Gagne's avatar
Don Gagne committed
191 192 193
    _enablePlaybackControls(false);
    _replayLink = NULL;
}