UASControlWidget.cc 7.79 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 42
/*=====================================================================

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 <QFileDialog>
#include <QProcess>
#include <QPalette>

#include "UASControlWidget.h"
#include <UASManager.h>
#include <UAS.h>
#include "QGC.h"
43
#include "AutoPilotPlugin.h"
44

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

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

54 55 56 57
    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()));

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

61 62 63
void UASControlWidget::updateModesList()
{
    // Detect autopilot type
64
    int autopilot = MAV_AUTOPILOT_GENERIC;
65 66 67 68 69 70
    if (this->uasID >= 0) {
        UASInterface *uas = UASManager::instance()->getUASForId(this->uasID);
        if (uas) {
            autopilot = UASManager::instance()->getUASForId(this->uasID)->getAutopilotType();
        }
    }
71 72 73 74
    
    AutoPilotPlugin* autopilotPlugin = AutoPilotPlugin::getInstanceForAutoPilotPlugin(autopilot);
    
    _modeList = autopilotPlugin->getModes();
75 76 77

    // Set combobox items
    ui.modeComboBox->clear();
78 79
    foreach (AutoPilotPlugin::FullMode_t fullMode, _modeList) {
        ui.modeComboBox->addItem(UAS::getShortModeTextFor(fullMode.baseMode, fullMode.customMode, autopilot).remove(0, 2));
80
    }
81

82 83 84 85
    // Select first mode in list
    modeIdx = 0;
    ui.modeComboBox->setCurrentIndex(modeIdx);
    ui.modeComboBox->update();
86 87 88 89
}

void UASControlWidget::setUAS(UASInterface* uas)
{
90
    if (this->uasID > 0) {
91
        UASInterface* oldUAS = UASManager::instance()->getUASForId(this->uasID);
92 93 94 95 96 97 98 99 100
        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)));
        }
101 102 103
    }

    // Connect user interface controls
104 105 106 107 108 109 110 111 112 113 114 115 116
    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());
117 118 119 120

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

121 122 123
    } else {
        this->uasID = -1;
    }
124 125 126 127 128 129 130
}

UASControlWidget::~UASControlWidget()
{

}

131
void UASControlWidget::updateArmText()
132
{
133
    if (armed) {
134
        ui.controlButton->setText(tr("DISARM SYSTEM"));
135
    } else {
136
        ui.controlButton->setText(tr("ARM SYSTEM"));
137 138 139 140 141 142 143 144 145 146 147 148 149 150
    }
}

/**
 * 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";
151
    uasColor = uasColor.darker(400);
152 153 154 155 156 157 158 159 160 161
    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);
}


162
void UASControlWidget::updateMode(int uas, QString mode, QString description)
163 164 165 166 167 168 169 170
{
    Q_UNUSED(uas);
    Q_UNUSED(mode);
    Q_UNUSED(description);
}

void UASControlWidget::updateState(int state)
{
171
    switch (state) {
172
    case (int)MAV_STATE_ACTIVE:
173
        armed = true;
174 175
        break;
    case (int)MAV_STATE_STANDBY:
176
        armed = false;
177 178
        break;
    }
179
    this->updateArmText();
180 181
}

182 183 184
/**
 * Called by the button
 */
185 186 187
void UASControlWidget::setMode(int mode)
{
    // Adapt context button mode
188
    modeIdx = mode;
189 190 191
    ui.modeComboBox->blockSignals(true);
    ui.modeComboBox->setCurrentIndex(mode);
    ui.modeComboBox->blockSignals(false);
192 193

    emit changedMode(mode);
194 195 196 197
}

void UASControlWidget::transmitMode()
{
198 199
    UASInterface* uas_iface = UASManager::instance()->getUASForId(this->uasID);
    if (uas_iface) {
200 201
        if (modeIdx >= 0 && modeIdx < _modeList.count()) {
            AutoPilotPlugin::FullMode_t fullMode = _modeList[modeIdx];
202 203
            // include armed state
            if (armed) {
204
                fullMode.baseMode |= MAV_MODE_FLAG_SAFETY_ARMED;
205
            } else {
206
                fullMode.baseMode &= ~MAV_MODE_FLAG_SAFETY_ARMED;
207 208
            }

209 210 211
            UAS* uas = dynamic_cast<UAS*>(uas_iface);

            if (uas->isHilEnabled()) {
212
                fullMode.baseMode |= MAV_MODE_FLAG_HIL_ENABLED;
213
            } else {
214
                fullMode.baseMode &= ~MAV_MODE_FLAG_HIL_ENABLED;
215 216
            }

217
            uas->setMode(fullMode.baseMode, fullMode.customMode);
218 219 220 221
            QString modeText = ui.modeComboBox->currentText();

            ui.lastActionLabel->setText(QString("Sent new mode %1 to %2").arg(modeText).arg(uas->getUASName()));
        }
222 223 224 225 226
    }
}

void UASControlWidget::cycleContextButton()
{
227 228 229 230 231
    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()));
232
        } else {
233 234
            uas->disarmSystem();
            ui.lastActionLabel->setText(QString("Disarm %1").arg(uas->getUASName()));
235 236 237 238
        }
    }
}