From 3ce4a88a25e04b6a073f0d4c6aaae1ef30de9b36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Wed, 11 Nov 2020 19:13:53 +0100 Subject: [PATCH] More debug output --- .../Calibration/calibration.cpp | 37 +++++++++++++++++-- .../PC_Application/Calibration/calkit.cpp | 19 +++++++--- Software/PC_Application/Device/device.cpp | 5 +-- Software/PC_Application/VNA/vna.cpp | 9 +++-- Software/PC_Application/appwindow.cpp | 14 ++++++- Software/PC_Application/mode.cpp | 9 ++--- Software/PC_Application/mode.h | 4 +- Software/PC_Application/preferences.cpp | 4 ++ 8 files changed, 75 insertions(+), 26 deletions(-) diff --git a/Software/PC_Application/Calibration/calibration.cpp b/Software/PC_Application/Calibration/calibration.cpp index c4f87a5..875af02 100644 --- a/Software/PC_Application/Calibration/calibration.cpp +++ b/Software/PC_Application/Calibration/calibration.cpp @@ -3,6 +3,8 @@ #include #include #include +#include "unit.h" +#include using namespace std; @@ -17,12 +19,14 @@ Calibration::Calibration() measurements[Measurement::Port2Load].datapoints = vector(); measurements[Measurement::Isolation].datapoints = vector(); measurements[Measurement::Through].datapoints = vector(); + measurements[Measurement::Line].datapoints = vector(); type = Type::None; } void Calibration::clearMeasurements() { + qDebug() << "Clearing all calibration measurements..."; for(auto m : measurements) { clearMeasurement(m.first); } @@ -32,6 +36,7 @@ void Calibration::clearMeasurement(Calibration::Measurement type) { measurements[type].datapoints.clear(); measurements[type].timestamp = QDateTime(); + qDebug() << "Deleted" << MeasurementToString(type) << "measurement"; } void Calibration::addMeasurement(Calibration::Measurement type, Protocol::Datapoint &d) @@ -46,9 +51,16 @@ bool Calibration::calculationPossible(Calibration::Type type) // always possible to reset to None return true; } + qDebug() << "Checking if" << TypeToString(type) << "calibration is possible..."; + auto ret = SanityCheckSamples(Measurements(type, false)); + if(ret) { + qDebug() << "...calibration possible"; + } else { + qDebug() << "...calibration not possible"; + } return SanityCheckSamples(Measurements(type, false)); } -#include + bool Calibration::constructErrorTerms(Calibration::Type type) { if(type == Type::None) { @@ -58,10 +70,18 @@ bool Calibration::constructErrorTerms(Calibration::Type type) if(!calculationPossible(type)) { return false; } + qDebug() << "Constructing error terms for" << TypeToString(type) << "calibration"; bool isTRL = type == Type::TRL; - if(minFreq < kit.minFreq(isTRL) || maxFreq > kit.maxFreq(isTRL)) { + double kit_minFreq = kit.minFreq(isTRL); + double kit_maxFreq = kit.maxFreq(isTRL); + if(minFreq < kit_minFreq || maxFreq > kit_maxFreq) { // Calkit does not support complete calibration range - QMessageBox::critical(nullptr, "Unable to perform calibration", "The calibration kit does not support the complete span. Please choose a different calibration kit or a narrower span."); + QString msg = QString("The calibration kit does not support the complete span.\n\n") + + "The measured calibration data covers " + Unit::ToString(minFreq, "Hz", " kMG", 4) + " to " + Unit::ToString(maxFreq, "Hz", " kMG", 4) + + ", however the calibration kit is only valid from " + Unit::ToString(kit_minFreq, "Hz", " kMG", 4) + " to " + Unit::ToString(kit_maxFreq, "Hz", " kMG", 4) + ".\n\n" + + "Please adjust the calibration kit or the span and take the calibration measurements again."; + QMessageBox::critical(nullptr, "Unable to perform calibration", msg); + qWarning() << msg; return false; } switch(type) { @@ -80,6 +100,7 @@ void Calibration::resetErrorTerms() { type = Type::None; points.clear(); + qDebug() << "Error terms reset"; } void Calibration::construct12TermPoints() @@ -609,6 +630,7 @@ bool Calibration::openFromFile(QString filename) return false; } } +qDebug() << "Attempting to open calibration from file" << filename; // reset all data before loading new calibration clearMeasurements(); @@ -621,10 +643,12 @@ bool Calibration::openFromFile(QString filename) calkit_file.truncate(dotPos); } calkit_file.append(".calkit"); + qDebug() << "Associated calibration kit expected in" << calkit_file; try { kit = Calkit::fromFile(calkit_file); } catch (runtime_error e) { QMessageBox::warning(nullptr, "Missing calibration kit", "The calibration kit file associated with the selected calibration could not be parsed. The calibration might not be accurate. (" + QString(e.what()) + ")"); + qWarning() << "Parsing of calibration kit failed while opening calibration file: " << e.what(); } ifstream file; @@ -633,6 +657,7 @@ bool Calibration::openFromFile(QString filename) file >> *this; } catch(runtime_error e) { QMessageBox::warning(nullptr, "File parsing error", e.what()); + qWarning() << "Calibration file parsing failed: " << e.what(); return false; } @@ -658,6 +683,7 @@ bool Calibration::saveToFile(QString filename) file << *this; auto calkit_file = filename + ".calkit"; + qDebug() << "Saving associated calibration kit to file" << calkit_file; kit.toFile(calkit_file); return true; @@ -689,12 +715,13 @@ istream& operator >>(istream &in, Calibration &c) for(auto m : c.Measurements()) { if(Calibration::MeasurementToString(m) == qLine) { // this is the correct measurement - c.clearMeasurement(m); + c.measurements[m].datapoints.clear(); uint timestamp; in >> timestamp; c.measurements[m].timestamp = QDateTime::fromSecsSinceEpoch(timestamp); unsigned int points; in >> points; + qDebug() << "Found measurement" << Calibration::MeasurementToString(m) << ", containing" << points << "points"; for(unsigned int i=0;i> p.pointNum >> p.frequency; @@ -711,6 +738,7 @@ istream& operator >>(istream &in, Calibration &c) for(auto t : Calibration::Types()) { if(Calibration::TypeToString(t) == qLine) { // try to apply this calibration type + qDebug() << "Specified calibration in file is" << Calibration::TypeToString(t); if(c.calculationPossible(t)) { c.constructErrorTerms(t); } else { @@ -720,6 +748,7 @@ istream& operator >>(istream &in, Calibration &c) } } } + qDebug() << "Calibration file parsing complete"; return in; } diff --git a/Software/PC_Application/Calibration/calkit.cpp b/Software/PC_Application/Calibration/calkit.cpp index 48055d6..dad964f 100644 --- a/Software/PC_Application/Calibration/calkit.cpp +++ b/Software/PC_Application/Calibration/calkit.cpp @@ -6,6 +6,7 @@ #include #include "json.hpp" #include +#include using json = nlohmann::json; using namespace std; @@ -24,13 +25,10 @@ Calkit::Calkit() } } - -#include void Calkit::toFile(QString filename) -{ - if(!filename.endsWith(".calkit")) { - filename.append(".calkit"); - } +{ + qDebug() << "Saving calkit to file" << filename; + TransformPathsToRelative(filename); json j; @@ -67,6 +65,8 @@ static QString readLine(ifstream &file) { Calkit Calkit::fromFile(QString filename) { + qDebug() << "Opening calkit to file" << filename; + auto c = Calkit(); ifstream file; file.open(filename.toStdString()); @@ -77,6 +77,7 @@ Calkit Calkit::fromFile(QString filename) json j; file >> j; if(j.contains("SOLT")) { + qDebug() << "JSON format detected"; // calkit file uses json format, parse for(auto e : c.json_descr) { auto list = e.name.split("/"); @@ -93,6 +94,7 @@ Calkit Calkit::fromFile(QString filename) } if(!entry_exists) { // missing entry in json file, nothing to do (default values already set in constructor) + qWarning() << "Entry" << e.name << "not present in file, assuming default value"; continue; } // json library does not now about QVariant, handle used cases @@ -112,6 +114,7 @@ Calkit Calkit::fromFile(QString filename) } } } else { + qDebug() << "Legacy format detected"; // legacy file format, return to beginning of file file.clear(); file.seekg(0); @@ -337,7 +340,9 @@ void Calkit::TransformPathsToRelative(QFileInfo d) continue; } if(QFileInfo(*f).isAbsolute()) { + QString buf = *f; *f = d.dir().relativeFilePath(*f); + qDebug() << "Transformed" << buf << "to" << *f << "(to relative)"; } } } @@ -351,7 +356,9 @@ void Calkit::TransformPathsToAbsolute(QFileInfo d) } if(QFileInfo(*f).isRelative()) { auto absDir = QDir(d.dir().path() + "/" + *f); + QString buf = *f; *f = absDir.absolutePath(); + qDebug() << "Transformed" << buf << "to" << *f << "(to absolute)"; } } } diff --git a/Software/PC_Application/Device/device.cpp b/Software/PC_Application/Device/device.cpp index 439cfe7..d61d2ec 100644 --- a/Software/PC_Application/Device/device.cpp +++ b/Software/PC_Application/Device/device.cpp @@ -141,8 +141,6 @@ Protocol::DeviceInfo Device::lastInfo = defaultInfo; Device::Device(QString serial) { - qDebug() << "Starting device connection..."; - lastInfo = defaultInfo; m_handle = nullptr; @@ -302,7 +300,7 @@ std::set Device::GetDevices() void Device::USBHandleThread() { - qInfo() << "Receive thread started" << flush; + qDebug() << "Receive thread started"; while (m_connected) { libusb_handle_events(m_context); } @@ -362,7 +360,6 @@ void Device::SearchDevices(std::function if (ret > 0) { /* managed to read the product string */ QString product(c_product); - qDebug() << "Opened device: " << product; if (product == "VNA") { // this is a match if(!foundCallback(handle, QString(c_serial))) { diff --git a/Software/PC_Application/VNA/vna.cpp b/Software/PC_Application/VNA/vna.cpp index a1cf3dc..924287a 100644 --- a/Software/PC_Application/VNA/vna.cpp +++ b/Software/PC_Application/VNA/vna.cpp @@ -403,7 +403,7 @@ void VNA::initializeDevice() auto key = "DefaultCalibration"+window->getDevice()->serial(); if (s.contains(key)) { auto filename = s.value(key).toString(); - qDebug() << "Attempting to load default calibration file \"" << filename << "\""; + qDebug() << "Attempting to load default calibration file " << filename; if(QFile::exists(filename)) { if(cal.openFromFile(filename)) { ApplyCalibration(cal.getType()); @@ -430,7 +430,6 @@ void VNA::NewDatapoint(Protocol::Datapoint d) { d = average.process(d); if(calMeasuring) { - if(average.currentSweep() == averages) { // this is the last averaging sweep, use values for calibration if(!calWaitFirst || d.pointNum == 0) { @@ -438,6 +437,7 @@ void VNA::NewDatapoint(Protocol::Datapoint d) cal.addMeasurement(calMeasurement, d); if(d.pointNum == settings.points - 1) { calMeasuring = false; + qDebug() << "Calibration measurement" << cal.MeasurementToString(calMeasurement) << "complete"; emit CalibrationMeasurementComplete(calMeasurement); } } @@ -627,6 +627,8 @@ void VNA::ApplyCalibration(Calibration::Type type) if(cal.constructErrorTerms(type)) { calValid = true; emit CalibrationApplied(type); + } else { + DisableCalibration(true); } } catch (runtime_error e) { QMessageBox::critical(window, "Calibration failure", e.what()); @@ -648,7 +650,8 @@ void VNA::StartCalibrationMeasurement(Calibration::Measurement m) } // Stop sweep StopSweep(); - calMeasurement = m; + qDebug() << "Taking" << Calibration::MeasurementToString(m) << "measurement"; + calMeasurement = m; // Delete any already captured data of this measurement cal.clearMeasurement(m); calWaitFirst = true; diff --git a/Software/PC_Application/appwindow.cpp b/Software/PC_Application/appwindow.cpp index 5b185e8..b4f3b6c 100644 --- a/Software/PC_Application/appwindow.cpp +++ b/Software/PC_Application/appwindow.cpp @@ -55,6 +55,10 @@ AppWindow::AppWindow(QWidget *parent) QCoreApplication::setOrganizationName("VNA"); QCoreApplication::setApplicationName("Application"); + qSetMessagePattern("%{time process}: [%{type}] %{message}"); + + qDebug() << "Application start"; + Preferences::getInstance().load(); device = nullptr; @@ -153,14 +157,20 @@ void AppWindow::closeEvent(QCloseEvent *event) void AppWindow::ConnectToDevice(QString serial) { + if(serial.isEmpty()) { + qDebug() << "Trying to connect to any device"; + } else { + qDebug() << "Trying to connect to" << serial; + } if(device) { + qDebug() << "Already connected to a device, disconnecting first..."; DisconnectDevice(); } try { qDebug() << "Attempting to connect to device..."; device = new Device(serial); lConnectionStatus.setText("Connected to " + device->serial()); - qInfo() << "Connected to " << device->serial(); + qInfo() << "Connected to" << device->serial(); lDeviceInfo.setText(device->getLastDeviceInfoString()); connect(device, &Device::LogLineReceived, &deviceLog, &DeviceLog::addLine); connect(device, &Device::ConnectionLost, this, &AppWindow::DeviceConnectionLost); @@ -184,6 +194,7 @@ void AppWindow::ConnectToDevice(QString serial) } } } catch (const runtime_error e) { + qWarning() << "Failed to connect:" << e.what(); DisconnectDevice(); UpdateDeviceList(); } @@ -269,6 +280,7 @@ int AppWindow::UpdateDeviceList() // no devices available, disable connection option ui->menuConnect_to->setEnabled(false); } + qDebug() << "Updated device list, found" << devices.size(); return devices.size(); } diff --git a/Software/PC_Application/mode.cpp b/Software/PC_Application/mode.cpp index 8bfca2d..02c05e7 100644 --- a/Software/PC_Application/mode.cpp +++ b/Software/PC_Application/mode.cpp @@ -3,6 +3,7 @@ #include #include #include "ui_main.h" +#include Mode* Mode::activeMode = nullptr; QWidget* Mode::cornerWidget = nullptr; @@ -47,6 +48,7 @@ void Mode::activate() activeMode->deactivate(); } + qDebug() << "Activating mode" << name; // show all mode specific GUI elements for(auto t : toolbars) { t->show(); @@ -64,13 +66,10 @@ void Mode::activate() window->getCentral()->setCurrentWidget(central); // restore dock and toolbar positions -// window->restoreGeometry(settings.value("geometry_"+name).toByteArray()); window->restoreState(settings.value("windowState_"+name).toByteArray()); // restore visibility of toolbars and docks -// window->getUi()->menuDocks->clear(); for(auto d : docks) { -// window->getUi()->menuDocks->addAction(d->toggleViewAction()); bool hidden = settings.value("dock_"+name+"_"+d->windowTitle(), d->isHidden()).toBool(); if(hidden) { d->hide(); @@ -78,9 +77,7 @@ void Mode::activate() d->show(); } } -// window->getUi()->menuToolbars->clear(); for(auto t : toolbars) { -// window->getUi()->menuToolbars->addAction(t->toggleViewAction()); bool hidden = settings.value("toolbar_"+name+"_"+t->windowTitle(), t->isHidden()).toBool(); if(hidden) { t->hide(); @@ -106,7 +103,6 @@ void Mode::deactivate() for(auto t : toolbars) { settings.setValue("toolbar_"+name+"_"+t->windowTitle(), t->isHidden()); } -// settings.setValue("geometry_"+name, window->saveGeometry()); settings.setValue("windowState_"+name, window->saveState()); // hide all mode specific GUI elements @@ -122,6 +118,7 @@ void Mode::deactivate() a->setVisible(false); } + qDebug() << "Deactivated mode" << name; activeMode = nullptr; } diff --git a/Software/PC_Application/mode.h b/Software/PC_Application/mode.h index bde9739..9119af7 100644 --- a/Software/PC_Application/mode.h +++ b/Software/PC_Application/mode.h @@ -14,8 +14,8 @@ class Mode : public QObject public: Mode(AppWindow *window, QString name); - virtual void activate(); - virtual void deactivate(); + virtual void activate(); // derived classes must call Mode::activate before doing anything + virtual void deactivate(); // derived classes must call Mode::deactivate before returning QString getName() const; static Mode *getActiveMode(); diff --git a/Software/PC_Application/preferences.cpp b/Software/PC_Application/preferences.cpp index 49e03d4..6ca99dd 100644 --- a/Software/PC_Application/preferences.cpp +++ b/Software/PC_Application/preferences.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using namespace std; @@ -168,11 +169,14 @@ void Preferences::load() { QSettings settings; // load settings, using default values if not present + qInfo() << "Loading preferences"; for(auto d : descr) { try { d.var.setValue(settings.value(d.name, d.def)); + qDebug() << "Setting" << d.name << "is set to" << d.var.value(); } catch (const exception& e){ d.var.setValue(d.def); + qDebug() << "Setting" << d.name << "reset to default:" << d.def; } } }