QGCRemoteControlView.cc 7.54 KB
Newer Older
lm's avatar
lm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Implementation of QGCRemoteControlView
 *   @author Lorenz Meier <mail@qgroundcontrol.org>
28
 *   @author Bryan Godbolt <godbolt@ece.ualberta.ca>
lm's avatar
lm committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
 */

#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QProgressBar>
#include "QGCRemoteControlView.h"
#include "ui_QGCRemoteControlView.h"
#include "UASManager.h"

QGCRemoteControlView::QGCRemoteControlView(QWidget *parent) :
    QWidget(parent),
    uasId(-1),
    rssi(0.0f),
    updated(false),
46 47
    channelLayout(new QVBoxLayout()),
    ui(NULL)
lm's avatar
lm committed
48
{
49
    ui->setupUi(this);
lm's avatar
lm committed
50 51 52 53
    QGridLayout* layout = new QGridLayout(this);
    layout->addLayout(channelLayout, 1, 0, 1, 2);
    nameLabel = new QLabel(this);
    layout->addWidget(nameLabel, 0, 0, 1, 2);
pixhawk's avatar
pixhawk committed
54

55 56
    this->setVisible(false);
    //setVisible(false);
lm's avatar
lm committed
57

58 59 60 61 62
    calibrate = new QPushButton(tr("Calibrate"), this);
    QHBoxLayout *calibrateButtonLayout = new QHBoxLayout();
    calibrateButtonLayout->addWidget(calibrate, 0, Qt::AlignHCenter);
    layout->addItem(calibrateButtonLayout, 3, 0, 1, 2);

63
    calibrationWindow = new RadioCalibrationWindow(this);
64 65
    connect(calibrate, SIGNAL(clicked()), calibrationWindow, SLOT(show()));

lm's avatar
lm committed
66
    connect(UASManager::instance(), SIGNAL(activeUASSet(int)), this, SLOT(setUASId(int)));
67 68 69

    connect(&updateTimer, SIGNAL(timeout()), this, SLOT(redraw()));
    updateTimer.start(1500);
lm's avatar
lm committed
70 71 72 73
}

QGCRemoteControlView::~QGCRemoteControlView()
{
74 75 76 77 78 79 80 81
	if(this->ui)
	{
		delete ui;
	}
	if(this->channelLayout)
	{
		delete channelLayout;
	}
lm's avatar
lm committed
82 83 84 85
}

void QGCRemoteControlView::setUASId(int id)
{
86 87 88 89 90
    if (uasId != -1)
    {
        UASInterface* uas = UASManager::instance()->getUASForId(uasId);
        if (uas)
        {
lm's avatar
lm committed
91
            // The UAS exists, disconnect any existing connections
92
            disconnect(uas, SIGNAL(remoteControlChannelRawChanged(int,float,float)), this, SLOT(setChannel(int,float,float)));
93
            disconnect(uas, SIGNAL(remoteControlRSSIChanged(float)), this, SLOT(setRemoteRSSI(float)));
94 95
            disconnect(uas, SIGNAL(radioCalibrationRawReceived(const QPointer<RadioCalibrationData>&)), calibrationWindow, SLOT(receive(const QPointer<RadioCalibrationData>&)));
            disconnect(uas, SIGNAL(remoteControlChannelRawChanged(int,float)), calibrationWindow, SLOT(setChannel(int,float)));
96
            disconnect(uas, SIGNAL(remoteControlChannelScaledChanged(int,float)), this, SLOT(setChannelScaled(int,float)));
lm's avatar
lm committed
97 98 99
        }
    }

100 101 102 103 104 105
    // Clear channel count
    raw.clear();
    raw.resize(0);
    normalized.clear();
    normalized.resize(0);

lm's avatar
lm committed
106 107 108 109 110 111 112 113 114 115 116
    foreach (QLabel* label, rawLabels)
    {
        label->deleteLater();
    }

    foreach(QProgressBar* bar, progressBars)
    {
        bar->deleteLater();
    }

    rawLabels.clear();
117
    rawLabels.resize(0);
lm's avatar
lm committed
118
    progressBars.clear();
119
    progressBars.resize(0);
lm's avatar
lm committed
120

lm's avatar
lm committed
121 122
    // Connect the new UAS
    UASInterface* newUAS = UASManager::instance()->getUASForId(id);
123 124
    if (newUAS)
    {
lm's avatar
lm committed
125 126
        // New UAS exists, connect
        nameLabel->setText(QString("RC Input of %1").arg(newUAS->getUASName()));
127
        calibrationWindow->setUASId(id);
128
        connect(newUAS, SIGNAL(radioCalibrationReceived(const QPointer<RadioCalibrationData>&)), calibrationWindow, SLOT(receive(const QPointer<RadioCalibrationData>&)));
129

130
        connect(newUAS, SIGNAL(remoteControlRSSIChanged(float)), this, SLOT(setRemoteRSSI(float)));
131 132
        connect(newUAS, SIGNAL(remoteControlChannelRawChanged(int,float)), this, SLOT(setChannelRaw(int,float)));
        connect(newUAS, SIGNAL(remoteControlChannelScaledChanged(int,float)), this, SLOT(setChannelScaled(int,float)));
133 134 135

        // only connect raw channels to calibration window widget
        connect(newUAS, SIGNAL(remoteControlChannelRawChanged(int,float)), calibrationWindow, SLOT(setChannel(int,float)));
lm's avatar
lm committed
136 137 138
    }
}

139
void QGCRemoteControlView::setChannelRaw(int channelId, float raw)
lm's avatar
lm committed
140
{
141

142
    if (this->raw.size() <= channelId) {
lm's avatar
lm committed
143 144
        // This is a new channel, append it
        this->raw.append(raw);
145
        //this->normalized.append(0);
146
        appendChannelWidget(channelId);
147
        updated = true;
148
    } else {
149
        // This is an existing channel, aupdate it
150
        if (this->raw[channelId] != raw) updated = true;
151 152 153 154
        this->raw[channelId] = raw;
    }
}

Bryan Godbolt's avatar
Bryan Godbolt committed
155 156
void QGCRemoteControlView::setChannelScaled(int channelId, float normalized)
{
157
    if (this->normalized.size() <= channelId) // using raw vector as size indicator
Bryan Godbolt's avatar
Bryan Godbolt committed
158 159 160 161
    {
        // This is a new channel, append it
        this->normalized.append(normalized);
        appendChannelWidget(channelId);
162
        updated = true;
Bryan Godbolt's avatar
Bryan Godbolt committed
163 164 165 166
    }
    else
    {
        // This is an existing channel, update it
167
        if (this->normalized[channelId] != normalized) updated = true;
Bryan Godbolt's avatar
Bryan Godbolt committed
168 169 170
        this->normalized[channelId] = normalized;
    }
}
lm's avatar
lm committed
171 172 173

void QGCRemoteControlView::setRemoteRSSI(float rssiNormalized)
{
174
    if (rssi != rssiNormalized) updated = true;
lm's avatar
lm committed
175 176 177 178 179 180
    rssi = rssiNormalized;
}

void QGCRemoteControlView::appendChannelWidget(int channelId)
{
    // Create new layout
181
    QHBoxLayout* layout = new QHBoxLayout(this);
lm's avatar
lm committed
182 183 184
    // Add content
    layout->addWidget(new QLabel(QString("Channel %1").arg(channelId + 1), this));
    QLabel* raw = new QLabel(this);
185

lm's avatar
lm committed
186 187 188 189 190
    // Append raw label
    rawLabels.append(raw);
    layout->addWidget(raw);
    // Append progress bar
    QProgressBar* normalized = new QProgressBar(this);
191
    normalized->setMinimum(-100);
lm's avatar
lm committed
192
    normalized->setMaximum(100);
193
    normalized->setFormat("%v%");
lm's avatar
lm committed
194 195 196 197 198 199 200
    progressBars.append(normalized);
    layout->addWidget(normalized);
    channelLayout->addLayout(layout);
}

void QGCRemoteControlView::redraw()
{
201 202
    if(isVisible() && updated)
    {
lm's avatar
lm committed
203
        // Update raw values
204 205
        //for(int i = 0; i < rawLabels.count(); i++)
        //{
206
        //rawLabels.at(i)->setText(QString("%1 us").arg(raw.at(i), 4, 10, QChar('0')));
207
        //}
lm's avatar
lm committed
208 209

        // Update percent bars
210 211
        for(int i = 0; i < progressBars.count(); i++)
        {
212
            rawLabels.at(i)->setText(QString("%1 us").arg(raw.at(i), 4, 10, QChar('0')));
Bryan Godbolt's avatar
Bryan Godbolt committed
213
            int vv = normalized.at(i)*100.0f;
214
            //progressBars.at(i)->setValue(vv);
Bryan Godbolt's avatar
Bryan Godbolt committed
215
//            int vv = raw.at(i)*1.0f;
216
            progressBars.at(i)->setValue(vv);
lm's avatar
lm committed
217
        }
218
        // Update RSSI
219
        if(rssi>0) {
220
            //rssiBar->setValue(rssi);//*100);
221 222
        }

lm's avatar
lm committed
223 224 225 226 227 228
        updated = false;
    }
}

void QGCRemoteControlView::changeEvent(QEvent *e)
{
229 230 231 232 233 234 235 236 237 238 239
    Q_UNUSED(e);
    // FIXME If the lines below are commented in
    // runtime errors can occur on x64 systems.
//    QWidget::changeEvent(e);
//    switch (e->type()) {
//    case QEvent::LanguageChange:
//        //ui->retranslateUi(this);
//        break;
//    default:
//        break;
//    }
lm's avatar
lm committed
240
}