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

4
#include "MainWindow.h"
Gus Grubba's avatar
Gus Grubba committed
5
#ifndef NO_SERIAL_LINK
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 13
#include "SettingsManager.h"
#include "AppSettings.h"
Don Gagne's avatar
Don Gagne committed
14
#include "LinkManager.h"
15
#include "QGCQFileDialog.h"
16
#include "QGCMessageBox.h"
17

Don Gagne's avatar
Don Gagne committed
18
QGCMAVLinkLogPlayer::QGCMAVLinkLogPlayer(QWidget *parent) :
19
    QWidget(parent),
Don Gagne's avatar
Don Gagne committed
20 21
    _replayLink(NULL),
    _ui(new Ui::QGCMAVLinkLogPlayer)
Gus Grubba's avatar
Gus Grubba committed
22
{
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
    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
31

32 33 34 35 36 37 38 39
#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
40
    _enablePlaybackControls(false);
Gus Grubba's avatar
Gus Grubba committed
41

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

45 46
}

Don Gagne's avatar
Don Gagne committed
47
QGCMAVLinkLogPlayer::~QGCMAVLinkLogPlayer()
48
{
Don Gagne's avatar
Don Gagne committed
49
    delete _ui;
50 51
}

Don Gagne's avatar
Don Gagne committed
52
void QGCMAVLinkLogPlayer::_playPauseToggle(void)
53
{
Don Gagne's avatar
Don Gagne committed
54 55 56 57
    if (_replayLink->isPlaying()) {
        _pause();
    } else {
        _replayLink->play();
58
    }
59 60
}

Don Gagne's avatar
Don Gagne committed
61
void QGCMAVLinkLogPlayer::_pause(void)
62
{
Don Gagne's avatar
Don Gagne committed
63
    _replayLink->pause();
64 65
}

Don Gagne's avatar
Don Gagne committed
66
void QGCMAVLinkLogPlayer::_selectLogFileForPlayback(void)
Lorenz Meier's avatar
Lorenz Meier committed
67
{
Don Gagne's avatar
Don Gagne committed
68
    // Disallow replay when any links are connected
69
    if (qgcApp()->toolbox()->multiVehicleManager()->activeVehicle()) {
70
        QGCMessageBox::information(tr("Log Replay"), tr("You must close all connections prior to replaying a log."));
Don Gagne's avatar
Don Gagne committed
71 72
        return;
    }
Gus Grubba's avatar
Gus Grubba committed
73

74
    QString logFilename = QGCQFileDialog::getOpenFileName(
75
        this,
Don Gagne's avatar
Don Gagne committed
76 77
        tr("Load Telemetry Log File"),
        qgcApp()->toolbox()->settingsManager()->appSettings()->telemetrySavePath(),
78
        tr("MAVLink Log Files (*.tlog);;All Files (*)"));
79

Don Gagne's avatar
Don Gagne committed
80 81
    if (logFilename.isEmpty()) {
        return;
82
    }
Gus Grubba's avatar
Gus Grubba committed
83

Don Gagne's avatar
Don Gagne committed
84
    LinkInterface* createConnectedLink(LinkConfiguration* config);
Gus Grubba's avatar
Gus Grubba committed
85

Don Gagne's avatar
Don Gagne committed
86 87 88 89
    LogReplayLinkConfiguration* linkConfig = new LogReplayLinkConfiguration(QString("Log Replay"));
    linkConfig->setLogFilename(logFilename);
    linkConfig->setName(linkConfig->logFilenameShort());
    _ui->logFileNameLabel->setText(linkConfig->logFilenameShort());
90 91 92 93

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

Don Gagne's avatar
Don Gagne committed
95 96 97 98 99
    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);
Gus Grubba's avatar
Gus Grubba committed
100

Don Gagne's avatar
Don Gagne committed
101
    _ui->positionSlider->setValue(0);
102
#if 0
Don Gagne's avatar
Don Gagne committed
103
    _ui->speedSlider->setValue(0);
104
#endif
105 106
}

Don Gagne's avatar
Don Gagne committed
107
void QGCMAVLinkLogPlayer::_playbackError(void)
108
{
Don Gagne's avatar
Don Gagne committed
109 110
    _ui->logFileNameLabel->setText("Error");
    _enablePlaybackControls(false);
111 112
}

Don Gagne's avatar
Don Gagne committed
113
QString QGCMAVLinkLogPlayer::_secondsToHMS(int seconds)
114
{
Don Gagne's avatar
Don Gagne committed
115 116 117 118 119
    int secondsPart  = seconds;
    int minutesPart  = secondsPart / 60;
    int hoursPart    = minutesPart / 60;
    secondsPart -= 60 * minutesPart;
    minutesPart -= 60 * hoursPart;
120

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

Don Gagne's avatar
Don Gagne committed
124 125 126 127
/// 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
128
{
Don Gagne's avatar
Don Gagne committed
129 130
    Q_UNUSED(logTimestamped);
    Q_UNUSED(binaryBaudRate);
Gus Grubba's avatar
Gus Grubba committed
131

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

Don Gagne's avatar
Don Gagne committed
134
    _ui->logStatsLabel->setText(_secondsToHMS(logDurationSeconds));
135 136
}

Don Gagne's avatar
Don Gagne committed
137 138
/// Signalled from LogReplayLink when replay starts
void QGCMAVLinkLogPlayer::_playbackStarted(void)
139
{
Don Gagne's avatar
Don Gagne committed
140 141 142
    _enablePlaybackControls(true);
    _ui->playButton->setChecked(true);
    _ui->playButton->setIcon(QIcon(":/res/Pause"));
Don Gagne's avatar
Don Gagne committed
143
    _ui->positionSlider->setEnabled(false);
144
}
145

Don Gagne's avatar
Don Gagne committed
146 147
/// Signalled from LogReplayLink when replay is paused
void QGCMAVLinkLogPlayer::_playbackPaused(void)
148
{
Don Gagne's avatar
Don Gagne committed
149 150
    _ui->playButton->setIcon(QIcon(":/res/Play"));
    _ui->playButton->setChecked(false);
Don Gagne's avatar
Don Gagne committed
151
    _ui->positionSlider->setEnabled(true);
152
}
153

Don Gagne's avatar
Don Gagne committed
154
void QGCMAVLinkLogPlayer::_playbackPercentCompleteChanged(int percentComplete)
155
{
Don Gagne's avatar
Don Gagne committed
156 157 158
    _ui->positionSlider->blockSignals(true);
    _ui->positionSlider->setValue(percentComplete);
    _ui->positionSlider->blockSignals(false);
159 160
}

Don Gagne's avatar
Don Gagne committed
161
void QGCMAVLinkLogPlayer::_setPlayheadFromSlider(int value)
162
{
Don Gagne's avatar
Don Gagne committed
163 164
    if (_replayLink) {
        _replayLink->movePlayhead(value);
165 166
    }
}
167

Don Gagne's avatar
Don Gagne committed
168
void QGCMAVLinkLogPlayer::_enablePlaybackControls(bool enabled)
169
{
Don Gagne's avatar
Don Gagne committed
170
    _ui->playButton->setEnabled(enabled);
171
#if 0
Don Gagne's avatar
Don Gagne committed
172
    _ui->speedSlider->setEnabled(enabled);
173
#endif
Don Gagne's avatar
Don Gagne committed
174
    _ui->positionSlider->setEnabled(enabled);
175
}
Don Gagne's avatar
Don Gagne committed
176

177
#if 0
Don Gagne's avatar
Don Gagne committed
178
void QGCMAVLinkLogPlayer::_setAccelerationFromSlider(int value)
Don Gagne's avatar
Don Gagne committed
179
{
dogmaphobic's avatar
dogmaphobic committed
180
    //qDebug() << value;
Don Gagne's avatar
Don Gagne committed
181 182 183
    if (_replayLink) {
        _replayLink->setAccelerationFactor(value);
    }
Gus Grubba's avatar
Gus Grubba committed
184

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

Don Gagne's avatar
Don Gagne committed
187 188 189 190 191 192 193 194 195 196 197 198
    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
199

Don Gagne's avatar
Don Gagne committed
200
    _ui->speedLabel->setText(QString("Speed: %1X").arg(accelerationFactor, 5, 'f', 2, '0'));
Don Gagne's avatar
Don Gagne committed
201
}
202
#endif
Don Gagne's avatar
Don Gagne committed
203

Don Gagne's avatar
Don Gagne committed
204
void QGCMAVLinkLogPlayer::_replayLinkDisconnected(void)
Don Gagne's avatar
Don Gagne committed
205
{
Don Gagne's avatar
Don Gagne committed
206 207
    _enablePlaybackControls(false);
    _replayLink = NULL;
Don Gagne's avatar
Don Gagne committed
208
}