[modules] Update ADC temperature to be configurable (#1607)

This commit is contained in:
Freek van Tienen
2016-04-20 17:58:09 +02:00
committed by Felix Ruess
parent d29c83aab9
commit bda5ae78df
4 changed files with 53 additions and 30 deletions
+8 -1
View File
@@ -5,11 +5,18 @@
<description> <description>
Temperature ADC Temperature ADC
Measure temperature of 3 sensores connecter to any ADC Measure temperature of 3 sensores connecter to any ADC
Can read LM35 or 10k / 100k NTC sensor Can read LM35 or NTC sensor
</description> </description>
<define name="TEMP_ADC_CHANNELX" value="ADC_X" description="choose which ADC is used for temperature sensor"/> <define name="TEMP_ADC_CHANNELX" value="ADC_X" description="choose which ADC is used for temperature sensor"/>
<define name="TEMP_ADC_CHANNELX_TYPE" value="XXXX" description="choose which type of sensor is used (LM35 or NTC)"/> <define name="TEMP_ADC_CHANNELX_TYPE" value="XXXX" description="choose which type of sensor is used (LM35 or NTC)"/>
</doc> </doc>
<settings>
<dl_settings>
<dl_settings name="temp_adc">
<dl_setting min="0" max="1" step="1" module="sensors/temp_adc" var="temp_adc_sync_send" shortname="sync_send" param="TEMP_ADC_SYNC_SEND" values="Off|On"/>
</dl_settings>
</dl_settings>
</settings>
<header> <header>
<file name="temp_adc.h"/> <file name="temp_adc.h"/>
</header> </header>
+9
View File
@@ -45,6 +45,15 @@
microcontroller architecture. microcontroller architecture.
*/ */
/* Set the correct ADC resolution */
#ifndef ADC_RESOLUTION
#if defined(STM32F1) || defined(STM32F4)
#define ADC_RESOLUTION 4096
#else
#define ADC_RESOLUTION 1024
#endif
#endif
/** /**
Struct to collect samples from ADC and building an average Struct to collect samples from ADC and building an average
over MAX_AV_NB_SAMPLE values. over MAX_AV_NB_SAMPLE values.
+35 -23
View File
@@ -31,7 +31,8 @@
#include "subsystems/datalink/downlink.h" #include "subsystems/datalink/downlink.h"
#include BOARD_CONFIG #include BOARD_CONFIG
float temp_c1, temp_c2, temp_c3; bool temp_adc_sync_send = false;
static float temp_c1, temp_c2, temp_c3;
#if PERIODIC_TELEMETRY #if PERIODIC_TELEMETRY
#include "subsystems/datalink/telemetry.h" #include "subsystems/datalink/telemetry.h"
@@ -48,26 +49,28 @@ float temp_c1, temp_c2, temp_c3;
#endif #endif
#endif #endif
#ifdef TEMP_ADC_CHANNEL1
static struct adc_buf temp_buf1;
#ifndef TEMP_ADC_CHANNEL1_TYPE #ifndef TEMP_ADC_CHANNEL1_TYPE
#define TEMP_ADC_CHANNEL1_TYPE LM35 #define TEMP_ADC_CHANNEL1_TYPE LM35
#endif #endif
#ifndef TEMP_ADC_CHANNEL2_TYPE
#define TEMP_ADC_CHANNEL2_TYPE LM35
#endif
#ifndef TEMP_ADC_CHANNEL3_TYPE
#define TEMP_ADC_CHANNEL3_TYPE LM35
#endif
#ifdef TEMP_ADC_CHANNEL1
static struct adc_buf temp_buf1;
#endif #endif
#ifdef TEMP_ADC_CHANNEL2 #ifdef TEMP_ADC_CHANNEL2
static struct adc_buf temp_buf2; static struct adc_buf temp_buf2;
#ifndef TEMP_ADC_CHANNEL2_TYPE
#define TEMP_ADC_CHANNEL2_TYPE LM35
#endif
#endif #endif
#ifdef TEMP_ADC_CHANNEL3 #ifdef TEMP_ADC_CHANNEL3
static struct adc_buf temp_buf3; static struct adc_buf temp_buf3;
#ifndef TEMP_ADC_CHANNEL3_TYPE
#define TEMP_ADC_CHANNEL3_TYPE LM35
#endif
#endif #endif
#ifndef TEMP_ADC_NB_SAMPLES #ifndef TEMP_ADC_NB_SAMPLES
@@ -81,19 +84,22 @@ static struct adc_buf temp_buf3;
#define TEMP_ADC_SYNC_SEND FALSE #define TEMP_ADC_SYNC_SEND FALSE
#endif #endif
float calc_ntc(int16_t raw_temp) /**
* Calculate the NTC tempreature in celcius based on the Steinhart equation
*/
static inline float calc_ntc(int16_t raw_adc, uint16_t pull_up_r, float a, float b, float c)
{ {
float temp_c; // Calculate the logaritmic resistance value based on the Pull up resistor
//calc for NTC float log_r = log((pull_up_r * raw_adc) / (ADC_RESOLUTION - raw_adc));
temp_c = log(((10240000 / raw_temp) - 10000) * 10);
//temp_c = 1/(0.001129148+(0.000234125*temp_c)+(0.0000000876741*temp_c*temp_c*temp_c)); // Steinhart equation (https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation)
temp_c = 1 / (0.000603985662844 + (0.000229995493730 * temp_c) + (0.000000067653027 * temp_c * // 1 / T = a + b*len(R) + c*ln(R)³
temp_c * temp_c)); float temp_c = 1 / (a + (b * log_r) + (c * log_r * log_r * log_r));
temp_c = temp_c - 273.15; //convert do celcius temp_c = temp_c - 273.15; // Convert do celcius
return temp_c; return temp_c;
} }
float calc_lm35(int16_t raw_temp) static inline float calc_lm35(int16_t raw_temp)
{ {
return ((float)raw_temp * (3300.0f / 1024.0f) / 10.0f); return ((float)raw_temp * (3300.0f / 1024.0f) / 10.0f);
} }
@@ -103,6 +109,9 @@ static void temp_adc_downlink(struct transport_tx *trans, struct link_device *de
pprz_msg_send_TEMP_ADC(trans, dev, AC_ID, &temp_c1, &temp_c2, &temp_c3); pprz_msg_send_TEMP_ADC(trans, dev, AC_ID, &temp_c1, &temp_c2, &temp_c3);
} }
/**
* Temperature ADC initialize channels
*/
void temp_adc_init(void) void temp_adc_init(void)
{ {
temp_adc_sync_send = TEMP_ADC_SYNC_SEND; temp_adc_sync_send = TEMP_ADC_SYNC_SEND;
@@ -134,7 +143,8 @@ void temp_adc_periodic(void)
#if TEMP_ADC_CHANNEL1_TYPE == LM35 #if TEMP_ADC_CHANNEL1_TYPE == LM35
temp_c1 = calc_lm35(adc_raw); temp_c1 = calc_lm35(adc_raw);
#elif TEMP_ADC_CHANNEL1_TYPE == NTC #elif TEMP_ADC_CHANNEL1_TYPE == NTC
temp_c1 = calc_ntc(adc_raw); temp_c1 = calc_ntc(adc_raw, TEMP_ADC_CHANNEL1_PU_R,
TEMP_ADC_CHANNEL1_A, TEMP_ADC_CHANNEL1_B, TEMP_ADC_CHANNEL1_C);
#endif #endif
#endif #endif
@@ -143,7 +153,8 @@ void temp_adc_periodic(void)
#if TEMP_ADC_CHANNEL2_TYPE == LM35 #if TEMP_ADC_CHANNEL2_TYPE == LM35
temp_c2 = calc_lm35(adc_raw); temp_c2 = calc_lm35(adc_raw);
#elif TEMP_ADC_CHANNEL2_TYPE == NTC #elif TEMP_ADC_CHANNEL2_TYPE == NTC
temp_c2 = calc_ntc(adc_raw); temp_c2 = calc_ntc(adc_raw, TEMP_ADC_CHANNEL2_PU_R,
TEMP_ADC_CHANNEL2_A, TEMP_ADC_CHANNEL2_B, TEMP_ADC_CHANNEL2_C);
#endif #endif
#endif #endif
@@ -152,12 +163,13 @@ void temp_adc_periodic(void)
#if TEMP_ADC_CHANNEL3_TYPE == LM35 #if TEMP_ADC_CHANNEL3_TYPE == LM35
temp_c3 = calc_lm35(adc_raw); temp_c3 = calc_lm35(adc_raw);
#elif TEMP_ADC_CHANNEL3_TYPE == NTC #elif TEMP_ADC_CHANNEL3_TYPE == NTC
temp_c3 = calc_ntc(adc_raw); temp_c3 = calc_ntc(adc_raw, TEMP_ADC_CHANNEL3_PU_R,
TEMP_ADC_CHANNEL3_A, TEMP_ADC_CHANNEL3_B, TEMP_ADC_CHANNEL3_C);
#endif #endif
#endif #endif
/* Send measurements as soon as they are calculated */
if (temp_adc_sync_send) { if (temp_adc_sync_send) {
temp_adc_downlink(&(DefaultChannel).trans_tx, &(DefaultDevice).device); temp_adc_downlink(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
} }
} }
+1 -6
View File
@@ -28,12 +28,7 @@
#include "std.h" #include "std.h"
/// flag to enable sending every new measurement via telemetry extern bool temp_adc_sync_send;
bool temp_adc_sync_send;
float calc_ntc(int16_t raw_temp);
float calc_lm35(int16_t raw_temp);
void temp_adc_init(void); void temp_adc_init(void);
void temp_adc_periodic(void); void temp_adc_periodic(void);