Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sonar_adc.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Gautier Hattenberger, 2013 Tobias M√ľnch
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  */
22 
24 #include "generated/airframe.h"
25 #include "mcu_periph/adc.h"
26 #ifdef SITL
27 #include "state.h"
28 #endif
29 
30 #include "mcu_periph/uart.h"
31 #include "messages.h"
33 #ifndef DOWNLINK_DEVICE
34 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
35 #endif
36 
41 #ifndef SONAR_OFFSET
42 #define SONAR_OFFSET 0.
43 #endif
44 
48 #ifndef SONAR_SCALE
49 #define SONAR_SCALE 0.0166
50 #endif
51 
57 
58 #ifndef SITL
59 static struct adc_buf sonar_adc;
60 #endif
61 
62 void sonar_adc_init(void) {
63  sonar_meas = 0;
65  sonar_distance = 0;
68 
69 #ifndef SITL
70  adc_buf_channel(ADC_CHANNEL_SONAR, &sonar_adc, DEFAULT_AV_NB_SAMPLE);
71 #endif
72 }
73 
76 void sonar_adc_read(void) {
77 #ifndef SITL
81 
82 #else // SITL
84  Bound(sonar_distance, 0.1f, 7.0f);
85 #endif // SITL
86 
87 #ifdef SENSOR_SYNC_SEND_SONAR
88  DOWNLINK_SEND_SONAR(DefaultChannel, DefaultDevice, &sonar_meas, &sonar_distance);
89 #endif
90 }
91 
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
uint16_t sonar_meas
Raw ADC value.
Definition: sonar_adc.c:52
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:59
float sonar_distance
Sonar distance in m.
Definition: sonar_adc.c:54
float z
in meters
static struct EnuCoor_f * stateGetPositionEnu_f(void)
Get position in local ENU coordinates (float).
Definition: state.h:672
uint8_t av_nb_sample
Definition: adc.h:64
void sonar_adc_read(void)
Read ADC value to update sonar measurement.
Definition: sonar_adc.c:76
arch independent ADC (Analog to Digital Converter) API
uint32_t sum
Definition: adc.h:61
#define FALSE
Definition: imu_chimu.h:141
void sonar_adc_init(void)
Definition: sonar_adc.c:62
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:60
#define SONAR_SCALE
Sonar scale.
Definition: sonar_adc.c:49
#define TRUE
Definition: imu_chimu.h:144
static struct adc_buf sonar_adc
Definition: sonar_adc.c:59
API to get/set the generic vehicle states.
float sonar_scale
Definition: sonar_adc.c:56
simple driver to deal with one sonar sensor on ADC
#define DEFAULT_AV_NB_SAMPLE
Definition: adc.h:48
bool_t sonar_data_available
New data available.
Definition: sonar_adc.c:53
#define SONAR_OFFSET
Sonar offset.
Definition: sonar_adc.c:42
float sonar_offset
Definition: sonar_adc.c:55