AutoPilotPlugin.cc 5.28 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed
10 11 12 13 14

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

#include "AutoPilotPlugin.h"
15
#include "QGCApplication.h"
16
#include "ParameterLoader.h"
17
#include "UAS.h"
18
#include "FirmwarePlugin.h"
Don Gagne's avatar
Don Gagne committed
19

20 21 22
AutoPilotPlugin::AutoPilotPlugin(Vehicle* vehicle, QObject* parent)
    : QObject(parent)
    , _vehicle(vehicle)
23
    , _firmwarePlugin(vehicle->firmwarePlugin())
24 25 26
    , _parametersReady(false)
    , _missingParameters(false)
	, _setupComplete(false)
Don Gagne's avatar
Don Gagne committed
27
{
28
    Q_ASSERT(vehicle);
29
	
30
	connect(_vehicle->uas(), &UASInterface::disconnected, this, &AutoPilotPlugin::_uasDisconnected);
31

32
	connect(this, &AutoPilotPlugin::parametersReadyChanged, this, &AutoPilotPlugin::_parametersReadyChanged);
33 34
}

35 36 37 38 39
AutoPilotPlugin::~AutoPilotPlugin()
{
    
}

40 41
void AutoPilotPlugin::_uasDisconnected(void)
{
42 43
	_parametersReady = false;
	emit parametersReadyChanged(_parametersReady);
Don Gagne's avatar
Don Gagne committed
44 45
}

46
void AutoPilotPlugin::_parametersReadyChanged(bool parametersReady)
47
{
48
	if (parametersReady) {
49 50
		_recalcSetupComplete();
		if (!_setupComplete) {
51
            qgcApp()->showMessage("One or more vehicle components require setup prior to flight.");
52 53
			
			// Take the user to Vehicle Summary
Don Gagne's avatar
Don Gagne committed
54
            qgcApp()->showSetupView();
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
			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)
{
82
	Q_ASSERT(_parametersReady);
83 84 85
	return _setupComplete;
}

86 87 88
void AutoPilotPlugin::resetAllParametersToDefaults(void)
{
    mavlink_message_t msg;
89
    MAVLinkProtocol* mavlink = qgcApp()->toolbox()->mavlinkProtocol();
90

91 92 93 94 95 96 97 98 99 100 101 102
    mavlink_msg_command_long_pack(mavlink->getSystemId(),
                                  mavlink->getComponentId(),
                                  &msg,
                                  _vehicle->id(),                   // Target systeem
                                  _vehicle->defaultComponentId(),   // Target component
                                  MAV_CMD_PREFLIGHT_STORAGE,
                                  0,                                // Confirmation
                                  2,                                // 2 = Reset params to default
                                  -1,                               // -1 = No change to mission storage
                                  0,                                // 0 = Ignore
                                  0, 0, 0, 0);                      // Unused
    _vehicle->sendMessageOnPriorityLink(msg);
103 104
}

dogmaphobic's avatar
dogmaphobic committed
105
void AutoPilotPlugin::refreshAllParameters(unsigned char componentID)
106
{
dogmaphobic's avatar
dogmaphobic committed
107
    _vehicle->getParameterLoader()->refreshAllParameters((uint8_t)componentID);
108 109 110 111
}

void AutoPilotPlugin::refreshParameter(int componentId, const QString& name)
{
112
    _vehicle->getParameterLoader()->refreshParameter(componentId, name);
113 114 115 116
}

void AutoPilotPlugin::refreshParametersPrefix(int componentId, const QString& namePrefix)
{
117
    _vehicle->getParameterLoader()->refreshParametersPrefix(componentId, namePrefix);
118 119
}

Don Gagne's avatar
Don Gagne committed
120
bool AutoPilotPlugin::parameterExists(int componentId, const QString& name)
121
{
122
    return _vehicle->getParameterLoader()->parameterExists(componentId, name);
123 124
}

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

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

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

Don Gagne's avatar
Don Gagne committed
156
QStringList AutoPilotPlugin::parameterNames(int componentId)
157
{
158
    return _vehicle->getParameterLoader()->parameterNames(componentId);
159 160 161 162
}

const QMap<int, QMap<QString, QStringList> >& AutoPilotPlugin::getGroupMap(void)
{
163
    return _vehicle->getParameterLoader()->getGroupMap();
164 165 166 167
}

void AutoPilotPlugin::writeParametersToStream(QTextStream &stream)
{
168
    _vehicle->getParameterLoader()->writeParametersToStream(stream);
169 170
}

171
QString AutoPilotPlugin::readParametersFromStream(QTextStream &stream)
172
{
173
    return _vehicle->getParameterLoader()->readParametersFromStream(stream);
174
}