From dfa6906fc2f75a4267c3ff908c8870e19c40f990 Mon Sep 17 00:00:00 2001 From: pixhawk Date: Sat, 17 Apr 2010 10:39:39 +0200 Subject: [PATCH] Added option to connect Simulation via UI --- qgroundcontrol.pri | 4 +- src/comm/MAVLinkSimulationLink.cc | 75 +++++++++++-------------------- src/comm/MAVLinkSimulationLink.h | 5 +-- src/ui/HUD.cc | 50 ++++++++++++--------- src/ui/HUD.h | 2 +- src/ui/MainWindow.cc | 32 ++++--------- src/ui/MainWindow.h | 3 +- src/ui/MainWindow.ui | 18 +++++++- 8 files changed, 87 insertions(+), 102 deletions(-) diff --git a/qgroundcontrol.pri b/qgroundcontrol.pri index 817f101a18..15bd839996 100644 --- a/qgroundcontrol.pri +++ b/qgroundcontrol.pri @@ -59,8 +59,8 @@ macx { # Enable function-profiling with the OS X saturn tool debug { - QMAKE_CXXFLAGS += -finstrument-functions - LIBS += -lSaturn + #QMAKE_CXXFLAGS += -finstrument-functions + #LIBS += -lSaturn } } else { # x64 Mac OS X Snow Leopard 10.6 and later diff --git a/src/comm/MAVLinkSimulationLink.cc b/src/comm/MAVLinkSimulationLink.cc index 303225ec49..c782f77b1d 100644 --- a/src/comm/MAVLinkSimulationLink.cc +++ b/src/comm/MAVLinkSimulationLink.cc @@ -504,26 +504,26 @@ void MAVLinkSimulationLink::writeBytes(const char* data, qint64 size) message_action_decode(&msg, &action); switch (action.action) { - case MAV_ACTION_LAUNCH: + case MAV_ACTION_LAUNCH: status.status = MAV_STATE_ACTIVE; status.mode = MAV_MODE_AUTO; break; - case MAV_ACTION_RETURN: + case MAV_ACTION_RETURN: status.status = MAV_STATE_LANDING; break; - case MAV_ACTION_MOTORS_START: + case MAV_ACTION_MOTORS_START: status.status = MAV_STATE_ACTIVE; status.mode = MAV_MODE_LOCKED; break; - case MAV_ACTION_MOTORS_STOP: + case MAV_ACTION_MOTORS_STOP: status.status = MAV_STATE_STANDBY; status.mode = MAV_MODE_LOCKED; break; - case MAV_ACTION_EMCY_KILL: + case MAV_ACTION_EMCY_KILL: status.status = MAV_STATE_EMERGENCY; status.mode = MAV_MODE_MANUAL; break; - case MAV_ACTION_SHUTDOWN: + case MAV_ACTION_SHUTDOWN: status.status = MAV_STATE_POWEROFF; status.mode = MAV_MODE_LOCKED; break; @@ -579,48 +579,6 @@ void MAVLinkSimulationLink::readBytes(char* const data, qint64 maxLength) { // } } -/** - * Set the maximum time deviation noise. This amount (in milliseconds) is - * the maximum time offset (+/-) from the specified message send rate. - * - * @param milliseconds The maximum time offset (in milliseconds) - * - * @bug The current implementation might induce one milliseconds additional - * discrepancy, this will be fixed by multithreading - **/ -void MAVLinkSimulationLink::setMaximumTimeNoise(int milliseconds) { - maxTimeNoise = milliseconds; -} - - -/** - * Add or subtract a pseudo random time offset. The maximum time offset is - * defined by setMaximumTimeNoise(). - * - * @see setMaximumTimeNoise() - **/ -void MAVLinkSimulationLink::addTimeNoise() { - /* Calculate the time deviation */ - if(maxTimeNoise == 0) { - /* Don't do expensive calculations if no noise is desired */ - timer->setInterval(rate); - } else { - /* Calculate random time noise (gauss distribution): - * - * (1) (2 * rand()) / RAND_MAX: Number between 0 and 2 - * (induces numerical noise through floating point representation, - * ignored here) - * - * (2) ((2 * rand()) / RAND_MAX) - 1: Number between -1 and 1 - * - * (3) Complete term: Number between -maxTimeNoise and +maxTimeNoise - */ - double timeDeviation = (((2 * rand()) / RAND_MAX) - 1) * maxTimeNoise; - timer->setInterval(static_cast(rate + floor(timeDeviation))); - } - -} - /** * Disconnect the connection. * @@ -636,7 +594,7 @@ bool MAVLinkSimulationLink::disconnect() { emit disconnected(); - exit(); + //exit(); } return true; @@ -657,6 +615,25 @@ bool MAVLinkSimulationLink::connect() return true; } +/** + * Connect the link. + * + * @param connect true connects the link, false disconnects it + * @return True if connection has been established, false if connection + * couldn't be established. + **/ +bool MAVLinkSimulationLink::connectLink(bool connect) +{ + _isConnected = connect; + + if(connect) + { + this->connect(); + } + + return true; +} + /** * Check if connection is active. * diff --git a/src/comm/MAVLinkSimulationLink.h b/src/comm/MAVLinkSimulationLink.h index 177dc53436..28abc26d72 100644 --- a/src/comm/MAVLinkSimulationLink.h +++ b/src/comm/MAVLinkSimulationLink.h @@ -84,6 +84,7 @@ public slots: void writeBytes(const char* data, qint64 size); void readBytes(char* const data, qint64 maxLength); void mainloop(); + bool connectLink(bool connect); protected: @@ -116,11 +117,9 @@ protected: qint64 timeOffset; sys_status_t status; - void setMaximumTimeNoise(int milliseconds); - void addTimeNoise(); void enqueue(uint8_t* stream, uint8_t* index, mavlink_message_t* msg); - signals: +signals: void valueChanged(int uasId, QString curve, double value, quint64 usec); }; diff --git a/src/ui/HUD.cc b/src/ui/HUD.cc index 59640eae41..8dc4440cdd 100644 --- a/src/ui/HUD.cc +++ b/src/ui/HUD.cc @@ -65,7 +65,7 @@ inline bool isinf(T value) * @param parent */ HUD::HUD(int width, int height, QWidget* parent) - : QGLWidget(parent), + : QGLWidget(QGLFormat(QGL::SampleBuffers), parent), uas(NULL), values(QMap()), valuesDot(QMap()), @@ -127,7 +127,7 @@ HUD::HUD(int width, int height, QWidget* parent) // Refresh timer refreshTimer->setInterval(40); // 25 Hz - connect(refreshTimer, SIGNAL(timeout()), this, SLOT(repaint())); + connect(refreshTimer, SIGNAL(timeout()), this, SLOT(update())); // Resize to correct size and fill with image resize(fill.size()); @@ -171,8 +171,8 @@ void HUD::stop() void HUD::updateValue(UASInterface* uas, QString name, double value, quint64 msec) { - // if (this->uas == uas) - //{ + // UAS is not needed + Q_UNUSED(uas); if (!isnan(value) && !isinf(value)) { @@ -316,6 +316,8 @@ void HUD::updateSpeed(UASInterface* uas,double x,double y,double z,quint64 times */ void HUD::updateState(UASInterface* uas,QString state) { + // Only one UAS is connected at a time + Q_UNUSED(uas); this->state = state; } @@ -327,6 +329,8 @@ void HUD::updateState(UASInterface* uas,QString state) */ void HUD::updateMode(UASInterface* uas,QString mode) { + // Only one UAS is connected at a time + Q_UNUSED(uas); this->mode = mode; } @@ -467,9 +471,9 @@ void HUD::initializeGL() } else { - //glDisable(GL_BLEND); - //glDisable(GL_POINT_SMOOTH); - //glDisable(GL_LINE_SMOOTH); + glDisable(GL_BLEND); + glDisable(GL_POINT_SMOOTH); + glDisable(GL_LINE_SMOOTH); } } @@ -510,18 +514,16 @@ void HUD::paintRollPitchStrips() } -void HUD::paintGL() -{ -} - void HUD::paintEvent(QPaintEvent *event) { + // Event is not needed + Q_UNUSED(event); + // Read out most important values to limit hash table lookups static float roll = 0.0; static float pitch = 0.0; static float yaw = 0.0; - // Read out most important values to limit hash table lookups roll = roll * 0.5 + 0.5 * values.value("roll", 0.0f); pitch = pitch * 0.5 + 0.5 * values.value("pitch", 0.0f); yaw = yaw * 0.5 + 0.5 * values.value("yaw", 0.0f); @@ -536,22 +538,29 @@ void HUD::paintEvent(QPaintEvent *event) // OPEN GL PAINTING + // Store model view matrix to be able to reset it to the previous state makeCurrent(); - //setupViewport(width(), height()); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Blue / Brown background if (noCamera) paintCenterBackground(roll, pitch, yaw); + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + // END OF OPENGL PAINTING + + //glEnable(GL_MULTISAMPLE); + // QT PAINTING + makeCurrent(); QPainter painter; painter.begin(this); - painter.setRenderHint(QPainter::Antialiasing); - painter.setRenderHint(QPainter::Antialiasing, true); - painter.setRenderHint(QPainter::HighQualityAntialiasing, true); + //painter.setRenderHint(QPainter::HighQualityAntialiasing, true); painter.translate((this->vwidth/2.0+xCenterOffset)*scalingFactor, (this->vheight/2.0+yCenterOffset)*scalingFactor); @@ -687,11 +696,10 @@ void HUD::paintEvent(QPaintEvent *event) // PITCH paintPitchLines((pitch/M_PI)*180.0f, &painter); - painter.end(); + //glDisable(GL_MULTISAMPLE); - - glFlush(); + //glFlush(); } /* @@ -1379,7 +1387,7 @@ void HUD::finishImage() void HUD::commitRawDataToGL() { - //qDebug() << __FILE__ << __LINE__ << "Copying raw data to GL buffer:" << rawImage << receivedWidth << receivedHeight << image->format(); + qDebug() << __FILE__ << __LINE__ << "Copying raw data to GL buffer:" << rawImage << receivedWidth << receivedHeight << image->format(); if (image != NULL) { QImage::Format format = image->format(); @@ -1410,7 +1418,7 @@ void HUD::commitRawDataToGL() //qDebug() << "Now buffer 1"; } } - updateGL(); + update(); } void HUD::saveImage(QString fileName) diff --git a/src/ui/HUD.h b/src/ui/HUD.h index ed0225eb8e..14437d95cf 100644 --- a/src/ui/HUD.h +++ b/src/ui/HUD.h @@ -58,7 +58,7 @@ public: public slots: void initializeGL(); - void paintGL(); + //void paintGL(); /** @brief Start updating the view at 30Hz */ void start(); diff --git a/src/ui/MainWindow.cc b/src/ui/MainWindow.cc index f6b6fb5c91..fb66935738 100644 --- a/src/ui/MainWindow.cc +++ b/src/ui/MainWindow.cc @@ -146,9 +146,6 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) // Add status bar setStatusBar(createStatusBar()); - // Create actions - connectActions(); - // Set the application style (not the same as a style sheet) // Set the style to Plastique qApp->setStyle("plastique"); @@ -167,12 +164,15 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) udpLink->connect(); simulationLink = new MAVLinkSimulationLink(MG::DIR::getSupportFilesDirectory() + "/demo-log.txt"); - //connect(simulationLink, SIGNAL(valueChanged(int,QString,double,quint64)), linechart, SLOT(appendData(int,QString,double,quint64))); + connect(simulationLink, SIGNAL(valueChanged(int,QString,double,quint64)), linechart, SLOT(appendData(int,QString,double,quint64))); LinkManager::instance()->addProtocol(simulationLink, mavlink); //CommConfigurationWindow* simulationWidget = new CommConfigurationWindow(simulationLink, mavlink, this); //ui.menuNetwork->addAction(commWidget->getAction()); //simulationLink->connect(); + // Create actions + connectActions(); + // Load widgets and show application window loadWidgets(); @@ -287,6 +287,7 @@ void MainWindow::connectActions() // Joystick configuration connect(ui.actionJoystickSettings, SIGNAL(triggered()), this, SLOT(configure())); + connect(ui.actionSimulate, SIGNAL(triggered(bool)), simulationLink, SLOT(connectLink(bool))); } void MainWindow::configure() @@ -338,6 +339,9 @@ void MainWindow::UASCreated(UASInterface* uas) reloadStylesheet(); } +/** + * Clears the current view completely + */ void MainWindow::clearView() { // Halt HUD @@ -502,26 +506,6 @@ void MainWindow::removeCommConfAct(QAction* action) ui.menuNetwork->removeAction(action); }*/ -//void MainWindow::startUAS() -//{ -// UASManager::instance()->getActiveUAS()->launch(); -//} -// -//void MainWindow::returnUAS() -//{ -// UASManager::instance()->getActiveUAS()->home(); -//} -// -//void MainWindow::stopUAS() -//{ -// UASManager::instance()->getActiveUAS()->emergencySTOP(); -//} -// -//void MainWindow::killUAS() -//{ -// UASManager::instance()->getActiveUAS()->emergencyKILL(); -//} - void MainWindow::runTests() { // TODO Remove after debugging: Add fake data diff --git a/src/ui/MainWindow.h b/src/ui/MainWindow.h index 0eb0f14824..661cd51e29 100644 --- a/src/ui/MainWindow.h +++ b/src/ui/MainWindow.h @@ -47,6 +47,7 @@ This file is part of the PIXHAWK project #include "CameraView.h" #include "UASListWidget.h" #include "MAVLinkProtocol.h" +#include "MAVLinkSimulationLink.h" #include "AS4Protocol.h" #include "ObjectDetectionView.h" #include "HUD.h" @@ -142,7 +143,7 @@ protected: MAVLinkProtocol* mavlink; AS4Protocol* as4link; - LinkInterface* simulationLink; + MAVLinkSimulationLink* simulationLink; LinkInterface* udpLink; QDockWidget* controlDock; diff --git a/src/ui/MainWindow.ui b/src/ui/MainWindow.ui index 68e854b2fd..d025588f81 100644 --- a/src/ui/MainWindow.ui +++ b/src/ui/MainWindow.ui @@ -35,7 +35,7 @@ 0 0 1000 - 25 + 22 @@ -43,6 +43,7 @@ File + @@ -225,6 +226,21 @@ Show settings view + + + true + + + + :/images/control/launch.svg:/images/control/launch.svg + + + Simulate + + + Simulate one vehicle to test and evaluate this application + + -- GitLab