PX4AutoPilotPlugin.cc 11.9 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
/*=====================================================================
 
 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"
Don Gagne's avatar
Don Gagne committed
25
#include "AutoPilotPluginManager.h"
Don Gagne's avatar
Don Gagne committed
26
#include "UASManager.h"
27
#include "PX4ParameterLoader.h"
28
#include "PX4AirframeLoader.h"
Don Gagne's avatar
Don Gagne committed
29
#include "FlightModesComponentController.h"
30
#include "AirframeComponentController.h"
31
#include "QGCMessageBox.h"
32 33 34 35 36 37 38 39 40 41 42 43

/// @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,
44
    PX4_CUSTOM_MAIN_MODE_STABILIZED,
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
};

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;
};

67
PX4AutoPilotPlugin::PX4AutoPilotPlugin(UASInterface* uas, QObject* parent) :
68
    AutoPilotPlugin(uas, parent),
Don Gagne's avatar
Don Gagne committed
69 70 71 72 73
    _parameterFacts(NULL),
    _airframeComponent(NULL),
    _radioComponent(NULL),
    _flightModesComponent(NULL),
    _sensorsComponent(NULL),
74
    _safetyComponent(NULL),
dogmaphobic's avatar
dogmaphobic committed
75
    _powerComponent(NULL),
76
    _incorrectParameterVersion(false)
77
{
78 79
    Q_ASSERT(uas);
    
80
    _parameterFacts = new PX4ParameterLoader(this, uas, this);
81
    Q_CHECK_PTR(_parameterFacts);
82
    
83
    connect(_parameterFacts, &PX4ParameterLoader::parametersReady, this, &PX4AutoPilotPlugin::_pluginReadyPreChecks);
84
    connect(_parameterFacts, &PX4ParameterLoader::parameterListProgress, this, &PX4AutoPilotPlugin::parameterListProgress);
85 86 87

    _airframeFacts = new PX4AirframeLoader(this, uas, this);
    Q_CHECK_PTR(_airframeFacts);
Don Gagne's avatar
Don Gagne committed
88
    
89
    PX4ParameterLoader::loadParameterFactMetaData();
90
    PX4AirframeLoader::loadAirframeFactMetaData();
Don Gagne's avatar
Don Gagne committed
91 92 93 94
}

PX4AutoPilotPlugin::~PX4AutoPilotPlugin()
{
95
    delete _parameterFacts;
96
    delete _airframeFacts;
97 98
}

99
QList<AutoPilotPluginManager::FullMode_t> PX4AutoPilotPlugin::getModes(void)
100
{
101 102 103
    union px4_custom_mode                       px4_cm;
    AutoPilotPluginManager::FullMode_t          fullMode;
    QList<AutoPilotPluginManager::FullMode_t>   modeList;
104 105 106 107 108 109
    
    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;
110 111 112 113 114 115 116 117 118 119 120 121

    px4_cm.data = 0;
    px4_cm.main_mode = PX4_CUSTOM_MAIN_MODE_ACRO;
    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_STABILIZED;
    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;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    
    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;
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
QString PX4AutoPilotPlugin::getAudioModeText(uint8_t baseMode, uint32_t customMode)
{
    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_ACRO) {
            mode = "caro";
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_STABILIZED) {
            mode = "stabilized";
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_ALTCTL) {
            mode = "altitude control";
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_POSCTL) {
            mode = "position control";
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_AUTO) {
            mode = "auto and ";
            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 += "taking off";
            } else if (px4_mode.sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_LOITER) {
                mode += "loitering";
            } else if (px4_mode.sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_MISSION) {
                mode += "following waypoints";
            } else if (px4_mode.sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_RTL) {
                mode += "returning to land";
            } else if (px4_mode.sub_mode == PX4_CUSTOM_SUB_MODE_AUTO_LAND) {
                mode += "landing";
            }
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_OFFBOARD) {
            mode = "offboard controlled";
        }

        if (baseMode != 0)
        {
            mode += " mode";
        }

        // ARMED STATE DECODING
        if (baseMode & (uint8_t)MAV_MODE_FLAG_DECODE_POSITION_SAFETY)
        {
            mode.append(" and armed");
        }

    } else {
        mode = "";
    }

    return mode;
}

207
QString PX4AutoPilotPlugin::getShortModeText(uint8_t baseMode, uint32_t customMode)
208 209 210 211 212 213 214 215 216 217 218
{
    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";
219 220 221 222
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_ACRO) {
            mode = "|ACRO";
        } else if (px4_mode.main_mode == PX4_CUSTOM_MAIN_MODE_STABILIZED) {
            mode = "|STAB";
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        } 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 {
246 247 248 249 250 251 252 253 254 255 256 257 258 259
        // 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";
            }
        }

260 261 262 263
    }
    
    return mode;
}
Don Gagne's avatar
Don Gagne committed
264

265
void PX4AutoPilotPlugin::clearStaticData(void)
Don Gagne's avatar
Don Gagne committed
266
{
267
    PX4ParameterLoader::clearStaticData();
268
    PX4AirframeLoader::clearStaticData();
Don Gagne's avatar
Don Gagne committed
269 270
}

271
const QVariantList& PX4AutoPilotPlugin::vehicleComponents(void)
272
{
273
    if (_components.count() == 0 && !_incorrectParameterVersion) {
274 275
        Q_ASSERT(_uas);
        
Don Gagne's avatar
Don Gagne committed
276 277
        _airframeComponent = new AirframeComponent(_uas, this);
        Q_CHECK_PTR(_airframeComponent);
278
        _airframeComponent->setupTriggerSignals();
Don Gagne's avatar
Don Gagne committed
279
        _components.append(QVariant::fromValue((VehicleComponent*)_airframeComponent));
280
        
Don Gagne's avatar
Don Gagne committed
281 282
        _radioComponent = new RadioComponent(_uas, this);
        Q_CHECK_PTR(_radioComponent);
283
        _radioComponent->setupTriggerSignals();
Don Gagne's avatar
Don Gagne committed
284
        _components.append(QVariant::fromValue((VehicleComponent*)_radioComponent));
285
        
Don Gagne's avatar
Don Gagne committed
286 287
        _flightModesComponent = new FlightModesComponent(_uas, this);
        Q_CHECK_PTR(_flightModesComponent);
288
        _flightModesComponent->setupTriggerSignals();
Don Gagne's avatar
Don Gagne committed
289
        _components.append(QVariant::fromValue((VehicleComponent*)_flightModesComponent));
290
        
Don Gagne's avatar
Don Gagne committed
291 292
        _sensorsComponent = new SensorsComponent(_uas, this);
        Q_CHECK_PTR(_sensorsComponent);
293
        _sensorsComponent->setupTriggerSignals();
Don Gagne's avatar
Don Gagne committed
294
        _components.append(QVariant::fromValue((VehicleComponent*)_sensorsComponent));
Don Gagne's avatar
Don Gagne committed
295

dogmaphobic's avatar
dogmaphobic committed
296 297
        _powerComponent = new PowerComponent(_uas, this);
        Q_CHECK_PTR(_powerComponent);
298
        _powerComponent->setupTriggerSignals();
dogmaphobic's avatar
dogmaphobic committed
299 300
        _components.append(QVariant::fromValue((VehicleComponent*)_powerComponent));

Don Gagne's avatar
Don Gagne committed
301 302
        _safetyComponent = new SafetyComponent(_uas, this);
        Q_CHECK_PTR(_safetyComponent);
303
        _safetyComponent->setupTriggerSignals();
Don Gagne's avatar
Don Gagne committed
304
        _components.append(QVariant::fromValue((VehicleComponent*)_safetyComponent));
305 306 307 308 309
    }
    
    return _components;
}

Don Gagne's avatar
Don Gagne committed
310 311
/// This will perform various checks prior to signalling that the plug in ready
void PX4AutoPilotPlugin::_pluginReadyPreChecks(void)
312
{
Don Gagne's avatar
Don Gagne committed
313 314 315
    // Check for older parameter version set
    // FIXME: Firmware is moving to version stamp parameter set. Once that is complete the version stamp
    // should be used instead.
Don Gagne's avatar
Don Gagne committed
316
    if (parameterExists(FactSystem::defaultComponentId, "SENS_GYRO_XOFF")) {
317
        _incorrectParameterVersion = true;
318 319 320 321
        QGCMessageBox::warning("Setup", "This version of GroundControl can only perform vehicle setup on a newer version of firmware. "
										"Please perform a Firmware Upgrade if you wish to use Vehicle Setup.");
	}
	
322 323
    _pluginReady = true;
    emit pluginReadyChanged(_pluginReady);
324
}