Change sensor interface to provide normalized values
This commit is contained in:
parent
03e2c950ed
commit
2704bb2636
@ -1,5 +1,6 @@
|
|||||||
#include "sensor.hpp"
|
#include "sensor.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
|
||||||
#include <QSerialPort>
|
#include <QSerialPort>
|
||||||
@ -7,25 +8,14 @@
|
|||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
|
|
||||||
#include "log_tr.hpp"
|
#include "log_tr.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
Sensor::~Sensor()
|
Sensor::~Sensor()
|
||||||
{
|
{
|
||||||
qDebug(ltr("Destroying sensor on serial port %1").arg(m_serialPortName));
|
qDebug(ltr("Destroying sensor on serial port %1").arg(m_serialPortName));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<int, int> Sensor::readRange()
|
std::vector<float> Sensor::readValues()
|
||||||
{
|
|
||||||
qDebug(ltr("Reading range of sensor on port %1").arg(m_serialPortName));
|
|
||||||
|
|
||||||
const auto rangeCmd = QByteArray(RANGE_CMD.data(), static_cast<int>(RANGE_CMD.size()));
|
|
||||||
QByteArray response;
|
|
||||||
if(!getSensorCommandResponse(m_serialPortName, rangeCmd, response))
|
|
||||||
return {-1, -1};
|
|
||||||
|
|
||||||
return parseRangeResponse(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<int> Sensor::readValues()
|
|
||||||
{
|
{
|
||||||
qDebug(ltr("Reading values of sensor on port %1").arg(m_serialPortName));
|
qDebug(ltr("Reading values of sensor on port %1").arg(m_serialPortName));
|
||||||
|
|
||||||
@ -34,7 +24,27 @@ std::vector<int> Sensor::readValues()
|
|||||||
if(!getSensorCommandResponse(m_serialPortName, readCmd, response))
|
if(!getSensorCommandResponse(m_serialPortName, readCmd, response))
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
return parseReadResponse(response);
|
const auto values = parseReadResponse(response);
|
||||||
|
|
||||||
|
if(!m_range) {
|
||||||
|
qDebug(ltr("No sensor range known"));
|
||||||
|
|
||||||
|
const auto sensorRange = readRange();
|
||||||
|
if(m_errorOccurred) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
m_range = sensorRange;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<float> normalizedValues;
|
||||||
|
|
||||||
|
for(const auto& value: values) {
|
||||||
|
const auto mappedValue = utils::map(value, m_range->first, m_range->second, 0.f, 1.f);
|
||||||
|
normalizedValues.push_back(std::clamp(mappedValue, 0.f, 1.f));
|
||||||
|
}
|
||||||
|
|
||||||
|
return normalizedValues;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sensor::Sensor(const QString& serialPortName) : m_serialPortName(serialPortName)
|
Sensor::Sensor(const QString& serialPortName) : m_serialPortName(serialPortName)
|
||||||
@ -156,3 +166,15 @@ std::vector<int> Sensor::parseReadResponse(const QByteArray& response) const
|
|||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::pair<int, int> Sensor::readRange()
|
||||||
|
{
|
||||||
|
qDebug(ltr("Reading range of sensor on port %1").arg(m_serialPortName));
|
||||||
|
|
||||||
|
const auto rangeCmd = QByteArray(RANGE_CMD.data(), static_cast<int>(RANGE_CMD.size()));
|
||||||
|
QByteArray response;
|
||||||
|
if(!getSensorCommandResponse(m_serialPortName, rangeCmd, response))
|
||||||
|
return {-1, -1};
|
||||||
|
|
||||||
|
return parseRangeResponse(response);
|
||||||
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@ -12,8 +13,7 @@ class Sensor {
|
|||||||
public:
|
public:
|
||||||
~Sensor();
|
~Sensor();
|
||||||
|
|
||||||
std::pair<int, int> readRange();
|
std::vector<float> readValues();
|
||||||
std::vector<int> readValues();
|
|
||||||
|
|
||||||
operator bool() const { return !m_errorOccurred; }
|
operator bool() const { return !m_errorOccurred; }
|
||||||
|
|
||||||
@ -29,6 +29,8 @@ class Sensor {
|
|||||||
const QString m_serialPortName;
|
const QString m_serialPortName;
|
||||||
bool m_errorOccurred = false;
|
bool m_errorOccurred = false;
|
||||||
|
|
||||||
|
std::optional<std::pair<int, int>> m_range;
|
||||||
|
|
||||||
Sensor(const QString& serialPortName);
|
Sensor(const QString& serialPortName);
|
||||||
|
|
||||||
bool isValidSensor();
|
bool isValidSensor();
|
||||||
@ -38,4 +40,6 @@ class Sensor {
|
|||||||
std::string parseVersionResponse(const QByteArray& response) const;
|
std::string parseVersionResponse(const QByteArray& response) const;
|
||||||
std::pair<int, int> parseRangeResponse(const QByteArray& response) const;
|
std::pair<int, int> parseRangeResponse(const QByteArray& response) const;
|
||||||
std::vector<int> parseReadResponse(const QByteArray& response) const;
|
std::vector<int> parseReadResponse(const QByteArray& response) const;
|
||||||
|
|
||||||
|
std::pair<int, int> readRange();
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user