FollowMe.cc 6.3 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 11 12 13

#include <QElapsedTimer>
#include <cmath>

#include "MultiVehicleManager.h"
14
#include "FirmwarePlugin.h"
Jimmy Johnson's avatar
Jimmy Johnson committed
15 16 17
#include "MAVLinkProtocol.h"
#include "FollowMe.h"
#include "Vehicle.h"
Jimmy Johnson's avatar
Jimmy Johnson committed
18
#include "PositionManager.h"
19 20
#include "SettingsManager.h"
#include "AppSettings.h"
Jimmy Johnson's avatar
Jimmy Johnson committed
21

22
FollowMe::FollowMe(QGCApplication* app, QGCToolbox* toolbox)
23
    : QGCTool(app, toolbox)
Jimmy Johnson's avatar
Jimmy Johnson committed
24
{
Jimmy Johnson's avatar
Jimmy Johnson committed
25
    _gcsMotionReportTimer.setSingleShot(false);
26 27 28 29 30
}

void FollowMe::setToolbox(QGCToolbox* toolbox)
{
    QGCTool::setToolbox(toolbox);
Jimmy Johnson's avatar
Jimmy Johnson committed
31

32 33 34 35
    connect(&_gcsMotionReportTimer,                                     &QTimer::timeout,       this, &FollowMe::_sendGCSMotionReport);
    connect(toolbox->settingsManager()->appSettings()->followTarget(),  &Fact::rawValueChanged, this, &FollowMe::_settingsChanged);

    _settingsChanged();
36
}
Jimmy Johnson's avatar
Jimmy Johnson committed
37

38
void FollowMe::_settingsChanged()
39
{
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    _currentMode = _toolbox->settingsManager()->appSettings()->followTarget()->rawValue().toUInt();

    switch (_currentMode) {
    case MODE_NEVER:
        disconnect(_toolbox->multiVehicleManager(), &MultiVehicleManager::vehicleAdded,     this, &FollowMe::_vehicleAdded);
        disconnect(_toolbox->multiVehicleManager(), &MultiVehicleManager::vehicleRemoved,   this, &FollowMe::_vehicleRemoved);
        _disableFollowSend();
        break;
    case MODE_ALWAYS:
        connect(_toolbox->multiVehicleManager(), &MultiVehicleManager::vehicleAdded,    this, &FollowMe::_vehicleAdded);
        connect(_toolbox->multiVehicleManager(), &MultiVehicleManager::vehicleRemoved,  this, &FollowMe::_vehicleRemoved);
        _enableFollowSend();
        break;
    case MODE_FOLLOWME:
        connect(_toolbox->multiVehicleManager(), &MultiVehicleManager::vehicleAdded,    this, &FollowMe::_vehicleAdded);
        connect(_toolbox->multiVehicleManager(), &MultiVehicleManager::vehicleRemoved,  this, &FollowMe::_vehicleRemoved);
        _enableIfVehicleInFollow();
        break;
58
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
59 60
}

61
void FollowMe::_enableFollowSend()
Jimmy Johnson's avatar
Jimmy Johnson committed
62
{
63
    if (!_gcsMotionReportTimer.isActive()) {
Don Gagne's avatar
Don Gagne committed
64
        _gcsMotionReportTimer.setInterval(qMin(_toolbox->qgcPositionManager()->updateInterval(), 250));
65 66
        _gcsMotionReportTimer.start();
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
67 68
}

69
void FollowMe::_disableFollowSend()
Jimmy Johnson's avatar
Jimmy Johnson committed
70
{
71 72 73
    if (_gcsMotionReportTimer.isActive()) {
        _gcsMotionReportTimer.stop();
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
74 75
}

76
void FollowMe::_sendGCSMotionReport()
Jimmy Johnson's avatar
Jimmy Johnson committed
77
{
78 79 80
    QGeoPositionInfo    geoPositionInfo =   _toolbox->qgcPositionManager()->geoPositionInfo();
    QGeoCoordinate      gcsCoordinate =     geoPositionInfo.coordinate();

81
    if (!geoPositionInfo.isValid()) {
82 83
        return;
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
84

85 86
    GCSMotionReport motionReport = {};
    uint8_t         estimatation_capabilities = 0;
Jimmy Johnson's avatar
Jimmy Johnson committed
87

88
    // get the current location coordinates
Jimmy Johnson's avatar
Jimmy Johnson committed
89

90 91 92 93
    motionReport.lat_int =          static_cast<int>(gcsCoordinate.latitude()  * 1e7);
    motionReport.lon_int =          static_cast<int>(gcsCoordinate.longitude() * 1e7);
    motionReport.altMetersAMSL =    gcsCoordinate.altitude();
    estimatation_capabilities |=    (1 << POS);
Jimmy Johnson's avatar
Jimmy Johnson committed
94

95
    if (geoPositionInfo.hasAttribute(QGeoPositionInfo::Direction) == true) {
96
        estimatation_capabilities |= (1 << HEADING);
97 98
        motionReport.headingDegrees = geoPositionInfo.attribute(QGeoPositionInfo::Direction);
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
99

100
    // get the current eph and epv
Jimmy Johnson's avatar
Jimmy Johnson committed
101

102 103 104
    if (geoPositionInfo.hasAttribute(QGeoPositionInfo::HorizontalAccuracy)) {
        motionReport.pos_std_dev[0] = motionReport.pos_std_dev[1] = geoPositionInfo.attribute(QGeoPositionInfo::HorizontalAccuracy);
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
105

106 107 108
    if (geoPositionInfo.hasAttribute(QGeoPositionInfo::VerticalAccuracy)) {
        motionReport.pos_std_dev[2] = geoPositionInfo.attribute(QGeoPositionInfo::VerticalAccuracy);
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
109

110 111 112 113 114
    // calculate z velocity if it's available

    if (geoPositionInfo.hasAttribute(QGeoPositionInfo::VerticalSpeed)) {
        motionReport.vzMetersPerSec = geoPositionInfo.attribute(QGeoPositionInfo::VerticalSpeed);
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
115

116
    // calculate x,y velocity if it's available
Jimmy Johnson's avatar
Jimmy Johnson committed
117

118 119
    if (geoPositionInfo.hasAttribute(QGeoPositionInfo::Direction) && geoPositionInfo.hasAttribute(QGeoPositionInfo::GroundSpeed) == true) {
        estimatation_capabilities |= (1 << VEL);
Jimmy Johnson's avatar
Jimmy Johnson committed
120

121 122
        qreal direction = _degreesToRadian(geoPositionInfo.attribute(QGeoPositionInfo::Direction));
        qreal velocity  = geoPositionInfo.attribute(QGeoPositionInfo::GroundSpeed);
Jimmy Johnson's avatar
Jimmy Johnson committed
123

124 125 126 127 128 129
        motionReport.vxMetersPerSec = cos(direction)*velocity;
        motionReport.vyMetersPerSec = sin(direction)*velocity;
    } else {
        motionReport.vxMetersPerSec = 0;
        motionReport.vyMetersPerSec = 0;
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
130

131
    QmlObjectListModel* vehicles = _toolbox->multiVehicleManager()->vehicles();
Jimmy Johnson's avatar
Jimmy Johnson committed
132

133 134 135 136
    for (int i=0; i<vehicles->count(); i++) {
        Vehicle* vehicle = vehicles->value<Vehicle*>(i);
        if (_currentMode == MODE_ALWAYS || (_currentMode == MODE_FOLLOWME && _isFollowFlightMode(vehicle, vehicle->flightMode()))) {
            vehicle->firmwarePlugin()->sendGCSMotionReport(vehicle, motionReport, estimatation_capabilities);
Jimmy Johnson's avatar
Jimmy Johnson committed
137 138 139 140
        }
    }
}

141 142 143 144 145 146
double FollowMe::_degreesToRadian(double deg)
{
    return deg * M_PI / 180.0;
}

void FollowMe::_vehicleAdded(Vehicle* vehicle)
Jimmy Johnson's avatar
Jimmy Johnson committed
147
{
148 149 150 151 152 153 154 155 156
    connect(vehicle, &Vehicle::flightModeChanged, this, &FollowMe::_enableIfVehicleInFollow);
    _enableIfVehicleInFollow();
}

void FollowMe::_vehicleRemoved(Vehicle* vehicle)
{
    disconnect(vehicle, &Vehicle::flightModeChanged, this, &FollowMe::_enableIfVehicleInFollow);
    _enableIfVehicleInFollow();
}
157

158 159 160 161
void FollowMe::_enableIfVehicleInFollow(void)
{
    if (_currentMode == MODE_ALWAYS) {
        // System always enabled
162 163 164
        return;
    }

165 166 167 168 169 170 171 172
    // Any vehicle in follow mode will enable the system
    QmlObjectListModel* vehicles = _toolbox->multiVehicleManager()->vehicles();

    for (int i=0; i< vehicles->count(); i++) {
        Vehicle* vehicle = vehicles->value<Vehicle*>(i);
        if (_isFollowFlightMode(vehicle, vehicle->flightMode())) {
            _enableFollowSend();
            return;
Jimmy Johnson's avatar
Jimmy Johnson committed
173 174
        }
    }
175 176

    _disableFollowSend();
Jimmy Johnson's avatar
Jimmy Johnson committed
177 178
}

179
bool FollowMe::_isFollowFlightMode (Vehicle* vehicle, const QString& flightMode)
Jimmy Johnson's avatar
Jimmy Johnson committed
180
{
181
    return flightMode.compare(vehicle->followFlightMode()) == 0;
Jimmy Johnson's avatar
Jimmy Johnson committed
182
}