AP_TempratureSensor: add support for analog sensor with polynomial

This commit is contained in:
Iampete1 2023-09-23 00:54:19 +01:00 committed by Andrew Tridgell
parent 53948b1070
commit e7ee287664
7 changed files with 155 additions and 1 deletions

View File

@ -28,6 +28,7 @@
#include "AP_TemperatureSensor_TSYS03.h"
#include "AP_TemperatureSensor_MCP9600.h"
#include "AP_TemperatureSensor_MAX31865.h"
#include "AP_TemperatureSensor_Analog.h"
#include <AP_Logger/AP_Logger.h>
#include <AP_Vehicle/AP_Vehicle_Type.h>
@ -189,6 +190,11 @@ void AP_TemperatureSensor::init()
case AP_TemperatureSensor_Params::Type::TSYS03:
drivers[instance] = new AP_TemperatureSensor_TSYS03(*this, _state[instance], _params[instance]);
break;
#endif
#if AP_TEMPERATURE_SENSOR_ANALOG_ENABLED
case AP_TemperatureSensor_Params::Type::ANALOG:
drivers[instance] = new AP_TemperatureSensor_Analog(*this, _state[instance], _params[instance]);
break;
#endif
case AP_TemperatureSensor_Params::Type::NONE:
default:

View File

@ -26,6 +26,7 @@ class AP_TemperatureSensor_TSYS01;
class AP_TemperatureSensor_MCP9600;
class AP_TemperatureSensor_MAX31865;
class AP_TemperatureSensor_TSYS03;
class AP_TemperatureSensor_Analog;
class AP_TemperatureSensor
{
@ -34,6 +35,7 @@ class AP_TemperatureSensor
friend class AP_TemperatureSensor_MCP9600;
friend class AP_TemperatureSensor_MAX31865;
friend class AP_TemperatureSensor_TSYS03;
friend class AP_TemperatureSensor_Analog;
public:

View File

@ -0,0 +1,97 @@
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AP_TemperatureSensor_config.h"
#if AP_TEMPERATURE_SENSOR_ANALOG_ENABLED
#include "AP_TemperatureSensor_Analog.h"
extern const AP_HAL::HAL &hal;
const AP_Param::GroupInfo AP_TemperatureSensor_Analog::var_info[] = {
// @Param: PIN
// @DisplayName: Temperature sensor analog voltage sensing pin
// @Description: Sets the analog input pin that should be used for temprature monitoring.
// @Values: -1:Disabled, 2:Pixhawk/Pixracer/Navio2/Pixhawk2_PM1, 5:Navigator, 13:Pixhawk2_PM2/CubeOrange_PM2, 14:CubeOrange, 16:Durandal, 100:PX4-v1
// @User: Standard
AP_GROUPINFO("PIN", 1, AP_TemperatureSensor_Analog, _pin, -1),
// @Param: A0
// @DisplayName: Temperature sensor analog 0th polynomial coefficient
// @Description: a0 in polynomial of form temperature in deg = a0 + a1*voltage + a2*voltage^2 + a3*voltage^3 + a4*voltage^4
AP_GROUPINFO("A0", 2, AP_TemperatureSensor_Analog, _a[0], 0),
// @Param: A1
// @DisplayName: Temperature sensor analog 1st polynomial coefficient
// @Description: a1 in polynomial of form temperature in deg = a0 + a1*voltage + a2*voltage^2 + a3*voltage^3 + a4*voltage^4
AP_GROUPINFO("A1", 3, AP_TemperatureSensor_Analog, _a[1], 0),
// @Param: A2
// @DisplayName: Temperature sensor analog 2nd polynomial coefficient
// @Description: a2 in polynomial of form temperature in deg = a0 + a1*voltage + a2*voltage^2 + a3*voltage^3 + a4*voltage^4
AP_GROUPINFO("A2", 4, AP_TemperatureSensor_Analog, _a[2], 0),
// @Param: A3
// @DisplayName: Temperature sensor analog 3rd polynomial coefficient
// @Description: a3 in polynomial of form temperature in deg = a0 + a1*voltage + a2*voltage^2 + a3*voltage^3 + a4*voltage^4
AP_GROUPINFO("A3", 5, AP_TemperatureSensor_Analog, _a[3], 0),
// @Param: A4
// @DisplayName: Temperature sensor analog 4th polynomial coefficient
// @Description: a4 in polynomial of form temperature in deg = a0 + a1*voltage + a2*voltage^2 + a3*voltage^3 + a4*voltage^4
AP_GROUPINFO("A4", 6, AP_TemperatureSensor_Analog, _a[4], 0),
AP_GROUPEND
};
AP_TemperatureSensor_Analog::AP_TemperatureSensor_Analog(AP_TemperatureSensor &front,
AP_TemperatureSensor::TemperatureSensor_State &state,
AP_TemperatureSensor_Params &params) :
AP_TemperatureSensor_Backend(front, state, params)
{
AP_Param::setup_object_defaults(this, var_info);
_state.var_info = var_info;
_analog_source = hal.analogin->channel(_pin);
}
// Update function called at 5Hz
void AP_TemperatureSensor_Analog::update()
{
if ((_analog_source == nullptr) || !_analog_source->set_pin(_pin)) {
// Invalid pln
return;
}
// Use ratiometric voltage, measured voltage is relative to supply
const float voltage = _analog_source->voltage_average_ratiometric();
// Evaluate polynomial
// temperature (deg) = a0 + a1*voltage + a2*voltage^2 + a3*voltage^3 + a4*voltage^4
float temp = 0.0;
float poly = 1.0;
for (uint8_t i = 0; i < ARRAY_SIZE(_a); i++) {
temp += _a[i] * poly;
poly *= voltage;
}
// update state
set_temperature(temp);
}
#endif // AP_TEMPERATURE_SENSOR_ANALOG_ENABLED

View File

@ -0,0 +1,45 @@
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "AP_TemperatureSensor_config.h"
#if AP_TEMPERATURE_SENSOR_ANALOG_ENABLED
#include "AP_TemperatureSensor_Backend.h"
#include <AP_Param/AP_Param.h>
class AP_TemperatureSensor_Analog : public AP_TemperatureSensor_Backend {
public:
AP_TemperatureSensor_Analog(AP_TemperatureSensor &front, AP_TemperatureSensor::TemperatureSensor_State &state, AP_TemperatureSensor_Params &params);
void update(void) override;
static const struct AP_Param::GroupInfo var_info[];
private:
AP_HAL::AnalogSource *_analog_source;
// Pin used to measure voltage
AP_Int8 _pin;
// Polynomial coefficients to calculate temperature from voltage
AP_Float _a[5];
};
#endif // AP_TEMPERATURE_SENSOR_ANALOG_ENABLED

View File

@ -34,7 +34,7 @@ const AP_Param::GroupInfo AP_TemperatureSensor_Params::var_info[] = {
// @Param: TYPE
// @DisplayName: Temperature Sensor Type
// @Description: Enables temperature sensors
// @Values: 0:Disabled, 1:TSYS01, 2:MCP9600, 3:MAX31865, 4:TSYS03
// @Values: 0:Disabled, 1:TSYS01, 2:MCP9600, 3:MAX31865, 4:TSYS03, 5:Analog
// @User: Standard
// @RebootRequired: True
AP_GROUPINFO_FLAGS("TYPE", 1, AP_TemperatureSensor_Params, type, (float)Type::NONE, AP_PARAM_FLAG_ENABLE),

View File

@ -32,6 +32,7 @@ public:
MCP9600 = 2,
MAX31865 = 3,
TSYS03 = 4,
ANALOG = 5,
};
// option to map to another system component

View File

@ -20,6 +20,9 @@
#define AP_TEMPERATURE_SENSOR_MAX31865_ENABLED AP_TEMPERATURE_SENSOR_ENABLED
#endif
#ifndef AP_TEMPERATURE_SENSOR_ANALOG_ENABLED
#define AP_TEMPERATURE_SENSOR_ANALOG_ENABLED AP_TEMPERATURE_SENSOR_ENABLED
#endif
// maximum number of Temperature Sensors