QGCFlightDisplay.cc 12.9 KB
Newer Older
dogmaphobic's avatar
dogmaphobic 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 30 31 32 33
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

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

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

/**
 * @file
 *   @brief QGC Main Flight Display
 *   @author Gus Grubba <mavlink@grubba.com>
 */

#include <QQmlContext>
#include <QQmlEngine>
#include <QSettings>

34
#include "MainWindow.h"
dogmaphobic's avatar
dogmaphobic committed
35 36 37
#include "QGCFlightDisplay.h"
#include "UASManager.h"

38
#define UPDATE_TIMER 100
dogmaphobic's avatar
dogmaphobic committed
39 40 41 42 43 44

const char* kMainFlightDisplayGroup = "MainFlightDisplay";

QGCFlightDisplay::QGCFlightDisplay(QWidget *parent)
    : QGCQmlWidgetHolder(parent)
    , _mav(NULL)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
    , _roll(0.0f)
    , _pitch(0.0f)
    , _heading(0.0f)
    , _altitudeAMSL(0.0f)
    , _altitudeWGS84(0.0f)
    , _altitudeRelative(0.0f)
    , _groundSpeed(0.0f)
    , _airSpeed(0.0f)
    , _climbRate(0.0f)
    , _navigationAltitudeError(0.0f)
    , _navigationSpeedError(0.0f)
    , _navigationCrosstrackError(0.0f)
    , _navigationTargetBearing(0.0f)
    , _latitude(37.803784f)
    , _longitude(-122.462276f)
dogmaphobic's avatar
dogmaphobic committed
60 61 62 63 64 65 66 67 68
    , _refreshTimer(new QTimer(this))
{
    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    setObjectName("MainFlightDisplay");
    // Get rid of layout default margins
    QLayout* pl = layout();
    if(pl) {
        pl->setContentsMargins(0,0,0,0);
    }
69 70 71 72
#ifndef __android__
    setMinimumWidth( 380 * MainWindow::pixelSizeFactor());
    setMinimumHeight(400 * MainWindow::pixelSizeFactor());
#endif
dogmaphobic's avatar
dogmaphobic committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    setContextPropertyObject("flightDisplay", this);
    setSource(QUrl::fromUserInput("qrc:/qml/FlightDisplay.qml"));
    setVisible(true);
    // Connect with UAS signal
    _setActiveUAS(UASManager::instance()->getActiveUAS());
    connect(UASManager::instance(), SIGNAL(UASDeleted(UASInterface*)),   this, SLOT(_forgetUAS(UASInterface*)));
    connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), this, SLOT(_setActiveUAS(UASInterface*)));
    // Refresh timer
    _refreshTimer->setInterval(UPDATE_TIMER);
    connect(_refreshTimer, SIGNAL(timeout()), this, SLOT(_checkUpdate()));
}

QGCFlightDisplay::~QGCFlightDisplay()
{
    _refreshTimer->stop();
}

90
void QGCFlightDisplay::saveSetting(const QString &name, const QString& value)
dogmaphobic's avatar
dogmaphobic committed
91 92 93 94 95 96 97
{
    QSettings settings;
    QString key(kMainFlightDisplayGroup);
    key += "/" + name;
    settings.setValue(key, value);
}

98
QString QGCFlightDisplay::loadSetting(const QString &name, const QString& defaultValue)
dogmaphobic's avatar
dogmaphobic committed
99 100 101 102
{
    QSettings settings;
    QString key(kMainFlightDisplayGroup);
    key += "/" + name;
103
    return settings.value(key, defaultValue).toString();
dogmaphobic's avatar
dogmaphobic committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117
}

void QGCFlightDisplay::_forgetUAS(UASInterface* uas)
{
    if (_mav != NULL && _mav == uas) {
        // Disconnect any previously connected active MAV
        disconnect(_mav, SIGNAL(attitudeChanged                  (UASInterface*, double,double,double,quint64)),            this, SLOT(_updateAttitude(UASInterface*, double, double, double, quint64)));
        disconnect(_mav, SIGNAL(attitudeChanged                  (UASInterface*, int,double,double,double,quint64)),        this, SLOT(_updateAttitude(UASInterface*,int,double, double, double, quint64)));
        disconnect(_mav, SIGNAL(speedChanged                     (UASInterface*, double, double, quint64)),                 this, SLOT(_updateSpeed(UASInterface*, double, double, quint64)));
        disconnect(_mav, SIGNAL(altitudeChanged                  (UASInterface*, double, double, double, double, quint64)), this, SLOT(_updateAltitude(UASInterface*, double, double, double, double, quint64)));
        disconnect(_mav, SIGNAL(navigationControllerErrorsChanged(UASInterface*, double, double, double)),                  this, SLOT(_updateNavigationControllerErrors(UASInterface*, double, double, double)));
        disconnect(_mav, &UASInterface::NavigationControllerDataChanged, this, &QGCFlightDisplay::_updateNavigationControllerData);
    }
    _mav = NULL;
118
    emit mavPresentChanged();
dogmaphobic's avatar
dogmaphobic committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
}

void QGCFlightDisplay::_setActiveUAS(UASInterface* uas)
{
    if (uas == _mav)
        return;
    // Disconnect the previous one (if any)
    if(_mav) {
        _forgetUAS(_mav);
    }
    if (uas) {
        // Now connect the new UAS
        // Setup communication
        connect(uas, SIGNAL(attitudeChanged                     (UASInterface*,double,double,double,quint64)),              this, SLOT(_updateAttitude(UASInterface*, double, double, double, quint64)));
        connect(uas, SIGNAL(attitudeChanged                     (UASInterface*,int,double,double,double,quint64)),          this, SLOT(_updateAttitude(UASInterface*,int,double, double, double, quint64)));
        connect(uas, SIGNAL(speedChanged                        (UASInterface*, double, double, quint64)),                  this, SLOT(_updateSpeed(UASInterface*, double, double, quint64)));
        connect(uas, SIGNAL(altitudeChanged                     (UASInterface*, double, double, double, double, quint64)),  this, SLOT(_updateAltitude(UASInterface*, double, double, double, double, quint64)));
        connect(uas, SIGNAL(navigationControllerErrorsChanged   (UASInterface*, double, double, double)),                   this, SLOT(_updateNavigationControllerErrors(UASInterface*, double, double, double)));
        connect(uas, &UASInterface::NavigationControllerDataChanged, this, &QGCFlightDisplay::_updateNavigationControllerData);
        // Set new UAS
        _mav = uas;
    }
141
    emit mavPresentChanged();
dogmaphobic's avatar
dogmaphobic committed
142 143 144 145 146 147 148
}

void QGCFlightDisplay::_updateAttitude(UASInterface*, double roll, double pitch, double yaw, quint64)
{
    if (isinf(roll)) {
        _roll = std::numeric_limits<double>::quiet_NaN();
    } else {
149
        float rolldeg = _oneDecimal(roll * (180.0 / M_PI));
150
        if (fabs(roll - rolldeg) > 1.0) {
151 152 153 154
            _roll = rolldeg;
            if(_refreshTimer->isActive()) {
                emit rollChanged();
            }
dogmaphobic's avatar
dogmaphobic committed
155
        }
156 157 158
        if(_roll != rolldeg) {
            _roll = rolldeg;
            _addChange(ROLL_CHANGED);
dogmaphobic's avatar
dogmaphobic committed
159 160 161 162 163
        }
    }
    if (isinf(pitch)) {
        _pitch = std::numeric_limits<double>::quiet_NaN();
    } else {
164
        float pitchdeg = _oneDecimal(pitch * (180.0 / M_PI));
165
        if (fabs(pitch - pitchdeg) > 1.0) {
166 167 168 169
            _pitch = pitchdeg;
            if(_refreshTimer->isActive()) {
                emit pitchChanged();
            }
dogmaphobic's avatar
dogmaphobic committed
170
        }
171 172 173
        if(_pitch != pitchdeg) {
            _pitch = pitchdeg;
            _addChange(PITCH_CHANGED);
dogmaphobic's avatar
dogmaphobic committed
174 175 176 177 178
        }
    }
    if (isinf(yaw)) {
        _heading = std::numeric_limits<double>::quiet_NaN();
    } else {
179
        yaw = _oneDecimal(yaw * (180.0 / M_PI));
dogmaphobic's avatar
dogmaphobic committed
180
        if (yaw < 0) yaw += 360;
181 182 183 184 185
        if (fabs(_heading - yaw) > 1.0) {
            _heading = yaw;
            if(_refreshTimer->isActive()) {
                emit headingChanged();
            }
dogmaphobic's avatar
dogmaphobic committed
186
        }
187 188 189
        if(_heading != yaw) {
            _heading = yaw;
            _addChange(HEADING_CHANGED);
dogmaphobic's avatar
dogmaphobic committed
190 191 192 193 194 195 196 197 198 199 200
        }
    }
}

void QGCFlightDisplay::_updateAttitude(UASInterface* uas, int, double roll, double pitch, double yaw, quint64 timestamp)
{
    _updateAttitude(uas, roll, pitch, yaw, timestamp);
}

void QGCFlightDisplay::_updateSpeed(UASInterface*, double groundSpeed, double airSpeed, quint64)
{
201 202 203 204 205 206 207 208 209 210 211 212 213
    groundSpeed = _oneDecimal(groundSpeed);
    if (fabs(_groundSpeed - groundSpeed) > 0.5) {
        _groundSpeed = groundSpeed;
        if(_refreshTimer->isActive()) {
            emit groundSpeedChanged();
        }
    }
    airSpeed = _oneDecimal(airSpeed);
    if (fabs(_airSpeed - airSpeed) > 1.0) {
        _airSpeed = airSpeed;
        if(_refreshTimer->isActive()) {
            emit airSpeedChanged();
        }
dogmaphobic's avatar
dogmaphobic committed
214
    }
215 216 217 218 219 220 221
    if(_groundSpeed != groundSpeed) {
        _groundSpeed = groundSpeed;
        _addChange(GROUNDSPEED_CHANGED);
    }
    if(_airSpeed != airSpeed) {
        _airSpeed = airSpeed;
        _addChange(AIRSPEED_CHANGED);
dogmaphobic's avatar
dogmaphobic committed
222 223 224 225
    }
}

void QGCFlightDisplay::_updateAltitude(UASInterface*, double altitudeAMSL, double altitudeWGS84, double altitudeRelative, double climbRate, quint64) {
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    altitudeAMSL = _oneDecimal(altitudeAMSL);
    if (fabs(_altitudeAMSL - altitudeAMSL) > 0.5) {
        _altitudeAMSL = altitudeAMSL;
        if(_refreshTimer->isActive()) {
            emit altitudeAMSLChanged();
        }
    }
    altitudeWGS84 = _oneDecimal(altitudeWGS84);
    if (fabs(_altitudeWGS84 - altitudeWGS84) > 0.5) {
        _altitudeWGS84 = altitudeWGS84;
        if(_refreshTimer->isActive()) {
            emit altitudeWGS84Changed();
        }
    }
    altitudeRelative = _oneDecimal(altitudeRelative);
    if (fabs(_altitudeRelative - altitudeRelative) > 0.5) {
        _altitudeRelative = altitudeRelative;
        if(_refreshTimer->isActive()) {
            emit altitudeRelativeChanged();
        }
    }
    climbRate = _oneDecimal(climbRate);
    if (fabs(_climbRate - climbRate) > 0.5) {
        _climbRate = climbRate;
        if(_refreshTimer->isActive()) {
            emit climbRateChanged();
        }
dogmaphobic's avatar
dogmaphobic committed
253
    }
254 255 256
    if(_altitudeAMSL != altitudeAMSL) {
        _altitudeAMSL = altitudeAMSL;
        _addChange(ALTITUDEAMSL_CHANGED);
dogmaphobic's avatar
dogmaphobic committed
257
    }
258 259 260
    if(_altitudeWGS84 != altitudeWGS84) {
        _altitudeWGS84 = altitudeWGS84;
        _addChange(ALTITUDEWGS84_CHANGED);
dogmaphobic's avatar
dogmaphobic committed
261
    }
262 263 264
    if(_altitudeRelative != altitudeRelative) {
        _altitudeRelative = altitudeRelative;
        _addChange(ALTITUDERELATIVE_CHANGED);
dogmaphobic's avatar
dogmaphobic committed
265
    }
266 267 268
    if(_climbRate != climbRate) {
        _climbRate = climbRate;
        _addChange(CLIMBRATE_CHANGED);
dogmaphobic's avatar
dogmaphobic committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    }
}

void QGCFlightDisplay::_updateNavigationControllerErrors(UASInterface*, double altitudeError, double speedError, double xtrackError) {
    _navigationAltitudeError   = altitudeError;
    _navigationSpeedError      = speedError;
    _navigationCrosstrackError = xtrackError;
}

void QGCFlightDisplay::_updateNavigationControllerData(UASInterface *uas, float, float, float, float targetBearing, float) {
    if (_mav == uas) {
        _navigationTargetBearing = targetBearing;
    }
}

/*
 * Internal
 */

bool QGCFlightDisplay::_isAirplane() {
    if (_mav)
        return _mav->isAirplane();
    return false;
}

// TODO: Implement. Should return true when navigating.
// That would be (PX4) in AUTO and RTL modes.
// This could forward to a virtual on UAS bool isNavigatingAutonomusly() or whatever.
bool QGCFlightDisplay::_shouldDisplayNavigationData() {
    return true;
}

void QGCFlightDisplay::showEvent(QShowEvent* event)
{
303
    // Enable updates
dogmaphobic's avatar
dogmaphobic committed
304 305 306 307 308 309
    QWidget::showEvent(event);
    _refreshTimer->start(UPDATE_TIMER);
}

void QGCFlightDisplay::hideEvent(QHideEvent* event)
{
310
    // Disable updates
dogmaphobic's avatar
dogmaphobic committed
311 312 313 314
    _refreshTimer->stop();
    QWidget::hideEvent(event);
}

315
void QGCFlightDisplay::_addChange(int id)
dogmaphobic's avatar
dogmaphobic committed
316
{
317 318
    if(!_changes.contains(id)) {
        _changes.append(id);
dogmaphobic's avatar
dogmaphobic committed
319
    }
320 321 322 323 324 325 326 327 328 329 330
}

float QGCFlightDisplay::_oneDecimal(float value)
{
    int i = (value * 10);
    return (float)i / 10.0;
}

void QGCFlightDisplay::_checkUpdate()
{
    // Update current location
dogmaphobic's avatar
dogmaphobic committed
331
    if(_mav) {
332 333 334 335 336 337 338 339
        if(_latitude != _mav->getLatitude()) {
            _latitude = _mav->getLatitude();
            emit latitudeChanged();
        }
        if(_longitude != _mav->getLongitude()) {
            _longitude = _mav->getLongitude();
            emit longitudeChanged();
        }
dogmaphobic's avatar
dogmaphobic committed
340
    }
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    // Check for changes
    // Significant changes, that is, whole number changes, are updated immediatelly.
    // For every message however, we set a flag for what changed and this timer updates
    // them to bring everything up-to-date. This prevents an avalanche of UI updates.
    foreach(int i, _changes) {
        switch (i) {
        case ROLL_CHANGED:
            emit rollChanged();
            break;
        case PITCH_CHANGED:
            emit pitchChanged();
            break;
        case HEADING_CHANGED:
            emit headingChanged();
            break;
        case GROUNDSPEED_CHANGED:
            emit groundSpeedChanged();
            break;
        case AIRSPEED_CHANGED:
            emit airSpeedChanged();
            break;
        case CLIMBRATE_CHANGED:
            emit climbRateChanged();
            break;
        case ALTITUDERELATIVE_CHANGED:
            emit altitudeRelativeChanged();
            break;
        case ALTITUDEWGS84_CHANGED:
            emit altitudeWGS84Changed();
            break;
        case ALTITUDEAMSL_CHANGED:
            emit altitudeAMSLChanged();
            break;
        default:
            break;
        }
    }
    _changes.clear();
dogmaphobic's avatar
dogmaphobic committed
379 380
}