From 697aa4297731c5d4a6d9426c1e8eb7fd05d7a946 Mon Sep 17 00:00:00 2001 From: Yury Shuvakin Date: Tue, 6 Aug 2024 17:11:59 +0900 Subject: [PATCH] Changed temperature calculation mechanism --- .gitignore | 1 + MainWindow.cpp | 80 ++++++++++++++++++++++++++++++++++++++++++++- MainWindow.h | 2 ++ TkrMonitor.pro.user | 4 ++- 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 3c29af1..f700249 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /build *.pro.user +TkrMonitor.pro.user diff --git a/MainWindow.cpp b/MainWindow.cpp index c35518b..0443ba3 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -123,7 +123,8 @@ void MainWindow::handlePackage(int index) coolantTemperatureIndex = 0; } - qint16 coolantTemperature = FlashTdegr[coolantTemperatureIndex << 2] - 55; + coolantTemperatureIndex = coolantTemperatureIndex << 2; + qint16 coolantTemperature = adcToTemperature(coolantTemperatureIndex); parameterModel_->setItem(5, 1, new QStandardItem(QString::number(coolantTemperature))); quint16 outputPhaseCurrent = (message->getByte(3) << 2) + ((message->getByte(7) & 0x0C) >> 2); @@ -290,3 +291,80 @@ QString MainWindow::statusToDescription(quint8 status) return description; } + +qint16 MainWindow::adcToTemperature(quint16 adc) +{ + const double vref = 2.5; // Напряжение опорное + const double vin = 5.0; // Входное напряжение + const double r = 10000; // Сопротивление резистора в Омах (например, 10kΩ) + + // Преобразуем значение АЦП в выходное напряжение + double vout = (adc / 4095.0) * vref; + + // Проверяем, чтобы Vout не было равно Vin + if (vout >= vin) + { + return 0; // Ошибка: Vout не может быть больше или равно Vin + } + + // Вычисляем сопротивление термистора + double r_ntc = r * (vout / (vin - vout)); + + QList> temp_table = + { + {-55, 96.3}, + {-50, 67.01}, + {-45, 47.17}, + {-40, 33.65}, + {-35, 24.26}, + {-30, 17.7}, + {-25, 13.04}, + {-20, 9.707}, + {-15, 7.293}, + {-10, 5.533}, + {-5, 4.232}, + {0, 3.265}, + {5, 2.539}, + {10, 1.99}, + {15, 1.571}, + {20, 1.249}, + {25, 1.0}, + {30, 0.8057}, + {35, 0.6531}, + {40, 0.5327}, + {45, 0.4369}, + {50, 0.3603}, + {55, 0.2986}, + {60, 0.2488}, + {65, 0.2083}, + {70, 0.1752}, + {75, 0.1481}, + {80, 0.1258}, + {85, 0.1072}, + {90, 0.09177}, + {95, 0.07885}, + {100, 0.068}, + {105, 0.05886}, + {110, 0.05112}, + {115, 0.04454}, + {120, 0.03893}, + {125, 0.03417}, + {130, 0.03009}, + {135, 0.02654}, + {140, 0.02348}, + {145, 0.02083}, + {150, 0.01853}, + {155, 0.01653}, + }; + + for (qsizetype i = 0; i < temp_table.size() - 1; i++) + { + if (r_ntc <= temp_table[i].second && r_ntc >= temp_table[i + 1].second) + { + return temp_table[i].first + (temp_table[i + 1].first - temp_table[i].first) * + (r_ntc - temp_table[i].second) / (temp_table[i + 1].second - temp_table[i].second); + } + } + + return 0; +} diff --git a/MainWindow.h b/MainWindow.h index 10e534b..ca0af62 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -35,6 +35,8 @@ private: QString statusToString(quint8 status); QString statusToDescription(quint8 status); + qint16 adcToTemperature(quint16 adc); + Ui::MainWindow* ui = nullptr; QStandardItemModel* parameterModel_ = nullptr; QStandardItemModel* statusModel_ = nullptr; diff --git a/TkrMonitor.pro.user b/TkrMonitor.pro.user index 1a25bb9..8d3ce47 100644 --- a/TkrMonitor.pro.user +++ b/TkrMonitor.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -69,7 +69,9 @@ true true + false + 0 true