AirframeComponentController.cc 4.83 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 29 30 31 32 33 34 35 36 37 38 39
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009, 2015 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/>.
 
 ======================================================================*/

/// @file
///     @author Don Gagne <don@thegagnes.com>

#include "AirframeComponentController.h"
#include "AirframeComponentAirframes.h"
#include "QGCMAVLink.h"
#include "UASManager.h"
#include "AutoPilotPluginManager.h"
#include "QGCApplication.h"
#include "QGCMessageBox.h"

#include <QVariant>
#include <QQmlProperty>

bool AirframeComponentController::_typesRegistered = false;

40
AirframeComponentController::AirframeComponentController(void) :
Don Gagne's avatar
Don Gagne committed
41
    _currentVehicleIndex(0),
42 43
    _autostartId(0),
    _showCustomConfigPanel(false)
44 45 46 47 48 49 50
{
    if (!_typesRegistered) {
        _typesRegistered = true;
        qmlRegisterUncreatableType<AirframeType>("QGroundControl.Controllers", 1, 0, "AiframeType", "Can only reference AirframeType");
        qmlRegisterUncreatableType<Airframe>("QGroundControl.Controllers", 1, 0, "Aiframe", "Can only reference Airframe");
    }
    
51 52 53 54 55 56
    QStringList usedFacts;
    usedFacts << "SYS_AUTOSTART" << "SYS_AUTOCONFIG";
    if (!_allFactsExists(usedFacts)) {
        return;
    }
    
57 58 59
    // Load up member variables
    
    bool autostartFound = false;
60
    _autostartId = _autopilot->getParameterFact("SYS_AUTOSTART")->value().toInt();
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    
    for (const AirframeComponentAirframes::AirframeType_t* pType=&AirframeComponentAirframes::rgAirframeTypes[0]; pType->name != NULL; pType++) {
        AirframeType* airframeType = new AirframeType(pType->name, pType->imageResource, this);
        Q_CHECK_PTR(airframeType);
        
        int index = 0;
        for (const AirframeComponentAirframes::AirframeInfo_t* pInfo=&pType->rgAirframeInfo[0]; pInfo->name != NULL; pInfo++) {
            if (_autostartId == pInfo->autostartId) {
                Q_ASSERT(!autostartFound);
                autostartFound = true;
                _currentAirframeType = pType->name;
                _currentVehicleName = pInfo->name;
                _currentVehicleIndex = index;
            }
            airframeType->addAirframe(pInfo->name, pInfo->autostartId);
            index++;
        }
        
        _airframeTypes.append(QVariant::fromValue(airframeType));
    }
81
    
82 83 84
    if (_autostartId != 0 && !autostartFound) {
        _showCustomConfigPanel = true;
        emit showCustomConfigPanelChanged(true);
Don Gagne's avatar
Don Gagne committed
85
    }
86 87 88 89 90 91 92 93 94
}

AirframeComponentController::~AirframeComponentController()
{

}

void AirframeComponentController::changeAutostart(void)
{
Don Gagne's avatar
Don Gagne committed
95
	if (UASManager::instance()->getUASList().count() > 1) {
Don Gagne's avatar
Don Gagne committed
96 97 98 99
		QGCMessageBox::warning("Airframe Config", "You cannot change airframe configuration while connected to multiple vehicles.");
		return;
	}
	
100 101
    qgcApp()->setOverrideCursor(Qt::WaitCursor);
    
102 103
    _autopilot->getParameterFact("SYS_AUTOSTART")->setValue(_autostartId);
    _autopilot->getParameterFact("SYS_AUTOCONFIG")->setValue(1);
104 105
    
    
106 107 108 109
    // Wait for the parameters to flow through system
    qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
    QGC::SLEEP::sleep(1);
    qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
110
    
111
    // Reboot board
112 113 114 115 116
    
    _uas->executeCommand(MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN, 1, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0);
    qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
    QGC::SLEEP::sleep(1);
    qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
117
    LinkManager::instance()->disconnectAll();
118
    
119
    qgcApp()->restoreOverrideCursor();
120 121 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 150 151 152 153 154
}

AirframeType::AirframeType(const QString& name, const QString& imageResource, QObject* parent) :
    QObject(parent),
    _name(name),
    _imageResource(imageResource)
{
    
}

AirframeType::~AirframeType()
{
    
}

void AirframeType::addAirframe(const QString& name, int autostartId)
{
    Airframe* airframe = new Airframe(name, autostartId);
    Q_CHECK_PTR(airframe);
    
    _airframes.append(QVariant::fromValue(airframe));
}

Airframe::Airframe(const QString& name, int autostartId, QObject* parent) :
    QObject(parent),
    _name(name),
    _autostartId(autostartId)
{
    
}

Airframe::~Airframe()
{
    
}