Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
temp_adc.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Eduardo Lavratti <agressiva@hotmail.com>
3  *
4  * This file is part of paparazzi
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
26 #include "std.h"
28 #include "mcu_periph/adc.h"
29 #include "mcu_periph/uart.h"
30 #include "pprzlink/messages.h"
32 #include BOARD_CONFIG
33 
34 bool temp_adc_sync_send = false;
35 static float temp_c1, temp_c2, temp_c3;
36 
37 #if PERIODIC_TELEMETRY
39 #endif
40 
41 #define LM35 0
42 #define NTC 1
43 
44 #ifndef TEMP_ADC_CHANNEL1
45 #ifndef TEMP_ADC_CHANNEL2
46 #ifndef TEMP_ADC_CHANNEL3
47 #error "at least one TEMP_ADC_CHANNEL1/2/3 needs to be defined to use the temp_adc module"
48 #endif
49 #endif
50 #endif
51 
52 #ifdef TEMP_ADC_CHANNEL1
53 static struct adc_buf temp_buf1;
54 
55 #ifndef TEMP_ADC_CHANNEL1_TYPE
56 #define TEMP_ADC_CHANNEL1_TYPE LM35
57 #endif
58 #endif
59 
60 #ifdef TEMP_ADC_CHANNEL2
61 static struct adc_buf temp_buf2;
62 
63 #ifndef TEMP_ADC_CHANNEL2_TYPE
64 #define TEMP_ADC_CHANNEL2_TYPE LM35
65 #endif
66 #endif
67 
68 #ifdef TEMP_ADC_CHANNEL3
69 static struct adc_buf temp_buf3;
70 
71 #ifndef TEMP_ADC_CHANNEL3_TYPE
72 #define TEMP_ADC_CHANNEL3_TYPE LM35
73 #endif
74 #endif
75 
76 #ifndef TEMP_ADC_NB_SAMPLES
77 #define TEMP_ADC_NB_SAMPLES DEFAULT_AV_NB_SAMPLE
78 #endif
79 
83 #ifndef TEMP_ADC_SYNC_SEND
84 #define TEMP_ADC_SYNC_SEND FALSE
85 #endif
86 
90 static inline float calc_ntc(int16_t raw_adc, uint16_t pull_up_r, float a, float b, float c)
91 {
92  // Calculate the logaritmic resistance value based on the Pull up resistor
93  float log_r = log((pull_up_r * raw_adc) / (ADC_RESOLUTION - raw_adc));
94 
95  // Steinhart equation (https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation)
96  // 1 / T = a + b*len(R) + c*ln(R)³
97  float temp_c = 1 / (a + (b * log_r) + (c * log_r * log_r * log_r));
98  temp_c = temp_c - 273.15; // Convert do celcius
99  return temp_c;
100 }
101 
102 static inline float calc_lm35(int16_t raw_temp)
103 {
104  return ((float)raw_temp * (3300.0f / 1024.0f) / 10.0f);
105 }
106 
107 static void temp_adc_downlink(struct transport_tx *trans, struct link_device *dev)
108 {
109  pprz_msg_send_TEMP_ADC(trans, dev, AC_ID, &temp_c1, &temp_c2, &temp_c3);
110 }
111 
115 void temp_adc_init(void)
116 {
118 
119 #ifdef TEMP_ADC_CHANNEL1
121 #endif
122 
123 #ifdef TEMP_ADC_CHANNEL2
125 #endif
126 
127 #ifdef TEMP_ADC_CHANNEL3
128  adc_buf_channel(TEMP_ADC_CHANNEL3, &temp_buf3, TEMP_ADC_NB_SAMPLES);
129 #endif
130 
131 #if PERIODIC_TELEMETRY
133 #endif
134 }
135 
136 
138 {
139  uint16_t adc_raw;
140 
141 #ifdef TEMP_ADC_CHANNEL1
142  adc_raw = temp_buf1.sum / temp_buf1.av_nb_sample;
143 #if TEMP_ADC_CHANNEL1_TYPE == LM35
144  temp_c1 = calc_lm35(adc_raw);
145 #elif TEMP_ADC_CHANNEL1_TYPE == NTC
148 #endif
149 #endif
150 
151 #ifdef TEMP_ADC_CHANNEL2
152  adc_raw = temp_buf2.sum / temp_buf2.av_nb_sample;
153 #if TEMP_ADC_CHANNEL2_TYPE == LM35
154  temp_c2 = calc_lm35(adc_raw);
155 #elif TEMP_ADC_CHANNEL2_TYPE == NTC
158 #endif
159 #endif
160 
161 #ifdef TEMP_ADC_CHANNEL3
162  adc_raw = temp_buf3.sum / temp_buf3.av_nb_sample;
163 #if TEMP_ADC_CHANNEL3_TYPE == LM35
164  temp_c3 = calc_lm35(adc_raw);
165 #elif TEMP_ADC_CHANNEL3_TYPE == NTC
166  temp_c3 = calc_ntc(adc_raw, TEMP_ADC_CHANNEL3_PU_R,
167  TEMP_ADC_CHANNEL3_A, TEMP_ADC_CHANNEL3_B, TEMP_ADC_CHANNEL3_C);
168 #endif
169 #endif
170 
171  /* Send measurements as soon as they are calculated */
172  if (temp_adc_sync_send) {
173  temp_adc_downlink(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
174  }
175 }
#define TEMP_ADC_NB_SAMPLES
Definition: temp_adc.c:77
unsigned short uint16_t
Definition: types.h:16
#define ADC_RESOLUTION
Definition: adc_arch.h:37
#define TEMP_ADC_CHANNEL1_PU_R
Definition: opa_ap_1.0.h:214
static float calc_ntc(int16_t raw_adc, uint16_t pull_up_r, float a, float b, float c)
Calculate the NTC tempreature in celcius based on the Steinhart equation.
Definition: temp_adc.c:90
#define TEMP_ADC_CHANNEL2_B
Definition: opa_ap_1.0.h:230
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
static float temp_c1
Definition: temp_adc.c:35
Periodic telemetry system header (includes downlink utility and generated code).
void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
Link between ChibiOS ADC drivers and Paparazzi adc_buffers.
Definition: adc_arch.c:265
arch independent ADC (Analog to Digital Converter) API
static float calc_lm35(int16_t raw_temp)
Definition: temp_adc.c:102
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:53
#define TEMP_ADC_CHANNEL2_PU_R
Definition: opa_ap_1.0.h:228
static void temp_adc_downlink(struct transport_tx *trans, struct link_device *dev)
Definition: temp_adc.c:107
signed short int16_t
Definition: types.h:17
void temp_adc_periodic(void)
Definition: temp_adc.c:137
static float temp_c3
Definition: temp_adc.c:35
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
#define TEMP_ADC_CHANNEL2
Definition: opa_ap_1.0.h:226
#define TEMP_ADC_CHANNEL2_A
Definition: opa_ap_1.0.h:229
static float temp_c2
Definition: temp_adc.c:35
#define TEMP_ADC_CHANNEL1_C
Definition: opa_ap_1.0.h:217
bool temp_adc_sync_send
Definition: temp_adc.c:34
temperature driver for LM35 and 100k NTC analog sensores
#define TEMP_ADC_CHANNEL2_C
Definition: opa_ap_1.0.h:231
void temp_adc_init(void)
Temperature ADC initialize channels.
Definition: temp_adc.c:115
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
#define TEMP_ADC_CHANNEL1_A
Definition: opa_ap_1.0.h:215
#define TEMP_ADC_CHANNEL1
Definition: opa_ap_1.0.h:212
#define TEMP_ADC_CHANNEL1_B
Definition: opa_ap_1.0.h:216
#define TEMP_ADC_SYNC_SEND
Send a TEMP_ADC message with every new measurement.
Definition: temp_adc.c:84