ComplexMissionItem.cc 12 KB
Newer Older
1 2 3
/*===================================================================
QGroundControl Open Source Ground Control Station

4
(c) 2009, 2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL 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.

    QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

#include "ComplexMissionItem.h"
24 25
#include "JsonHelper.h"
#include "MissionController.h"
26
#include "QGCGeo.h"
27

28 29 30 31 32 33 34
const char* ComplexMissionItem::_jsonVersionKey =       "version";
const char* ComplexMissionItem::_jsonTypeKey =          "type";
const char* ComplexMissionItem::_jsonPolygonKey =       "polygon";
const char* ComplexMissionItem::_jsonIdKey =            "id";
const char* ComplexMissionItem::_jsonGridAltitudeKey =  "gridAltitude";
const char* ComplexMissionItem::_jsonGridAngleKey =     "gridAngle";
const char* ComplexMissionItem::_jsonGridSpacingKey =   "gridSpacing";
35 36

const char* ComplexMissionItem::_complexType = "survey";
37 38

ComplexMissionItem::ComplexMissionItem(Vehicle* vehicle, QObject* parent)
39
    : VisualMissionItem(vehicle, parent)
40
    , _sequenceNumber(0)
41
    , _dirty(false)
42 43 44
    , _gridAltitudeFact (0, "Altitude:",        FactMetaData::valueTypeDouble)
    , _gridAngleFact    (0, "Grid angle:",      FactMetaData::valueTypeDouble)
    , _gridSpacingFact  (0, "Grid spacing:",    FactMetaData::valueTypeDouble)
45
{
46 47
    _gridAltitudeFact.setRawValue(25);
    _gridSpacingFact.setRawValue(5);
48

49 50
    connect(&_gridSpacingFact,  &Fact::valueChanged, this, &ComplexMissionItem::_generateGrid);
    connect(&_gridAngleFact,    &Fact::valueChanged, this, &ComplexMissionItem::_generateGrid);
51 52 53
}

ComplexMissionItem::ComplexMissionItem(const ComplexMissionItem& other, QObject* parent)
54
    : VisualMissionItem(other, parent)
55
    , _sequenceNumber(other.sequenceNumber())
56
    , _dirty(false)
57 58 59 60 61 62
{

}

void ComplexMissionItem::clearPolygon(void)
{
63 64 65 66
    // Bug workaround, see below
    while (_polygonPath.count() > 1) {
        _polygonPath.takeLast();
    }
67
    emit polygonPathChanged();
68 69 70

    // Although this code should remove the polygon from the map it doesn't. There appears
    // to be a bug in MapPolygon which causes it to not be redrawn if the list is empty. So
71
    // we work around it by using the code above to remove all but the last point which in turn
72 73
    // will cause the polygon to go away.
    _polygonPath.clear();
74 75 76

    _clearGrid();

77
    emit specifiesCoordinateChanged();
78 79 80 81 82 83
}

void ComplexMissionItem::addPolygonCoordinate(const QGeoCoordinate coordinate)
{
    _polygonPath << QVariant::fromValue(coordinate);
    emit polygonPathChanged();
84

85 86
    int pointCount = _polygonPath.count();
    if (pointCount == 1) {
87
        setCoordinate(coordinate);
88 89
    } else if (pointCount == 3) {
        emit specifiesCoordinateChanged();
90
    }
91
    _setExitCoordinate(coordinate);
92 93

    _generateGrid();
94
}
95

96
int ComplexMissionItem::lastSequenceNumber(void) const
97
{
98 99 100 101 102 103
    int lastSeq = _sequenceNumber;

    if (_gridPoints.count()) {
        lastSeq += _gridPoints.count() - 1;
    }
    return lastSeq;
104 105 106 107 108 109 110
}

void ComplexMissionItem::setCoordinate(const QGeoCoordinate& coordinate)
{
    if (_coordinate != coordinate) {
        _coordinate = coordinate;
        emit coordinateChanged(_coordinate);
111
        _setExitCoordinate(coordinate);
112 113 114 115 116
    }
}

void ComplexMissionItem::setDirty(bool dirty)
{
117 118 119 120
    if (_dirty != dirty) {
        _dirty = dirty;
        emit dirtyChanged(_dirty);
    }
121 122
}

123 124
void ComplexMissionItem::save(QJsonObject& saveObject) const
{
125 126 127 128 129 130
    saveObject[_jsonVersionKey] =       1;
    saveObject[_jsonTypeKey] =          _complexType;
    saveObject[_jsonIdKey] =            sequenceNumber();
    saveObject[_jsonGridAltitudeKey] =  _gridAltitudeFact.rawValue().toDouble();
    saveObject[_jsonGridAngleKey] =     _gridAngleFact.rawValue().toDouble();
    saveObject[_jsonGridSpacingKey] =   _gridSpacingFact.rawValue().toDouble();
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

    // Polygon shape

    QJsonArray polygonArray;

    for (int i=0; i<_polygonPath.count(); i++) {
        const QVariant& polyVar = _polygonPath[i];

        QJsonValue jsonValue;
        JsonHelper::writeQGeoCoordinate(jsonValue, polyVar.value<QGeoCoordinate>(), false /* writeAltitude */);
        polygonArray += jsonValue;
    }

    saveObject[_jsonPolygonKey] = polygonArray;
}

void ComplexMissionItem::setSequenceNumber(int sequenceNumber)
{
149 150 151
    if (_sequenceNumber != sequenceNumber) {
        _sequenceNumber = sequenceNumber;
        emit sequenceNumberChanged(sequenceNumber);
152
        emit lastSequenceNumberChanged(lastSequenceNumber());
153 154 155 156
    }
}

void ComplexMissionItem::_clear(void)
157
{
158 159
    clearPolygon();
    _clearGrid();
160 161 162 163 164 165 166 167 168
}


bool ComplexMissionItem::load(const QJsonObject& complexObject, QString& errorString)
{
    _clear();

    // Validate requires keys
    QStringList requiredKeys;
169
    requiredKeys << _jsonVersionKey << _jsonTypeKey << _jsonIdKey << _jsonPolygonKey << _jsonGridAltitudeKey << _jsonGridAngleKey << _jsonGridSpacingKey;
170 171 172 173 174 175 176 177
    if (!JsonHelper::validateRequiredKeys(complexObject, requiredKeys, errorString)) {
        _clear();
        return false;
    }

    // Validate types
    QStringList keyList;
    QList<QJsonValue::Type> typeList;
178 179
    keyList << _jsonVersionKey << _jsonTypeKey << _jsonIdKey << _jsonPolygonKey << _jsonGridAltitudeKey << _jsonGridAngleKey << _jsonGridSpacingKey;
    typeList << QJsonValue::Double << QJsonValue::String << QJsonValue::Double << QJsonValue::Array << QJsonValue::Double << QJsonValue::Double<< QJsonValue::Double;
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    if (!JsonHelper::validateKeyTypes(complexObject, keyList, typeList, errorString)) {
        _clear();
        return false;
    }

    // Version check
    if (complexObject[_jsonVersionKey].toInt() != 1) {
        errorString = tr("QGroundControl does not support this version of survey items");
        _clear();
        return false;
    }
    QString complexType = complexObject[_jsonTypeKey].toString();
    if (complexType != _complexType) {
        errorString = tr("QGroundControl does not support loading this complex mission item type: %1").arg(complexType);
        _clear();
        return false;
    }

    setSequenceNumber(complexObject[_jsonIdKey].toInt());
199 200 201
    _gridAltitudeFact.setRawValue(complexObject[_jsonGridAltitudeKey].toDouble());
    _gridAngleFact.setRawValue(complexObject[_jsonGridAngleKey].toDouble());
    _gridSpacingFact.setRawValue(complexObject[_jsonGridSpacingKey].toDouble());
202 203 204 205 206 207 208 209 210 211 212 213 214 215

    // Polygon shape
    QJsonArray polygonArray(complexObject[_jsonPolygonKey].toArray());
    for (int i=0; i<polygonArray.count(); i++) {
        const QJsonValue& pointValue = polygonArray[i];

        QGeoCoordinate pointCoord;
        if (!JsonHelper::toQGeoCoordinate(pointValue, pointCoord, false /* altitudeRequired */, errorString)) {
            _clear();
            return false;
        }
        _polygonPath << QVariant::fromValue(pointCoord);
    }

216
    _generateGrid();
217

218 219
    return true;
}
220

221 222 223 224 225
void ComplexMissionItem::_setExitCoordinate(const QGeoCoordinate& coordinate)
{
    if (_exitCoordinate != coordinate) {
        _exitCoordinate = coordinate;
        emit exitCoordinateChanged(coordinate);
226
    }
227
}
228

229 230 231 232 233 234 235 236 237 238
bool ComplexMissionItem::specifiesCoordinate(void) const
{
    return _polygonPath.count() > 2;
}

void ComplexMissionItem::_clearGrid(void)
{
    // Bug workaround
    while (_gridPoints.count() > 1) {
        _gridPoints.takeLast();
239
    }
240 241 242
    emit gridPointsChanged();
    _gridPoints.clear();
    emit gridPointsChanged();
243

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
}

void ComplexMissionItem::_generateGrid(void)
{
    if (_polygonPath.count() < 3) {
        _clearGrid();
        return;
    }

    _gridPoints.clear();

    QList<Point_t> polygonPoints;
    QList<Point_t> gridPoints;

    // Convert polygon to NED
    qDebug() << "Convert polygon";
    QGeoCoordinate tangentOrigin = _polygonPath[0].value<QGeoCoordinate>();
    for (int i=0; i<_polygonPath.count(); i++) {
        double x, y, z;
        convertGeoToNed(_polygonPath[i].value<QGeoCoordinate>(), tangentOrigin, &x, &y, &z);
        polygonPoints += Point_t(x, y);
        qDebug() << _polygonPath[i].value<QGeoCoordinate>() << polygonPoints.last().x << polygonPoints.last().y;
    }

    // Generate grid
    _gridGenerator(polygonPoints, gridPoints);

    // Convert to Geo and set altitude
    for (int i=0; i<gridPoints.count(); i++) {
        Point_t& point = gridPoints[i];

        QGeoCoordinate geoCoord;
        convertNedToGeo(point.x, point.y, 0, tangentOrigin, &geoCoord);
        _gridPoints += QVariant::fromValue(geoCoord);
    }
    emit gridPointsChanged();
    emit lastSequenceNumberChanged(lastSequenceNumber());

    if (_gridPoints.count()) {
        setCoordinate(_gridPoints.first().value<QGeoCoordinate>());
        _setExitCoordinate(_gridPoints.last().value<QGeoCoordinate>());
    }
286

287 288
}

289
void ComplexMissionItem::_gridGenerator(const QList<Point_t>& polygonPoints, QList<Point_t>& gridPoints)
290
{
291
    // FIXME: Hack implementataion
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    gridPoints.clear();

    // Convert polygon to bounding square

    Point_t upperLeft = polygonPoints[0];
    Point_t lowerRight = polygonPoints[0];
    for (int i=1; i<polygonPoints.count(); i++) {
        const Point_t& point = polygonPoints[i];

        upperLeft.x = std::min(upperLeft.x, point.x);
        upperLeft.y = std::max(upperLeft.y, point.y);
        lowerRight.x = std::max(lowerRight.x, point.x);
        lowerRight.y = std::min(lowerRight.y, point.y);
    }
    qDebug() << "bounding rect" << upperLeft.x <<  upperLeft.y << lowerRight.x << lowerRight.y;

    // Create simplistic set of parallel lines

    QList<QPair<Point_t, Point_t>> lineList;

    double x = upperLeft.x;
    double gridSpacing = _gridSpacingFact.rawValue().toDouble();
    while (x < lowerRight.x) {
        double yTop = upperLeft.y;
        double yBottom = lowerRight.y;

        lineList += qMakePair(Point_t(x, yTop), Point_t(x, yBottom));
        qDebug() << "line" << lineList.last().first.x<< lineList.last().first.y<< lineList.last().second.x<< lineList.last().second.y;

        x += gridSpacing;
    }

    // Turn into a path

    for (int i=0; i<lineList.count(); i++) {
        const QPair<Point_t, Point_t> points = lineList[i];

        if (i & 1) {
            gridPoints << points.second << points.first;
        } else {
            gridPoints << points.first << points.second;
334 335
        }
    }
336
}
337

338
QmlObjectListModel*  ComplexMissionItem::getMissionItems(void) const
339
{
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    QmlObjectListModel* pMissionItems = new QmlObjectListModel;

    int seqNum = _sequenceNumber;
    for (int i=0; i<_gridPoints.count(); i++) {
        QGeoCoordinate coord = _gridPoints[i].value<QGeoCoordinate>();
        double altitude = _gridAltitudeFact.rawValue().toDouble();

        MissionItem* item = new MissionItem(seqNum++,                       // sequence number
                                            MAV_CMD_NAV_WAYPOINT,           // MAV_CMD
                                            MAV_FRAME_GLOBAL_RELATIVE_ALT,  // MAV_FRAME
                                            0.0, 0.0, 0.0, 0.0,             // param 1-4
                                            coord.latitude(),
                                            coord.longitude(),
                                            altitude,
                                            true,                           // autoContinue
                                            false,                          // isCurrentItem
                                            pMissionItems);                 // parent - allow delete on pMissionItems to delete everthing
        pMissionItems->append(item);
    }

    return pMissionItems;
361
}