From c28456d44219e8a668f20d996082337e54e66ff3 Mon Sep 17 00:00:00 2001 From: pixhawk Date: Fri, 4 Feb 2011 11:25:11 +0100 Subject: [PATCH] Cleaned up debug output and fixed logging using settings --- src/comm/MAVLinkProtocol.cc | 42 ++++++++++++++++++++++++---------------- src/uas/UAS.cc | 38 ++++++++++++++++++++---------------- src/uas/UAS.h | 4 ++-- src/uas/UASManager.cc | 5 +++++ src/ui/MainWindow.cc | 40 +++++++++++--------------------------- src/ui/designer/QGCToolWidget.cc | 2 +- 6 files changed, 65 insertions(+), 66 deletions(-) diff --git a/src/comm/MAVLinkProtocol.cc b/src/comm/MAVLinkProtocol.cc index 2a937c4..a12fa9d 100644 --- a/src/comm/MAVLinkProtocol.cc +++ b/src/comm/MAVLinkProtocol.cc @@ -73,15 +73,16 @@ void MAVLinkProtocol::loadSettings() QSettings settings; settings.sync(); settings.beginGroup("QGC_MAVLINK_PROTOCOL"); - m_heartbeatsEnabled = settings.value("HEARTBEATS_ENABLED", m_heartbeatsEnabled).toBool(); - m_loggingEnabled = settings.value("LOGGING_ENABLED", m_loggingEnabled).toBool(); - m_enable_version_check = settings.value("VERION_CHECK_ENABLED", m_enable_version_check).toBool(); + enableHeartbeats(settings.value("HEARTBEATS_ENABLED", m_heartbeatsEnabled).toBool()); + enableVersionCheck(settings.value("VERION_CHECK_ENABLED", m_enable_version_check).toBool()); // Only set logfile if there is a name present in settings if (settings.contains("LOGFILE_NAME") && m_logfile == NULL) { m_logfile = new QFile(settings.value("LOGFILE_NAME").toString()); } + // Enable logging + enableLogging(settings.value("LOGGING_ENABLED", m_loggingEnabled).toBool()); // Only set system id if it was valid int temp = settings.value("GCS_SYSTEM_ID", systemId).toInt(); @@ -116,8 +117,11 @@ MAVLinkProtocol::~MAVLinkProtocol() storeSettings(); if (m_logfile) { - m_logfile->flush(); - m_logfile->close(); + if (m_logfile->isOpen()) + { + m_logfile->flush(); + m_logfile->close(); + } delete m_logfile; } } @@ -137,7 +141,7 @@ QString MAVLinkProtocol::getLogfileName() } else { - return QDesktopServices::storageLocation(QDesktopServices::HomeLocation) + "qgrouncontrol_packetlog.mavlink"; + return QDesktopServices::storageLocation(QDesktopServices::HomeLocation) + "/qgrouncontrol_packetlog.mavlink"; } } @@ -161,20 +165,21 @@ void MAVLinkProtocol::receiveBytes(LinkInterface* link, QByteArray b) if (decodeState == 1) { // Log data - if (m_loggingEnabled) + if (m_loggingEnabled && m_logfile) { const int len = MAVLINK_MAX_PACKET_LEN+sizeof(quint64); uint8_t buf[len]; quint64 time = QGC::groundTimeUsecs(); memcpy(buf, (void*)&time, sizeof(quint64)); - // int packetlen = -// quint64 checktime = *((quint64*)buf); -// qDebug() << "TIME" << time << "CHECKTIME:" << checktime; + // Write message to buffer mavlink_msg_to_send_buffer(buf+sizeof(quint64), &message); QByteArray b((const char*)buf, len); - //int packetlen = - if(m_logfile->write(b) < MAVLINK_MAX_PACKET_LEN+sizeof(quint64)) qDebug() << "WRITING TO LOG FAILED!"; - //qDebug() << "WROTE LOGFILE"; + if(m_logfile->write(b) < MAVLINK_MAX_PACKET_LEN+sizeof(quint64)) + { + emit protocolStatusMessage(tr("MAVLink Logging failed"), tr("Could not write to file %1, disabling logging.").arg(m_logfile->fileName())); + // Stop logging + enableLogging(false); + } } // ORDER MATTERS HERE! @@ -399,8 +404,8 @@ void MAVLinkProtocol::enableLogging(bool enabled) { if (!m_logfile->open(QIODevice::WriteOnly | QIODevice::Append)) { - emit protocolStatusMessage(tr("Opening MAVLink logfile for writing failed"), tr("MAVLink cannot log to the file %1, please choose a different file.").arg(m_logfile->fileName())); - qDebug() << "OPENING LOGFILE FAILED!"; + emit protocolStatusMessage(tr("Opening MAVLink logfile for writing failed"), tr("MAVLink cannot log to the file %1, please choose a different file. Stopping logging.").arg(m_logfile->fileName())); + m_loggingEnabled = false; } } } @@ -408,8 +413,11 @@ void MAVLinkProtocol::enableLogging(bool enabled) { if (m_logfile) { - m_logfile->flush(); - m_logfile->close(); + if (m_logfile->isOpen()) + { + m_logfile->flush(); + m_logfile->close(); + } delete m_logfile; m_logfile = NULL; } diff --git a/src/uas/UAS.cc b/src/uas/UAS.cc index 4e8eef7..8ce3ce5 100644 --- a/src/uas/UAS.cc +++ b/src/uas/UAS.cc @@ -16,10 +16,10 @@ #include #include #include +#include #include "UAS.h" #include "LinkInterface.h" #include "UASManager.h" -//#include "MG.h" #include "QGC.h" #include "GAudioOutput.h" #include "MAVLinkProtocol.h" @@ -27,20 +27,6 @@ #include "LinkManager.h" #include "SerialLink.h" -#ifndef M_PI -#define M_PI 3.14159265358979323846 /* pi */ -#endif - -#ifndef M_PI_2 -#define M_PI_2 1.57079632679489661923 /* pi/2 */ -#endif - -#ifndef M_PI_4 -#define M_PI_4 0.78539816339744830962 /* pi/4 */ -#endif - - - UAS::UAS(MAVLinkProtocol* protocol, int id) : UASInterface(), uasId(id), startTime(MG::TIME::getGroundTimeNow()), @@ -322,8 +308,7 @@ void UAS::receiveMessage(LinkInterface* link, mavlink_message_t message) emit voltageChanged(message.sysid, state.vbat/1000.0f); // LOW BATTERY ALARM - float chargeLevel = getChargeLevel(); - if (chargeLevel <= 20.0f) + if (lpVoltage < warnVoltage) { startLowBattAlarm(); } @@ -1426,6 +1411,25 @@ void UAS::setParameter(const int component, const QString& id, const float value } } +void UAS::setSystemType(int systemType) +{ + type = systemType; + // If the airframe is still generic, change it to a close default type + if (airframe == 0) + { + switch (systemType) + { + case MAV_FIXED_WING: + airframe = QGC_AIRFRAME_EASYSTAR; + break; + case MAV_QUADROTOR: + airframe = QGC_AIRFRAME_MIKROKOPTER; + break; + } + } + emit systemSpecsChanged(uasId); +} + void UAS::setUASName(const QString& name) { this->name = name; diff --git a/src/uas/UAS.h b/src/uas/UAS.h index 77bea1f..e7a0484 100644 --- a/src/uas/UAS.h +++ b/src/uas/UAS.h @@ -122,8 +122,8 @@ protected: //COMMENTS FOR TEST UNIT // Battery stats float fullVoltage; ///< Voltage of the fully charged battery (100%) float emptyVoltage; ///< Voltage of the empty battery (0%) - float warnVoltage; ///< Voltage where QGC will start to warn about low battery float startVoltage; ///< Voltage at system start + float warnVoltage; ///< Voltage where QGC will start to warn about low battery double currentVoltage; ///< Voltage currently measured float lpVoltage; ///< Low-pass filtered voltage int timeRemaining; ///< Remaining time calculated based on previous and current @@ -181,7 +181,7 @@ public slots: /** @brief Set the autopilot type */ void setAutopilotType(int apType) { autopilot = apType; emit systemSpecsChanged(uasId); } /** @brief Set the type of airframe */ - void setSystemType(int systemType) { type = systemType; emit systemSpecsChanged(uasId); } + void setSystemType(int systemType); /** @brief Set the specific airframe type */ void setAirframe(int airframe) { this->airframe = airframe; emit systemSpecsChanged(uasId); } /** @brief Set a new name **/ diff --git a/src/uas/UASManager.cc b/src/uas/UASManager.cc index d7539a9..f493e62 100644 --- a/src/uas/UASManager.cc +++ b/src/uas/UASManager.cc @@ -64,6 +64,11 @@ UASManager::UASManager() : UASManager::~UASManager() { + // Delete all systems + foreach (UASInterface* mav, systems) + { + delete mav; + } } diff --git a/src/ui/MainWindow.cc b/src/ui/MainWindow.cc index 6300e6e..259e6f7 100644 --- a/src/ui/MainWindow.cc +++ b/src/ui/MainWindow.cc @@ -260,7 +260,6 @@ void MainWindow::buildCustomWidget() for(int i = 0; i < widgets.size(); ++i) { - qDebug() << "ADDING WIDGET #" << i << widgets.at(i); // Check if this widget already has a parent, do not create it in this case QDockWidget* dock = dynamic_cast(widgets.at(i)->parentWidget()); if (!dock) @@ -771,20 +770,16 @@ void MainWindow::showToolWidget(bool visible) QDockWidget* dockWidget = qobject_cast (dockWidgets[tool]); - qDebug() << "DATA:" << tool << "FLOATING" << dockWidget->isFloating() << "checked" << action->isChecked() << "visible" << dockWidget->isVisible() << "action vis:" << action->isVisible(); if (dockWidget && dockWidget->isVisible() != visible) { if (visible) { - qDebug() << "DOCK WIDGET ADDED"; addDockWidget(dockWidgetLocations[tool], dockWidget); dockWidget->show(); } else { - qDebug() << "DOCK WIDGET REMOVED"; removeDockWidget(dockWidget); - //dockWidget->hide(); } QHashIterator i(dockWidgets); @@ -795,7 +790,7 @@ void MainWindow::showToolWidget(bool visible) { QString chKey = buildMenuKey (SUB_SECTION_CHECKED,static_cast(i.key()), currentView); settings.setValue(chKey,visible); - qDebug() << "showToolWidget(): Set key" << chKey << "to" << visible; + //qDebug() << "showToolWidget(): Set key" << chKey << "to" << visible; break; } } @@ -804,14 +799,14 @@ void MainWindow::showToolWidget(bool visible) QDockWidget* dockWidget = qobject_cast(QObject::sender()); - qDebug() << "Trying to cast dockwidget" << dockWidget << "isvisible" << visible; + //qDebug() << "Trying to cast dockwidget" << dockWidget << "isvisible" << visible; if (dockWidget) { // Get action int tool = dockWidgets.key(dockWidget); - qDebug() << "Updating widget setting" << tool << "to" << visible; + //qDebug() << "Updating widget setting" << tool << "to" << visible; QAction* action = toolsMenuActions[tool]; action->blockSignals(true); @@ -826,7 +821,7 @@ void MainWindow::showToolWidget(bool visible) { QString chKey = buildMenuKey (SUB_SECTION_CHECKED,static_cast(i.key()), currentView); settings.setValue(chKey,visible); - qDebug() << "showToolWidget(): Set key" << chKey << "to" << visible; + // qDebug() << "showToolWidget(): Set key" << chKey << "to" << visible; break; } } @@ -843,7 +838,7 @@ void MainWindow::showTheWidget (TOOLS_WIDGET_NAMES widget, VIEW_SECTIONS view) tempVisible = settings.value(buildMenuKey(SUB_SECTION_CHECKED,widget,view), false).toBool(); - qDebug() << "showTheWidget(): Set key" << buildMenuKey(SUB_SECTION_CHECKED,widget,view) << "to" << tempVisible; + //qDebug() << "showTheWidget(): Set key" << buildMenuKey(SUB_SECTION_CHECKED,widget,view) << "to" << tempVisible; if (tempWidget) { @@ -855,14 +850,6 @@ void MainWindow::showTheWidget (TOOLS_WIDGET_NAMES widget, VIEW_SECTIONS view) tempLocation = static_cast (settings.value(buildMenuKey (SUB_SECTION_LOCATION,widget, view), QVariant(Qt::RightDockWidgetArea)).toInt()); - // if (widget == MainWindow::MENU_UAS_LIST) - // { - // if (!settings.contains(buildMenuKey (SUB_SECTION_LOCATION,widget, view))) - // { - // tempLocation = Qt::RightDockWidgetArea; - // } - // } - if (tempWidget != NULL) { if (tempVisible) @@ -897,6 +884,7 @@ void MainWindow::closeEvent(QCloseEvent *event) //settings.setValue("windowState", saveState()); aboutToCloseFlag = true; mavlink->storeSettings(); + joystick->deleteLater(); // Save the last current view in any case settings.setValue("CURRENT_VIEW", currentView); // Save the current window state, but only if a system is connected (else no real number of widgets would be present) @@ -997,7 +985,6 @@ void MainWindow::connectCommonWidgets() void MainWindow::createCustomWidget() { - //qDebug() << "ADDING CUSTOM WIDGET"; QGCToolWidget* tool = new QGCToolWidget("Unnamed Tool", this); if (QGCToolWidget::instances()->size() < 2) @@ -1425,9 +1412,6 @@ void MainWindow::addLink(LinkInterface *link) // Error handling connect(link, SIGNAL(communicationError(QString,QString)), this, SLOT(showCriticalMessage(QString,QString)), Qt::QueuedConnection); - - //qDebug() << "ADDING LINK:" << link->getName() << "ACTION IS: " << commWidget->getAction(); - // Special case for simulationlink MAVLinkSimulationLink* sim = dynamic_cast(link); if (sim) @@ -1599,8 +1583,6 @@ void MainWindow::UASCreated(UASInterface* uas) // the currently active UAS if (UASManager::instance()->getUASList().size() == 1) { - //qDebug() << "UPDATING THE VIEW SINCE THIS IS THE FIRST CONNECTED SYSTEM"; - // Load last view if setting is present if (settings.contains("CURRENT_VIEW_WITH_UAS_CONNECTED")) { @@ -1659,12 +1641,12 @@ void MainWindow::clearView() if (temp) { - qDebug() << "TOOL:" << chKey << "IS:" << temp->isChecked(); + //qDebug() << "TOOL:" << chKey << "IS:" << temp->isChecked(); settings.setValue(chKey,temp->isChecked()); } else { - qDebug() << "TOOL:" << chKey << "IS DEFAULT AND UNCHECKED"; + //qDebug() << "TOOL:" << chKey << "IS DEFAULT AND UNCHECKED"; settings.setValue(chKey,false); } } @@ -1876,16 +1858,16 @@ void MainWindow::showTheCentralWidget (TOOLS_WIDGET_NAMES centralWidget, VIEW_SE QWidget* tempWidget = dockWidgets[centralWidget]; tempVisible = settings.value(buildMenuKey(SUB_SECTION_CHECKED,centralWidget,view), false).toBool(); - qDebug() << buildMenuKey (SUB_SECTION_CHECKED,centralWidget,view) << tempVisible; + //qDebug() << buildMenuKey (SUB_SECTION_CHECKED,centralWidget,view) << tempVisible; if (toolsMenuActions[centralWidget]) { - qDebug() << "SETTING TO:" << tempVisible; + //qDebug() << "SETTING TO:" << tempVisible; toolsMenuActions[centralWidget]->setChecked(tempVisible); } if (centerStack && tempWidget && tempVisible) { - qDebug() << "ACTIVATING MAIN WIDGET"; + //qDebug() << "ACTIVATING MAIN WIDGET"; centerStack->setCurrentWidget(tempWidget); } } diff --git a/src/ui/designer/QGCToolWidget.cc b/src/ui/designer/QGCToolWidget.cc index dad5c8a..f25445f 100644 --- a/src/ui/designer/QGCToolWidget.cc +++ b/src/ui/designer/QGCToolWidget.cc @@ -32,7 +32,7 @@ QGCToolWidget::QGCToolWidget(const QString& title, QWidget *parent) : } // Try with parent - dock = dynamic_cast(this->parentWidget()); + dock = dynamic_cast(parent); if (dock) { dock->setWindowTitle(title);