From 64c4534f55612ca128ecd502d99a05fa694c2ead Mon Sep 17 00:00:00 2001 From: peabody124 Date: Mon, 19 Jul 2010 22:03:03 +0000 Subject: [PATCH] Can now click two boxes to control manual control settings. Shamelessly ripping off dial code to make pretty SVG joystick (to come). git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1140 ebee16cc-31ac-478f-84a7-5cbb03baadba --- ground/src/plugins/gcscontrol/gcscontrol.pro | 8 +- ground/src/plugins/gcscontrol/gcscontrol.qrc | 5 + ground/src/plugins/gcscontrol/gcscontrol.ui | 116 +++++++++++++++--- .../gcscontrol/gcscontrolgadgetwidget.cpp | 79 ++++++++---- .../gcscontrol/gcscontrolgadgetwidget.h | 5 +- .../gcscontrol/images/joystickBackground.svg | 41 +++++++ .../plugins/gcscontrol/joystickcontrol.cpp | 90 ++++++++++++++ .../src/plugins/gcscontrol/joystickcontrol.h | 33 +++++ 8 files changed, 337 insertions(+), 40 deletions(-) create mode 100644 ground/src/plugins/gcscontrol/gcscontrol.qrc create mode 100644 ground/src/plugins/gcscontrol/images/joystickBackground.svg create mode 100644 ground/src/plugins/gcscontrol/joystickcontrol.cpp create mode 100644 ground/src/plugins/gcscontrol/joystickcontrol.h diff --git a/ground/src/plugins/gcscontrol/gcscontrol.pro b/ground/src/plugins/gcscontrol/gcscontrol.pro index a680cf100..3dfb1434b 100644 --- a/ground/src/plugins/gcscontrol/gcscontrol.pro +++ b/ground/src/plugins/gcscontrol/gcscontrol.pro @@ -1,11 +1,13 @@ -TEMPLATE = lib +TEMPLATE = lib TARGET = GCSControl +QT += svg include(../../openpilotgcsplugin.pri) include(../../plugins/coreplugin/coreplugin.pri) include(../../plugins/uavobjects/uavobjects.pri) HEADERS += gcscontrolgadget.h +HEADERS += joystickcontrol.h HEADERS += gcscontrolgadgetwidget.h HEADERS += gcscontrolgadgetfactory.h HEADERS += gcscontrolplugin.h @@ -14,7 +16,11 @@ SOURCES += gcscontrolgadget.cpp SOURCES += gcscontrolgadgetwidget.cpp SOURCES += gcscontrolgadgetfactory.cpp SOURCES += gcscontrolplugin.cpp +SOURCES += joystickcontrol.cpp OTHER_FILES += GCSControl.pluginspec FORMS += gcscontrol.ui + +RESOURCES += \ + gcscontrol.qrc diff --git a/ground/src/plugins/gcscontrol/gcscontrol.qrc b/ground/src/plugins/gcscontrol/gcscontrol.qrc new file mode 100644 index 000000000..6b9f76c37 --- /dev/null +++ b/ground/src/plugins/gcscontrol/gcscontrol.qrc @@ -0,0 +1,5 @@ + + + images/joystickBackground.svg + + diff --git a/ground/src/plugins/gcscontrol/gcscontrol.ui b/ground/src/plugins/gcscontrol/gcscontrol.ui index 98d495a11..579d65815 100644 --- a/ground/src/plugins/gcscontrol/gcscontrol.ui +++ b/ground/src/plugins/gcscontrol/gcscontrol.ui @@ -6,27 +6,113 @@ 0 0 - 400 - 300 + 653 + 290 + + + 0 + 0 + + Form - - - - 73 - 61 - 181 - 191 - - - - FlightMode - - + + + + + + + + + GCS Control + + + + + + + Flight Mode: + + + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 10 + + + 0 + + + + + + 0 + 0 + + + + + 200 + 200 + + + + false + + + + + + + + 0 + 0 + + + + + 200 + 200 + + + + + + + + + + JoystickControl + QWidget +
joystickcontrol.h
+ 1 +
+
diff --git a/ground/src/plugins/gcscontrol/gcscontrolgadgetwidget.cpp b/ground/src/plugins/gcscontrol/gcscontrolgadgetwidget.cpp index 4a37c9de4..12b6ad7b9 100644 --- a/ground/src/plugins/gcscontrol/gcscontrolgadgetwidget.cpp +++ b/ground/src/plugins/gcscontrol/gcscontrolgadgetwidget.cpp @@ -43,7 +43,16 @@ GCSControlGadgetWidget::GCSControlGadgetWidget(QWidget *parent) : QLabel(parent) { m_gcscontrol = new Ui_GCSControl(); m_gcscontrol->setupUi(this); - connect(m_gcscontrol->pushButton, SIGNAL(clicked()), this, SLOT(buttonPressed())); + + // Set up the drop down box for the flightmode + m_gcscontrol->comboBoxFlightMode->addItem(QString("Manual")); + m_gcscontrol->comboBoxFlightMode->addItem(QString("Stabilized")); + m_gcscontrol->comboBoxFlightMode->addItem(QString("Auto")); + + // Set up slots and signals + connect(m_gcscontrol->checkBoxGcsControl, SIGNAL(stateChanged(int)), this, SLOT(gcsControlToggle(int))); + connect(m_gcscontrol->comboBoxFlightMode, SIGNAL(currentIndexChanged(int)), this, SLOT(flightModeChanged(int))); + } GCSControlGadgetWidget::~GCSControlGadgetWidget() @@ -51,31 +60,55 @@ GCSControlGadgetWidget::~GCSControlGadgetWidget() // Do nothing } -void GCSControlGadgetWidget::buttonPressed() +/*! + \brief Returns the ManualControlCommand UAVObject + */ + +ManualControlCommand* GCSControlGadgetWidget::getMCC() { - // Get access to the ManualControlObject ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); UAVObjectManager *objManager = pm->getObject(); ManualControlCommand* obj = dynamic_cast( objManager->getObject(QString("ManualControlCommand")) ); - - // Need to set the metadata to let GCS override OpenPilot - UAVObject::Metadata mdata = obj->getMetadata(); - mdata.gcsAccess = UAVObject::ACCESS_READWRITE; - mdata.flightAccess = UAVObject::ACCESS_READONLY; - obj->setMetadata(mdata); - - // Set values to some constants for now - ManualControlCommand::DataFields data = obj->getData(); - data.FlightMode = ManualControlCommand::FLIGHTMODE_STABILIZED; - data.Pitch = .5; - data.Roll = .3; - data.Throttle = .2; - data.Yaw = .3; - obj->setData(data); - - // Visual confirmation - m_gcscontrol->pushButton->setText(obj->toString()); - - //Q_ASSERT( 0 ); + return obj; } +/*! + \brief Called when the gcs control is toggled and enabled or disables flight write access to manual control command + */ + +void GCSControlGadgetWidget::gcsControlToggle(int state) +{ + UAVObject::Metadata mdata = getMCC()->getMetadata(); + if (state) + { + mdata.flightAccess = UAVObject::ACCESS_READONLY; + } + else + { + mdata.flightAccess = UAVObject::ACCESS_READWRITE; + } + getMCC()->setMetadata(mdata); +} + +/*! + \brief Called when the flight mode drop down is changed and sets the ManualControlCommand->FlightMode accordingly + */ +void GCSControlGadgetWidget::flightModeChanged(int state) +{ + ManualControlCommand::DataFields data = getMCC()->getData(); + if( state == 0 ) + { + data.FlightMode = ManualControlCommand::FLIGHTMODE_MANUAL; + } + else if ( state == 1 ) + { + data.FlightMode = ManualControlCommand::FLIGHTMODE_STABILIZED; + } + else if ( state == 2 ) + { + data.FlightMode = ManualControlCommand::FLIGHTMODE_AUTO; + } + getMCC()->setData(data); +} + + diff --git a/ground/src/plugins/gcscontrol/gcscontrolgadgetwidget.h b/ground/src/plugins/gcscontrol/gcscontrolgadgetwidget.h index eeeb5ec43..437d4c094 100644 --- a/ground/src/plugins/gcscontrol/gcscontrolgadgetwidget.h +++ b/ground/src/plugins/gcscontrol/gcscontrolgadgetwidget.h @@ -29,6 +29,7 @@ #define GCSControlGADGETWIDGET_H_ #include +#include "uavobjects/manualcontrolcommand.h" class Ui_GCSControl; @@ -41,10 +42,12 @@ public: ~GCSControlGadgetWidget(); private slots: - void buttonPressed(); + void gcsControlToggle(int state); + void flightModeChanged(int state); private: Ui_GCSControl *m_gcscontrol; + ManualControlCommand* getMCC(); }; #endif /* GCSControlGADGETWIDGET_H_ */ diff --git a/ground/src/plugins/gcscontrol/images/joystickBackground.svg b/ground/src/plugins/gcscontrol/images/joystickBackground.svg new file mode 100644 index 000000000..203854f67 --- /dev/null +++ b/ground/src/plugins/gcscontrol/images/joystickBackground.svg @@ -0,0 +1,41 @@ + + + + + + + + + image/svg+xml + + + + + + + + + diff --git a/ground/src/plugins/gcscontrol/joystickcontrol.cpp b/ground/src/plugins/gcscontrol/joystickcontrol.cpp new file mode 100644 index 000000000..64e0f966f --- /dev/null +++ b/ground/src/plugins/gcscontrol/joystickcontrol.cpp @@ -0,0 +1,90 @@ +#include "joystickcontrol.h" +#include "ui_joystickcontrol.h" +#include "extensionsystem/pluginmanager.h" +#include +#include +#include +#include +#include +#include +#include +#include + +JoystickControl::JoystickControl(QWidget *parent) : + QGraphicsView(parent) +{ + setMinimumSize(64,64); + setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); + setScene(new QGraphicsScene(this)); + setRenderHints(QPainter::Antialiasing); + + m_renderer = new QSvgRenderer(); + m_renderer->load(QString(":/gcscontrol/images/joystickBackground.svg")); + m_background = new QGraphicsSvgItem(); + m_background->setSharedRenderer(m_renderer); + + QGraphicsScene *l_scene = scene(); + l_scene->clear(); // This also deletes all items contained in the scene. + l_scene->addItem(m_background); + +} + +JoystickControl::~JoystickControl() +{ + +} + +ManualControlCommand* JoystickControl::getMCC() +{ + ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); + UAVObjectManager *objManager = pm->getObject(); + ManualControlCommand* obj = dynamic_cast( objManager->getObject(QString("ManualControlCommand")) ); + return obj; +} + +void JoystickControl::mousePressEvent(QMouseEvent *event) +{ + Qt::MouseButton button = event->button(); + QPoint point = event->pos(); + QSize widgetSize = this->size(); + double x = 2 * ( ((double)point.x()) / ((double)widgetSize.width()) - .5 ); + double y = 2 * ( ((double)point.y()) / ((double)widgetSize.height()) - .5); + + if( button == Qt::LeftButton && this->objectName() == QString("widgetLeftStick")) + { + + ManualControlCommand::DataFields data = getMCC()->getData(); + data.Pitch = x; + data.Yaw = y; + getMCC()->setData(data); + } + + if( button == Qt::LeftButton && this->objectName() == QString("widgetRightStick")) + { + + ManualControlCommand::DataFields data = getMCC()->getData(); + data.Throttle = x; + data.Roll = y; + getMCC()->setData(data); + } +} + +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; + } + QGraphicsView::paintEvent(event); +} + +void JoystickControl::resizeEvent(QResizeEvent *event) +{ + fitInView(m_background, Qt::KeepAspectRatio ); +} diff --git a/ground/src/plugins/gcscontrol/joystickcontrol.h b/ground/src/plugins/gcscontrol/joystickcontrol.h new file mode 100644 index 000000000..16798fecb --- /dev/null +++ b/ground/src/plugins/gcscontrol/joystickcontrol.h @@ -0,0 +1,33 @@ +#ifndef JOYSTICKCONTROL_H +#define JOYSTICKCONTROL_H + +#include +#include +#include +#include +#include "uavobjects/manualcontrolcommand.h" + +namespace Ui { + class JoystickControl; +} + +class JoystickControl : public QGraphicsView +{ + Q_OBJECT + +public: + explicit JoystickControl(QWidget *parent = 0); + ~JoystickControl(); + void paint(); +protected: + void mousePressEvent(QMouseEvent *event); + void paintEvent(QPaintEvent *event); + void resizeEvent(QResizeEvent *event); + +private: + ManualControlCommand* getMCC(); + QSvgRenderer *m_renderer; + QGraphicsSvgItem *m_background; +}; + +#endif // JOYSTICKCONTROL_H