AutoPilotPlugin.cc 5.22 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 25 26 27
/*=====================================================================
 
 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 "AutoPilotPlugin.h"
28 29 30
#include "QGCApplication.h"
#include "QGCMessageBox.h"
#include "MainWindow.h"
31
#include "ParameterLoader.h"
32
#include "UAS.h"
33
#include "FirmwarePlugin.h"
Don Gagne's avatar
Don Gagne committed
34

35 36 37
AutoPilotPlugin::AutoPilotPlugin(Vehicle* vehicle, QObject* parent)
    : QObject(parent)
    , _vehicle(vehicle)
38
    , _firmwarePlugin(vehicle->firmwarePlugin())
39 40 41
    , _parametersReady(false)
    , _missingParameters(false)
	, _setupComplete(false)
Don Gagne's avatar
Don Gagne committed
42
{
43
    Q_ASSERT(vehicle);
44
	
45
	connect(_vehicle->uas(), &UASInterface::disconnected, this, &AutoPilotPlugin::_uasDisconnected);
46

47
	connect(this, &AutoPilotPlugin::parametersReadyChanged, this, &AutoPilotPlugin::_parametersReadyChanged);
48 49
}

50 51 52 53 54
AutoPilotPlugin::~AutoPilotPlugin()
{
    
}

55 56
void AutoPilotPlugin::_uasDisconnected(void)
{
57 58
	_parametersReady = false;
	emit parametersReadyChanged(_parametersReady);
Don Gagne's avatar
Don Gagne committed
59 60
}

61
void AutoPilotPlugin::_parametersReadyChanged(bool parametersReady)
62
{
63
	if (parametersReady) {
64 65 66 67 68
		_recalcSetupComplete();
		if (!_setupComplete) {
			QGCMessageBox::warning("Setup", "One or more vehicle components require setup prior to flight.");
			
			// Take the user to Vehicle Summary
69
            MainWindow::instance()->showSetupView();
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
			qgcApp()->processEvents(QEventLoop::ExcludeUserInputEvents);
		}
	}
}

void AutoPilotPlugin::_recalcSetupComplete(void)
{
	bool newSetupComplete = true;
	
	foreach(const QVariant componentVariant, vehicleComponents()) {
		VehicleComponent* component = qobject_cast<VehicleComponent*>(qvariant_cast<QObject *>(componentVariant));
		Q_ASSERT(component);
		
		if (!component->setupComplete()) {
			newSetupComplete = false;
			break;
		}
	}
	
	if (_setupComplete != newSetupComplete) {
		_setupComplete = newSetupComplete;
		emit setupCompleteChanged(_setupComplete);
	}
}

bool AutoPilotPlugin::setupComplete(void)
{
97
	Q_ASSERT(_parametersReady);
98 99 100
	return _setupComplete;
}

101 102 103
void AutoPilotPlugin::resetAllParametersToDefaults(void)
{
    mavlink_message_t msg;
104
    MAVLinkProtocol* mavlink = qgcApp()->toolbox()->mavlinkProtocol();
105

106 107
    mavlink_msg_command_long_pack(mavlink->getSystemId(), mavlink->getComponentId(), &msg, _vehicle->uas()->getUASID(), 0, MAV_CMD_PREFLIGHT_STORAGE, 0, 2, -1, 0, 0, 0, 0, 0);
    _vehicle->sendMessage(msg);
108 109
}

110 111
void AutoPilotPlugin::refreshAllParameters(void)
{
112
    _vehicle->getParameterLoader()->refreshAllParameters();
113 114 115 116
}

void AutoPilotPlugin::refreshParameter(int componentId, const QString& name)
{
117
    _vehicle->getParameterLoader()->refreshParameter(componentId, name);
118 119 120 121
}

void AutoPilotPlugin::refreshParametersPrefix(int componentId, const QString& namePrefix)
{
122
    _vehicle->getParameterLoader()->refreshParametersPrefix(componentId, namePrefix);
123 124
}

Don Gagne's avatar
Don Gagne committed
125
bool AutoPilotPlugin::parameterExists(int componentId, const QString& name)
126
{
127
    return _vehicle->getParameterLoader()->parameterExists(componentId, name);
128 129
}

Don Gagne's avatar
Don Gagne committed
130
Fact* AutoPilotPlugin::getParameterFact(int componentId, const QString& name)
131
{
132
    return _vehicle->getParameterLoader()->getFact(componentId, name);
133 134
}

135
bool AutoPilotPlugin::factExists(FactSystem::Provider_t provider, int componentId, const QString& name)
Don Gagne's avatar
Don Gagne committed
136
{
137 138
    switch (provider) {
        case FactSystem::ParameterProvider:
139
            return _vehicle->getParameterLoader()->parameterExists(componentId, name);
140 141
            
        // Other providers will go here once they come online
Don Gagne's avatar
Don Gagne committed
142
    }
Don Gagne's avatar
Don Gagne committed
143 144 145
    
    Q_ASSERT(false);
    return false;
Don Gagne's avatar
Don Gagne committed
146
}
147

148
Fact* AutoPilotPlugin::getFact(FactSystem::Provider_t provider, int componentId, const QString& name)
149
{
150 151
    switch (provider) {
        case FactSystem::ParameterProvider:
152
            return _vehicle->getParameterLoader()->getFact(componentId, name);
153 154 155
            
        // Other providers will go here once they come online
    }
Don Gagne's avatar
Don Gagne committed
156 157
    
    Q_ASSERT(false);
Don Gagne's avatar
Don Gagne committed
158
    return NULL;
159
}
160

Don Gagne's avatar
Don Gagne committed
161
QStringList AutoPilotPlugin::parameterNames(int componentId)
162
{
163
    return _vehicle->getParameterLoader()->parameterNames(componentId);
164 165 166 167
}

const QMap<int, QMap<QString, QStringList> >& AutoPilotPlugin::getGroupMap(void)
{
168
    return _vehicle->getParameterLoader()->getGroupMap();
169 170 171 172
}

void AutoPilotPlugin::writeParametersToStream(QTextStream &stream)
{
173
    _vehicle->getParameterLoader()->writeParametersToStream(stream);
174 175
}

176
QString AutoPilotPlugin::readParametersFromStream(QTextStream &stream)
177
{
178
    return _vehicle->getParameterLoader()->readParametersFromStream(stream);
179
}