Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
board.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 The Paparazzi Team
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 "boards/swing.h"
29 #include "mcu.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 #include "subsystems/electrical.h"
38 
42 static void *bat_read(void *data __attribute__((unused)))
43 {
44  FILE *fp;
45  char path[16];
46 
47  while (TRUE) {
48  /* Open the command for reading. */
49  fp = popen("cat /sys/devices/platform/p6-spi.2/spi2.0/vbat", "r");
50  if (fp == NULL) {
51  printf("Failed to read battery\n");
52  } else {
53  /* Read the output a line at a time - output it. */
54  while (fgets(path, sizeof(path) - 1, fp) != NULL) {
55  int raw_bat = atoi(path);
56  // from /bin/mcu_vbat.sh: MILLIVOLTS_VALUE=$(( ($RAW_VALUE * 4250) / 1023 ))
57  electrical.vsupply = (float)((raw_bat * 4250) / 1023) / 1000.f;
58  }
59  /* close */
60  pclose(fp);
61  }
62 
63  // Wait 100ms
64  // reading is done at 10Hz like the electrical_periodic from rotorcraft main program
65  usleep(100000);
66  }
67 
68  return NULL;
69 }
70 
74 static void *button_read(void *data __attribute__((unused)))
75 {
76  struct input_event ev;
77  ssize_t n;
78 
79  /* Open power button event sysfs file */
80  int fd_button = open("/dev/input/pm_mcu_event", O_RDONLY);
81  if (fd_button == -1) {
82  printf("Unable to open mcu_event to read power button state\n");
83  return NULL;
84  }
85 
86  while (TRUE) {
87  /* Check power button (read is blocking) */
88  n = read(fd_button, &ev, sizeof(ev));
89  if (n == sizeof(ev) && ev.type == EV_KEY && ev.code == KEY_POWER && ev.value > 0) {
90  printf("Stopping Paparazzi from power button and rebooting\n");
91  usleep(1000);
92  int ret __attribute__((unused)) = system("reboot.sh");
93  exit(0);
94  }
95  }
96 
97  return NULL;
98 }
99 
100 void board_init(void)
101 {
102  /*
103  * Stop original processes using pstop/ptart commands
104  * Don't kill to avoid automatic restart
105  *
106  */
107  int ret __attribute__((unused));
108  ret = system("pstop delosd");
109  ret = system("pstop dragon-prog");
110  usleep(50000); /* Give 50ms time to end on a busy system */
111 
112  /* Start bat reading thread */
113  pthread_t bat_thread;
114  if (pthread_create(&bat_thread, NULL, bat_read, NULL) != 0) {
115  printf("[swing_board] Could not create battery reading thread!\n");
116  }
117  pthread_setname_np(bat_thread, "pprz_bat_thread");
118 
119  /* Start button reading thread */
120  pthread_t button_thread;
121  if (pthread_create(&button_thread, NULL, button_read, NULL) != 0) {
122  printf("[swing_board] Could not create button reading thread!\n");
123  }
124  pthread_setname_np(button_thread, "pprz_button_thread");
125 
126 }
127 
128 void board_init2(void)
129 {
130 }
131 
board_init
void board_init(void)
Optional board init function called at the start of mcu_init().
Definition: board.c:130
electrical.h
Electrical::vsupply
float vsupply
supply voltage in V
Definition: electrical.h:45
button_read
static void * button_read(void *data)
Check button thread.
Definition: board.c:74
mcu.h
Arch independent mcu ( Micro Controller Unit ) utilities.
ev
abi_event ev
Definition: rssi.c:45
f
uint16_t f
Camera baseline, in meters (i.e. horizontal distance between the two cameras of the stereo setup)
Definition: wedgebug.c:204
swing.h
board_init2
void board_init2(void)
Optional board init function called at the end of mcu_init().
Definition: board.c:92
electrical
struct Electrical electrical
Definition: electrical.c:66
bat_read
static void * bat_read(void *data)
Battery reading thread.
Definition: board.c:42
TRUE
#define TRUE
Definition: std.h:4