Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
baro_board.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
28 #include "subsystems/abi.h"
29 #include "baro_board.h"
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <pthread.h>
36 #include <linux/input.h>
37 
40 static pthread_mutex_t baro_swing_mutex = PTHREAD_MUTEX_INITIALIZER;
41 
46 static void *baro_read(void *data __attribute__((unused)))
47 {
48  struct input_event ev;
49  ssize_t n;
50 
51  int fd_sonar = open("/dev/input/baro_event", O_RDONLY);
52  if (fd_sonar == -1) {
53  printf("Unable to open baro event to read pressure\n");
54  return NULL;
55  }
56 
57  while (TRUE) {
58  /* Check new pressure */
59  n = read(fd_sonar, &ev, sizeof(ev));
60  if (n == sizeof(ev) && ev.type == EV_ABS && ev.code == ABS_PRESSURE) {
61  pthread_mutex_lock(&baro_swing_mutex);
62  baro_swing_available = true;
63  baro_swing_raw = ev.value;
64  pthread_mutex_unlock(&baro_swing_mutex);
65  }
66 
67  // Wait 100ms
68  //usleep(100000);
69  }
70 
71  return NULL;
72 }
73 
74 void baro_init(void)
75 {
76  baro_swing_available = false;
77  baro_swing_raw = 0;
78 
79  /* Start baro reading thread */
80  pthread_t baro_thread;
81  if (pthread_create(&baro_thread, NULL, baro_read, NULL) != 0) {
82  printf("[swing_board] Could not create baro reading thread!\n");
83  }
84  pthread_setname_np(baro_thread, "pprz_baro_thread");
85 
86 }
87 
88 void baro_periodic(void) {}
89 
90 
91 void baro_event(void)
92 {
93  pthread_mutex_lock(&baro_swing_mutex);
95  // From datasheet: raw_pressure / 4096 -> pressure in hPa
96  // send data in Pa
97  float pressure = 100.f * ((float)baro_swing_raw) / 4096.f;
98  AbiSendMsgBARO_ABS(BARO_BOARD_SENDER_ID, pressure);
99  baro_swing_available = false;
100  }
101  pthread_mutex_unlock(&baro_swing_mutex);
102 }
103 
Common barometric sensor implementation.
Main include for ABI (AirBorneInterface).
void baro_init(void)
Definition: baro_board.c:76
void baro_periodic(void)
Definition: baro_board.c:90
static bool baro_swing_available
Definition: baro_board.c:38
#define TRUE
Definition: std.h:4
void baro_event(void)
Definition: baro_board.c:72
static void * baro_read(void *data)
Check baro thread TODO something better ?
Definition: baro_board.c:46
#define BARO_BOARD_SENDER_ID
default onboard baro
signed long int32_t
Definition: types.h:19
static int32_t baro_swing_raw
Definition: baro_board.c:39
static pthread_mutex_t baro_swing_mutex
Definition: baro_board.c:40
Paparazzi Swing Baro Sensor implementation.