MissionCommands.cc 5.97 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.
 *
 ****************************************************************************/
Don Gagne's avatar
Don Gagne committed
9 10 11 12


#include "MissionCommands.h"
#include "FactMetaData.h"
13 14 15 16
#include "Vehicle.h"
#include "FirmwarePluginManager.h"
#include "QGCApplication.h"
#include "QGroundControlQmlGlobal.h"
Don Gagne's avatar
Don Gagne committed
17

18 19
#include <QQmlEngine>

Don Gagne's avatar
Don Gagne committed
20 21
MissionCommands::MissionCommands(QGCApplication* app)
    : QGCTool(app)
22
    , _commonMissionCommands(QStringLiteral(":/json/MavCmdInfoCommon.json"))
Don Gagne's avatar
Don Gagne committed
23
{
24 25 26 27 28
}

void MissionCommands::setToolbox(QGCToolbox* toolbox)
{
    QGCTool::setToolbox(toolbox);
Don Gagne's avatar
Don Gagne committed
29

30 31 32 33
    // Setup overrides
    QString overrideCommonJsonFilename;
    QString overrideFixedWingJsonFilename;
    QString overrideMultiRotorJsonFilename;
Don Gagne's avatar
Don Gagne committed
34

35 36
    QList<MAV_AUTOPILOT> firmwareList;
    firmwareList << MAV_AUTOPILOT_GENERIC << MAV_AUTOPILOT_PX4 << MAV_AUTOPILOT_ARDUPILOTMEGA;
Don Gagne's avatar
Don Gagne committed
37

38 39
    foreach (MAV_AUTOPILOT firmwareType, firmwareList) {
        FirmwarePlugin* plugin = _toolbox->firmwarePluginManager()->firmwarePluginForAutopilot(firmwareType, MAV_TYPE_QUADROTOR);
Don Gagne's avatar
Don Gagne committed
40

41 42 43 44
        plugin->missionCommandOverrides(overrideCommonJsonFilename, overrideFixedWingJsonFilename, overrideMultiRotorJsonFilename);
        _autopilotToCommonMissionCommands[firmwareType] =       new MissionCommandList(overrideCommonJsonFilename);
        _autopilotToFixedWingMissionCommands[firmwareType] =    new MissionCommandList(overrideFixedWingJsonFilename);
        _autopilotToMultiRotorMissionCommands[firmwareType] =   new MissionCommandList(overrideMultiRotorJsonFilename);
Don Gagne's avatar
Don Gagne committed
45 46
    }

47 48
    _createCategories();
}
Don Gagne's avatar
Don Gagne committed
49

50 51 52 53 54 55
/// Create category hierarchy for support commands
void MissionCommands::_createCategories(void)
{
    // FIXME: This isn't quite right since it's hardcoding the firmware providers. But ok for now.
    QList<MAV_AUTOPILOT>    firmwareList;
    firmwareList << MAV_AUTOPILOT_GENERIC << MAV_AUTOPILOT_PX4 << MAV_AUTOPILOT_ARDUPILOTMEGA;
Don Gagne's avatar
Don Gagne committed
56

57 58
    foreach (MAV_AUTOPILOT firmwareType, firmwareList) {
        FirmwarePlugin* plugin = _toolbox->firmwarePluginManager()->firmwarePluginForAutopilot(firmwareType, MAV_TYPE_QUADROTOR);
Don Gagne's avatar
Don Gagne committed
59

60 61 62 63 64
        bool allCommandsSupported = false;
        QList<MAV_CMD> cmdList = plugin->supportedMissionCommands();
        if (cmdList.isEmpty()) {
            allCommandsSupported = true;
            cmdList = _commonMissionCommands.commandsIds();
Don Gagne's avatar
Don Gagne committed
65 66
        }

67 68
        foreach (MAV_CMD command, cmdList) {
            MavCmdInfo* mavCmdInfo = _commonMissionCommands.getMavCmdInfo(command);
Don Gagne's avatar
Don Gagne committed
69

Don Gagne's avatar
Don Gagne committed
70 71 72 73 74 75 76 77
            if (mavCmdInfo) {
                if (mavCmdInfo->friendlyEdit()) {
                    _categoryToMavCmdListMap[firmwareType][mavCmdInfo->category()].append(command);
                } else if (!allCommandsSupported) {
                    qWarning() << "Attempt to add non friendly edit supported command" << command;
                }
            } else {
                qCDebug(MissionCommandsLog) << "Command missing from json" << command;
Don Gagne's avatar
Don Gagne committed
78 79 80
            }
        }
    }
81

Don Gagne's avatar
Don Gagne committed
82
}
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

MAV_AUTOPILOT MissionCommands::_firmwareTypeFromVehicle(Vehicle* vehicle) const
{
    if (vehicle) {
        return vehicle->firmwareType();
    } else {
        QSettings settings;

        // FIXME: Hack duplicated code from QGroundControlQmlGlobal. Had to do this for now since
        // QGroundControlQmlGlobal is not available from C++ side.

        return (MAV_AUTOPILOT)settings.value("OfflineEditingFirmwareType", MAV_AUTOPILOT_ARDUPILOTMEGA).toInt();
    }
}

QString MissionCommands::categoryFromCommand(MavlinkQmlSingleton::Qml_MAV_CMD command) const
{
100
    return _commonMissionCommands.getMavCmdInfo((MAV_CMD)command)->category();
101 102 103 104
}

QVariant MissionCommands::getCommandsForCategory(Vehicle* vehicle, const QString& category) const
{
105 106 107 108 109 110 111 112
    QmlObjectListModel* list = new QmlObjectListModel();
    QQmlEngine::setObjectOwnership(list, QQmlEngine::JavaScriptOwnership);

    foreach (MAV_CMD command, _categoryToMavCmdListMap[_firmwareTypeFromVehicle(vehicle)][category]) {
        list->append(getMavCmdInfo(command, vehicle));
    }

    return QVariant::fromValue(list);
113 114 115 116 117 118
}

const QStringList MissionCommands::categories(Vehicle* vehicle) const
{
    QStringList list;

119
    foreach (const QString &category, _categoryToMavCmdListMap[_firmwareTypeFromVehicle(vehicle)].keys()) {
120 121 122 123 124 125
        list << category;
    }

    return list;
}

126
bool MissionCommands::contains(MAV_CMD command) const
127
{
128 129
    return _commonMissionCommands.contains(command);
}
130

131 132 133 134 135 136
MavCmdInfo* MissionCommands::getMavCmdInfo(MAV_CMD command, Vehicle* vehicle) const
{
    if (!contains(command)) {
        qWarning() << "Unknown command" << command;
        return NULL;
    }
137

138 139
    MavCmdInfo*     mavCmdInfo = NULL;
    MAV_AUTOPILOT   firmwareType = _firmwareTypeFromVehicle(vehicle);
140

141 142
    if (vehicle) {
        if (vehicle->fixedWing()) {
Don Gagne's avatar
Don Gagne committed
143
            if (_autopilotToFixedWingMissionCommands.contains(firmwareType) && _autopilotToFixedWingMissionCommands[firmwareType]->contains(command)) {
144 145 146
                mavCmdInfo = _autopilotToFixedWingMissionCommands[firmwareType]->getMavCmdInfo(command);
            }
        } else if (vehicle->multiRotor()) {
Don Gagne's avatar
Don Gagne committed
147
            if (_autopilotToMultiRotorMissionCommands.contains(firmwareType) && _autopilotToMultiRotorMissionCommands[firmwareType]->contains(command)) {
148 149
                mavCmdInfo = _autopilotToMultiRotorMissionCommands[firmwareType]->getMavCmdInfo(command);
            }
150 151
        }
    }
152

Don Gagne's avatar
Don Gagne committed
153
    if (!mavCmdInfo && _autopilotToCommonMissionCommands.contains(firmwareType) && _autopilotToCommonMissionCommands[firmwareType]->contains(command)) {
154 155 156
        mavCmdInfo = _autopilotToCommonMissionCommands[firmwareType]->getMavCmdInfo(command);
    }

157 158 159 160 161 162 163 164 165 166
    if (!mavCmdInfo) {
        mavCmdInfo = _commonMissionCommands.getMavCmdInfo(command);
    }

    return mavCmdInfo;
}

QList<MAV_CMD> MissionCommands::commandsIds(void) const
{
    return _commonMissionCommands.commandsIds();
167
}