-
Notifications
You must be signed in to change notification settings - Fork 18k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AP_TempratureSensor: add support for analog sensor with polynomial
- Loading branch information
Showing
7 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
libraries/AP_TemperatureSensor/AP_TemperatureSensor_Analog.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ¶ms) : | ||
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 | ||
|
45 changes: 45 additions & 0 deletions
45
libraries/AP_TemperatureSensor/AP_TemperatureSensor_Analog.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ¶ms); | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters