2012-06-25 19:41:59 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file modeluavproxy.cpp
|
2012-08-20 16:10:17 +01:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
2012-06-25 19:41:59 +01:00
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
|
|
|
* @addtogroup OPMapPlugin OpenPilot Map Plugin
|
|
|
|
* @{
|
|
|
|
* @brief The OpenPilot Map plugin
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program 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.
|
|
|
|
*
|
|
|
|
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2012-06-17 20:09:21 +01:00
|
|
|
#include "modeluavoproxy.h"
|
2012-06-20 15:00:15 +01:00
|
|
|
#include "extensionsystem/pluginmanager.h"
|
2012-06-24 20:04:13 +01:00
|
|
|
#include <math.h>
|
2013-05-19 17:37:30 +03:00
|
|
|
modelUavoProxy::modelUavoProxy(QObject *parent, flightDataModel *model) : QObject(parent), myModel(model)
|
2012-06-20 15:00:15 +01:00
|
|
|
{
|
|
|
|
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
2013-05-19 17:37:30 +03:00
|
|
|
|
2012-06-20 15:00:15 +01:00
|
|
|
Q_ASSERT(pm != NULL);
|
2013-05-19 17:37:30 +03:00
|
|
|
objManager = pm->getObject<UAVObjectManager>();
|
2012-06-20 15:00:15 +01:00
|
|
|
Q_ASSERT(objManager != NULL);
|
2013-05-19 17:37:30 +03:00
|
|
|
waypointObj = Waypoint::GetInstance(objManager);
|
2012-06-20 15:00:15 +01:00
|
|
|
Q_ASSERT(waypointObj != NULL);
|
2013-05-19 17:37:30 +03:00
|
|
|
pathactionObj = PathAction::GetInstance(objManager);
|
2012-06-20 15:00:15 +01:00
|
|
|
Q_ASSERT(pathactionObj != NULL);
|
|
|
|
}
|
|
|
|
void modelUavoProxy::modelToObjects()
|
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
PathAction *act = NULL;
|
|
|
|
Waypoint *wp = NULL;
|
2012-06-20 15:00:15 +01:00
|
|
|
QModelIndex index;
|
|
|
|
double distance;
|
|
|
|
double bearing;
|
|
|
|
double altitude;
|
2013-05-19 17:37:30 +03:00
|
|
|
int lastaction = -1;
|
|
|
|
|
|
|
|
for (int x = 0; x < myModel->rowCount(); ++x) {
|
|
|
|
int instances = objManager->getNumInstances(waypointObj->getObjID());
|
|
|
|
if (x > instances - 1) {
|
|
|
|
wp = new Waypoint;
|
|
|
|
wp->initialize(x, wp->getMetaObject());
|
2012-06-24 20:04:13 +01:00
|
|
|
objManager->registerObject(wp);
|
2013-05-19 17:37:30 +03:00
|
|
|
} else {
|
|
|
|
wp = Waypoint::GetInstance(objManager, x);
|
2012-06-24 20:04:13 +01:00
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
act = new PathAction;
|
2012-06-24 20:04:13 +01:00
|
|
|
Q_ASSERT(act);
|
|
|
|
Q_ASSERT(wp);
|
|
|
|
Waypoint::DataFields waypoint = wp->getData();
|
|
|
|
PathAction::DataFields action = act->getData();
|
|
|
|
|
|
|
|
///Waypoint object data
|
2013-05-19 17:37:30 +03:00
|
|
|
index = myModel->index(x, flightDataModel::DISRELATIVE);
|
|
|
|
distance = myModel->data(index).toDouble();
|
|
|
|
index = myModel->index(x, flightDataModel::BEARELATIVE);
|
|
|
|
bearing = myModel->data(index).toDouble();
|
|
|
|
index = myModel->index(x, flightDataModel::ALTITUDERELATIVE);
|
|
|
|
altitude = myModel->data(index).toFloat();
|
|
|
|
index = myModel->index(x, flightDataModel::VELOCITY);
|
|
|
|
waypoint.Velocity = myModel->data(index).toFloat();
|
|
|
|
|
|
|
|
waypoint.Position[Waypoint::POSITION_NORTH] = distance * cos(bearing / 180 * M_PI);
|
|
|
|
waypoint.Position[Waypoint::POSITION_EAST] = distance * sin(bearing / 180 * M_PI);
|
|
|
|
waypoint.Position[Waypoint::POSITION_DOWN] = (-1.0f) * altitude;
|
2012-06-20 15:00:15 +01:00
|
|
|
|
2012-06-24 20:04:13 +01:00
|
|
|
///PathAction object data
|
2013-05-19 17:37:30 +03:00
|
|
|
index = myModel->index(x, flightDataModel::MODE);
|
|
|
|
action.Mode = myModel->data(index).toInt();
|
|
|
|
index = myModel->index(x, flightDataModel::MODE_PARAMS0);
|
|
|
|
action.ModeParameters[0] = myModel->data(index).toFloat();
|
|
|
|
index = myModel->index(x, flightDataModel::MODE_PARAMS1);
|
|
|
|
action.ModeParameters[1] = myModel->data(index).toFloat();
|
|
|
|
index = myModel->index(x, flightDataModel::MODE_PARAMS2);
|
|
|
|
action.ModeParameters[2] = myModel->data(index).toFloat();
|
|
|
|
index = myModel->index(x, flightDataModel::MODE_PARAMS3);
|
|
|
|
action.ModeParameters[3] = myModel->data(index).toFloat();
|
|
|
|
|
|
|
|
index = myModel->index(x, flightDataModel::CONDITION);
|
|
|
|
action.EndCondition = myModel->data(index).toInt();
|
|
|
|
index = myModel->index(x, flightDataModel::CONDITION_PARAMS0);
|
|
|
|
action.ConditionParameters[0] = myModel->data(index).toFloat();
|
|
|
|
index = myModel->index(x, flightDataModel::CONDITION_PARAMS1);
|
|
|
|
action.ConditionParameters[1] = myModel->data(index).toFloat();
|
|
|
|
index = myModel->index(x, flightDataModel::CONDITION_PARAMS2);
|
|
|
|
action.ConditionParameters[2] = myModel->data(index).toFloat();
|
|
|
|
index = myModel->index(x, flightDataModel::CONDITION_PARAMS3);
|
|
|
|
action.ConditionParameters[3] = myModel->data(index).toFloat();
|
|
|
|
|
|
|
|
index = myModel->index(x, flightDataModel::COMMAND);
|
|
|
|
action.Command = myModel->data(index).toInt();
|
|
|
|
index = myModel->index(x, flightDataModel::JUMPDESTINATION);
|
|
|
|
action.JumpDestination = myModel->data(index).toInt() - 1;
|
|
|
|
index = myModel->index(x, flightDataModel::ERRORDESTINATION);
|
|
|
|
action.ErrorDestination = myModel->data(index).toInt() - 1;
|
|
|
|
|
|
|
|
int actionNumber = addAction(act, action, lastaction);
|
|
|
|
if (actionNumber > lastaction) {
|
|
|
|
lastaction = actionNumber;
|
|
|
|
}
|
|
|
|
waypoint.Action = actionNumber;
|
2012-06-24 20:04:13 +01:00
|
|
|
wp->setData(waypoint);
|
|
|
|
wp->updated();
|
2012-06-20 15:00:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void modelUavoProxy::objectsToModel()
|
2012-06-17 20:09:21 +01:00
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
Waypoint *wp;
|
2012-06-24 20:04:13 +01:00
|
|
|
Waypoint::DataFields wpfields;
|
2013-05-19 17:37:30 +03:00
|
|
|
PathAction *action;
|
2012-06-24 20:04:13 +01:00
|
|
|
QModelIndex index;
|
|
|
|
double distance;
|
|
|
|
double bearing;
|
|
|
|
|
|
|
|
PathAction::DataFields actionfields;
|
|
|
|
|
2013-05-19 17:37:30 +03:00
|
|
|
myModel->removeRows(0, myModel->rowCount());
|
|
|
|
for (int x = 0; x < objManager->getNumInstances(waypointObj->getObjID()); ++x) {
|
|
|
|
wp = Waypoint::GetInstance(objManager, x);
|
2012-06-24 20:04:13 +01:00
|
|
|
Q_ASSERT(wp);
|
2013-05-19 17:37:30 +03:00
|
|
|
if (!wp) {
|
2012-06-24 20:04:13 +01:00
|
|
|
continue;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
wpfields = wp->getData();
|
2012-06-24 20:04:13 +01:00
|
|
|
myModel->insertRow(x);
|
2013-05-19 17:37:30 +03:00
|
|
|
index = myModel->index(x, flightDataModel::VELOCITY);
|
|
|
|
myModel->setData(index, wpfields.Velocity);
|
|
|
|
distance = sqrt(wpfields.Position[Waypoint::POSITION_NORTH] * wpfields.Position[Waypoint::POSITION_NORTH] +
|
|
|
|
wpfields.Position[Waypoint::POSITION_EAST] * wpfields.Position[Waypoint::POSITION_EAST]);
|
|
|
|
bearing = atan2(wpfields.Position[Waypoint::POSITION_EAST], wpfields.Position[Waypoint::POSITION_NORTH]) * 180 / M_PI;
|
|
|
|
|
|
|
|
if (bearing != bearing) {
|
|
|
|
bearing = 0;
|
|
|
|
}
|
|
|
|
index = myModel->index(x, flightDataModel::DISRELATIVE);
|
|
|
|
myModel->setData(index, distance);
|
|
|
|
index = myModel->index(x, flightDataModel::BEARELATIVE);
|
|
|
|
myModel->setData(index, bearing);
|
|
|
|
index = myModel->index(x, flightDataModel::ALTITUDERELATIVE);
|
|
|
|
myModel->setData(index, (-1.0f) * wpfields.Position[Waypoint::POSITION_DOWN]);
|
|
|
|
|
|
|
|
action = PathAction::GetInstance(objManager, wpfields.Action);
|
2012-06-24 20:04:13 +01:00
|
|
|
Q_ASSERT(action);
|
2013-05-19 17:37:30 +03:00
|
|
|
if (!action) {
|
2012-06-24 20:04:13 +01:00
|
|
|
continue;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
actionfields = action->getData();
|
|
|
|
|
|
|
|
index = myModel->index(x, flightDataModel::ISRELATIVE);
|
|
|
|
myModel->setData(index, true);
|
|
|
|
|
|
|
|
index = myModel->index(x, flightDataModel::COMMAND);
|
|
|
|
myModel->setData(index, actionfields.Command);
|
|
|
|
|
|
|
|
index = myModel->index(x, flightDataModel::CONDITION_PARAMS0);
|
|
|
|
myModel->setData(index, actionfields.ConditionParameters[0]);
|
|
|
|
index = myModel->index(x, flightDataModel::CONDITION_PARAMS1);
|
|
|
|
myModel->setData(index, actionfields.ConditionParameters[1]);
|
|
|
|
index = myModel->index(x, flightDataModel::CONDITION_PARAMS2);
|
|
|
|
myModel->setData(index, actionfields.ConditionParameters[2]);
|
|
|
|
index = myModel->index(x, flightDataModel::CONDITION_PARAMS3);
|
|
|
|
myModel->setData(index, actionfields.ConditionParameters[3]);
|
|
|
|
|
|
|
|
index = myModel->index(x, flightDataModel::CONDITION);
|
|
|
|
myModel->setData(index, actionfields.EndCondition);
|
|
|
|
|
|
|
|
index = myModel->index(x, flightDataModel::ERRORDESTINATION);
|
|
|
|
myModel->setData(index, actionfields.ErrorDestination + 1);
|
|
|
|
|
|
|
|
index = myModel->index(x, flightDataModel::JUMPDESTINATION);
|
|
|
|
myModel->setData(index, actionfields.JumpDestination + 1);
|
|
|
|
|
|
|
|
index = myModel->index(x, flightDataModel::MODE);
|
|
|
|
myModel->setData(index, actionfields.Mode);
|
|
|
|
|
|
|
|
index = myModel->index(x, flightDataModel::MODE_PARAMS0);
|
|
|
|
myModel->setData(index, actionfields.ModeParameters[0]);
|
|
|
|
index = myModel->index(x, flightDataModel::MODE_PARAMS1);
|
|
|
|
myModel->setData(index, actionfields.ModeParameters[1]);
|
|
|
|
index = myModel->index(x, flightDataModel::MODE_PARAMS2);
|
|
|
|
myModel->setData(index, actionfields.ModeParameters[2]);
|
|
|
|
index = myModel->index(x, flightDataModel::MODE_PARAMS3);
|
|
|
|
myModel->setData(index, actionfields.ModeParameters[3]);
|
2012-06-24 20:04:13 +01:00
|
|
|
}
|
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
int modelUavoProxy::addAction(PathAction *actionObj, PathAction::DataFields actionFields, int lastaction)
|
2012-06-24 20:04:13 +01:00
|
|
|
{
|
2013-05-19 17:37:30 +03:00
|
|
|
// check if a similar action already exhists
|
|
|
|
int instances = objManager->getNumInstances(pathactionObj->getObjID());
|
|
|
|
|
|
|
|
for (int x = 0; x < lastaction + 1; ++x) {
|
|
|
|
PathAction *action = PathAction::GetInstance(objManager, x);
|
2012-06-24 20:04:13 +01:00
|
|
|
Q_ASSERT(action);
|
2013-05-19 17:37:30 +03:00
|
|
|
if (!action) {
|
2012-06-24 20:04:13 +01:00
|
|
|
continue;
|
2013-05-19 17:37:30 +03:00
|
|
|
}
|
|
|
|
PathAction::DataFields fields = action->getData();
|
|
|
|
if (fields.Command == actionFields.Command
|
|
|
|
&& fields.ConditionParameters[0] == actionFields.ConditionParameters[0]
|
|
|
|
&& fields.ConditionParameters[1] == actionFields.ConditionParameters[1]
|
|
|
|
&& fields.ConditionParameters[2] == actionFields.ConditionParameters[2]
|
|
|
|
&& fields.EndCondition == actionFields.EndCondition
|
|
|
|
&& fields.ErrorDestination == actionFields.ErrorDestination
|
|
|
|
&& fields.JumpDestination == actionFields.JumpDestination
|
|
|
|
&& fields.Mode == actionFields.Mode
|
|
|
|
&& fields.ModeParameters[0] == actionFields.ModeParameters[0]
|
|
|
|
&& fields.ModeParameters[1] == actionFields.ModeParameters[1]
|
|
|
|
&& fields.ModeParameters[2] == actionFields.ModeParameters[2]) {
|
|
|
|
qDebug() << "ModelUAVProxy:" << "found similar action instance:" << x;
|
2012-06-24 20:04:13 +01:00
|
|
|
actionObj->deleteLater();
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
// if we get here it means no similar action was found, we have to create it
|
|
|
|
if (instances < lastaction + 2) {
|
|
|
|
actionObj->initialize(instances, actionObj->getMetaObject());
|
2012-06-24 20:04:13 +01:00
|
|
|
objManager->registerObject(actionObj);
|
|
|
|
actionObj->setData(actionFields);
|
|
|
|
actionObj->updated();
|
2013-05-19 17:37:30 +03:00
|
|
|
qDebug() << "ModelUAVProxy:" << "created new action instance:" << instances;
|
|
|
|
return lastaction + 1;
|
|
|
|
} else {
|
|
|
|
PathAction *action = PathAction::GetInstance(objManager, lastaction + 1);
|
2012-06-24 20:04:13 +01:00
|
|
|
Q_ASSERT(action);
|
|
|
|
action->setData(actionFields);
|
|
|
|
action->updated();
|
|
|
|
actionObj->deleteLater();
|
2013-05-19 17:37:30 +03:00
|
|
|
qDebug() << "ModelUAVProxy:" << "reused action instance:" << lastaction + 1;
|
|
|
|
return lastaction + 1;
|
2012-06-24 20:04:13 +01:00
|
|
|
}
|
2013-05-19 17:37:30 +03:00
|
|
|
return -1; // error we should never get here
|
2012-06-17 20:09:21 +01:00
|
|
|
}
|