PX4AutoPilotPlugin.cc 7.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 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 "PX4AutoPilotPlugin.h"
#include "AirframeComponent.h"
#include "RadioComponent.h"
#include "SensorsComponent.h"
#include "FlightModesComponent.h"
Don Gagne's avatar
Don Gagne committed
29
#include "SafetyComponent.h"
Don Gagne's avatar
Don Gagne committed
30
#include "AutoPilotPluginManager.h"
Don Gagne's avatar
Don Gagne committed
31 32 33
#include "UASManager.h"
#include "QGCUASParamManagerInterface.h"
#include "PX4ParameterFacts.h"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

/// @file
///     @brief This is the AutoPilotPlugin implementatin for the MAV_AUTOPILOT_PX4 type.
///     @author Don Gagne <don@thegagnes.com>


enum PX4_CUSTOM_MAIN_MODE {
    PX4_CUSTOM_MAIN_MODE_MANUAL = 1,
    PX4_CUSTOM_MAIN_MODE_ALTCTL,
    PX4_CUSTOM_MAIN_MODE_POSCTL,
    PX4_CUSTOM_MAIN_MODE_AUTO,
    PX4_CUSTOM_MAIN_MODE_ACRO,
    PX4_CUSTOM_MAIN_MODE_OFFBOARD,
};

enum PX4_CUSTOM_SUB_MODE_AUTO {
    PX4_CUSTOM_SUB_MODE_AUTO_READY = 1,
    PX4_CUSTOM_SUB_MODE_AUTO_TAKEOFF,
    PX4_CUSTOM_SUB_MODE_AUTO_LOITER,
    PX4_CUSTOM_SUB_MODE_AUTO_MISSION,
    PX4_CUSTOM_SUB_MODE_AUTO_RTL,
    PX4_CUSTOM_SUB_MODE_AUTO_LAND,
    PX4_CUSTOM_SUB_MODE_AUTO_RTGS
};

union px4_custom_mode {
    struct {
        uint16_t reserved;
        uint8_t main_mode;
        uint8_t sub_mode;
    };
    uint32_t data;
    float data_float;
};

69 70 71 72
PX4AutoPilotPlugin::PX4AutoPilotPlugin(UASInterface* uas, QObject* parent) :
    AutoPilotPlugin(parent),
    _uas(uas),
    _parameterFacts(NULL)
73
{
74 75 76 77
    Q_ASSERT(uas);
    
    _parameterFacts = new PX4ParameterFacts(uas, this);
    Q_CHECK_PTR(_parameterFacts);
78
    
79
    connect(_parameterFacts, &PX4ParameterFacts::factsReady, this, &PX4AutoPilotPlugin::pluginReady);
Don Gagne's avatar
Don Gagne committed
80 81 82 83 84 85
    
    PX4ParameterFacts::loadParameterFactMetaData();
}

PX4AutoPilotPlugin::~PX4AutoPilotPlugin()
{
86
    delete _parameterFacts;
Don Gagne's avatar
Don Gagne committed
87
    PX4ParameterFacts::deleteParameterFactMetaData();
88 89
}

90
QList<AutoPilotPluginManager::FullMode_t> PX4AutoPilotPlugin::getModes(void)
91
{
92 93 94
    union px4_custom_mode                       px4_cm;
    AutoPilotPluginManager::FullMode_t          fullMode;
    QList<AutoPilotPluginManager::FullMode_t>   modeList;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    
    px4_cm.data = 0;
    px4_cm.main_mode = PX4_CUSTOM_MAIN_MODE_MANUAL;
    fullMode.baseMode = MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | MAV_MODE_FLAG_MANUAL_INPUT_ENABLED;
    fullMode.customMode = px4_cm.data;
    modeList << fullMode;
    
    px4_cm.data = 0;
    px4_cm.main_mode = PX4_CUSTOM_MAIN_MODE_ALTCTL;
    fullMode.baseMode = MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | MAV_MODE_FLAG_MANUAL_INPUT_ENABLED | MAV_MODE_FLAG_STABILIZE_ENABLED;
    fullMode.customMode = px4_cm.data;
    modeList << fullMode;
    
    px4_cm.data = 0;
    px4_cm.main_mode = PX4_CUSTOM_MAIN_MODE_POSCTL;
    fullMode.baseMode = MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | MAV_MODE_FLAG_MANUAL_INPUT_ENABLED | MAV_MODE_FLAG_STABILIZE_ENABLED | MAV_MODE_FLAG_GUIDED_ENABLED;
    fullMode.customMode = px4_cm.data;
    modeList << fullMode;
    
    px4_cm.data = 0;
    px4_cm.main_mode = PX4_CUSTOM_MAIN_MODE_AUTO;
    fullMode.baseMode = MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | MAV_MODE_FLAG_AUTO_ENABLED | MAV_MODE_FLAG_STABILIZE_ENABLED | MAV_MODE_FLAG_GUIDED_ENABLED;
    fullMode.customMode = px4_cm.data;
    modeList << fullMode;
    
    px4_cm.data = 0;
    px4_cm.main_mode = PX4_CUSTOM_MAIN_MODE_OFFBOARD;
    fullMode.baseMode = MAV_MODE_FLAG_CUSTOM_MODE_ENABLED | MAV_MODE_FLAG_AUTO_ENABLED | MAV_MODE_FLAG_STABILIZE_ENABLED | MAV_MODE_FLAG_GUIDED_ENABLED;
    fullMode.customMode = px4_cm.data;
    modeList << fullMode;
    
    return modeList;
}

129
QString PX4AutoPilotPlugin::getShortModeText(uint8_t baseMode, uint32_t customMode)
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
{
    QString mode;
    
    Q_ASSERT(baseMode & MAV_MODE_FLAG_CUSTOM_MODE_ENABLED);
    
    if (baseMode & MAV_MODE_FLAG_CUSTOM_MODE_ENABLED) {
        union px4_custom_mode px4_mode;
        px4_mode.data = customMode;
        
        if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_MANUAL) {
            mode = "|MANUAL";
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_ALTCTL) {
            mode = "|ALTCTL";
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_POSCTL) {
            mode = "|POSCTL";
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_AUTO) {
            mode = "|AUTO";
            if (px4_mode.sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_READY) {
                mode += "|READY";
            } else if (px4_mode.sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_TAKEOFF) {
                mode += "|TAKEOFF";
            } else if (px4_mode.sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_LOITER) {
                mode += "|LOITER";
            } else if (px4_mode.sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_MISSION) {
                mode += "|MISSION";
            } else if (px4_mode.sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_RTL) {
                mode += "|RTL";
            } else if (px4_mode.sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_LAND) {
                mode += "|LAND";
            }
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_OFFBOARD) {
            mode = "|OFFBOARD";
        }
    } else {
164 165 166 167 168 169 170 171 172 173 174 175 176 177
        // use base_mode - not autopilot-specific
        if (baseMode == 0) {
            mode = "|PREFLIGHT";
        } else if (baseMode & MAV_MODE_FLAG_DECODE_POSITION_AUTO) {
            mode = "|AUTO";
        } else if (baseMode & MAV_MODE_FLAG_DECODE_POSITION_MANUAL) {
            mode = "|MANUAL";
            if (baseMode & MAV_MODE_FLAG_DECODE_POSITION_GUIDED) {
                mode += "|GUIDED";
            } else if (baseMode & MAV_MODE_FLAG_DECODE_POSITION_STABILIZE) {
                mode += "|STABILIZED";
            }
        }

178 179 180 181
    }
    
    return mode;
}
Don Gagne's avatar
Don Gagne committed
182

183
void PX4AutoPilotPlugin::clearStaticData(void)
Don Gagne's avatar
Don Gagne committed
184
{
185
    PX4ParameterFacts::clearStaticData();
Don Gagne's avatar
Don Gagne committed
186 187
}

188
bool PX4AutoPilotPlugin::pluginIsReady(void) const
Don Gagne's avatar
Don Gagne committed
189
{
190
    return _parameterFacts->factsAreReady();
Don Gagne's avatar
Don Gagne committed
191
}
192 193 194 195 196 197 198 199

const QVariantList& PX4AutoPilotPlugin::components(void)
{
    if (_components.count() == 0) {
        VehicleComponent* component;

        Q_ASSERT(_uas);
        
Don Gagne's avatar
Don Gagne committed
200
        component = new AirframeComponent(_uas, this);
201 202 203
        Q_CHECK_PTR(component);
        _components.append(QVariant::fromValue(component));
        
Don Gagne's avatar
Don Gagne committed
204
        component = new RadioComponent(_uas, this);
205 206 207
        Q_CHECK_PTR(component);
        _components.append(QVariant::fromValue(component));
        
Don Gagne's avatar
Don Gagne committed
208
        component = new FlightModesComponent(_uas, this);
209 210 211
        Q_CHECK_PTR(component);
        _components.append(QVariant::fromValue(component));
        
Don Gagne's avatar
Don Gagne committed
212 213 214 215 216
        component = new SensorsComponent(_uas, this);
        Q_CHECK_PTR(component);
        _components.append(QVariant::fromValue(component));

        component = new SafetyComponent(_uas, this);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        Q_CHECK_PTR(component);
        _components.append(QVariant::fromValue(component));
    }
    
    return _components;
}

const QVariantMap& PX4AutoPilotPlugin::parameters(void)
{
    return _parameterFacts->factMap();
}

QUrl PX4AutoPilotPlugin::setupBackgroundImage(void)
{
    return QUrl::fromUserInput("qrc:/qml/px4fmu_2.x.png");
}