QGCFlightDisplay.cc 11.5 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 38 39 40 41 42 43 44
#include "QGCFlightDisplay.h"
#include "UASManager.h"

#define UPDATE_TIMER 50

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 69 70
    , _refreshTimer(new QTimer(this))
    , _valuesChanged(false)
    , _valuesLastPainted(0)
{
    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    setObjectName("MainFlightDisplay");
    // Get rid of layout default margins
    QLayout* pl = layout();
    if(pl) {
        pl->setContentsMargins(0,0,0,0);
    }
71 72 73 74
#ifndef __android__
    setMinimumWidth( 380 * MainWindow::pixelSizeFactor());
    setMinimumHeight(400 * MainWindow::pixelSizeFactor());
#endif
dogmaphobic's avatar
dogmaphobic committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    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();
}

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

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

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;
120
    emit mavPresentChanged();
dogmaphobic's avatar
dogmaphobic committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
}

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;
    }
143
    emit mavPresentChanged();
dogmaphobic's avatar
dogmaphobic committed
144 145 146 147 148 149 150 151 152
}

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

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)
{
    double oldgroundSpeed = _groundSpeed;
    double oldairSpeed    = _airSpeed;
    _groundSpeed = groundSpeed;
    _airSpeed    = airSpeed;
Don Gagne's avatar
Don Gagne committed
207
    if (fabs(oldgroundSpeed - groundSpeed) > 0.5) {
dogmaphobic's avatar
dogmaphobic committed
208 209 210
        if(_refreshTimer->isActive()) emit groundSpeedChanged();
        _valuesChanged = true;
    }
Don Gagne's avatar
Don Gagne committed
211
    if (fabs(oldairSpeed - airSpeed) > 1.0) {
dogmaphobic's avatar
dogmaphobic committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        if(_refreshTimer->isActive()) emit airSpeedChanged();
        _valuesChanged = true;
    }
}

void QGCFlightDisplay::_updateAltitude(UASInterface*, double altitudeAMSL, double altitudeWGS84, double altitudeRelative, double climbRate, quint64) {
    double oldclimbRate         = _climbRate;
    double oldaltitudeRelative  = _altitudeRelative;
    double oldaltitudeWGS84     = _altitudeWGS84;
    double oldaltitudeAMSL      = _altitudeAMSL;
    _climbRate          = climbRate;
    _altitudeRelative   = altitudeRelative;
    _altitudeWGS84      = altitudeWGS84;
    _altitudeAMSL       = altitudeAMSL;
    if(_climbRate > -0.01 && _climbRate < 0.01) {
        _climbRate = 0.0;
    }
Don Gagne's avatar
Don Gagne committed
229
    if (fabs(oldaltitudeAMSL - altitudeAMSL) > 0.5) {
dogmaphobic's avatar
dogmaphobic committed
230 231 232
        if(_refreshTimer->isActive()) emit altitudeAMSLChanged();
        _valuesChanged = true;
    }
Don Gagne's avatar
Don Gagne committed
233
    if (fabs(oldaltitudeWGS84 - altitudeWGS84) > 0.5) {
dogmaphobic's avatar
dogmaphobic committed
234 235 236
        if(_refreshTimer->isActive()) emit altitudeWGS84Changed();
        _valuesChanged = true;
    }
Don Gagne's avatar
Don Gagne committed
237
    if (fabs(oldaltitudeRelative - altitudeRelative) > 0.5) {
dogmaphobic's avatar
dogmaphobic committed
238 239 240
        if(_refreshTimer->isActive()) emit altitudeRelativeChanged();
        _valuesChanged = true;
    }
Don Gagne's avatar
Don Gagne committed
241
    if (fabs(oldclimbRate - climbRate) > 0.5) {
dogmaphobic's avatar
dogmaphobic committed
242 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        if(_refreshTimer->isActive()) emit climbRateChanged();
        _valuesChanged = true;
    }
}

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)
{
    // React only to internal (pre-display) events
    QWidget::showEvent(event);
    _refreshTimer->start(UPDATE_TIMER);
}

void QGCFlightDisplay::hideEvent(QHideEvent* event)
{
    // React only to internal (pre-display) events
    _refreshTimer->stop();
    QWidget::hideEvent(event);
}

void QGCFlightDisplay::_checkUpdate()
{
    if (_mav && (_valuesChanged || (QGC::groundTimeMilliseconds() - _valuesLastPainted) > 260)) {
        _valuesChanged = false;
        _valuesLastPainted = QGC::groundTimeMilliseconds();
        emit rollChanged();
        emit pitchChanged();
        emit headingChanged();
        emit altitudeAMSLChanged();
        emit altitudeWGS84Changed();
        emit altitudeRelativeChanged();
        emit climbRateChanged();
        emit groundSpeedChanged();
        emit airSpeedChanged();
        emit repaintRequestedChanged();
        emit latitudeChanged();
        emit longitudeChanged();
    }
    if(_mav) {
309 310 311 312 313 314 315 316
        if(_latitude != _mav->getLatitude()) {
            _latitude = _mav->getLatitude();
            emit latitudeChanged();
        }
        if(_longitude != _mav->getLongitude()) {
            _longitude = _mav->getLongitude();
            emit longitudeChanged();
        }
dogmaphobic's avatar
dogmaphobic committed
317 318 319
    }
}