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

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"
39 40
#include "MultiVehicleManager.h"
#include "UAS.h"
41
#include "QGC.h"
42
#include "AutoPilotPluginManager.h"
Don Gagne's avatar
Don Gagne committed
43
#include "FirmwarePluginManager.h"
44

45
UASControlWidget::UASControlWidget(QWidget *parent) : QWidget(parent),
46
    _uas(NULL),
47
    armed(false)
48 49 50
{
    ui.setupUi(this);

51 52 53
    _activeVehicleChanged(MultiVehicleManager::instance()->activeVehicle());
    
    connect(MultiVehicleManager::instance(), &MultiVehicleManager::activeVehicleChanged, this, &UASControlWidget::_activeVehicleChanged);
54 55
    connect(ui.setModeButton, SIGNAL(clicked()), this, SLOT(transmitMode()));

56 57 58 59
    ui.liftoffButton->hide();
    ui.landButton->hide();
    ui.shutdownButton->hide();

60 61
    ui.gridLayout->setAlignment(Qt::AlignTop);
}
62

63 64
void UASControlWidget::updateModesList()
{
65
    if (!_uas) {
66
        return;
67
    }
68
    _modeList = FirmwarePluginManager::instance()->firmwarePluginForAutopilot((MAV_AUTOPILOT)_uas->getAutopilotType())->flightModes();
Don Gagne's avatar
Don Gagne committed
69
    
70 71
    // Set combobox items
    ui.modeComboBox->clear();
Don Gagne's avatar
Don Gagne committed
72 73
    foreach (QString flightMode, _modeList) {
        ui.modeComboBox->addItem(flightMode);
74
    }
75

76
    // Select first mode in list
Don Gagne's avatar
Don Gagne committed
77
    ui.modeComboBox->setCurrentIndex(0);
78
    ui.modeComboBox->update();
79 80
}

81
void UASControlWidget::_activeVehicleChanged(Vehicle* vehicle)
82
{
83 84 85 86 87 88 89
    if (_uas) {
        disconnect(ui.controlButton, SIGNAL(clicked()), _uas, SLOT(armSystem()));
        disconnect(ui.liftoffButton, SIGNAL(clicked()), _uas, SLOT(launch()));
        disconnect(ui.landButton, SIGNAL(clicked()), _uas, SLOT(home()));
        disconnect(ui.shutdownButton, SIGNAL(clicked()), _uas, SLOT(shutdown()));
        disconnect(_uas, SIGNAL(statusChanged(int)), this, SLOT(updateState(int)));
        _uas = NULL;
90 91 92
    }

    // Connect user interface controls
93 94
    if (vehicle) {
        _uas = vehicle->uas();
95
        connect(ui.controlButton, SIGNAL(clicked()), this, SLOT(cycleContextButton()));
96 97 98 99
        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(_uas, SIGNAL(statusChanged(int)), this, SLOT(updateState(int)));
100

101
        ui.controlStatusLabel->setText(tr("Connected to ") + _uas->getUASName());
102

103
        setBackgroundColor(_uas->getColor());
104 105 106

        this->updateModesList();
        this->updateArmText();
107
    }
108 109 110 111 112 113 114
}

UASControlWidget::~UASControlWidget()
{

}

115
void UASControlWidget::updateArmText()
116
{
117
    if (armed) {
118
        ui.controlButton->setText(tr("DISARM SYSTEM"));
119
    } else {
120
        ui.controlButton->setText(tr("ARM SYSTEM"));
121 122 123 124 125 126 127 128 129 130 131 132 133 134
    }
}

/**
 * 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";
135
    uasColor = uasColor.darker(400);
136 137 138 139 140 141 142 143 144 145 146 147
    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);
}


void UASControlWidget::updateState(int state)
{
148
    switch (state) {
149
    case (int)MAV_STATE_ACTIVE:
150
        armed = true;
151 152
        break;
    case (int)MAV_STATE_STANDBY:
153
        armed = false;
154 155
        break;
    }
156
    this->updateArmText();
157 158 159 160
}

void UASControlWidget::transmitMode()
{
161
    if (_uas) {
Don Gagne's avatar
Don Gagne committed
162 163 164 165
        uint8_t     base_mode;
        uint32_t    custom_mode;
        QString     flightMode = ui.modeComboBox->itemText(ui.modeComboBox->currentIndex());
        
166
        if (FirmwarePluginManager::instance()->firmwarePluginForAutopilot((MAV_AUTOPILOT)_uas->getAutopilotType())->setFlightMode(flightMode, &base_mode, &custom_mode)) {
167
            if (armed) {
Don Gagne's avatar
Don Gagne committed
168
                base_mode |= MAV_MODE_FLAG_SAFETY_ARMED;
169
            }
Don Gagne's avatar
Don Gagne committed
170
            
171
            if (_uas->isHilEnabled() || _uas->isHilActive()) {
Don Gagne's avatar
Don Gagne committed
172
                base_mode |= MAV_MODE_FLAG_HIL_ENABLED;
173
            }
Don Gagne's avatar
Don Gagne committed
174
            
175
            _uas->setMode(base_mode, custom_mode);
176
            QString modeText = ui.modeComboBox->currentText();
Don Gagne's avatar
Don Gagne committed
177
            
178
            ui.lastActionLabel->setText(QString("Sent new mode %1 to %2").arg(flightMode).arg(_uas->getUASName()));
179
        }
180 181 182 183 184
    }
}

void UASControlWidget::cycleContextButton()
{
185
    if (_uas) {
186
        if (!armed) {
187 188
            _uas->armSystem();
            ui.lastActionLabel->setText(QString("Arm %1").arg(_uas->getUASName()));
189
        } else {
190 191
            _uas->disarmSystem();
            ui.lastActionLabel->setText(QString("Disarm %1").arg(_uas->getUASName()));
192 193 194
        }
    }
}