WaypointView.cc 8.27 KB
Newer Older
pixhawk's avatar
pixhawk committed
1
/*===================================================================
pixhawk's avatar
pixhawk committed
2 3 4 5 6 7 8 9
======================================================================*/

/**
 * @file
 *   @brief Displays one waypoint
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *   @author Benjamin Knecht <mavteam@student.ethz.ch>
10
 *   @author Petri Tanskanen <mavteam@student.ethz.ch>
pixhawk's avatar
pixhawk committed
11 12 13 14 15 16
 *
 */

#include <QDoubleSpinBox>
#include <QDebug>

pixhawk's avatar
pixhawk committed
17
#include <cmath>    //M_PI
18 19 20
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
pixhawk's avatar
pixhawk committed
21

pixhawk's avatar
pixhawk committed
22 23 24 25
#include "WaypointView.h"
#include "ui_WaypointView.h"

WaypointView::WaypointView(Waypoint* wp, QWidget* parent) :
pixhawk's avatar
pixhawk committed
26 27
        QWidget(parent),
        m_ui(new Ui::WaypointView)
pixhawk's avatar
pixhawk committed
28 29 30 31
{
    m_ui->setupUi(this);

    this->wp = wp;
32 33 34 35 36 37 38 39 40 41 42

    // 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
43
    // Read values and set user interface
44
    updateValues();
pixhawk's avatar
pixhawk committed
45

46 47 48 49
    // defaults
    //changedAction(wp->getAction());
    //changedFrame(wp->getFrame());

50 51 52 53 54 55 56
    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
57 58 59 60

    //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
61 62 63 64 65

    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
66 67
    connect(m_ui->autoContinue, SIGNAL(stateChanged(int)), this, SLOT(changedAutoContinue(int)));
    connect(m_ui->selectedBox, SIGNAL(stateChanged(int)), this, SLOT(changedCurrent(int)));
68 69
    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
70 71 72

    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
73 74 75 76 77
}

void WaypointView::setYaw(int yawDegree)
{
    emit setYaw((double)yawDegree*M_PI/180.);
pixhawk's avatar
pixhawk committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

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
96
void WaypointView::changedAutoContinue(int state)
pixhawk's avatar
pixhawk committed
97 98
{
    if (state == 0)
99
        wp->setAutocontinue(false);
pixhawk's avatar
pixhawk committed
100
    else
101
        wp->setAutocontinue(true);
pixhawk's avatar
pixhawk committed
102 103
}

104 105 106 107 108 109 110 111 112 113 114 115
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
116
    MAV_ACTION action = (MAV_ACTION) m_ui->comboBox_action->itemData(index).toUInt();
117 118 119 120 121 122 123 124 125 126 127 128
    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();
pixhawk's avatar
pixhawk committed
129
        m_ui->orbitSpinBox->show();
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        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)
{
    // set waypoint action
    MAV_FRAME frame = (MAV_FRAME)m_ui->comboBox_frame->itemData(index).toUInt();
    wp->setFrame(frame);

    switch(frame)
    {
    case MAV_FRAME_GLOBAL:
pixhawk's avatar
pixhawk committed
149 150 151
        m_ui->posNSpinBox->hide();
        m_ui->posESpinBox->hide();
        m_ui->posDSpinBox->hide();
152 153 154 155 156
        m_ui->lonSpinBox->show();
        m_ui->latSpinBox->show();
        m_ui->altSpinBox->show();
        break;
    case MAV_FRAME_LOCAL:
pixhawk's avatar
pixhawk committed
157 158 159
        m_ui->lonSpinBox->hide();
        m_ui->latSpinBox->hide();
        m_ui->altSpinBox->hide();
160 161 162 163 164 165 166 167 168
        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
169
void WaypointView::changedCurrent(int state)
pixhawk's avatar
pixhawk committed
170
{
pixhawk's avatar
pixhawk committed
171 172
    if (state == 0)
    {
pixhawk's avatar
pixhawk committed
173 174 175
        m_ui->selectedBox->setChecked(true);
        m_ui->selectedBox->setCheckState(Qt::Checked);
        wp->setCurrent(false);
pixhawk's avatar
pixhawk committed
176 177 178
    }
    else
    {
pixhawk's avatar
pixhawk committed
179
        wp->setCurrent(true);
180
        emit changeCurrentWaypoint(wp->getId());   //the slot changeCurrentWaypoint() in WaypointList sets all other current flags to false
pixhawk's avatar
pixhawk committed
181
    }
pixhawk's avatar
pixhawk committed
182 183
}

184 185
void WaypointView::updateValues()
{
186 187
    // Deactivate signals from the WP
    wp->blockSignals(true);
188 189
    // update frame
    MAV_FRAME frame = wp->getFrame();
190
    int frame_index = m_ui->comboBox_frame->findData(frame);
191 192 193 194
    if (m_ui->comboBox_frame->currentIndex() != frame_index)
    {
        m_ui->comboBox_frame->setCurrentIndex(frame_index);
    }
195 196 197
    switch(frame)
    {
    case(MAV_FRAME_LOCAL):
pixhawk's avatar
pixhawk committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211
        {
            if (m_ui->posNSpinBox->value() != wp->getX())
            {
                m_ui->posNSpinBox->setValue(wp->getX());
            }
            if (m_ui->posESpinBox->value() != wp->getY())
            {
                m_ui->posESpinBox->setValue(wp->getY());
            }
            if (m_ui->posDSpinBox->value() != wp->getZ())
            {
                m_ui->posDSpinBox->setValue(wp->getZ());
            }
        }
212 213
        break;
    case(MAV_FRAME_GLOBAL):
pixhawk's avatar
pixhawk committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227
        {
            if (m_ui->lonSpinBox->value() != wp->getX())
            {
                m_ui->lonSpinBox->setValue(wp->getX());
            }
            if (m_ui->latSpinBox->value() != wp->getY())
            {
                m_ui->latSpinBox->setValue(wp->getY());
            }
            if (m_ui->altSpinBox->value() != wp->getZ())
            {
                m_ui->altSpinBox->setValue(wp->getZ());
            }
        }
228 229
        break;
    }
230
    changedFrame(frame_index);
231 232 233

    // update action
    MAV_ACTION action = wp->getAction();
234
    int action_index = m_ui->comboBox_action->findData(action);
pixhawk's avatar
pixhawk committed
235 236 237 238
    if (m_ui->comboBox_action->currentIndex() != action_index)
    {
        m_ui->comboBox_action->setCurrentIndex(action_index);
    }
239 240 241 242 243 244 245 246 247 248 249 250 251
    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;
    }
252
    changedAction(action_index);
253

pixhawk's avatar
pixhawk committed
254 255 256 257
    if (m_ui->yawSpinBox->value() != wp->getYaw()/M_PI*180.)
    {
        m_ui->yawSpinBox->setValue(wp->getYaw()/M_PI*180.);
    }
pixhawk's avatar
pixhawk committed
258 259 260 261 262 263 264 265
    if (m_ui->selectedBox->isChecked() != wp->getCurrent())
    {
        m_ui->selectedBox->setChecked(wp->getCurrent());
    }
    if (m_ui->autoContinue->isChecked() != wp->getAutoContinue())
    {
        m_ui->autoContinue->setChecked(wp->getAutoContinue());
    }
pixhawk's avatar
pixhawk committed
266 267 268 269 270 271 272 273 274
    m_ui->idLabel->setText(QString("%1").arg(wp->getId()));
    if (m_ui->orbitSpinBox->value() != wp->getOrbit())
    {
        m_ui->orbitSpinBox->setValue(wp->getOrbit());
    }
    if (m_ui->holdTimeSpinBox->value() != wp->getHoldTime())
    {
        m_ui->holdTimeSpinBox->setValue(wp->getHoldTime());
    }
275
    wp->blockSignals(false);
276 277
}

pixhawk's avatar
pixhawk committed
278
void WaypointView::setCurrent(bool state)
pixhawk's avatar
pixhawk committed
279
{
pixhawk's avatar
pixhawk committed
280 281
    if (state)
    {
pixhawk's avatar
pixhawk committed
282
        m_ui->selectedBox->setCheckState(Qt::Checked);
pixhawk's avatar
pixhawk committed
283 284 285
    }
    else
    {
pixhawk's avatar
pixhawk committed
286
        m_ui->selectedBox->setCheckState(Qt::Unchecked);
pixhawk's avatar
pixhawk committed
287
    }
pixhawk's avatar
pixhawk committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
}

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

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