PositionManager.cpp 2.92 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 15 16 17
QGCPositionManager::QGCPositionManager(QGCApplication* app, QGCToolbox* toolbox)
    : QGCTool(app, toolbox)
    , _updateInterval(0)
    , _currentSource(nullptr)
Jimmy Johnson's avatar
Jimmy Johnson committed
18 19 20 21 22 23 24 25 26
{

}

QGCPositionManager::~QGCPositionManager()
{
    delete(_simulatedSource);
}

27
void QGCPositionManager::setToolbox(QGCToolbox *toolbox)
Jimmy Johnson's avatar
Jimmy Johnson committed
28
{
29 30 31 32 33 34
   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);
35
       qDebug() << _defaultSource;
36 37
   }
   _simulatedSource = new SimulatedPosition();
Jimmy Johnson's avatar
Jimmy Johnson committed
38

39 40 41 42 43 44 45
   // Enable this to get a simulated target on desktop
   // if (_defaultSource == nullptr) {
   //     _defaultSource = _simulatedSource;
   // }

   setPositionSource(QGCPositionSource::GPS);
}
Jimmy Johnson's avatar
Jimmy Johnson committed
46

47
void QGCPositionManager::_positionUpdated(const QGeoPositionInfo &update)
48 49
{
    emit lastPositionUpdated(update.isValid(), QVariant::fromValue(update.coordinate()));
Jimmy Johnson's avatar
Jimmy Johnson committed
50 51 52 53 54 55 56 57 58 59
    emit positionInfoUpdated(update);
}

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

void QGCPositionManager::setPositionSource(QGCPositionManager::QGCPositionSource source)
{
60
    if (_currentSource != nullptr) {
Jimmy Johnson's avatar
Jimmy Johnson committed
61
        _currentSource->stopUpdates();
62
        disconnect(_currentSource);
Jimmy Johnson's avatar
Jimmy Johnson committed
63 64
    }

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

Jimmy Johnson's avatar
Jimmy Johnson committed
70 71 72 73 74 75 76 77 78 79 80 81
    switch(source) {
    case QGCPositionManager::Log:
        break;
    case QGCPositionManager::Simulated:
        _currentSource = _simulatedSource;
        break;
    case QGCPositionManager::GPS:
    default:        
        _currentSource = _defaultSource;
        break;
    }

82 83 84 85
    if (_currentSource != nullptr) {
        _updateInterval = _currentSource->minimumUpdateInterval();
        _currentSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
        _currentSource->setUpdateInterval(_updateInterval);
86 87
        connect(_currentSource, &QGeoPositionInfoSource::positionUpdated,       this, &QGCPositionManager::_positionUpdated);
        connect(_currentSource, SIGNAL(error(QGeoPositionInfoSource::Error)),   this, SLOT(_error(QGeoPositionInfoSource::Error)));
88
        _currentSource->startUpdates();
89
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
90 91
}

92 93 94 95
void QGCPositionManager::_error(QGeoPositionInfoSource::Error positioningError)
{
    qWarning() << "QGCPositionManager error" << positioningError;
}