UASControlWidget.cc 6.85 KB
Newer Older
pixhawk's avatar
pixhawk 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*=====================================================================

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 <QDebug>
#include <QProcess>

#include <MG.h>
#include "UASControlWidget.h"
#include <UASManager.h>
#include <UAS.h>
//#include <mavlink.h>

lm's avatar
lm committed
45 46 47 48 49
#define CONTROL_MODE_LOCKED "MODE LOCKED"
#define CONTROL_MODE_MANUAL "MODE MANUAL"
#define CONTROL_MODE_GUIDED "MODE GUIDED"
#define CONTROL_MODE_AUTO   "MODE AUTO"
#define CONTROL_MODE_TEST1  "MODE TEST1"
lm's avatar
lm committed
50 51
#define CONTROL_MODE_TEST2  "MODE TEST2"
#define CONTROL_MODE_TEST3  "MODE TEST3"
lm's avatar
lm committed
52 53 54 55 56 57

#define CONTROL_MODE_LOCKED_INDEX 2
#define CONTROL_MODE_MANUAL_INDEX 3
#define CONTROL_MODE_GUIDED_INDEX 4
#define CONTROL_MODE_AUTO_INDEX   5
#define CONTROL_MODE_TEST1_INDEX  6
lm's avatar
lm committed
58 59
#define CONTROL_MODE_TEST2_INDEX  7
#define CONTROL_MODE_TEST3_INDEX  8
lm's avatar
lm committed
60

pixhawk's avatar
pixhawk committed
61
UASControlWidget::UASControlWidget(QWidget *parent) : QWidget(parent),
62 63
        uas(NULL),
        engineOn(false)
pixhawk's avatar
pixhawk committed
64 65 66 67
{
    ui.setupUi(this);

    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(setUAS(UASInterface*)));
lm's avatar
lm committed
68
    ui.modeComboBox->clear();
lm's avatar
lm committed
69 70 71 72 73 74
    ui.modeComboBox->insertItem(0, "Select..");
    ui.modeComboBox->insertItem(CONTROL_MODE_LOCKED_INDEX, CONTROL_MODE_LOCKED);
    ui.modeComboBox->insertItem(CONTROL_MODE_MANUAL_INDEX, CONTROL_MODE_MANUAL);
    ui.modeComboBox->insertItem(CONTROL_MODE_GUIDED_INDEX, CONTROL_MODE_GUIDED);
    ui.modeComboBox->insertItem(CONTROL_MODE_AUTO_INDEX, CONTROL_MODE_AUTO);
    ui.modeComboBox->insertItem(CONTROL_MODE_TEST1_INDEX, CONTROL_MODE_TEST1);
lm's avatar
lm committed
75 76
    ui.modeComboBox->insertItem(CONTROL_MODE_TEST2_INDEX, CONTROL_MODE_TEST2);
    ui.modeComboBox->insertItem(CONTROL_MODE_TEST3_INDEX, CONTROL_MODE_TEST3);
lm's avatar
lm committed
77 78

    ui.modeComboBox->setCurrentIndex(0);
pixhawk's avatar
pixhawk committed
79 80 81 82 83 84 85 86 87 88
}

void UASControlWidget::setUAS(UASInterface* uas)
{
    if (this->uas != NULL)
    {
        disconnect(ui.controlButton, SIGNAL(clicked()), uas, SLOT(enable_motors()));
        disconnect(ui.liftoffButton, SIGNAL(clicked()), uas, SLOT(launch()));
        disconnect(ui.landButton, SIGNAL(clicked()), uas, SLOT(home()));
        disconnect(ui.shutdownButton, SIGNAL(clicked()), uas, SLOT(shutdown()));
89 90
        disconnect(ui.modeComboBox, SIGNAL(activated(int)), this, SLOT(setMode(int)));
        disconnect(ui.setModeButton, SIGNAL(clicked()), this, SLOT(transmitMode()));
pixhawk's avatar
pixhawk committed
91 92 93 94 95 96 97 98 99
    }
    else
    {
        // Connect user interface controls
        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.modeComboBox, SIGNAL(activated(int)), this, SLOT(setMode(int)));
100
        connect(ui.setModeButton, SIGNAL(clicked()), this, SLOT(transmitMode()));
pixhawk's avatar
pixhawk committed
101 102 103 104
        ui.modeComboBox->insertItem(0, "Select..");

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

105 106 107
        connect(uas, SIGNAL(modeChanged(int,QString,QString)), this, SLOT(updateMode(int,QString,QString)));
        connect(uas, SIGNAL(statusChanged(int)), this, SLOT(updateState(int)));

pixhawk's avatar
pixhawk committed
108 109 110 111 112 113 114 115
        this->uas = uas;
    }
}

UASControlWidget::~UASControlWidget() {

}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
void UASControlWidget::updateMode(int uas,QString mode,QString description)
{
    Q_UNUSED(uas);
    Q_UNUSED(mode);
    Q_UNUSED(description);
}

void UASControlWidget::updateState(int state)
{
    switch (state)
    {
    case (int)MAV_STATE_ACTIVE:
        engineOn = true;
        ui.controlButton->setText(tr("Stop Engine"));
        break;
    case (int)MAV_STATE_STANDBY:
        engineOn = false;
        ui.controlButton->setText(tr("Activate Engine"));
        break;
    }
}

pixhawk's avatar
pixhawk committed
138 139
void UASControlWidget::setMode(int mode)
{
140
    // Adapt context button mode
lm's avatar
lm committed
141
    if (mode == CONTROL_MODE_LOCKED_INDEX)
pixhawk's avatar
pixhawk committed
142
    {
lm's avatar
lm committed
143 144
        uasMode = (unsigned int)MAV_MODE_LOCKED;
        ui.modeComboBox->setCurrentIndex(mode);
pixhawk's avatar
pixhawk committed
145
    }
lm's avatar
lm committed
146 147 148 149 150 151
    else if (mode == CONTROL_MODE_MANUAL_INDEX)
    {
        uasMode = (unsigned int)MAV_MODE_MANUAL;
        ui.modeComboBox->setCurrentIndex(mode);
    }
    else if (mode == CONTROL_MODE_GUIDED_INDEX)
152
    {
lm's avatar
lm committed
153
        uasMode = (unsigned int)MAV_MODE_GUIDED;
154 155
        ui.modeComboBox->setCurrentIndex(mode);
    }
lm's avatar
lm committed
156 157 158 159 160 161 162 163 164 165
    else if (mode == CONTROL_MODE_AUTO_INDEX)
    {
        uasMode = (unsigned int)MAV_MODE_AUTO;
        ui.modeComboBox->setCurrentIndex(mode);
    }
    else if (mode == CONTROL_MODE_TEST1_INDEX)
    {
        uasMode = (unsigned int)MAV_MODE_TEST1;
        ui.modeComboBox->setCurrentIndex(mode);
    }
lm's avatar
lm committed
166 167 168 169 170 171 172 173 174 175
    else if (mode == CONTROL_MODE_TEST2_INDEX)
    {
        uasMode = (unsigned int)MAV_MODE_TEST2;
        ui.modeComboBox->setCurrentIndex(mode);
    }
    else if (mode == CONTROL_MODE_TEST3_INDEX)
    {
        uasMode = (unsigned int)MAV_MODE_TEST3;
        ui.modeComboBox->setCurrentIndex(mode);
    }
lm's avatar
lm committed
176 177 178 179 180 181 182 183
    else
    {
        qDebug() << "ERROR! MODE NOT FOUND";
        uasMode = 0;
    }


    qDebug() << "SET MODE REQUESTED" << uasMode;
184 185 186 187
}

void UASControlWidget::transmitMode()
{
lm's avatar
lm committed
188 189 190 191 192
    if (uasMode != 0)
    {
        this->uas->setMode(uasMode);
        ui.lastActionLabel->setText(QString("Set new mode for system %1").arg(uas->getUASName()));
    }
pixhawk's avatar
pixhawk committed
193 194 195 196 197 198 199 200
}

void UASControlWidget::cycleContextButton()
{
    UAS* mav = dynamic_cast<UAS*>(this->uas);
    if (mav)
    {

201
        if (!engineOn)
pixhawk's avatar
pixhawk committed
202 203 204
        {
            ui.controlButton->setText(tr("Stop Engine"));
            mav->enable_motors();
205
            ui.lastActionLabel->setText(QString("Attempted to enable motors on %1").arg(uas->getUASName()));
206 207 208
        }
        else
        {
pixhawk's avatar
pixhawk committed
209 210
            ui.controlButton->setText(tr("Activate Engine"));
            mav->disable_motors();
211
            ui.lastActionLabel->setText(QString("Attempted to disable motors on %1").arg(uas->getUASName()));
pixhawk's avatar
pixhawk committed
212
        }
213 214
            //ui.controlButton->setText(tr("Force Landing"));
            //ui.controlButton->setText(tr("KILL VEHICLE"));
pixhawk's avatar
pixhawk committed
215 216 217 218
    }

}