PositionManager.cpp 4.29 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
Jimmy Johnson's avatar
Jimmy Johnson committed
9 10

#include "PositionManager.h"
11 12
#include "QGCApplication.h"
#include "QGCCorePlugin.h"
Jimmy Johnson's avatar
Jimmy Johnson committed
13

14
QGCPositionManager::QGCPositionManager(QGCApplication* app, QGCToolbox* toolbox)
Don Gagne's avatar
Don Gagne committed
15 16
    : QGCTool           (app, toolbox)
    , _updateInterval   (0)
17
    , _gcsHeading       (NAN)
Don Gagne's avatar
Don Gagne committed
18 19 20 21
    , _currentSource    (NULL)
    , _defaultSource    (NULL)
    , _nmeaSource       (NULL)
    , _simulatedSource  (NULL)
Jimmy Johnson's avatar
Jimmy Johnson committed
22 23 24 25 26 27 28
{

}

QGCPositionManager::~QGCPositionManager()
{
    delete(_simulatedSource);
29
    delete(_nmeaSource);
Jimmy Johnson's avatar
Jimmy Johnson committed
30 31
}

32
void QGCPositionManager::setToolbox(QGCToolbox *toolbox)
Jimmy Johnson's avatar
Jimmy Johnson committed
33
{
34 35 36 37 38 39 40 41
   QGCTool::setToolbox(toolbox);
   //-- First see if plugin provides a position source
   _defaultSource = toolbox->corePlugin()->createPositionSource(this);
   if(!_defaultSource) {
       //-- Otherwise, create a default one
       _defaultSource = QGeoPositionInfoSource::createDefaultSource(this);
   }
   _simulatedSource = new SimulatedPosition();
Jimmy Johnson's avatar
Jimmy Johnson committed
42

43 44 45 46 47
   // Enable this to get a simulated target on desktop
   // if (_defaultSource == nullptr) {
   //     _defaultSource = _simulatedSource;
   // }

48 49 50 51 52
   setPositionSource(QGCPositionSource::InternalGPS);
}

void QGCPositionManager::setNmeaSourceDevice(QIODevice* device)
{
53
    // stop and release _nmeaSource
54
    if (_nmeaSource) {
55 56 57 58 59 60 61 62
        _nmeaSource->stopUpdates();
        disconnect(_nmeaSource);

        // if _currentSource is pointing there, point to null
        if (_currentSource == _nmeaSource){
            _currentSource = nullptr;
        }

63
        delete _nmeaSource;
64 65
        _nmeaSource = nullptr;

66 67 68 69
    }
    _nmeaSource = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::RealTimeMode, this);
    _nmeaSource->setDevice(device);
    setPositionSource(QGCPositionManager::NmeaGPS);
70
}
Jimmy Johnson's avatar
Jimmy Johnson committed
71

72
void QGCPositionManager::_positionUpdated(const QGeoPositionInfo &update)
73
{
74
    QGeoCoordinate newGCSPosition = QGeoCoordinate();
75
    qreal newGCSHeading = update.attribute(QGeoPositionInfo::Direction);
76 77 78 79 80 81 82 83 84 85 86

    if (update.isValid()) {
        // Note that gcsPosition filters out possible crap values
        if (qAbs(update.coordinate().latitude()) > 0.001 && qAbs(update.coordinate().longitude()) > 0.001) {
            newGCSPosition = update.coordinate();
        }
    }
    if (newGCSPosition != _gcsPosition) {
        _gcsPosition = newGCSPosition;
        emit gcsPositionChanged(_gcsPosition);
    }
87 88 89 90
    if (newGCSHeading != _gcsHeading) {
        _gcsHeading = newGCSHeading;
        emit gcsHeadingChanged(_gcsHeading);
    }
91

Jimmy Johnson's avatar
Jimmy Johnson committed
92 93 94 95 96 97 98 99 100 101
    emit positionInfoUpdated(update);
}

int QGCPositionManager::updateInterval() const
{
    return _updateInterval;
}

void QGCPositionManager::setPositionSource(QGCPositionManager::QGCPositionSource source)
{
102
    if (_currentSource != nullptr) {
Jimmy Johnson's avatar
Jimmy Johnson committed
103
        _currentSource->stopUpdates();
104
        disconnect(_currentSource);
Jimmy Johnson's avatar
Jimmy Johnson committed
105 106
    }

107 108 109 110 111
    if (qgcApp()->runningUnitTests()) {
        // Units test on travis fail due to lack of position source
        return;
    }

Jimmy Johnson's avatar
Jimmy Johnson committed
112 113 114 115 116 117
    switch(source) {
    case QGCPositionManager::Log:
        break;
    case QGCPositionManager::Simulated:
        _currentSource = _simulatedSource;
        break;
118 119 120 121
    case QGCPositionManager::NmeaGPS:
        _currentSource = _nmeaSource;
        break;
    case QGCPositionManager::InternalGPS:
Jimmy Johnson's avatar
Jimmy Johnson committed
122 123 124 125 126
    default:        
        _currentSource = _defaultSource;
        break;
    }

127 128 129 130
    if (_currentSource != nullptr) {
        _updateInterval = _currentSource->minimumUpdateInterval();
        _currentSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
        _currentSource->setUpdateInterval(_updateInterval);
131 132
        connect(_currentSource, &QGeoPositionInfoSource::positionUpdated,       this, &QGCPositionManager::_positionUpdated);
        connect(_currentSource, SIGNAL(error(QGeoPositionInfoSource::Error)),   this, SLOT(_error(QGeoPositionInfoSource::Error)));
133
        _currentSource->startUpdates();
134
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
135 136
}

137 138 139 140
void QGCPositionManager::_error(QGeoPositionInfoSource::Error positioningError)
{
    qWarning() << "QGCPositionManager error" << positioningError;
}