PositionManager.cpp 3.45 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 17 18 19 20
    : QGCTool           (app, toolbox)
    , _updateInterval   (0)
    , _currentSource    (NULL)
    , _defaultSource    (NULL)
    , _nmeaSource       (NULL)
    , _simulatedSource  (NULL)
Jimmy Johnson's avatar
Jimmy Johnson committed
21 22 23 24 25 26 27
{

}

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

31
void QGCPositionManager::setToolbox(QGCToolbox *toolbox)
Jimmy Johnson's avatar
Jimmy Johnson committed
32
{
33 34 35 36 37 38
   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);
39
       qDebug() << _defaultSource;
40 41
   }
   _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 53 54 55 56 57 58
   setPositionSource(QGCPositionSource::InternalGPS);
}

void QGCPositionManager::setNmeaSourceDevice(QIODevice* device)
{
    if (_nmeaSource) {
        delete _nmeaSource;
    }
    _nmeaSource = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::RealTimeMode, this);
    _nmeaSource->setDevice(device);
    setPositionSource(QGCPositionManager::NmeaGPS);
59
}
Jimmy Johnson's avatar
Jimmy Johnson committed
60

61
void QGCPositionManager::_positionUpdated(const QGeoPositionInfo &update)
62 63
{
    emit lastPositionUpdated(update.isValid(), QVariant::fromValue(update.coordinate()));
Jimmy Johnson's avatar
Jimmy Johnson committed
64 65 66 67 68 69 70 71 72 73
    emit positionInfoUpdated(update);
}

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

void QGCPositionManager::setPositionSource(QGCPositionManager::QGCPositionSource source)
{
74
    if (_currentSource != nullptr) {
Jimmy Johnson's avatar
Jimmy Johnson committed
75
        _currentSource->stopUpdates();
76
        disconnect(_currentSource);
Jimmy Johnson's avatar
Jimmy Johnson committed
77 78
    }

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

Jimmy Johnson's avatar
Jimmy Johnson committed
84 85 86 87 88 89
    switch(source) {
    case QGCPositionManager::Log:
        break;
    case QGCPositionManager::Simulated:
        _currentSource = _simulatedSource;
        break;
90 91 92 93
    case QGCPositionManager::NmeaGPS:
        _currentSource = _nmeaSource;
        break;
    case QGCPositionManager::InternalGPS:
Jimmy Johnson's avatar
Jimmy Johnson committed
94 95 96 97 98
    default:        
        _currentSource = _defaultSource;
        break;
    }

99 100 101 102
    if (_currentSource != nullptr) {
        _updateInterval = _currentSource->minimumUpdateInterval();
        _currentSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
        _currentSource->setUpdateInterval(_updateInterval);
103 104
        connect(_currentSource, &QGeoPositionInfoSource::positionUpdated,       this, &QGCPositionManager::_positionUpdated);
        connect(_currentSource, SIGNAL(error(QGeoPositionInfoSource::Error)),   this, SLOT(_error(QGeoPositionInfoSource::Error)));
105
        _currentSource->startUpdates();
106
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
107 108
}

109 110 111 112
void QGCPositionManager::_error(QGeoPositionInfoSource::Error positioningError)
{
    qWarning() << "QGCPositionManager error" << positioningError;
}