Waypoint.cc 8.41 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
Waypoint::Waypoint(
Gus Grubba's avatar
Gus Grubba committed
38
    QObject *parent,
39 40 41 42 43 44 45 46 47 48 49 50 51 52
    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
    )
Gus Grubba's avatar
Gus Grubba committed
53 54
    : QObject(parent)
    , _id(id)
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    , _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
69
{
70 71 72 73

}

Waypoint::Waypoint(const Waypoint& other)
Gus Grubba's avatar
Gus Grubba committed
74
    : QObject(NULL)
75 76
{
    *this = other;
pixhawk's avatar
pixhawk committed
77 78 79
}

Waypoint::~Waypoint()
80
{    
pixhawk's avatar
pixhawk committed
81 82
}

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
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
103 104
bool Waypoint::isNavigationType()
{
105
    return (_action < MAV_CMD_NAV_LAST);
lm's avatar
lm committed
106 107
}

108 109
void Waypoint::save(QTextStream &saveStream)
{
lm's avatar
lm committed
110
    QString position("%1\t%2\t%3");
111 112 113
    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
114
    QString parameters("%1\t%2\t%3\t%4");
115
    parameters = parameters.arg(_param1, 0, 'g', 18).arg(_param2, 0, 'g', 18).arg(_orbit, 0, 'g', 18).arg(_yaw, 0, 'g', 18);
116
    // 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
117
    // as documented here: http://qgroundcontrol.org/waypoint_protocol
118
    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";
119 120 121 122 123
}

bool Waypoint::load(QTextStream &loadStream)
{
    const QStringList &wpParams = loadStream.readLine().split("\t");
lm's avatar
lm committed
124
    if (wpParams.size() == 12) {
125 126 127 128 129 130 131 132 133 134 135 136 137
        _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];
138 139 140 141 142
        return true;
    }
    return false;
}

143

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

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

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

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

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

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

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

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

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

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

        // Flick defaults according to WP type

230
        if (_action == MAV_CMD_NAV_TAKEOFF) {
231
            // We default to 15 degrees minimum takeoff pitch
232
            _param1 = 15.0;
233 234
        }

235 236
        emit changed(this);
    }
237 238
}

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

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

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

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

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

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

void Waypoint::setParam3(double param3)
{
296 297
    if (_orbit != param3) {
        _orbit = param3;
298 299 300 301 302 303
        emit changed(this);
    }
}

void Waypoint::setParam4(double param4)
{
304 305
    if (_yaw != param4) {
        _yaw = param4;
306 307 308 309 310 311
        emit changed(this);
    }
}

void Waypoint::setParam5(double param5)
{
312 313
    if (_x != param5) {
        _x = param5;
314 315 316 317 318 319
        emit changed(this);
    }
}

void Waypoint::setParam6(double param6)
{
320 321
    if (_y != param6) {
        _y = param6;
322 323 324 325
        emit changed(this);
    }
}

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

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

void Waypoint::setHoldTime(int holdTime)
{
344 345
    if (_param1 != holdTime) {
        _param1 = holdTime;
346 347 348 349
        emit changed(this);
    }
}

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

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