-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
baro_board.c
103 lines (88 loc) · 2.7 KB
/
baro_board.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Copyright (C) 2017 Gautier Hattenberger <gautier.hattenberger@enac.fr>
*
* This file is part of Paparazzi.
*
* Paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* Paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file boards/swing/baro_board.c
* Paparazzi Swing Baro Sensor implementation.
* Sensor is LPS22HB (I2C) from ST but is accessed through sysfs interface
*/
#include "subsystems/sensors/baro.h"
#include "subsystems/abi.h"
#include "baro_board.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <linux/input.h>
static bool baro_swing_available;
static int32_t baro_swing_raw;
static pthread_mutex_t baro_swing_mutex = PTHREAD_MUTEX_INITIALIZER;
/**
* Check baro thread
* TODO something better ?
*/
static void *baro_read(void *data __attribute__((unused)))
{
struct input_event ev;
ssize_t n;
int fd_sonar = open("/dev/input/baro_event", O_RDONLY);
if (fd_sonar == -1) {
printf("Unable to open baro event to read pressure\n");
return NULL;
}
while (TRUE) {
/* Check new pressure */
n = read(fd_sonar, &ev, sizeof(ev));
if (n == sizeof(ev) && ev.type == EV_ABS && ev.code == ABS_PRESSURE) {
pthread_mutex_lock(&baro_swing_mutex);
baro_swing_available = true;
baro_swing_raw = ev.value;
pthread_mutex_unlock(&baro_swing_mutex);
}
// Wait 100ms
//usleep(100000);
}
return NULL;
}
void baro_init(void)
{
baro_swing_available = false;
baro_swing_raw = 0;
/* Start baro reading thread */
pthread_t baro_thread;
if (pthread_create(&baro_thread, NULL, baro_read, NULL) != 0) {
printf("[swing_board] Could not create baro reading thread!\n");
}
pthread_setname_np(baro_thread, "pprz_baro_thread");
}
void baro_periodic(void) {}
void baro_event(void)
{
pthread_mutex_lock(&baro_swing_mutex);
if (baro_swing_available) {
// From datasheet: raw_pressure / 4096 -> pressure in hPa
// send data in Pa
float pressure = 100.f * ((float)baro_swing_raw) / 4096.f;
AbiSendMsgBARO_ABS(BARO_BOARD_SENDER_ID, pressure);
baro_swing_available = false;
}
pthread_mutex_unlock(&baro_swing_mutex);
}