diff --git a/hit2023v2_RMS/device_config.ini b/hit2023v2_RMS/device_config.ini index 2b5051d..954e87f 100644 --- a/hit2023v2_RMS/device_config.ini +++ b/hit2023v2_RMS/device_config.ini @@ -1,5 +1,5 @@ [Global] -NrDevices=1 +NrDevices=3 HostIp=10.0.7.1 [Device0] @@ -11,10 +11,11 @@ Sensors=5 Master=1 MasterDelay=61 SlaveDelay=34 -Threshold=10 +Threshold=11 Size=4 -CalibFile=C:/Users/lhcb/Desktop/program_fpga_rms_cali/calibration_factor/board0.txt +CalibFile=C:/Users/leverington/Downloads/board0.txt ClusterSize=4 +Device0\CalibFile=C:/Users/leverington/Downloads/board0.txt [Device1] IP=10.0.7.18 @@ -25,9 +26,10 @@ Sensors=5 Master=0 MasterDelay=7 SlaveDelay=1 -Threshold=10 +Threshold=12 Size=4 CalibFile= +ClusterSize=5 [Trigger] Period=9000 @@ -46,6 +48,10 @@ Sensors=2 Master=0 MasterDelay=7 SlaveDelay=1 +Device0\CalibFile=C:/Users/leverington/Downloads/board0.txt +Threshold=13 +ClusterSize=6 +CalibFile= [Device3] IP=10.0.7.6 diff --git a/hit2023v2_RMS/dialogdevices.cpp b/hit2023v2_RMS/dialogdevices.cpp index 421d31b..130e0db 100644 --- a/hit2023v2_RMS/dialogdevices.cpp +++ b/hit2023v2_RMS/dialogdevices.cpp @@ -13,8 +13,12 @@ DialogDevices::DialogDevices(QWidget *parent) : ui(new Ui::DialogDevices) { ui->setupUi(this); + deviceSettings = new QSettings("./device_config.ini", QSettings::IniFormat); + } + + DialogDevices::~DialogDevices() { delete ui; @@ -40,12 +44,25 @@ void DialogDevices::applyCalibrationDataToDevice(int dev_nr, const QVectorsetValue("Device%1/CalibrationData", data); // if storing in settings + // Check if device number is valid + if (dev_nr < 0 || dev_nr >= deviceCalibrationData.size()) { + QMessageBox::warning(this, "Invalid Device", "The device number is out of range."); + return; + } + + // Check if the provided data is valid + if (data.size() != 320) { + QMessageBox::warning(this, "Invalid Data", "The calibration data must contain exactly 320 values."); + return; + } + + // Apply the calibration data to the specified device + deviceCalibrationData[dev_nr] = data; } + // Load calibration factors from a file and store them void DialogDevices::selectCalibrationFile(int dev_nr) { @@ -80,6 +97,11 @@ void DialogDevices::selectCalibrationFile(int dev_nr) index++; } + // Ensure remaining indices are filled with default values + while (index < 320) { + calibrationFactors[index++] = 8192; + } + file.close(); // Store calibration factors for the device @@ -153,7 +175,7 @@ void DialogDevices::importSettings() ui->tableDevices->setRowCount(nr_devices); // Ensure the device calibration data vector is initialized properly - deviceCalibrationData.resize(nr_devices); + deviceCalibrationData.resize(nr_devices, QVector(320, 8192)); for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) { @@ -180,11 +202,26 @@ void DialogDevices::importSettings() ui->tableDevices->setItem(dev_nr, 6, newItem); newItem = new QTableWidgetItem(deviceSettings->value("SlaveDelay", "1").toString()); ui->tableDevices->setItem(dev_nr, 7, newItem); - newItem = new QTableWidgetItem(deviceSettings->value("ClusThresh", "10").toString()); + newItem = new QTableWidgetItem(deviceSettings->value("Threshold", "10").toString()); ui->tableDevices->setItem(dev_nr, 8, newItem); - newItem = new QTableWidgetItem(deviceSettings->value("ClusSize", "4").toString()); + newItem = new QTableWidgetItem(deviceSettings->value("ClusterSize", "4").toString()); ui->tableDevices->setItem(dev_nr, 9, newItem); + // Load calibration file path and update table + QString calibFilePath = deviceSettings->value("CalibFile", "").toString(); + newItem = new QTableWidgetItem(calibFilePath); + ui->tableDevices->setItem(dev_nr, 10, newItem); + + // If the calibration file exists, load the factors + if (!calibFilePath.isEmpty()) { + QVector calibrationFactors(320, 8192); + if (loadCalibrationFactorsFromFile(calibFilePath, calibrationFactors)) { + deviceCalibrationData[dev_nr] = calibrationFactors; + applyCalibrationDataToDevice(dev_nr, calibrationFactors); + } + } + + // Add a button for selecting calibration files QPushButton* calibButton = new QPushButton("Select File"); ui->tableDevices->setCellWidget(dev_nr, 11, calibButton); @@ -309,8 +346,8 @@ int DialogDevices::validateAndSave() num_value = user_data.toInt(); if (num_value < 1) num_value = 1; - if (num_value > 255) - num_value = 255; + if (num_value > 65535) + num_value = 65535; generated = QString("%1").arg(num_value); //if the strings are identical, save the value and return 1 if (generated.compare(user_data) != 0) @@ -322,8 +359,8 @@ int DialogDevices::validateAndSave() num_value = user_data.toInt(); if (num_value < 1) num_value = 1; - if (num_value > 255) - num_value = 255; + if (num_value > 320) + num_value = 320; generated = QString("%1").arg(num_value); //if the strings are identical, save the value and return 1 if (generated.compare(user_data) != 0) @@ -333,49 +370,84 @@ int DialogDevices::validateAndSave() // Calibration file path user_data = ui->tableDevices->item(dev_nr, 10)->text(); - if (user_data.isEmpty()) - data_ok = 0; - - // Save the calibration file path + if (user_data.isEmpty()) { + // Set default calibration factors if the path is empty + QVector defaultCalibration(320, 8192); + deviceCalibrationData[dev_nr] = defaultCalibration; + applyCalibrationDataToDevice(dev_nr, defaultCalibration); + } else { + deviceSettings->setValue("Device" + QString::number(dev_nr) + "/CalibFile", user_data); // Save the file path + } + // Save settings for each device top(deviceSettings); QString group_label = QString("Device%1").arg(dev_nr); deviceSettings->beginGroup(group_label); - deviceSettings->setValue("CalibFile", user_data); // Save the file path - deviceSettings->endGroup(); + deviceSettings->setValue("IP", ui->tableDevices->item(dev_nr, 0)->text()); + deviceSettings->setValue("HardwareVer", ui->tableDevices->item(dev_nr, 1)->text()); + deviceSettings->setValue("Plane", ui->tableDevices->item(dev_nr, 2)->text()); + deviceSettings->setValue("Position", ui->tableDevices->item(dev_nr, 3)->text()); + deviceSettings->setValue("Sensors", ui->tableDevices->item(dev_nr, 4)->text()); + deviceSettings->setValue("Master", ui->tableDevices->item(dev_nr, 5)->text()); + deviceSettings->setValue("MasterDelay", ui->tableDevices->item(dev_nr, 6)->text()); + deviceSettings->setValue("SlaveDelay", ui->tableDevices->item(dev_nr, 7)->text()); + deviceSettings->setValue("Threshold", ui->tableDevices->item(dev_nr, 8)->text()); + deviceSettings->setValue("ClusterSize", ui->tableDevices->item(dev_nr, 9)->text()); + deviceSettings->setValue("CalibFile", ui->tableDevices->item(dev_nr, 10)->text()); + deviceSettings->endGroup(); } - //now store the data - if (!data_ok) - return 0; + // Store the global number of devices top(deviceSettings); deviceSettings->beginGroup("Global"); deviceSettings->setValue("NrDevices", nr_devices); + deviceSettings->endGroup(); - for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) - { - top(deviceSettings); - QString group_label = QString("Device%1").arg(dev_nr); - deviceSettings->beginGroup(group_label); - deviceSettings->setValue("IP", ui->tableDevices->item(dev_nr,0)->text()); - deviceSettings->setValue("HardwareVer", ui->tableDevices->item(dev_nr,1)->text()); - deviceSettings->setValue("Plane", ui->tableDevices->item(dev_nr,2)->text()); - deviceSettings->setValue("Position", ui->tableDevices->item(dev_nr,3)->text()); - deviceSettings->setValue("Sensors", ui->tableDevices->item(dev_nr,4)->text()); - deviceSettings->setValue("Master", ui->tableDevices->item(dev_nr,5)->text()); - deviceSettings->setValue("MasterDelay", ui->tableDevices->item(dev_nr,6)->text()); - deviceSettings->setValue("SlaveDelay", ui->tableDevices->item(dev_nr,7)->text()); - deviceSettings->setValue("Threshold", ui->tableDevices->item(dev_nr,8)->text()); - deviceSettings->setValue("ClusterSize", ui->tableDevices->item(dev_nr,9)->text()); - deviceSettings->setValue("CalibFile", ui->tableDevices->item(dev_nr, 10)->text()); + if (!data_ok) { + QMessageBox::warning(this, "Invalid Data", "Some device settings were invalid and have been corrected. Please review the settings."); } - return 1; + + +return data_ok; } +// Function to load calibration factors from a file +bool DialogDevices::loadCalibrationFactorsFromFile(const QString& filePath, QVector& calibrationFactors) +{ + QFile file(filePath); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + qWarning() << "Could not open calibration file:" << filePath; + return false; + } + QTextStream in(&file); + int index = 0; + while (!in.atEnd() && index < 320) { + QString line = in.readLine().trimmed(); + bool ok; + int value = line.toInt(&ok); + + // Validate value and ensure it is within the range of an unsigned short + if (ok && value >= 0 && value <= 65535) { + calibrationFactors[index] = static_cast(value); + } else { + qWarning() << "Invalid calibration factor in file:" << line << "at index" << index; + calibrationFactors[index] = 8192; // Use default if invalid + } + index++; + } + + // Ensure remaining indices are filled with default values + while (index < 320) { + calibrationFactors[index++] = 8192; + } + + file.close(); + return true; +} void DialogDevices::on_spinNrDevices_valueChanged(int arg1) { if (initialized) diff --git a/hit2023v2_RMS/dialogdevices.h b/hit2023v2_RMS/dialogdevices.h index 0b80329..deb5d23 100644 --- a/hit2023v2_RMS/dialogdevices.h +++ b/hit2023v2_RMS/dialogdevices.h @@ -5,6 +5,7 @@ #include #include #include // Include QVector for calibration data storage +#include namespace Ui { class DialogDevices; @@ -25,6 +26,7 @@ public slots: void accept(); protected: int validateAndSave(); + bool loadCalibrationFactorsFromFile(const QString &filePath, QVector &calibrationFactors); // Function to load calibration factors from a file void applyCalibrationDataToDevice(int dev_nr, const QVector& data); // Function to apply calibration data to a device void importSettings(); int last_nr_devices = -1;