2010-07-20 21:57:29 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file joystickcontrol.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
|
|
|
* @addtogroup GCSControlGadgetPlugin GCSControl Gadget Plugin
|
|
|
|
* @{
|
|
|
|
* @brief A that mimics a transmitter joystick and updates the MCC
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2010-07-20 00:03:03 +02:00
|
|
|
#include "joystickcontrol.h"
|
|
|
|
#include "extensionsystem/pluginmanager.h"
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QtGui/QWidget>
|
|
|
|
#include <QtGui/QTextEdit>
|
|
|
|
#include <QtGui/QVBoxLayout>
|
|
|
|
#include <QtGui/QPushButton>
|
|
|
|
#include <QtGui/QMessageBox>
|
|
|
|
#include <QMouseEvent>
|
2010-07-20 05:40:21 +02:00
|
|
|
#include <QtGlobal>
|
2010-07-20 00:03:03 +02:00
|
|
|
|
2010-07-20 21:57:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Constructor for JoystickControl widget. Sets up the image of a joystick
|
|
|
|
*/
|
2010-07-20 00:03:03 +02:00
|
|
|
JoystickControl::JoystickControl(QWidget *parent) :
|
|
|
|
QGraphicsView(parent)
|
|
|
|
{
|
|
|
|
setMinimumSize(64,64);
|
|
|
|
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
|
|
|
|
setScene(new QGraphicsScene(this));
|
|
|
|
setRenderHints(QPainter::Antialiasing);
|
|
|
|
|
2010-07-20 21:57:29 +02:00
|
|
|
// Connect object updated event from UAVObject to also animate sticks
|
|
|
|
connect(getMCC(), SIGNAL(objectUpdated(UAVObject*)), this, SLOT(mccChanged(UAVObject*)));
|
|
|
|
|
|
|
|
|
2010-07-20 00:03:03 +02:00
|
|
|
m_renderer = new QSvgRenderer();
|
2010-07-20 21:57:29 +02:00
|
|
|
Q_ASSERT( m_renderer->load(QString(":/gcscontrol/images/joystick.svg")) );
|
2010-07-20 05:40:21 +02:00
|
|
|
|
2010-07-20 21:57:29 +02:00
|
|
|
m_background = new QGraphicsSvgItem();
|
2010-07-20 00:03:03 +02:00
|
|
|
m_background->setSharedRenderer(m_renderer);
|
2010-07-20 21:57:29 +02:00
|
|
|
m_background->setElementId(QString("background"));
|
|
|
|
|
|
|
|
m_joystickEnd = new QGraphicsSvgItem();
|
|
|
|
m_joystickEnd->setSharedRenderer(m_renderer);
|
|
|
|
m_joystickEnd->setElementId(QString("joystickEnd"));
|
|
|
|
m_joystickEnd->setPos(0,0);
|
2010-07-20 00:03:03 +02:00
|
|
|
|
|
|
|
QGraphicsScene *l_scene = scene();
|
|
|
|
l_scene->clear(); // This also deletes all items contained in the scene.
|
|
|
|
l_scene->addItem(m_background);
|
2010-07-20 21:57:29 +02:00
|
|
|
l_scene->addItem(m_joystickEnd);
|
2010-07-20 05:40:21 +02:00
|
|
|
l_scene->setSceneRect(m_background->boundingRect());
|
2010-07-20 00:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
JoystickControl::~JoystickControl()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-07-20 21:57:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Draw stick positions when ManualControlCommand gets updated
|
|
|
|
*/
|
|
|
|
void JoystickControl::mccChanged(UAVObject*)
|
|
|
|
{
|
|
|
|
QPen pen;
|
|
|
|
pen.setColor(QColor(Qt::white));
|
|
|
|
pen.setWidth(2);
|
|
|
|
|
|
|
|
// draw stick positions
|
|
|
|
if( this->objectName() == QString("widgetLeftStick"))
|
|
|
|
{
|
|
|
|
ManualControlCommand::DataFields data = getMCC()->getData();
|
|
|
|
double x = (data.Yaw + 1) / 2 * scene()->sceneRect().width();
|
2010-09-04 04:19:02 +02:00
|
|
|
double y = (data.Pitch + 1) / 2 * scene()->sceneRect().height();
|
2010-07-26 22:47:24 +02:00
|
|
|
m_joystickEnd->setPos(x-m_joystickEnd->boundingRect().width()/2,y-m_joystickEnd->boundingRect().height()/2);
|
2010-07-20 21:57:29 +02:00
|
|
|
}
|
|
|
|
else if( this->objectName() == QString("widgetRightStick"))
|
|
|
|
{
|
|
|
|
ManualControlCommand::DataFields data = getMCC()->getData();
|
|
|
|
double x = (data.Roll + 1) / 2 * scene()->sceneRect().width();
|
2010-09-04 04:19:06 +02:00
|
|
|
double y = data.Throttle * scene()->sceneRect().height();
|
|
|
|
m_joystickEnd->setPos(x-m_joystickEnd->boundingRect().width()/2,scene()->sceneRect().height()-y - 200/scene()->sceneRect().height());
|
2010-07-20 21:57:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the Manual Control Command UAV Object
|
|
|
|
*/
|
2010-07-20 00:03:03 +02:00
|
|
|
ManualControlCommand* JoystickControl::getMCC()
|
|
|
|
{
|
|
|
|
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
|
|
|
UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
|
|
|
|
ManualControlCommand* obj = dynamic_cast<ManualControlCommand*>( objManager->getObject(QString("ManualControlCommand")) );
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-07-20 21:57:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Redirect mouse move events to control joystick position
|
|
|
|
*/
|
|
|
|
void JoystickControl::mouseMoveEvent(QMouseEvent *event)
|
2010-07-20 00:03:03 +02:00
|
|
|
{
|
2010-07-20 21:57:29 +02:00
|
|
|
updateMCC(mapToScene(event->pos()));
|
|
|
|
}
|
2010-07-20 00:03:03 +02:00
|
|
|
|
2010-07-20 21:57:29 +02:00
|
|
|
/**
|
|
|
|
* @brief Redirect mouse move clicks to control joystick position
|
|
|
|
*/
|
|
|
|
void JoystickControl::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if( event->button() == Qt::LeftButton )
|
|
|
|
updateMCC(mapToScene(event->pos()));
|
2010-07-20 00:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JoystickControl::paint()
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JoystickControl::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
// Skip painting until the dial file is loaded
|
|
|
|
if (! m_renderer->isValid()) {
|
|
|
|
qDebug()<<"Dial file not loaded, not rendering";
|
|
|
|
// return;
|
|
|
|
}
|
2010-07-20 05:40:21 +02:00
|
|
|
|
|
|
|
QGraphicsView::paintEvent(event);
|
2010-07-20 00:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void JoystickControl::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
fitInView(m_background, Qt::KeepAspectRatio );
|
|
|
|
}
|
2010-07-20 21:57:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Update the roll,pitch,yaw,throttle for the Manual Control Command based on stick position (from mouse event)
|
|
|
|
*/
|
|
|
|
void JoystickControl::updateMCC(QPointF point)
|
|
|
|
{
|
|
|
|
QRectF sceneSize = scene()->sceneRect();
|
|
|
|
|
|
|
|
double x = 2 * ( point.x() / sceneSize.width() - .5 );
|
2010-08-18 17:24:47 +02:00
|
|
|
x = qBound( (double) -1, x, (double) 1);
|
2010-07-20 21:57:29 +02:00
|
|
|
if( this->objectName() == QString("widgetLeftStick"))
|
|
|
|
{
|
2010-09-04 04:19:06 +02:00
|
|
|
double y = 2 * ( point.y() / sceneSize.height() - .5);
|
|
|
|
y = qBound( (double) -1, y, (double) 1);
|
2010-07-20 21:57:29 +02:00
|
|
|
|
|
|
|
ManualControlCommand::DataFields data = getMCC()->getData();
|
|
|
|
data.Pitch = y;
|
|
|
|
data.Yaw = x;
|
|
|
|
getMCC()->setData(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if( this->objectName() == QString("widgetRightStick"))
|
|
|
|
{
|
2010-09-04 04:19:06 +02:00
|
|
|
double y = 1-( point.y() / sceneSize.height());
|
|
|
|
y = qBound( (double) 0, y, (double) 1);
|
2010-07-20 21:57:29 +02:00
|
|
|
|
|
|
|
ManualControlCommand::DataFields data = getMCC()->getData();
|
2010-09-04 04:19:06 +02:00
|
|
|
data.Throttle = y;
|
2010-07-20 21:57:29 +02:00
|
|
|
data.Roll = x;
|
|
|
|
getMCC()->setData(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|