QGCMAVLinkLogPlayer.cc 6.12 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

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)
Don Gagne's avatar
Don Gagne 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);
Don Gagne's avatar
Don Gagne 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);
Don Gagne's avatar
Don Gagne committed
39
    
Don Gagne's avatar
Don Gagne committed
40 41
    _ui->positionSlider->setMinimum(0);
    _ui->positionSlider->setMaximum(100);
Don Gagne's avatar
Don Gagne 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 71
        return;
    }
    
Don Gagne's avatar
Don Gagne committed
72
    QString logFilename = QGCFileDialog::getOpenFileName(
73 74
        this,
        tr("Load MAVLink Log File"),
75
        QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
76
        tr("MAVLink Log Files (*.mavlink);;All Files (*)"));
77

Don Gagne's avatar
Don Gagne committed
78 79
    if (logFilename.isEmpty()) {
        return;
80
    }
Don Gagne's avatar
Don Gagne committed
81 82 83 84 85 86 87
    
    LinkInterface* createConnectedLink(LinkConfiguration* config);
    
    LogReplayLinkConfiguration* linkConfig = new LogReplayLinkConfiguration(QString("Log Replay"));
    linkConfig->setLogFilename(logFilename);
    linkConfig->setName(linkConfig->logFilenameShort());
    _ui->logFileNameLabel->setText(linkConfig->logFilenameShort());
88
    _replayLink = (LogReplayLink*)qgcApp()->toolbox()->linkManager()->createConnectedLink(linkConfig);
Don Gagne's avatar
Don Gagne committed
89 90 91 92 93 94 95 96
    
    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);
97
#if 0
Don Gagne's avatar
Don Gagne committed
98
    _ui->speedSlider->setValue(0);
99
#endif
100 101
}

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

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

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

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

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

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

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

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

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

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

Don Gagne's avatar
Don Gagne committed
197
void QGCMAVLinkLogPlayer::_replayLinkDisconnected(void)
Don Gagne's avatar
Don Gagne committed
198
{
Don Gagne's avatar
Don Gagne committed
199 200
    _enablePlaybackControls(false);
    _replayLink = NULL;
Don Gagne's avatar
Don Gagne committed
201
}