Paparazzi UAS  v5.8.2_stable-0-g6260b7c
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 "messages.h"
32 #include BOARD_CONFIG
33 
35 
36 #if PERIODIC_TELEMETRY
38 #endif
39 
40 #define LM35 0
41 #define NTC 1
42 
43 #ifndef TEMP_ADC_CHANNEL1
44 #ifndef TEMP_ADC_CHANNEL2
45 #ifndef TEMP_ADC_CHANNEL3
46 #error "at least one TEMP_ADC_CHANNEL1/2/3 needs to be defined to use the temp_adc module"
47 #endif
48 #endif
49 #endif
50 
51 #ifndef TEMP_ADC_CHANNEL1_TYPE
52 #define TEMP_ADC_CHANNEL1_TYPE LM35
53 #endif
54 #ifndef TEMP_ADC_CHANNEL2_TYPE
55 #define TEMP_ADC_CHANNEL2_TYPE LM35
56 #endif
57 #ifndef TEMP_ADC_CHANNEL3_TYPE
58 #define TEMP_ADC_CHANNEL3_TYPE LM35
59 #endif
60 
61 #ifdef TEMP_ADC_CHANNEL1
62 static struct adc_buf temp_buf1;
63 #endif
64 
65 #ifdef TEMP_ADC_CHANNEL2
66 static struct adc_buf temp_buf2;
67 #endif
68 
69 #ifdef TEMP_ADC_CHANNEL3
70 static struct adc_buf temp_buf3;
71 #endif
72 
73 #ifndef TEMP_ADC_NB_SAMPLES
74 #define TEMP_ADC_NB_SAMPLES DEFAULT_AV_NB_SAMPLE
75 #endif
76 
80 #ifndef TEMP_ADC_SYNC_SEND
81 #define TEMP_ADC_SYNC_SEND FALSE
82 #endif
83 
84 float calc_ntc(int16_t raw_temp)
85 {
86  float temp_c;
87  //calc for NTC
88  temp_c = log(((10240000 / raw_temp) - 10000) * 10);
89  //temp_c = 1/(0.001129148+(0.000234125*temp_c)+(0.0000000876741*temp_c*temp_c*temp_c));
90  temp_c = 1 / (0.000603985662844 + (0.000229995493730 * temp_c) + (0.000000067653027 * temp_c *
91  temp_c * temp_c));
92  temp_c = temp_c - 273.15; //convert do celcius
93  return temp_c;
94 }
95 
96 float calc_lm35(int16_t raw_temp)
97 {
98  return ((float)raw_temp * (3300.0f / 1024.0f) / 10.0f);
99 }
100 
101 static void temp_adc_downlink(struct transport_tx *trans, struct link_device *dev)
102 {
103  pprz_msg_send_TEMP_ADC(trans, dev, AC_ID, &temp_c1, &temp_c2, &temp_c3);
104 }
105 
106 void temp_adc_init(void)
107 {
109 
110 #ifdef TEMP_ADC_CHANNEL1
111  adc_buf_channel(TEMP_ADC_CHANNEL1, &temp_buf1, TEMP_ADC_NB_SAMPLES);
112 #endif
113 
114 #ifdef TEMP_ADC_CHANNEL2
115  adc_buf_channel(TEMP_ADC_CHANNEL2, &temp_buf2, TEMP_ADC_NB_SAMPLES);
116 #endif
117 
118 #ifdef TEMP_ADC_CHANNEL3
119  adc_buf_channel(TEMP_ADC_CHANNEL3, &temp_buf3, TEMP_ADC_NB_SAMPLES);
120 #endif
121 
122 #if PERIODIC_TELEMETRY
124 #endif
125 }
126 
127 
129 {
130  uint16_t adc_raw;
131 
132 #ifdef TEMP_ADC_CHANNEL1
133  adc_raw = temp_buf1.sum / temp_buf1.av_nb_sample;
134 #if TEMP_ADC_CHANNEL1_TYPE == LM35
135  temp_c1 = calc_lm35(adc_raw);
136 #elif TEMP_ADC_CHANNEL1_TYPE == NTC
137  temp_c1 = calc_ntc(adc_raw);
138 #endif
139 #endif
140 
141 #ifdef TEMP_ADC_CHANNEL2
142  adc_raw = temp_buf2.sum / temp_buf2.av_nb_sample;
143 #if TEMP_ADC_CHANNEL2_TYPE == LM35
144  temp_c2 = calc_lm35(adc_raw);
145 #elif TEMP_ADC_CHANNEL2_TYPE == NTC
146  temp_c2 = calc_ntc(adc_raw);
147 #endif
148 #endif
149 
150 #ifdef TEMP_ADC_CHANNEL3
151  adc_raw = temp_buf3.sum / temp_buf3.av_nb_sample;
152 #if TEMP_ADC_CHANNEL3_TYPE == LM35
153  temp_c3 = calc_lm35(adc_raw);
154 #elif TEMP_ADC_CHANNEL3_TYPE == NTC
155  temp_c3 = calc_ntc(adc_raw);
156 #endif
157 #endif
158 
159  if (temp_adc_sync_send) {
160  temp_adc_downlink(&(DefaultChannel).trans_tx, &(DefaultDevice).device);
161  }
162 
163 }
#define TEMP_ADC_NB_SAMPLES
Definition: temp_adc.c:74
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
Generic transmission transport header.
Definition: transport.h:89
float temp_c1
Definition: temp_adc.c:34
Periodic telemetry system header (includes downlink utility and generated code).
bool_t temp_adc_sync_send
flag to enable sending every new measurement via telemetry
Definition: temp_adc.h:32
arch independent ADC (Analog to Digital Converter) API
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:53
float calc_ntc(int16_t raw_temp)
Definition: temp_adc.c:84
float calc_lm35(int16_t raw_temp)
Definition: temp_adc.c:96
static void temp_adc_downlink(struct transport_tx *trans, struct link_device *dev)
Definition: temp_adc.c:101
signed short int16_t
Definition: types.h:17
void temp_adc_periodic(void)
Definition: temp_adc.c:128
float temp_c3
Definition: temp_adc.c:34
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
void adc_buf_channel(uint8_t adc_channel, struct adc_buf *s, uint8_t av_nb_sample)
Registers a buffer to be used to store the specified converted channel Usage:
Definition: adc_arch.c:91
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
float temp_c2
Definition: temp_adc.c:34
temperature driver for LM35 and 100k NTC analog sensores
void temp_adc_init(void)
Definition: temp_adc.c:106
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_SYNC_SEND
Send a TEMP_ADC message with every new measurement.
Definition: temp_adc.c:81