Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
mf_ptu.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Gautier Hattenberger
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 
23 #include "modules/meteo/mf_ptu.h"
24 
25 #include "mcu_periph/gpio.h"
26 #include "mcu_periph/adc.h"
27 #include "mcu_periph/pwm_input.h"
28 #include "generated/airframe.h"
29 
33 #ifndef PTU_PRESSURE_OFFSET
34 #define PTU_PRESSURE_OFFSET 0
35 #endif
36 #ifndef PTU_PRESSURE_SCALE
37 #define PTU_PRESSURE_SCALE 1.0
38 #endif
39 #ifndef PTU_TEMPERATURE_OFFSET
40 #define PTU_TEMPERATURE_OFFSET 0
41 #endif
42 #ifndef PTU_TEMPERATURE_SCALE
43 #define PTU_TEMPERATURE_SCALE 1.0
44 #endif
45 #ifndef PTU_HUMIDTY_OFFSET
46 #define PTU_HUMIDTY_OFFSET 0
47 #endif
48 #ifndef PTU_HUMIDTY_SCALE
49 #define PTU_HUMIDTY_SCALE 1.0
50 #endif
51 
53 #ifndef ADC_CHANNEL_PTU_NB_SAMPLES
54 #define ADC_CHANNEL_PTU_NB_SAMPLES DEFAULT_AV_NB_SAMPLE
55 #endif
58 
63 
64 #if LOG_PTU
66 bool log_ptu_started;
67 #endif
68 
69 #if SEND_PTU
70 #include "mcu_periph/uart.h"
71 #include "pprzlink/messages.h"
73 #include "subsystems/gps.h"
74 #endif
75 
76 void mf_ptu_init(void)
77 {
79  adc_buf_channel(ADC_CHANNEL_TEMPERATURE, &temp_buf, ADC_CHANNEL_PTU_NB_SAMPLES);
80 
81 #ifdef PTU_POWER_GPIO
82  gpio_setup_output(PTU_POWER_GPIO);
83  gpio_set(PTU_POWER_GPIO);
84 #endif
85 
86  pressure_adc = 0;
87  temp_adc = 0;
88  humid_period = 0;
89 
90 #if LOG_PTU
91  log_ptu_started = false;
92 #endif
93 }
94 
95 void mf_ptu_periodic(void)
96 {
97  // Read ADC
100  // Read PWM
101  humid_period = get_pwm_input_period_in_usec(PWM_INPUT_CHANNEL_HUMIDITY);
102 
103  // Log data
104 #if LOG_PTU
105  if (pprzLogFile != -1) {
106  if (!log_ptu_started) {
107  sdLogWriteLog(pprzLogFile,
108  "P(adc) T(adc) H(usec) GPS_fix TOW(ms) Week Lat(1e7rad) Lon(1e7rad) HMSL(mm) gpseed(cm/s) course(1e7rad) climb(cm/s)\n");
109  log_ptu_started = true;
110  } else {
111  sdLogWriteLog(pprzLogFile, "%d %d %d %d %d %d %d %d %d %d %d %d\n",
113  gps.fix, gps.tow, gps.week,
116  }
117  }
118 #endif
119 
120  // Send data
121 #if SEND_PTU
122 #define PTU_DATA_SIZE 3
123  float ptu_data[PTU_DATA_SIZE];
124  ptu_data[0] = (float)(PTU_PRESSURE_SCALE * ((int16_t)pressure_adc - PTU_PRESSURE_OFFSET));
125  ptu_data[1] = (float)(PTU_TEMPERATURE_SCALE * ((int16_t)temp_adc - PTU_TEMPERATURE_OFFSET));
126  ptu_data[2] = (float)(PTU_HUMIDTY_SCALE * ((int16_t)humid_period - PTU_HUMIDTY_OFFSET));
127  DOWNLINK_SEND_PAYLOAD_FLOAT(DefaultChannel, DefaultDevice, PTU_DATA_SIZE, ptu_data);
128 #endif
129 }
130 
131 
PTU_HUMIDTY_OFFSET
#define PTU_HUMIDTY_OFFSET
Definition: mf_ptu.c:46
pressure_buf
struct adc_buf pressure_buf
Definition: mf_ptu.c:56
LlaCoor_i::lon
int32_t lon
in degrees*1e7
Definition: pprz_geodetic_int.h:61
uint16_t
unsigned short uint16_t
Definition: types.h:16
pprzLogFile
FileDes pprzLogFile
Definition: sdlog_chibios.c:86
PTU_PRESSURE_SCALE
#define PTU_PRESSURE_SCALE
Definition: mf_ptu.c:37
PTU_HUMIDTY_SCALE
#define PTU_HUMIDTY_SCALE
Definition: mf_ptu.c:49
GpsState::tow
uint32_t tow
GPS time of week in ms.
Definition: gps.h:109
ADC_CHANNEL_PTU_NB_SAMPLES
#define ADC_CHANNEL_PTU_NB_SAMPLES
ADC buffers.
Definition: mf_ptu.c:54
adc_buf_channel
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:289
temp_adc
uint16_t temp_adc
Definition: mf_ptu.c:61
gpio_setup_output
void gpio_setup_output(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.c:33
uint32_t
unsigned long uint32_t
Definition: types.h:18
adc.h
arch independent ADC (Analog to Digital Converter) API
adc_buf::sum
uint32_t sum
Definition: adc.h:54
GpsState::ned_vel
struct NedCoor_i ned_vel
speed NED in cm/s
Definition: gps.h:96
pressure_adc
uint16_t pressure_adc
Raw values.
Definition: mf_ptu.c:60
NedCoor_i::z
int32_t z
Down.
Definition: pprz_geodetic_int.h:71
sdlog_chibios.h
LlaCoor_i::lat
int32_t lat
in degrees*1e7
Definition: pprz_geodetic_int.h:60
uart.h
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
gps.h
Device independent GPS code (interface)
GpsState::fix
uint8_t fix
status of fix
Definition: gps.h:107
get_pwm_input_period_in_usec
uint32_t get_pwm_input_period_in_usec(uint32_t channel)
Definition: pwm_input.c:44
GpsState::week
uint16_t week
GPS week.
Definition: gps.h:108
int16_t
signed short int16_t
Definition: types.h:17
humid_period
uint32_t humid_period
Definition: mf_ptu.c:62
GpsState::gspeed
uint16_t gspeed
norm of 2d ground speed in cm/s
Definition: gps.h:97
PTU_TEMPERATURE_SCALE
#define PTU_TEMPERATURE_SCALE
Definition: mf_ptu.c:43
adc_buf::av_nb_sample
uint8_t av_nb_sample
Definition: adc.h:57
mf_ptu_periodic
void mf_ptu_periodic(void)
Definition: mf_ptu.c:95
mf_ptu.h
temp_buf
struct adc_buf temp_buf
Definition: mf_ptu.c:57
pwm_input.h
arch independent PWM input capture API
GpsState::course
int32_t course
GPS course over ground in rad*1e7, [0, 2*Pi]*1e7 (CW/north)
Definition: gps.h:99
gpio.h
GpsState::lla_pos
struct LlaCoor_i lla_pos
position in LLA (lat,lon: deg*1e7; alt: mm over ellipsoid)
Definition: gps.h:92
PTU_TEMPERATURE_OFFSET
#define PTU_TEMPERATURE_OFFSET
Definition: mf_ptu.c:40
GpsState::hmsl
int32_t hmsl
height above mean sea level (MSL) in mm
Definition: gps.h:94
mf_ptu_init
void mf_ptu_init(void)
Data acquisition module for Meteo France PTU board.
Definition: mf_ptu.c:76
gpio_set
static void gpio_set(ioportid_t port, uint16_t pin)
Set a gpio output to high level.
Definition: gpio_arch.h:98
gps
struct GpsState gps
global GPS state
Definition: gps.c:69
PTU_PRESSURE_OFFSET
#define PTU_PRESSURE_OFFSET
Default scale and offset send raw values if nothing defined in airframe file.
Definition: mf_ptu.c:34
adc_buf
Generic interface for all ADC hardware drivers, independent from microcontroller architecture.
Definition: adc.h:53