UASControlWidget.cc 7.58 KB
Newer Older
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*=====================================================================

PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009, 2010 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

This file is part of the PIXHAWK project

    PIXHAWK 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.

    PIXHAWK 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 PIXHAWK. If not, see <http://www.gnu.org/licenses/>.

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

/**
 * @file
 *   @brief Definition of widget controlling one MAV
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#include <QString>
#include <QTimer>
#include <QLabel>
#include <QProcess>
#include <QPalette>

#include "UASControlWidget.h"
#include <UASManager.h>
#include <UAS.h>
#include "QGC.h"
42
#include "AutoPilotPluginManager.h"
43

44
UASControlWidget::UASControlWidget(QWidget *parent) : QWidget(parent),
45 46 47
    uasID(-1),
    modeIdx(0),
    armed(false)
48 49 50
{
    ui.setupUi(this);

51 52
    this->setUAS(UASManager::instance()->getActiveUAS());

53 54 55 56
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setUAS(UASInterface*)));
    connect(ui.modeComboBox, SIGNAL(activated(int)), this, SLOT(setMode(int)));
    connect(ui.setModeButton, SIGNAL(clicked()), this, SLOT(transmitMode()));

57 58
    ui.gridLayout->setAlignment(Qt::AlignTop);
}
59

60 61
void UASControlWidget::updateModesList()
{
62 63
    if (this->uasID == 0) {
        return;
64
    }
65
    
66 67
    UASInterface*uas = UASManager::instance()->getUASForId(this->uasID);
    Q_ASSERT(uas);
68
    
69
    _modeList = AutoPilotPluginManager::instance()->getModes(uas->getAutopilotType());
70 71 72

    // Set combobox items
    ui.modeComboBox->clear();
73 74
    foreach (AutoPilotPluginManager::FullMode_t fullMode, _modeList) {
        ui.modeComboBox->addItem(uas->getShortModeTextFor(fullMode.baseMode, fullMode.customMode).remove(0, 2));
75
    }
76

77 78 79 80
    // Select first mode in list
    modeIdx = 0;
    ui.modeComboBox->setCurrentIndex(modeIdx);
    ui.modeComboBox->update();
81 82 83 84
}

void UASControlWidget::setUAS(UASInterface* uas)
{
85
    if (this->uasID > 0) {
86
        UASInterface* oldUAS = UASManager::instance()->getUASForId(this->uasID);
87 88 89 90 91 92 93 94 95
        if (oldUAS) {
            disconnect(ui.controlButton, SIGNAL(clicked()), oldUAS, SLOT(armSystem()));
            disconnect(ui.liftoffButton, SIGNAL(clicked()), oldUAS, SLOT(launch()));
            disconnect(ui.landButton, SIGNAL(clicked()), oldUAS, SLOT(home()));
            disconnect(ui.shutdownButton, SIGNAL(clicked()), oldUAS, SLOT(shutdown()));
            //connect(ui.setHomeButton, SIGNAL(clicked()), uas, SLOT(setLocalOriginAtCurrentGPSPosition()));
            disconnect(oldUAS, SIGNAL(modeChanged(int,QString,QString)), this, SLOT(updateMode(int, QString, QString)));
            disconnect(oldUAS, SIGNAL(statusChanged(int)), this, SLOT(updateState(int)));
        }
96 97 98
    }

    // Connect user interface controls
99 100 101 102 103 104 105 106 107 108 109 110 111
    if (uas) {
        connect(ui.controlButton, SIGNAL(clicked()), this, SLOT(cycleContextButton()));
        connect(ui.liftoffButton, SIGNAL(clicked()), uas, SLOT(launch()));
        connect(ui.landButton, SIGNAL(clicked()), uas, SLOT(home()));
        connect(ui.shutdownButton, SIGNAL(clicked()), uas, SLOT(shutdown()));
        //connect(ui.setHomeButton, SIGNAL(clicked()), uas, SLOT(setLocalOriginAtCurrentGPSPosition()));
        connect(uas, SIGNAL(modeChanged(int, QString, QString)), this, SLOT(updateMode(int, QString, QString)));
        connect(uas, SIGNAL(statusChanged(int)), this, SLOT(updateState(int)));

        ui.controlStatusLabel->setText(tr("Connected to ") + uas->getUASName());

        this->uasID = uas->getUASID();
        setBackgroundColor(uas->getColor());
112 113 114 115

        this->updateModesList();
        this->updateArmText();

116 117 118
    } else {
        this->uasID = -1;
    }
119 120 121 122 123 124 125
}

UASControlWidget::~UASControlWidget()
{

}

126
void UASControlWidget::updateArmText()
127
{
128
    if (armed) {
129
        ui.controlButton->setText(tr("DISARM SYSTEM"));
130
    } else {
131
        ui.controlButton->setText(tr("ARM SYSTEM"));
132 133 134 135 136 137 138 139 140 141 142 143 144 145
    }
}

/**
 * Set the background color based on the MAV color. If the MAV is selected as the
 * currently actively controlled system, the frame color is highlighted
 */
void UASControlWidget::setBackgroundColor(QColor color)
{
    // UAS color
    QColor uasColor = color;
    QString colorstyle;
    QString borderColor = "#4A4A4F";
    borderColor = "#FA4A4F";
146
    uasColor = uasColor.darker(400);
147 148 149 150 151 152 153 154 155 156
    colorstyle = colorstyle.sprintf("QLabel { border-radius: 3px; padding: 0px; margin: 0px; background-color: #%02X%02X%02X; border: 0px solid %s; }",
                                    uasColor.red(), uasColor.green(), uasColor.blue(), borderColor.toStdString().c_str());
    setStyleSheet(colorstyle);
    QPalette palette = this->palette();
    palette.setBrush(QPalette::Window, QBrush(uasColor));
    setPalette(palette);
    setAutoFillBackground(true);
}


157
void UASControlWidget::updateMode(int uas, QString mode, QString description)
158 159 160 161 162 163 164 165
{
    Q_UNUSED(uas);
    Q_UNUSED(mode);
    Q_UNUSED(description);
}

void UASControlWidget::updateState(int state)
{
166
    switch (state) {
167
    case (int)MAV_STATE_ACTIVE:
168
        armed = true;
169 170
        break;
    case (int)MAV_STATE_STANDBY:
171
        armed = false;
172 173
        break;
    }
174
    this->updateArmText();
175 176
}

177 178 179
/**
 * Called by the button
 */
180 181 182
void UASControlWidget::setMode(int mode)
{
    // Adapt context button mode
183
    modeIdx = mode;
184 185 186
    ui.modeComboBox->blockSignals(true);
    ui.modeComboBox->setCurrentIndex(mode);
    ui.modeComboBox->blockSignals(false);
187 188

    emit changedMode(mode);
189 190 191 192
}

void UASControlWidget::transmitMode()
{
193 194
    UASInterface* uas_iface = UASManager::instance()->getUASForId(this->uasID);
    if (uas_iface) {
195
        if (modeIdx >= 0 && modeIdx < _modeList.count()) {
196
            AutoPilotPluginManager::FullMode_t fullMode = _modeList[modeIdx];
197 198
            // include armed state
            if (armed) {
199
                fullMode.baseMode |= MAV_MODE_FLAG_SAFETY_ARMED;
200
            } else {
201
                fullMode.baseMode &= ~MAV_MODE_FLAG_SAFETY_ARMED;
202 203
            }

204 205
            UAS* uas = dynamic_cast<UAS*>(uas_iface);

206
            if (uas->isHilEnabled() || uas->isHilActive()) {
207
                fullMode.baseMode |= MAV_MODE_FLAG_HIL_ENABLED;
208
            } else {
209
                fullMode.baseMode &= ~MAV_MODE_FLAG_HIL_ENABLED;
210 211
            }

212
            uas->setMode(fullMode.baseMode, fullMode.customMode);
213 214 215 216
            QString modeText = ui.modeComboBox->currentText();

            ui.lastActionLabel->setText(QString("Sent new mode %1 to %2").arg(modeText).arg(uas->getUASName()));
        }
217 218 219 220 221
    }
}

void UASControlWidget::cycleContextButton()
{
222 223 224 225 226
    UAS* uas = dynamic_cast<UAS*>(UASManager::instance()->getUASForId(this->uasID));
    if (uas) {
        if (!armed) {
            uas->armSystem();
            ui.lastActionLabel->setText(QString("Arm %1").arg(uas->getUASName()));
227
        } else {
228 229
            uas->disarmSystem();
            ui.lastActionLabel->setText(QString("Disarm %1").arg(uas->getUASName()));
230 231 232 233
        }
    }
}