PositionManager.cpp 4.42 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)
18 19 20 21
    , _currentSource    (nullptr)
    , _defaultSource    (nullptr)
    , _nmeaSource       (nullptr)
    , _simulatedSource  (nullptr)
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
   QGCTool::setToolbox(toolbox);
   //-- First see if plugin provides a position source
   _defaultSource = toolbox->corePlugin()->createPositionSource(this);
37 38 39 40 41 42 43

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

   if (!_defaultSource) {
44 45 46 47
       //-- Otherwise, create a default one
       _defaultSource = QGeoPositionInfoSource::createDefaultSource(this);
   }
   _simulatedSource = new SimulatedPosition();
Jimmy Johnson's avatar
Jimmy Johnson committed
48

49 50 51 52 53
   // Enable this to get a simulated target on desktop
   // if (_defaultSource == nullptr) {
   //     _defaultSource = _simulatedSource;
   // }

54 55 56 57 58
   setPositionSource(QGCPositionSource::InternalGPS);
}

void QGCPositionManager::setNmeaSourceDevice(QIODevice* device)
{
59
    // stop and release _nmeaSource
60
    if (_nmeaSource) {
61 62 63 64 65 66 67 68
        _nmeaSource->stopUpdates();
        disconnect(_nmeaSource);

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

69
        delete _nmeaSource;
70 71
        _nmeaSource = nullptr;

72 73 74 75
    }
    _nmeaSource = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::RealTimeMode, this);
    _nmeaSource->setDevice(device);
    setPositionSource(QGCPositionManager::NmeaGPS);
76
}
Jimmy Johnson's avatar
Jimmy Johnson committed
77

78
void QGCPositionManager::_positionUpdated(const QGeoPositionInfo &update)
79
{
80
    QGeoCoordinate newGCSPosition = QGeoCoordinate();
81
    qreal newGCSHeading = update.attribute(QGeoPositionInfo::Direction);
82 83 84 85 86 87 88 89 90 91 92

    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);
    }
93 94 95 96
    if (newGCSHeading != _gcsHeading) {
        _gcsHeading = newGCSHeading;
        emit gcsHeadingChanged(_gcsHeading);
    }
97

Jimmy Johnson's avatar
Jimmy Johnson committed
98 99 100 101 102 103 104 105 106 107
    emit positionInfoUpdated(update);
}

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

void QGCPositionManager::setPositionSource(QGCPositionManager::QGCPositionSource source)
{
108
    if (_currentSource != nullptr) {
Jimmy Johnson's avatar
Jimmy Johnson committed
109
        _currentSource->stopUpdates();
110
        disconnect(_currentSource);
Jimmy Johnson's avatar
Jimmy Johnson committed
111 112
    }

113 114 115 116 117
    if (qgcApp()->runningUnitTests()) {
        // Units test on travis fail due to lack of position source
        return;
    }

Jimmy Johnson's avatar
Jimmy Johnson committed
118 119 120 121 122 123
    switch(source) {
    case QGCPositionManager::Log:
        break;
    case QGCPositionManager::Simulated:
        _currentSource = _simulatedSource;
        break;
124 125 126 127
    case QGCPositionManager::NmeaGPS:
        _currentSource = _nmeaSource;
        break;
    case QGCPositionManager::InternalGPS:
Jimmy Johnson's avatar
Jimmy Johnson committed
128 129 130 131 132
    default:        
        _currentSource = _defaultSource;
        break;
    }

133 134 135 136
    if (_currentSource != nullptr) {
        _updateInterval = _currentSource->minimumUpdateInterval();
        _currentSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
        _currentSource->setUpdateInterval(_updateInterval);
137 138
        connect(_currentSource, &QGeoPositionInfoSource::positionUpdated,       this, &QGCPositionManager::_positionUpdated);
        connect(_currentSource, SIGNAL(error(QGeoPositionInfoSource::Error)),   this, SLOT(_error(QGeoPositionInfoSource::Error)));
139
        _currentSource->startUpdates();
140
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
141 142
}

143 144
void QGCPositionManager::_error(QGeoPositionInfoSource::Error positioningError)
{
145
    qWarning() << "QGCPositionManager error" << positioningError;
146
}