Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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  uint32_t now_ts = get_sys_time_usec();
98  float pressure = 100.f * ((float)baro_swing_raw) / 4096.f;
99  AbiSendMsgBARO_ABS(BARO_BOARD_SENDER_ID, now_ts, pressure);
100  baro_swing_available = false;
101  }
102  pthread_mutex_unlock(&baro_swing_mutex);
103 }
104 
BARO_BOARD_SENDER_ID
#define BARO_BOARD_SENDER_ID
default onboard baro
Definition: abi_sender_ids.h:33
abi.h
baro_board.h
uint32_t
unsigned long uint32_t
Definition: types.h:18
baro_init
void baro_init(void)
Definition: baro_board.c:76
baro_periodic
void baro_periodic(void)
Definition: baro_board.c:90
baro_swing_available
static bool baro_swing_available
Definition: baro_board.c:38
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
baro_swing_mutex
static pthread_mutex_t baro_swing_mutex
Definition: baro_board.c:40
ev
abi_event ev
Definition: rssi.c:45
baro.h
baro_event
void baro_event(void)
Definition: baro_board.c:72
baro_read
static void * baro_read(void *data)
Check baro thread TODO something better ?
Definition: baro_board.c:46
baro_swing_raw
static int32_t baro_swing_raw
Definition: baro_board.c:39
int32_t
signed long int32_t
Definition: types.h:19
TRUE
#define TRUE
Definition: std.h:4