AutoPilotPlugin.cc 4.84 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 "ParameterManager.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
{
107
    _vehicle->getParameterManager()->refreshAllParameters((uint8_t)componentID);
108 109 110 111
}

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

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

120
bool AutoPilotPlugin::factExists(FactSystem::Provider_t provider, int componentId, const QString& name)
Don Gagne's avatar
Don Gagne committed
121
{
122 123
    switch (provider) {
        case FactSystem::ParameterProvider:
124
            return _vehicle->getParameterManager()->parameterExists(componentId, name);
125 126
            
        // Other providers will go here once they come online
Don Gagne's avatar
Don Gagne committed
127
    }
Don Gagne's avatar
Don Gagne committed
128 129 130
    
    Q_ASSERT(false);
    return false;
Don Gagne's avatar
Don Gagne committed
131
}
132

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

Don Gagne's avatar
Don Gagne committed
146
QStringList AutoPilotPlugin::parameterNames(int componentId)
147
{
148
    return _vehicle->getParameterManager()->parameterNames(componentId);
149 150 151 152
}

void AutoPilotPlugin::writeParametersToStream(QTextStream &stream)
{
153
    _vehicle->getParameterManager()->writeParametersToStream(stream);
154 155
}

156
QString AutoPilotPlugin::readParametersFromStream(QTextStream &stream)
157
{
158
    return _vehicle->getParameterManager()->readParametersFromStream(stream);
159
}