Waypoint.cc 8.35 KB
Newer Older
1
/*===================================================================
pixhawk's avatar
pixhawk committed
2
QGroundControl Open Source Ground Control Station
pixhawk's avatar
pixhawk committed
3

pixhawk's avatar
pixhawk committed
4
(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
pixhawk's avatar
pixhawk committed
5

pixhawk's avatar
pixhawk committed
6
This file is part of the QGROUNDCONTROL project
pixhawk's avatar
pixhawk committed
7

pixhawk's avatar
pixhawk committed
8
    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
9 10 11
    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's avatar
pixhawk committed
12

pixhawk's avatar
pixhawk committed
13
    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
14 15 16
    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.
pixhawk's avatar
pixhawk committed
17

pixhawk's avatar
pixhawk committed
18
    You should have received a copy of the GNU General Public License
pixhawk's avatar
pixhawk committed
19
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
pixhawk's avatar
pixhawk committed
20

pixhawk's avatar
pixhawk committed
21 22 23 24 25 26 27
======================================================================*/

/**
 * @file
 *   @brief Waypoint class
 *
 *   @author Benjamin Knecht <mavteam@student.ethz.ch>
28
 *   @author Petri Tanskanen <mavteam@student.ethz.ch>
pixhawk's avatar
pixhawk committed
29 30 31
 *
 */

32
#include <QStringList>
lm's avatar
lm committed
33 34 35
#include <QDebug>

#include "Waypoint.h"
pixhawk's avatar
pixhawk committed
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
Waypoint::Waypoint(
    quint16 id,
    double  x,
    double  y,
    double  z,
    double  param1,
    double  param2,
    double  param3,
    double  param4,
    bool    autocontinue,
    bool    current,
    int     frame,
    int     action,
    const QString& description
    )
    : _id(id)
    , _x(x)
    , _y(y)
    , _z(z)
    , _yaw(param4)
    , _frame(frame)
    , _action(action)
    , _autocontinue(autocontinue)
    , _current(current)
    , _orbit(param3)
    , _param1(param1)
    , _param2(param2)
    , _name(QString("WP%1").arg(id, 2, 10, QChar('0')))
    , _description(description)
    , _reachedTime(0)
pixhawk's avatar
pixhawk committed
67
{
68 69 70 71 72 73

}

Waypoint::Waypoint(const Waypoint& other)
{
    *this = other;
pixhawk's avatar
pixhawk committed
74 75 76
}

Waypoint::~Waypoint()
77
{    
pixhawk's avatar
pixhawk committed
78 79
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
const Waypoint& Waypoint::operator=(const Waypoint& other)
{
    _id              = other.getId();
    _x               = other.getX();
    _y               = other.getY();
    _z               = other.getZ();
    _yaw             = other.getYaw();
    _frame           = other.getFrame();
    _action          = other.getAction();
    _autocontinue    = other.getAutoContinue();
    _current         = other.getCurrent();
    _orbit           = other.getParam3();
    _param1          = other.getParam1();
    _param2          = other.getParam2();
    _name            = other.getName();
    _description     = other.getDescription();
    _reachedTime     = other.getReachedTime();
    return *this;
}

lm's avatar
lm committed
100 101
bool Waypoint::isNavigationType()
{
102
    return (_action < MAV_CMD_NAV_LAST);
lm's avatar
lm committed
103 104
}

105 106
void Waypoint::save(QTextStream &saveStream)
{
lm's avatar
lm committed
107
    QString position("%1\t%2\t%3");
108 109 110
    position = position.arg(_x, 0, 'g', 18);
    position = position.arg(_y, 0, 'g', 18);
    position = position.arg(_z, 0, 'g', 18);
lm's avatar
lm committed
111
    QString parameters("%1\t%2\t%3\t%4");
112
    parameters = parameters.arg(_param1, 0, 'g', 18).arg(_param2, 0, 'g', 18).arg(_orbit, 0, 'g', 18).arg(_yaw, 0, 'g', 18);
113
    // FORMAT: <INDEX> <CURRENT WP> <COORD FRAME> <COMMAND> <PARAM1> <PARAM2> <PARAM3> <PARAM4> <PARAM5/X/LONGITUDE> <PARAM6/Y/LATITUDE> <PARAM7/Z/ALTITUDE> <AUTOCONTINUE> <DESCRIPTION>
lm's avatar
lm committed
114
    // as documented here: http://qgroundcontrol.org/waypoint_protocol
115
    saveStream << this->getId() << "\t" << this->getCurrent() << "\t" << this->getFrame() << "\t" << this->getAction() << "\t"  << parameters << "\t" << position  << "\t" << this->getAutoContinue() << "\r\n"; //"\t" << this->getDescription() << "\r\n";
116 117 118 119 120
}

bool Waypoint::load(QTextStream &loadStream)
{
    const QStringList &wpParams = loadStream.readLine().split("\t");
lm's avatar
lm committed
121
    if (wpParams.size() == 12) {
122 123 124 125 126 127 128 129 130 131 132 133 134
        _id = wpParams[0].toInt();
        _current = (wpParams[1].toInt() == 1 ? true : false);
        _frame = (MAV_FRAME) wpParams[2].toInt();
        _action = (MAV_CMD) wpParams[3].toInt();
        _param1 = wpParams[4].toDouble();
        _param2 = wpParams[5].toDouble();
        _orbit = wpParams[6].toDouble();
        _yaw = wpParams[7].toDouble();
        _x = wpParams[8].toDouble();
        _y = wpParams[9].toDouble();
        _z = wpParams[10].toDouble();
        _autocontinue = (wpParams[11].toInt() == 1 ? true : false);
        //_description = wpParams[12];
135 136 137 138 139
        return true;
    }
    return false;
}

140

141
void Waypoint::setId(quint16 id)
pixhawk's avatar
pixhawk committed
142
{
143 144
    _id = id;
    _name = QString("WP%1").arg(id, 2, 10, QChar('0'));
145
    emit changed(this);
pixhawk's avatar
pixhawk committed
146 147
}

pixhawk's avatar
pixhawk committed
148
void Waypoint::setX(double x)
pixhawk's avatar
pixhawk committed
149
{
150
    if (!isinf(x) && !isnan(x) && ((_frame == MAV_FRAME_LOCAL_NED) || (_frame == MAV_FRAME_LOCAL_ENU)))
151
    {
152
        _x = x;
153 154
        emit changed(this);
    }
pixhawk's avatar
pixhawk committed
155 156
}

pixhawk's avatar
pixhawk committed
157
void Waypoint::setY(double y)
pixhawk's avatar
pixhawk committed
158
{
159
    if (!isinf(y) && !isnan(y) && ((_frame == MAV_FRAME_LOCAL_NED) || (_frame == MAV_FRAME_LOCAL_ENU)))
160
    {
161
        _y = y;
162 163
        emit changed(this);
    }
pixhawk's avatar
pixhawk committed
164 165
}

pixhawk's avatar
pixhawk committed
166
void Waypoint::setZ(double z)
pixhawk's avatar
pixhawk committed
167
{
168
    if (!isinf(z) && !isnan(z) && ((_frame == MAV_FRAME_LOCAL_NED) || (_frame == MAV_FRAME_LOCAL_ENU)))
169
    {
170
        _z = z;
171 172
        emit changed(this);
    }
pixhawk's avatar
pixhawk committed
173 174
}

175 176
void Waypoint::setLatitude(double lat)
{
177
    if (_x != lat && ((_frame == MAV_FRAME_GLOBAL) || (_frame == MAV_FRAME_GLOBAL_RELATIVE_ALT)))
178
    {
179
        _x = lat;
180 181 182 183 184 185
        emit changed(this);
    }
}

void Waypoint::setLongitude(double lon)
{
186
    if (_y != lon && ((_frame == MAV_FRAME_GLOBAL) || (_frame == MAV_FRAME_GLOBAL_RELATIVE_ALT)))
187
    {
188
        _y = lon;
189 190 191 192 193 194
        emit changed(this);
    }
}

void Waypoint::setAltitude(double altitude)
{
195
    if (_z != altitude && ((_frame == MAV_FRAME_GLOBAL) || (_frame == MAV_FRAME_GLOBAL_RELATIVE_ALT)))
196
    {
197
        _z = altitude;
198 199 200 201
        emit changed(this);
    }
}

lm's avatar
lm committed
202 203
void Waypoint::setYaw(int yaw)
{
204
    if (_yaw != yaw)
205
    {
206
        _yaw = yaw;
lm's avatar
lm committed
207 208 209 210
        emit changed(this);
    }
}

pixhawk's avatar
pixhawk committed
211
void Waypoint::setYaw(double yaw)
pixhawk's avatar
pixhawk committed
212
{
213
    if (_yaw != yaw)
214
    {
215
        _yaw = yaw;
216 217 218 219
        emit changed(this);
    }
}

220
void Waypoint::setAction(int /*MAV_CMD*/ action)
James Goppert's avatar
James Goppert committed
221
{
222 223
    if (_action != action) {
        _action = action;
224 225 226

        // Flick defaults according to WP type

227
        if (_action == MAV_CMD_NAV_TAKEOFF) {
228
            // We default to 15 degrees minimum takeoff pitch
229
            _param1 = 15.0;
230 231
        }

232 233
        emit changed(this);
    }
234 235
}

236
void Waypoint::setFrame(int /*MAV_FRAME*/ frame)
237
{
238 239
    if (_frame != frame) {
        _frame = frame;
240 241
        emit changed(this);
    }
James Goppert's avatar
James Goppert committed
242 243
}

pixhawk's avatar
pixhawk committed
244 245
void Waypoint::setAutocontinue(bool autoContinue)
{
246 247
    if (_autocontinue != autoContinue) {
        _autocontinue = autoContinue;
248 249
        emit changed(this);
    }
pixhawk's avatar
pixhawk committed
250 251 252 253
}

void Waypoint::setCurrent(bool current)
{
254
    if (_current != current)
255
    {
256
        _current = current;
257 258
        // The current waypoint index is handled by the list
        // and not part of the individual waypoint update state
259
    }
pixhawk's avatar
pixhawk committed
260
}
261

262 263
void Waypoint::setAcceptanceRadius(double radius)
{
264
    if (_param2 != radius)
265
    {
266
        _param2 = radius;
267 268 269 270 271 272
        emit changed(this);
    }
}

void Waypoint::setParam1(double param1)
{
273 274
    //// // qDebug() << "SENDER:" << QObject::sender();
    //// // qDebug() << "PARAM1 SET REQ:" << param1;
275
    if (_param1 != param1)
276
    {
277
        _param1 = param1;
278 279 280 281 282 283
        emit changed(this);
    }
}

void Waypoint::setParam2(double param2)
{
284
    if (_param2 != param2)
285
    {
286
        _param2 = param2;
287 288 289 290 291 292
        emit changed(this);
    }
}

void Waypoint::setParam3(double param3)
{
293 294
    if (_orbit != param3) {
        _orbit = param3;
295 296 297 298 299 300
        emit changed(this);
    }
}

void Waypoint::setParam4(double param4)
{
301 302
    if (_yaw != param4) {
        _yaw = param4;
303 304 305 306 307 308
        emit changed(this);
    }
}

void Waypoint::setParam5(double param5)
{
309 310
    if (_x != param5) {
        _x = param5;
311 312 313 314 315 316
        emit changed(this);
    }
}

void Waypoint::setParam6(double param6)
{
317 318
    if (_y != param6) {
        _y = param6;
319 320 321 322
        emit changed(this);
    }
}

lm's avatar
lm committed
323 324
void Waypoint::setParam7(double param7)
{
325 326
    if (_z != param7) {
        _z = param7;
lm's avatar
lm committed
327 328 329 330
        emit changed(this);
    }
}

331
void Waypoint::setLoiterOrbit(double orbit)
pixhawk's avatar
pixhawk committed
332
{
333 334
    if (_orbit != orbit) {
        _orbit = orbit;
335 336
        emit changed(this);
    }
pixhawk's avatar
pixhawk committed
337 338 339 340
}

void Waypoint::setHoldTime(int holdTime)
{
341 342
    if (_param1 != holdTime) {
        _param1 = holdTime;
343 344 345 346
        emit changed(this);
    }
}

lm's avatar
lm committed
347 348
void Waypoint::setHoldTime(double holdTime)
{
349 350
    if (_param1 != holdTime) {
        _param1 = holdTime;
lm's avatar
lm committed
351 352 353 354
        emit changed(this);
    }
}

355 356
void Waypoint::setTurns(int turns)
{
357 358
    if (_param1 != turns) {
        _param1 = turns;
359 360
        emit changed(this);
    }
pixhawk's avatar
pixhawk committed
361
}