MissionCommands.cc 6.21 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*===================================================================
QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

#include "MissionCommands.h"
#include "FactMetaData.h"
25 26 27 28
#include "Vehicle.h"
#include "FirmwarePluginManager.h"
#include "QGCApplication.h"
#include "QGroundControlQmlGlobal.h"
Don Gagne's avatar
Don Gagne committed
29 30 31

MissionCommands::MissionCommands(QGCApplication* app)
    : QGCTool(app)
32
    , _commonMissionCommands(QStringLiteral(":/json/MavCmdInfoCommon.json"))
Don Gagne's avatar
Don Gagne committed
33
{
34 35 36 37 38
}

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

40 41 42 43
    // Setup overrides
    QString overrideCommonJsonFilename;
    QString overrideFixedWingJsonFilename;
    QString overrideMultiRotorJsonFilename;
Don Gagne's avatar
Don Gagne committed
44

45 46
    QList<MAV_AUTOPILOT> firmwareList;
    firmwareList << MAV_AUTOPILOT_GENERIC << MAV_AUTOPILOT_PX4 << MAV_AUTOPILOT_ARDUPILOTMEGA;
Don Gagne's avatar
Don Gagne committed
47

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

51 52 53 54
        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
55 56
    }

57 58
    _createCategories();
}
Don Gagne's avatar
Don Gagne committed
59

60 61 62 63 64 65
/// 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
66

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

70 71 72 73 74
        bool allCommandsSupported = false;
        QList<MAV_CMD> cmdList = plugin->supportedMissionCommands();
        if (cmdList.isEmpty()) {
            allCommandsSupported = true;
            cmdList = _commonMissionCommands.commandsIds();
Don Gagne's avatar
Don Gagne committed
75 76
        }

77 78
        foreach (MAV_CMD command, cmdList) {
            MavCmdInfo* mavCmdInfo = _commonMissionCommands.getMavCmdInfo(command);
Don Gagne's avatar
Don Gagne committed
79 80

            if (mavCmdInfo->friendlyEdit()) {
81 82 83
                _categoryToMavCmdListMap[firmwareType][mavCmdInfo->category()].append(command);
            } else if (!allCommandsSupported) {
                qWarning() << "Attempt to add non friendly edit supported command";
Don Gagne's avatar
Don Gagne committed
84 85 86
            }
        }
    }
87

Don Gagne's avatar
Don Gagne committed
88
}
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

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
{
106
    return _commonMissionCommands.getMavCmdInfo((MAV_CMD)command)->category();
107 108 109 110
}

QVariant MissionCommands::getCommandsForCategory(Vehicle* vehicle, const QString& category) const
{
111 112 113 114 115 116 117 118
    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);
119 120 121 122 123 124
}

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

125
    foreach (const QString &category, _categoryToMavCmdListMap[_firmwareTypeFromVehicle(vehicle)].keys()) {
126 127 128 129 130 131
        list << category;
    }

    return list;
}

132
bool MissionCommands::contains(MAV_CMD command) const
133
{
134 135
    return _commonMissionCommands.contains(command);
}
136

137 138 139 140 141 142
MavCmdInfo* MissionCommands::getMavCmdInfo(MAV_CMD command, Vehicle* vehicle) const
{
    if (!contains(command)) {
        qWarning() << "Unknown command" << command;
        return NULL;
    }
143

144 145
    MavCmdInfo*     mavCmdInfo = NULL;
    MAV_AUTOPILOT   firmwareType = _firmwareTypeFromVehicle(vehicle);
146

147 148 149 150 151 152 153 154 155 156 157 158
    if (vehicle) {
        if (vehicle->fixedWing()) {
            if (_autopilotToFixedWingMissionCommands[firmwareType]->contains(command)) {
                mavCmdInfo = _autopilotToFixedWingMissionCommands[firmwareType]->getMavCmdInfo(command);
            }
        } else if (vehicle->multiRotor()) {
            if (_autopilotToMultiRotorMissionCommands[firmwareType]->contains(command)) {
                mavCmdInfo = _autopilotToMultiRotorMissionCommands[firmwareType]->getMavCmdInfo(command);
            }
        } else {
            if (_autopilotToCommonMissionCommands[firmwareType]->contains(command)) {
                mavCmdInfo = _autopilotToCommonMissionCommands[firmwareType]->getMavCmdInfo(command);
159 160 161
            }
        }
    }
162 163 164 165 166 167 168 169 170 171 172

    if (!mavCmdInfo) {
        mavCmdInfo = _commonMissionCommands.getMavCmdInfo(command);
    }

    return mavCmdInfo;
}

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