QGCRemoteControlView.cc 7.29 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 46 47 48
 */

#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),
    channelLayout(new QVBoxLayout()),
    ui(new Ui::QGCRemoteControlView)
{
Bryan Godbolt's avatar
Bryan Godbolt committed
49

lm's avatar
lm committed
50 51 52 53 54 55 56
    //ui->setupUi(this);
    QGridLayout* layout = new QGridLayout(this);
    layout->addLayout(channelLayout, 1, 0, 1, 2);
    // Name label
    nameLabel = new QLabel(this);
    nameLabel->setText("No MAV selected yet..");
    layout->addWidget(nameLabel, 0, 0, 1, 2);
pixhawk's avatar
pixhawk committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    // RSSI bar
    // Create new layout
    QHBoxLayout* rssiLayout = new QHBoxLayout();
    rssiLayout->setSpacing(5);
    // Add content
    rssiLayout->addWidget(new QLabel(tr("Signal"), this));
    // Append raw label
    // Append progress bar
    rssiBar = new QProgressBar(this);
    rssiBar->setMinimum(0);
    rssiBar->setMaximum(100);
    rssiBar->setValue(0);
    rssiLayout->addWidget(rssiBar);
    layout->addItem(rssiLayout, 2, 0, 1, 2);
lm's avatar
lm committed
72 73
    setVisible(false);

74 75 76 77 78
    calibrate = new QPushButton(tr("Calibrate"), this);
    QHBoxLayout *calibrateButtonLayout = new QHBoxLayout();
    calibrateButtonLayout->addWidget(calibrate, 0, Qt::AlignHCenter);
    layout->addItem(calibrateButtonLayout, 3, 0, 1, 2);

79
    calibrationWindow = new RadioCalibrationWindow(this);
80 81
    connect(calibrate, SIGNAL(clicked()), calibrationWindow, SLOT(show()));

lm's avatar
lm committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    connect(UASManager::instance(), SIGNAL(activeUASSet(int)), this, SLOT(setUASId(int)));
}

QGCRemoteControlView::~QGCRemoteControlView()
{
    delete ui;
    delete channelLayout;
}

void QGCRemoteControlView::setUASId(int id)
{
    if (uasId != -1)
    {
        UASInterface* uas = UASManager::instance()->getUASForId(id);
        if (uas)
        {
            // The UAS exists, disconnect any existing connections
99 100 101 102 103
            disconnect(uas, SIGNAL(remoteControlChannelRawChanged(int,float,float)), this, SLOT(setChannel(int,float,float)));
            disconnect(uas, SIGNAL(remoteControlRSSIRawChanged(float)), this, SLOT(setRemoteRSSI(float)));
            disconnect(uas, SIGNAL(radioCalibrationRawReceived(const QPointer<RadioCalibrationData>)), calibrationWindow, SLOT(receive(const QPointer<RadioCalibrationData>&)));
            disconnect(uas, SIGNAL(remoteControlChannelRawChanged(int,float)), calibrationWindow, SLOT(setChannelRaw(int,float)));
            disconnect(uas, SIGNAL(remoteControlChannelScaledChanged(int,float,float)), calibrationWindow, SLOT(setChannelScaled(int,float)));
lm's avatar
lm committed
104 105 106 107 108 109 110 111 112
        }
    }

    // Connect the new UAS
    UASInterface* newUAS = UASManager::instance()->getUASForId(id);
    if (newUAS)
    {
        // New UAS exists, connect
        nameLabel->setText(QString("RC Input of %1").arg(newUAS->getUASName()));
113
        calibrationWindow->setUASId(id);
114
        connect(newUAS, SIGNAL(radioCalibrationReceived(const QPointer<RadioCalibrationData>&)), calibrationWindow, SLOT(receive(const QPointer<RadioCalibrationData>&)));
pixhawk's avatar
pixhawk committed
115
        connect(newUAS, SIGNAL(remoteControlRSSIChanged(float)), this, SLOT(setRemoteRSSI(float)));
116 117 118 119 120 121

        connect(newUAS, SIGNAL(remoteControlChannelRawChanged(int,float)), this, SLOT(setChannelRaw(int,float)));
        connect(newUAS, SIGNAL(remoteControlChannelRawChanged(int,float)), calibrationWindow, SLOT(setChannelRaw(int,float)));

        connect(newUAS, SIGNAL(remoteControlChannelScaledChanged(int,float)), this, SLOT(setChannelScaled(int,float)));
        connect(newUAS, SIGNAL(remoteControlChannelScaledChanged(int,float)), calibrationWindow, SLOT(setChannelScaled(int,float)));
lm's avatar
lm committed
122 123 124
    }
}

125
void QGCRemoteControlView::setChannelRaw(int channelId, float raw)
lm's avatar
lm committed
126
{
127

lm's avatar
lm committed
128 129 130 131
    if (this->raw.size() <= channelId)
    {
        // This is a new channel, append it
        this->raw.append(raw);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        this->normalized.append(0);
        appendChannelWidget(channelId);
    }
    else
    {
        // This is an existing channel, aupdate it
        this->raw[channelId] = raw;
    }
    updated = true;

    // FIXME Will be timer based in the future
    redraw();
}

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

    // FIXME Will be timer based in the future
    redraw();
}

void QGCRemoteControlView::setRemoteRSSI(float rssiNormalized)
{
    rssi = rssiNormalized;
    updated = true;
}

void QGCRemoteControlView::appendChannelWidget(int channelId)
{
    // Create new layout
pixhawk's avatar
pixhawk committed
175
    QHBoxLayout* layout = new QHBoxLayout();
lm's avatar
lm committed
176 177 178 179 180 181 182 183
    // Add content
    layout->addWidget(new QLabel(QString("Channel %1").arg(channelId + 1), this));
    QLabel* raw = new QLabel(this);
    // Append raw label
    rawLabels.append(raw);
    layout->addWidget(raw);
    // Append progress bar
    QProgressBar* normalized = new QProgressBar(this);
184
    normalized->setMinimum(-100);
lm's avatar
lm committed
185
    normalized->setMaximum(100);
186
    normalized->setFormat("%v%");
lm's avatar
lm committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    progressBars.append(normalized);
    layout->addWidget(normalized);
    channelLayout->addLayout(layout);
}

void QGCRemoteControlView::redraw()
{
    if(isVisible() && updated)
    {
        // Update raw values
        for(int i = 0; i < rawLabels.count(); i++)
        {
            rawLabels.at(i)->setText(QString("%1 us").arg(raw.at(i)));
        }

        // Update percent bars
        for(int i = 0; i < progressBars.count(); i++)
        {
            progressBars.at(i)->setValue(normalized.at(i)*100.0f);
        }
        updated = false;
    }
}

void QGCRemoteControlView::changeEvent(QEvent *e)
{
213 214 215 216 217 218 219 220 221 222 223
    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
224
}