WaypointView.cc 7.81 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
/*=====================================================================

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 Displays one waypoint
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *   @author Benjamin Knecht <mavteam@student.ethz.ch>
30
 *   @author Petri Tanskanen <mavteam@student.ethz.ch>
pixhawk's avatar
pixhawk committed
31 32 33 34 35 36
 *
 */

#include <QDoubleSpinBox>
#include <QDebug>

pixhawk's avatar
pixhawk committed
37
#include <cmath>    //M_PI
38 39 40
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
pixhawk's avatar
pixhawk committed
41

pixhawk's avatar
pixhawk committed
42 43 44 45 46 47 48 49 50 51
#include "WaypointView.h"
#include "ui_WaypointView.h"

WaypointView::WaypointView(Waypoint* wp, QWidget* parent) :
    QWidget(parent),
    m_ui(new Ui::WaypointView)
{
    m_ui->setupUi(this);

    this->wp = wp;
52 53 54 55 56 57 58 59 60 61 62

    // add actions
    m_ui->comboBox_action->addItem("Navigate",MAV_ACTION_NAVIGATE);
    m_ui->comboBox_action->addItem("TakeOff",MAV_ACTION_TAKEOFF);
    m_ui->comboBox_action->addItem("Land",MAV_ACTION_LAND);
    m_ui->comboBox_action->addItem("Loiter",MAV_ACTION_LOITER);

    // add frames 
    m_ui->comboBox_frame->addItem("Global",MAV_FRAME_GLOBAL);
    m_ui->comboBox_frame->addItem("Local",MAV_FRAME_LOCAL);

pixhawk's avatar
pixhawk committed
63
    // Read values and set user interface
64
    updateValues();
pixhawk's avatar
pixhawk committed
65

66 67 68 69
    // defaults
    //changedAction(wp->getAction());
    //changedFrame(wp->getFrame());

70 71 72 73 74 75 76
    connect(m_ui->posNSpinBox, SIGNAL(valueChanged(double)), wp, SLOT(setX(double)));
    connect(m_ui->posESpinBox, SIGNAL(valueChanged(double)), wp, SLOT(setY(double)));
    connect(m_ui->posDSpinBox, SIGNAL(valueChanged(double)), wp, SLOT(setZ(double)));

    connect(m_ui->lonSpinBox, SIGNAL(valueChanged(double)), wp, SLOT(setX(double)));
    connect(m_ui->latSpinBox, SIGNAL(valueChanged(double)), wp, SLOT(setY(double)));
    connect(m_ui->altSpinBox, SIGNAL(valueChanged(double)), wp, SLOT(setZ(double)));
pixhawk's avatar
pixhawk committed
77 78 79 80

    //hidden degree to radian conversion of the yaw angle
    connect(m_ui->yawSpinBox, SIGNAL(valueChanged(int)), this, SLOT(setYaw(int)));
    connect(this, SIGNAL(setYaw(double)), wp, SLOT(setYaw(double)));
pixhawk's avatar
pixhawk committed
81 82 83 84 85

    connect(m_ui->upButton, SIGNAL(clicked()), this, SLOT(moveUp()));
    connect(m_ui->downButton, SIGNAL(clicked()), this, SLOT(moveDown()));
    connect(m_ui->removeButton, SIGNAL(clicked()), this, SLOT(remove()));

pixhawk's avatar
pixhawk committed
86 87
    connect(m_ui->autoContinue, SIGNAL(stateChanged(int)), this, SLOT(changedAutoContinue(int)));
    connect(m_ui->selectedBox, SIGNAL(stateChanged(int)), this, SLOT(changedCurrent(int)));
88 89
    connect(m_ui->comboBox_action, SIGNAL(activated(int)), this, SLOT(changedAction(int)));
    connect(m_ui->comboBox_frame, SIGNAL(activated(int)), this, SLOT(changedFrame(int)));
pixhawk's avatar
pixhawk committed
90 91 92

    connect(m_ui->orbitSpinBox, SIGNAL(valueChanged(double)), wp, SLOT(setOrbit(double)));
    connect(m_ui->holdTimeSpinBox, SIGNAL(valueChanged(int)), wp, SLOT(setHoldTime(int)));
pixhawk's avatar
pixhawk committed
93 94 95 96 97
}

void WaypointView::setYaw(int yawDegree)
{
    emit setYaw((double)yawDegree*M_PI/180.);
pixhawk's avatar
pixhawk committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
}

void WaypointView::moveUp()
{
    emit moveUpWaypoint(wp);
}

void WaypointView::moveDown()
{
    emit moveDownWaypoint(wp);
}

void WaypointView::remove()
{
    emit removeWaypoint(wp);
    delete this;
}

pixhawk's avatar
pixhawk committed
116
void WaypointView::changedAutoContinue(int state)
pixhawk's avatar
pixhawk committed
117 118
{
    if (state == 0)
119
        wp->setAutocontinue(false);
pixhawk's avatar
pixhawk committed
120
    else
121
        wp->setAutocontinue(true);
pixhawk's avatar
pixhawk committed
122 123
}

124 125 126 127 128 129 130 131 132 133 134 135
void WaypointView::changedAction(int index)
{
    // set action for waypoint


    // hide everything to start
    m_ui->orbitSpinBox->hide();
    m_ui->takeOffAngleSpinBox->hide();
    m_ui->autoContinue->hide();
    m_ui->holdTimeSpinBox->hide();

    // set waypoint action
136
    MAV_ACTION action = (MAV_ACTION) m_ui->comboBox_action->itemData(index).toUInt();
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    wp->setAction(action);

    // expose ui based on action
    switch(action)
    {
    case MAV_ACTION_TAKEOFF:
        m_ui->takeOffAngleSpinBox->show();
        break;
    case MAV_ACTION_LAND:
        break;
    case MAV_ACTION_NAVIGATE:
        m_ui->autoContinue->show();
        break;
    case MAV_ACTION_LOITER:
        m_ui->orbitSpinBox->show();
        m_ui->holdTimeSpinBox->show();
        break;
    default:
        std::cerr << "unknown action" << std::endl;
    }
}

void WaypointView::changedFrame(int index)
{
    // hide everything to start
    m_ui->lonSpinBox->hide();
    m_ui->latSpinBox->hide();
    m_ui->altSpinBox->hide();
    m_ui->posNSpinBox->hide();
    m_ui->posESpinBox->hide();
    m_ui->posDSpinBox->hide();

    // set waypoint action
    MAV_FRAME frame = (MAV_FRAME)m_ui->comboBox_frame->itemData(index).toUInt();
    wp->setFrame(frame);

    switch(frame)
    {
    case MAV_FRAME_GLOBAL:
        m_ui->lonSpinBox->show();
        m_ui->latSpinBox->show();
        m_ui->altSpinBox->show();
        break;
    case MAV_FRAME_LOCAL:
        m_ui->posNSpinBox->show();
        m_ui->posESpinBox->show();
        m_ui->posDSpinBox->show();
        break;
    default:
        std::cerr << "unknown frame" << std::endl;
    }
}

pixhawk's avatar
pixhawk committed
190
void WaypointView::changedCurrent(int state)
pixhawk's avatar
pixhawk committed
191
{
pixhawk's avatar
pixhawk committed
192 193
    if (state == 0)
    {
194 195 196
        //m_ui->selectedBox->setChecked(true);
        //m_ui->selectedBox->setCheckState(Qt::Checked);
        //wp->setCurrent(false);
pixhawk's avatar
pixhawk committed
197 198 199
    }
    else
    {
200
        //wp->setCurrent(true);
201
        emit changeCurrentWaypoint(wp->getId());   //the slot changeCurrentWaypoint() in WaypointList sets all other current flags to false
pixhawk's avatar
pixhawk committed
202
    }
pixhawk's avatar
pixhawk committed
203 204
}

205 206
void WaypointView::updateValues()
{
207 208
    // update frame
    MAV_FRAME frame = wp->getFrame();
209 210
    int frame_index = m_ui->comboBox_frame->findData(frame);
    m_ui->comboBox_frame->setCurrentIndex(frame_index);
211 212 213 214 215 216 217 218 219 220 221 222 223
    switch(frame)
    {
    case(MAV_FRAME_LOCAL):
        m_ui->posNSpinBox->setValue(wp->getX());
        m_ui->posESpinBox->setValue(wp->getY());
        m_ui->posDSpinBox->setValue(wp->getZ());
        break;
    case(MAV_FRAME_GLOBAL):
        m_ui->lonSpinBox->setValue(wp->getX());
        m_ui->latSpinBox->setValue(wp->getY());
        m_ui->altSpinBox->setValue(wp->getZ());
        break;
    }
224
    changedFrame(frame_index);
225 226 227

    // update action
    MAV_ACTION action = wp->getAction();
228 229
    int action_index = m_ui->comboBox_action->findData(action);
    m_ui->comboBox_action->setCurrentIndex(action_index);
230 231 232 233 234 235 236 237 238 239 240 241 242
    switch(action)
    {
    case MAV_ACTION_TAKEOFF:
        break;
    case MAV_ACTION_LAND:
        break;
    case MAV_ACTION_NAVIGATE:
        break;
    case MAV_ACTION_LOITER:
        break;
    default:
        std::cerr << "unknown action" << std::endl;
    }
243
    changedAction(action_index);
244

245 246 247 248 249 250 251 252
    m_ui->yawSpinBox->setValue(wp->getYaw()/M_PI*180.);
    m_ui->selectedBox->setChecked(wp->getCurrent());
    m_ui->autoContinue->setChecked(wp->getAutoContinue());
    m_ui->idLabel->setText(QString("%1").arg(wp->getId()));\
    m_ui->orbitSpinBox->setValue(wp->getOrbit());
    m_ui->holdTimeSpinBox->setValue(wp->getHoldTime());
}

pixhawk's avatar
pixhawk committed
253
void WaypointView::setCurrent(bool state)
pixhawk's avatar
pixhawk committed
254
{
pixhawk's avatar
pixhawk committed
255 256
    if (state)
    {
pixhawk's avatar
pixhawk committed
257
        m_ui->selectedBox->setCheckState(Qt::Checked);
pixhawk's avatar
pixhawk committed
258 259 260 261 262
    }
    else
    {
         m_ui->selectedBox->setCheckState(Qt::Unchecked);
    }
pixhawk's avatar
pixhawk committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
}

WaypointView::~WaypointView()
{
    delete m_ui;
}

void WaypointView::changeEvent(QEvent *e)
{
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}