QGCFlightDisplay.cc 11.3 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 34 35 36 37 38 39 40 41 42 43
/*=====================================================================

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>

#include "QGCFlightDisplay.h"
#include "UASManager.h"

#define UPDATE_TIMER 50

const char* kMainFlightDisplayGroup = "MainFlightDisplay";

QGCFlightDisplay::QGCFlightDisplay(QWidget *parent)
    : QGCQmlWidgetHolder(parent)
    , _mav(NULL)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    , _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
59 60 61 62 63 64 65 66 67 68 69
    , _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);
    }
70 71
    setMinimumWidth(380);
    setMinimumHeight(360);
dogmaphobic's avatar
dogmaphobic committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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();
}

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

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

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

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

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);
150
        if (fabs(roll - rolldeg) > 1.0) {
dogmaphobic's avatar
dogmaphobic committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164
            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);
165
        if (fabs(pitch - pitchdeg) > 1.0) {
dogmaphobic's avatar
dogmaphobic committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            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
181
        if (fabs(_heading - yaw) > 10.0) {
dogmaphobic's avatar
dogmaphobic committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
            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
204
    if (fabs(oldgroundSpeed - groundSpeed) > 0.5) {
dogmaphobic's avatar
dogmaphobic committed
205 206 207
        if(_refreshTimer->isActive()) emit groundSpeedChanged();
        _valuesChanged = true;
    }
Don Gagne's avatar
Don Gagne committed
208
    if (fabs(oldairSpeed - airSpeed) > 1.0) {
dogmaphobic's avatar
dogmaphobic committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        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
226
    if (fabs(oldaltitudeAMSL - altitudeAMSL) > 0.5) {
dogmaphobic's avatar
dogmaphobic committed
227 228 229
        if(_refreshTimer->isActive()) emit altitudeAMSLChanged();
        _valuesChanged = true;
    }
Don Gagne's avatar
Don Gagne committed
230
    if (fabs(oldaltitudeWGS84 - altitudeWGS84) > 0.5) {
dogmaphobic's avatar
dogmaphobic committed
231 232 233
        if(_refreshTimer->isActive()) emit altitudeWGS84Changed();
        _valuesChanged = true;
    }
Don Gagne's avatar
Don Gagne committed
234
    if (fabs(oldaltitudeRelative - altitudeRelative) > 0.5) {
dogmaphobic's avatar
dogmaphobic committed
235 236 237
        if(_refreshTimer->isActive()) emit altitudeRelativeChanged();
        _valuesChanged = true;
    }
Don Gagne's avatar
Don Gagne committed
238
    if (fabs(oldclimbRate - climbRate) > 0.5) {
dogmaphobic's avatar
dogmaphobic committed
239 240 241 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 309 310 311 312
        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) {
        _latitude  = _mav->getLatitude();
        _longitude = _mav->getLongitude();
        if(_latitude)  emit latitudeChanged();
        if(_longitude) emit longitudeChanged();
    }
}