PX4 Firmware
PX4 Autopilot Software http://px4.io
hott_sensors.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2012, 2013 PX4 Development Team. All rights reserved.
4  * Author: Simon Wilks <sjwilks@gmail.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * 3. Neither the name PX4 nor the names of its contributors may be
17  * used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  ****************************************************************************/
34 
35 /**
36  * @file hott_sensors.c
37  * @author Simon Wilks <sjwilks@gmail.com>
38  *
39  * Graupner HoTT sensor driver implementation.
40  *
41  * Poll any sensors connected to the PX4 via the telemetry wire.
42  */
43 
44 #include <fcntl.h>
45 #include <px4_platform_common/px4_config.h>
46 #include <px4_platform_common/defines.h>
47 #include <px4_platform_common/tasks.h>
48 #include <poll.h>
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <sys/ioctl.h>
53 #include <unistd.h>
54 #include <systemlib/err.h>
55 
56 #include "../comms.h"
57 #include "../messages.h"
58 
59 #define DEFAULT_UART "/dev/ttyS0"; /**< USART1 */
60 
61 static int thread_should_exit = false; /**< Deamon exit flag */
62 static int thread_running = false; /**< Deamon status flag */
63 static int deamon_task; /**< Handle of deamon task / thread */
64 static const char daemon_name[] = "hott_sensors";
65 static const char commandline_usage[] = "usage: hott_sensors start|status|stop [-d <device>]";
66 
67 /**
68  * Deamon management function.
69  */
70 extern "C" __EXPORT int hott_sensors_main(int argc, char *argv[]);
71 
72 /**
73  * Mainloop of daemon.
74  */
75 int hott_sensors_thread_main(int argc, char *argv[]);
76 
77 static int recv_data(int uart, uint8_t *buffer, size_t *size, uint8_t *id);
78 static int send_poll(int uart, uint8_t *buffer, size_t size);
79 
80 int
81 send_poll(int uart, uint8_t *buffer, size_t size)
82 {
83  for (size_t i = 0; i < size; i++) {
84  write(uart, &buffer[i], sizeof(buffer[i]));
85 
86  /* Sleep before sending the next byte. */
88  }
89 
90  /* A hack the reads out what was written so the next read from the receiver doesn't get it. */
91  /* TODO: Fix this!! */
92  uint8_t dummy[size];
93  read(uart, &dummy, size);
94 
95  return OK;
96 }
97 
98 int
99 recv_data(int uart, uint8_t *buffer, size_t *size, uint8_t *id)
100 {
101  static const int timeout_ms = 1000;
102 
103  struct pollfd fds;
104  fds.fd = uart;
105  fds.events = POLLIN;
106 
107  // XXX should this poll be inside the while loop???
108  if (poll(&fds, 1, timeout_ms) > 0) {
109  int i = 0;
110  bool stop_byte_read = false;
111 
112  while (true) {
113  read(uart, &buffer[i], sizeof(buffer[i]));
114 
115  if (stop_byte_read) {
116  // XXX process checksum
117  *size = ++i;
118  return OK;
119  }
120 
121  // XXX can some other field not have the STOP BYTE value?
122  if (buffer[i] == STOP_BYTE) {
123  *id = buffer[1];
124  stop_byte_read = true;
125  }
126 
127  i++;
128  }
129  }
130 
131  return PX4_ERROR;
132 }
133 
134 int
135 hott_sensors_thread_main(int argc, char *argv[])
136 {
137  warnx("starting");
138 
139  thread_running = true;
140 
141  const char *device = DEFAULT_UART;
142 
143  /* read commandline arguments */
144  for (int i = 0; i < argc && argv[i]; i++) {
145  if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--device") == 0) { //device set
146  if (argc > i + 1) {
147  device = argv[i + 1];
148 
149  } else {
150  thread_running = false;
151  errx(1, "missing parameter to -d\n%s", commandline_usage);
152  }
153  }
154  }
155 
156  /* enable UART, writes potentially an empty buffer, but multiplexing is disabled */
157  const int uart = open_uart(device);
158 
159  if (uart < 0) {
160  errx(1, "Open fail, exiting.");
161  thread_running = false;
162  }
163 
165 
166  uint8_t buffer[MAX_MESSAGE_BUFFER_SIZE];
167  size_t size = 0;
168  uint8_t id = 0;
169 
170  while (!thread_should_exit) {
171  // Currently we only support a General Air Module sensor.
172  build_gam_request(&buffer[0], &size);
173  send_poll(uart, buffer, size);
174 
175  // The sensor will need a little time before it starts sending.
176  usleep(5000);
177 
178  recv_data(uart, &buffer[0], &size, &id);
179 
180  // Determine which module sent it and process accordingly.
181  if (id == GAM_SENSOR_ID) {
182  publish_gam_message(buffer);
183 
184  } else {
185  warnx("Unknown sensor ID: %d", id);
186  }
187  }
188 
189  warnx("exiting");
190  close(uart);
191  thread_running = false;
192 
193  return 0;
194 }
195 
196 /**
197  * Process command line arguments and start the daemon.
198  */
199 int
200 hott_sensors_main(int argc, char *argv[])
201 {
202  if (argc < 2) {
203  errx(1, "missing command\n%s", commandline_usage);
204  }
205 
206  if (!strcmp(argv[1], "start")) {
207 
208  if (thread_running) {
209  warnx("already running");
210  exit(0);
211  }
212 
213  thread_should_exit = false;
214  deamon_task = px4_task_spawn_cmd(daemon_name,
215  SCHED_DEFAULT,
216  SCHED_PRIORITY_DEFAULT,
217  1024,
219  (argv) ? (char *const *)&argv[2] : (char *const *)NULL);
220  exit(0);
221  }
222 
223  if (!strcmp(argv[1], "stop")) {
224  thread_should_exit = true;
225  exit(0);
226  }
227 
228  if (!strcmp(argv[1], "status")) {
229  if (thread_running) {
230  warnx("is running");
231 
232  } else {
233  warnx("not started");
234  }
235 
236  exit(0);
237  }
238 
239  errx(1, "unrecognized command\n%s", commandline_usage);
240 }
static const char daemon_name[]
static int thread_should_exit
Deamon exit flag.
static const char commandline_usage[]
static int timeout_ms
Definition: I2C.hpp:51
#define DEFAULT_UART
USART1.
#define MAX_MESSAGE_BUFFER_SIZE
Definition: messages.h:240
static int deamon_task
Handle of deamon task / thread.
Namespace encapsulating all device framework classes, functions and data.
Definition: CDev.cpp:47
#define STOP_BYTE
Definition: messages.h:64
int hott_sensors_thread_main(int argc, char *argv[])
Mainloop of daemon.
static void read(bootloader_app_shared_t *pshared)
void build_gam_request(uint8_t *buffer, size_t *size)
Definition: messages.cpp:90
#define GAM_SENSOR_ID
Definition: messages.h:128
void init_pub_messages(void)
Definition: messages.cpp:85
#define warnx(...)
Definition: err.h:95
Simple error/warning functions, heavily inspired by the BSD functions of the same names...
static int recv_data(int uart, uint8_t *buffer, size_t *size, uint8_t *id)
#define POST_WRITE_DELAY_IN_USECS
Definition: messages.h:59
int open_uart(const char *device)
Definition: comms.cpp:49
static void write(bootloader_app_shared_t *pshared)
#define errx(eval,...)
Definition: err.h:89
static int thread_running
Deamon status flag.
#define OK
Definition: uavcan_main.cpp:71
void publish_gam_message(const uint8_t *buffer)
Definition: messages.cpp:103
static int send_poll(int uart, uint8_t *buffer, size_t size)
__EXPORT int hott_sensors_main(int argc, char *argv[])
Deamon management function.