fixed calibration insert

This commit is contained in:
Blake Leverington 2024-08-06 15:06:06 +02:00
parent aa3c506327
commit 818fdc8197
4 changed files with 151 additions and 180 deletions

View File

@ -22,6 +22,84 @@ DialogDevices::~DialogDevices()
//***************** Initialization ****************** //***************** Initialization ******************
//***************** Processing ******************
void DialogDevices::accept()
{
// qInfo("Accepted!");
if (validateAndSave())
QDialog::accept();
}
void DialogDevices::applyCalibrationDataToDevice(int dev_nr, const QVector<unsigned short>& data)
{
// Implementation for applying the calibration data to the specific device
// For example, updating device settings or performing calculations
qDebug() << "Calibration data loaded for Device" << dev_nr << ":" << data;
// Example: Set the calibration data in your application logic
// deviceSettings->setValue("Device%1/CalibrationData", data); // if storing in settings
}
// Load calibration factors from a file and store them
void DialogDevices::selectCalibrationFile(int dev_nr)
{
QString filename = QFileDialog::getOpenFileName(this, "Select Calibration File", "", "Text Files (*.txt);;All Files (*)");
if (filename.isEmpty()) {
return; // User canceled the dialog
}
QVector<unsigned short> calibrationFactors(320, 8192); // Initialize with default value 8192
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(this, "Error", "Could not open calibration file: " + filename);
return;
}
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] = value;
} else {
qWarning() << "Invalid calibration factor in file:" << line << "at index" << index;
calibrationFactors[index] = 8192; // Use default if invalid
}
index++;
}
file.close();
// Store calibration factors for the device
if (dev_nr < deviceCalibrationData.size()) {
deviceCalibrationData[dev_nr] = calibrationFactors;
} else {
deviceCalibrationData.append(calibrationFactors);
}
// Update the table display to show the file path (optional)
QTableWidgetItem* calibFileItem = new QTableWidgetItem(filename);
ui->tableDevices->setItem(dev_nr, 10, calibFileItem);
}
// Retrieve all calibration factors for each device
QVector<QVector<unsigned short>> DialogDevices::getAllCalibrationFactors() const
{
return deviceCalibrationData;
}
void DialogDevices::showEvent(QShowEvent * event) void DialogDevices::showEvent(QShowEvent * event)
{ {
if (!event->spontaneous()) if (!event->spontaneous())
@ -60,121 +138,49 @@ void DialogDevices::showEvent(QShowEvent * event)
QDialog::showEvent(event); QDialog::showEvent(event);
} }
//***************** Processing ******************
void DialogDevices::accept()
{
// qInfo("Accepted!");
if (validateAndSave())
QDialog::accept();
}
void DialogDevices::selectCalibrationFile(int dev_nr)
{
// Open a file dialog to select a calibration file
QString filename = QFileDialog::getOpenFileName(this, "Select Calibration File", "", "Text Files (*.txt);;All Files (*)");
if (filename.isEmpty())
{
return; // User canceled the dialog
}
// Set the file name in the table for the corresponding device
QTableWidgetItem* calibItem = new QTableWidgetItem(filename);
ui->tableDevices->setItem(dev_nr, 10, calibItem);
// Load calibration data from the file
QVector<unsigned short> calibrationData;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::critical(this, "Error", "Could not open file for reading");
return;
}
QTextStream in(&file);
while (!in.atEnd())
{
QString line = in.readLine();
bool ok;
unsigned short value = line.toUShort(&ok);
if (ok)
{
calibrationData.append(value);
}
else
{
calibrationData.append(32768); // assume calibration range is 0 = and 1 = 65536
QMessageBox::warning(this, "Warning", "Invalid data in file. Skipping line.");
}
}
file.close();
// Store the calibration data for the device
applyCalibrationDataToDevice(dev_nr, calibrationData);
}
void DialogDevices::applyCalibrationDataToDevice(int dev_nr, const QVector<unsigned short>& data)
{
// Implementation for applying the calibration data to the specific device
// For example, updating device settings or performing calculations
qDebug() << "Calibration data loaded for Device" << dev_nr << ":" << data;
// Example: Set the calibration data in your application logic
// deviceSettings->setValue("Device%1/CalibrationData", data); // if storing in settings
}
void DialogDevices::importSettings() void DialogDevices::importSettings()
{ {
int nr_devices = ui->spinNrDevices->value(); int nr_devices = ui->spinNrDevices->value();
ui->tableDevices->setRowCount(nr_devices); ui->tableDevices->setRowCount(nr_devices);
// Ensure the device calibration data vector is initialized properly
deviceCalibrationData.resize(nr_devices);
for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++)
{ {
if (dev_nr >= last_nr_devices) //update only new rows! if (dev_nr >= last_nr_devices) // Update only new rows!
{ {
top(deviceSettings); top(deviceSettings);
QString group_label = QString("Device%1").arg(dev_nr); QString group_label = QString("Device%1").arg(dev_nr);
deviceSettings->beginGroup(group_label); deviceSettings->beginGroup(group_label);
QTableWidgetItem* newItem; QTableWidgetItem* newItem;
newItem = new QTableWidgetItem(deviceSettings->value("IP","0.0.0.0").toString()); newItem = new QTableWidgetItem(deviceSettings->value("IP", "0.0.0.0").toString());
ui->tableDevices->setItem(dev_nr, 0, newItem ); ui->tableDevices->setItem(dev_nr, 0, newItem);
newItem = new QTableWidgetItem(deviceSettings->value("HardwareVer","1").toString()); newItem = new QTableWidgetItem(deviceSettings->value("HardwareVer", "1").toString());
ui->tableDevices->setItem(dev_nr, 1, newItem ); ui->tableDevices->setItem(dev_nr, 1, newItem);
newItem = new QTableWidgetItem(deviceSettings->value("Plane","0").toString()); newItem = new QTableWidgetItem(deviceSettings->value("Plane", "0").toString());
ui->tableDevices->setItem(dev_nr, 2, newItem ); ui->tableDevices->setItem(dev_nr, 2, newItem);
newItem = new QTableWidgetItem(deviceSettings->value("Position","0").toString()); newItem = new QTableWidgetItem(deviceSettings->value("Position", "0").toString());
ui->tableDevices->setItem(dev_nr, 3, newItem ); ui->tableDevices->setItem(dev_nr, 3, newItem);
newItem = new QTableWidgetItem(deviceSettings->value("Sensors","2").toString()); newItem = new QTableWidgetItem(deviceSettings->value("Sensors", "2").toString());
ui->tableDevices->setItem(dev_nr, 4, newItem ); ui->tableDevices->setItem(dev_nr, 4, newItem);
newItem = new QTableWidgetItem(deviceSettings->value("Master","0").toString()); newItem = new QTableWidgetItem(deviceSettings->value("Master", "0").toString());
ui->tableDevices->setItem(dev_nr, 5, newItem ); ui->tableDevices->setItem(dev_nr, 5, newItem);
newItem = new QTableWidgetItem(deviceSettings->value("MasterDelay","22").toString()); newItem = new QTableWidgetItem(deviceSettings->value("MasterDelay", "22").toString());
ui->tableDevices->setItem(dev_nr, 6, newItem ); ui->tableDevices->setItem(dev_nr, 6, newItem);
newItem = new QTableWidgetItem(deviceSettings->value("SlaveDelay","1").toString()); newItem = new QTableWidgetItem(deviceSettings->value("SlaveDelay", "1").toString());
ui->tableDevices->setItem(dev_nr, 7, newItem ); ui->tableDevices->setItem(dev_nr, 7, newItem);
newItem = new QTableWidgetItem(deviceSettings->value("ClusThresh","10").toString()); newItem = new QTableWidgetItem(deviceSettings->value("ClusThresh", "10").toString());
ui->tableDevices->setItem(dev_nr, 8, newItem ); ui->tableDevices->setItem(dev_nr, 8, newItem);
newItem = new QTableWidgetItem(deviceSettings->value("ClusSize","4").toString()); newItem = new QTableWidgetItem(deviceSettings->value("ClusSize", "4").toString());
ui->tableDevices->setItem(dev_nr, 9, newItem ); ui->tableDevices->setItem(dev_nr, 9, newItem);
// Add an item for the calibration file path (optional, for display)
newItem = new QTableWidgetItem(deviceSettings->value("CalibFile", "").toString());
ui->tableDevices->setItem(dev_nr, 10, newItem);
// Add a button for selecting calibration files // Add a button for selecting calibration files
QPushButton* calibButton = new QPushButton("Select File"); QPushButton* calibButton = new QPushButton("Select File");
ui->tableDevices->setCellWidget(dev_nr, 11, calibButton); // Column index 11 for button ui->tableDevices->setCellWidget(dev_nr, 11, calibButton);
// Connect the button to a slot with the sensor index as a parameter // Connect the button to the slot
connect(calibButton, &QPushButton::clicked, this, [this, dev_nr]() { selectCalibrationFile(dev_nr); }); connect(calibButton, &QPushButton::clicked, this, [this, dev_nr]() { selectCalibrationFile(dev_nr); });
} }
} }
@ -354,22 +360,3 @@ void DialogDevices::on_spinNrDevices_valueChanged(int arg1)
importSettings(); importSettings();
} }
QVector<QVector<int>> DialogDevices::getAllCalibrationFactors() const {
QVector<QVector<int>> calibrationFactors;
for (int row = 0; row < ui->tableDevices->rowCount(); ++row) {
QVector<int> factors(320, 8192); // Initialize with default value of 8192
QTableWidgetItem* item = ui->tableDevices->item(row, 10); // Assuming column 10 is for calibration factors
if (item) {
QStringList factorStrings = item->text().split(',', Qt::SkipEmptyParts);
for (int i = 0; i < factorStrings.size() && i < 320; ++i) {
bool ok;
int factor = factorStrings[i].toInt(&ok);
if (ok && factor >= 0 && factor <= 65535) {
factors[i] = factor;
}
}
}
calibrationFactors.append(factors);
}
return calibrationFactors;
}

View File

@ -19,7 +19,7 @@ public:
~DialogDevices(); ~DialogDevices();
QSettings* deviceSettings = NULL; QSettings* deviceSettings = NULL;
QVector<QVector<int>> getAllCalibrationFactors() const; // Declaration of the method QVector<QVector<unsigned short>> getAllCalibrationFactors() const; // Declaration of the method
public slots: public slots:
void showEvent(QShowEvent *event); void showEvent(QShowEvent *event);
void accept(); void accept();
@ -29,7 +29,7 @@ protected:
void importSettings(); void importSettings();
int last_nr_devices = -1; int last_nr_devices = -1;
int initialized = 0; int initialized = 0;
QVector<QVector<unsigned short>> calibrationData; // Storage for calibration data for each device QVector<QVector<unsigned short>> deviceCalibrationData; // Storage for calibration data for each device
private slots: private slots:
void on_spinNrDevices_valueChanged(int arg1); void on_spinNrDevices_valueChanged(int arg1);
void selectCalibrationFile(int dev_nr); // Slot for handling calibration file selection void selectCalibrationFile(int dev_nr); // Slot for handling calibration file selection

View File

@ -118,41 +118,51 @@ void MainWindow::setupHardware()
int tint_v2 = deviceSettings->value("Tint_v2").toInt(); int tint_v2 = deviceSettings->value("Tint_v2").toInt();
int gain_v2 = deviceSettings->value("Gain_v2").toInt(); int gain_v2 = deviceSettings->value("Gain_v2").toInt();
// Retrieve device-specific settings from DialogDevices // Retrieve device-specific settings from DialogDevices
// Open the dialog to configure devices and retrieve calibration factors
DialogDevices dlg; DialogDevices dlg;
dlg.deviceSettings = deviceSettings; dlg.deviceSettings = deviceSettings;
QVector<QVector<int>> allCalibrationFactors = dlg.getAllCalibrationFactors(); if (dlg.exec() == QDialog::Accepted) {
QVector<QVector<unsigned short>> allCalibrationFactors = dlg.getAllCalibrationFactors();
for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) for (int dev_nr = 0; dev_nr < nr_devices; dev_nr++) {
{ top(deviceSettings);
top(deviceSettings); QString group_label = QString("Device%1").arg(dev_nr);
QString group_label = QString("Device%1").arg(dev_nr); deviceSettings->beginGroup(group_label);
deviceSettings->beginGroup(group_label);
dc.device_id = dev_nr; dc.device_id = dev_nr;
dc.hardware_ver = deviceSettings->value("HardwareVer").toInt(); dc.hardware_ver = deviceSettings->value("HardwareVer").toInt();
ip2num(deviceSettings->value("IP").toString(), ip); ip2num(deviceSettings->value("IP").toString(), ip);
for (int i = 0; i < 4; i++)//4 bytes for (int i = 0; i < 4; i++) { // 4 bytes
dc.device_ip[i] = ip[i]; dc.device_ip[i] = ip[i];
dc.master = deviceSettings->value("Master").toInt();
dc.plane = deviceSettings->value("Plane").toInt();
dc.position = deviceSettings->value("Position").toInt();
dc.nr_sensors = deviceSettings->value("Sensors").toInt();
dc.master_delay = deviceSettings->value("MasterDelay").toInt();
dc.slave_delay = deviceSettings->value("SlaveDelay").toInt();
dc.threshold = deviceSettings->value("Threshold").toInt();
dc.clustersize = deviceSettings->value("ClusterSize").toInt();
/*
// Get calibration factors for this device
QVector<int> calibFactors = allCalibrationFactors[dev_nr];
for (int i = 0; i < 320; i++) {
if (i < calibFactors.size() && calibFactors[i] >= 0 && calibFactors[i] <= 65535) {
dc.calibrationFactor[i] = calibFactors[i];
} else {
dc.calibrationFactor[i] = 8192; // Default value if not set or invalid
} }
} dc.master = deviceSettings->value("Master").toInt();
*/ dc.plane = deviceSettings->value("Plane").toInt();
switch (dc.hardware_ver) dc.position = deviceSettings->value("Position").toInt();
{ dc.nr_sensors = deviceSettings->value("Sensors").toInt();
dc.master_delay = deviceSettings->value("MasterDelay").toInt();
dc.slave_delay = deviceSettings->value("SlaveDelay").toInt();
dc.threshold = deviceSettings->value("Threshold").toInt();
dc.clustersize = deviceSettings->value("ClusterSize").toInt();
// Get calibration factors for this device
if (dev_nr < allCalibrationFactors.size()) {
QVector<unsigned short> calibFactors = allCalibrationFactors[dev_nr];
for (int i = 0; i < 320; i++) {
if (i < calibFactors.size() && calibFactors[i] >= 0 && calibFactors[i] <= 65535) {
dc.calibrationFactor[i] = calibFactors[i];
} else {
dc.calibrationFactor[i] = 8192; // Default value if not set or invalid
}
}
} else {
// Default to 8192 if no calibration factors were set for this device
std::fill(std::begin(dc.calibrationFactor), std::end(dc.calibrationFactor), 8192);
}
switch (dc.hardware_ver)
{
case 1: case 1:
dc.period = period_v1; dc.period = period_v1;
dc.tint = tint_v1; dc.tint = tint_v1;
@ -165,40 +175,14 @@ void MainWindow::setupHardware()
break; break;
default: default:
qCritical("Unsupported hardware version!"); qCritical("Unsupported hardware version!");
break; break;
} }
theHW->configureDevice(dev_nr, dc); //configure the device and an entry in base address table in the event builder theHW->configureDevice(dev_nr, dc); // Configure the device and an entry in base address table in the event builder
}
/*
// Assume calibration factors are stored as a list or comma-separated string
QString calibFactorsStr = deviceSettings->value("CalibFactors").toString();
QStringList calibFactorList = calibFactorsStr.split(',', Qt::SkipEmptyParts);
// Clear any existing calibration data
for (int i = 0; i < 320; i++) {
dc.calibrationFactor[i] = 0; // Initialize to 0 or some default
}
// Load calibration factors
int index = 0;
for (const QString& factor : calibFactorList) {
if (index >= 320) break; // Ensure we do not overflow the array
bool ok;
int value = factor.toInt(&ok);
if (ok) {
dc.calibrationFactor[index] = value;
index++;
} else {
qWarning() << "Invalid calibration factor:" << factor;
} }
} }
*/
//theDisplay.setup(&theHW);
} }

Binary file not shown.