QGCMAVLinkLogPlayer.cc 6.22 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
#include "LinkManager.h"
13
#include "QGCQFileDialog.h"
14
#include "QGCMessageBox.h"
15

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

24
    // Setup buttons
Don Gagne's avatar
Don Gagne committed
25 26 27 28
    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
29

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

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

43 44
}

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

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

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

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

72
    QString logFilename = QGCQFileDialog::getOpenFileName(
73 74
        this,
        tr("Load MAVLink Log File"),
75
        QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
76
        tr("MAVLink Log Files (*.tlog);;All Files (*)"));
77

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

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

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

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

Don Gagne's avatar
Don Gagne committed
93 94 95 96 97
    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
98

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

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

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

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

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

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

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

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

Don Gagne's avatar
Don Gagne committed
143 144
/// Signalled from LogReplayLink when replay is paused
void QGCMAVLinkLogPlayer::_playbackPaused(void)
145
{
Don Gagne's avatar
Don Gagne committed
146 147
    _ui->playButton->setIcon(QIcon(":/res/Play"));
    _ui->playButton->setChecked(false);
148
}
149

Don Gagne's avatar
Don Gagne committed
150
void QGCMAVLinkLogPlayer::_playbackPercentCompleteChanged(int percentComplete)
151
{
Don Gagne's avatar
Don Gagne committed
152 153 154
    _ui->positionSlider->blockSignals(true);
    _ui->positionSlider->setValue(percentComplete);
    _ui->positionSlider->blockSignals(false);
155 156
}

Don Gagne's avatar
Don Gagne committed
157
void QGCMAVLinkLogPlayer::_setPlayheadFromSlider(int value)
158
{
Don Gagne's avatar
Don Gagne committed
159 160
    if (_replayLink) {
        _replayLink->movePlayhead(value);
161 162
    }
}
163

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

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

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

Don Gagne's avatar
Don Gagne committed
183 184 185 186 187 188 189 190 191 192 193 194
    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
195

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

Don Gagne's avatar
Don Gagne committed
200
void QGCMAVLinkLogPlayer::_replayLinkDisconnected(void)
Don Gagne's avatar
Don Gagne committed
201
{
Don Gagne's avatar
Don Gagne committed
202 203
    _enablePlaybackControls(false);
    _replayLink = NULL;
Don Gagne's avatar
Don Gagne committed
204
}