FollowMe.cc 6.25 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 96 97
    if (geoPositionInfo.hasAttribute(QGeoPositionInfo::Direction) == true) {
        motionReport.headingDegrees = geoPositionInfo.attribute(QGeoPositionInfo::Direction);
    }
Jimmy Johnson's avatar
Jimmy Johnson committed
98

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

101 102 103
    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
104

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

109 110 111 112 113
    // 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
114

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

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

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

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

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

132 133 134 135
    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
136 137 138 139
        }
    }
}

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

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

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

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

164 165 166 167 168 169 170 171
    // 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
172 173
        }
    }
174 175

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

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