Paparazzi UAS  v5.15_devel-59-g8c145b7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
airspeed_otf.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Martin Mueller <martinmm@pfump.org>
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, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
29 #include <stdbool.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "mcu_periph/uart.h"
34 #include "pprzlink/messages.h"
36 #include <ctype.h>
37 
38 #include "met_module.h"
39 #include "airspeed_otf.h"
40 
41 
42 #define OTF_UNINIT 0x00
43 #define OTF_WAIT_START OTF_UNINIT
44 #define OTF_WAIT_COUNTER 0x01
45 #define OTF_WAIT_ANGLES 0x02
46 #define OTF_WAIT_ALTITUDE 0x03
47 #define OTF_WAIT_CHECKSUM 0x04
48 
49 #define OTF_START 0x0A
50 #define OTF_LIMITER ','
51 #define OTF_END 0x0D
52 
53 /* workaround for newlib */
54 void *_sbrk(int);
55 void *_sbrk(int a __attribute__((unused))) {return 0;}
56 
57 /* airspeed_otf_parse */
58 void airspeed_otf_parse(char c)
59 {
60  static uint8_t otf_status = OTF_UNINIT, otf_idx = 0, otf_crs_idx;
61  static char otf_inp[64];
62  static uint32_t counter;
63  static int16_t course[3];
64  static int32_t altitude;
65  static uint8_t checksum;
66 
67  switch (otf_status) {
68 
69  case OTF_WAIT_START:
70  if (c == OTF_START) {
71  otf_status++;
72  otf_idx = 0;
73  } else {
74  otf_status = OTF_UNINIT;
75  }
76  break;
77 
78  case OTF_WAIT_COUNTER:
79  if (isdigit((int)c)) {
80  if (otf_idx == 0) {
81 //FIXME otf_timestamp = getclock();
82  }
83  otf_inp[otf_idx++] = c;
84  } else {
85  if ((otf_idx == 5) && (c == OTF_LIMITER)) {
86  otf_inp[otf_idx] = 0;
87  counter = atoi(otf_inp);
88  otf_idx = 0;
89  otf_crs_idx = 0;
90  otf_status++;
91  } else {
92  otf_status = OTF_UNINIT;
93  }
94  }
95  break;
96 
97  case OTF_WAIT_ANGLES:
98  if (isdigit((int)c) || (c == '-') || (c == '.')) {
99  otf_inp[otf_idx++] = c;
100  } else {
101  if ((otf_idx > 1) && (otf_idx < 9) && (c == OTF_LIMITER)) {
102  otf_inp[otf_idx] = 0;
103  course[otf_crs_idx] = (int16_t)(100. * atof(otf_inp));
104  otf_idx = 0;
105  if (otf_crs_idx++ == 2) {
106  otf_status++;
107  }
108  } else {
109  otf_status = OTF_UNINIT;
110  }
111  }
112  break;
113 
114  case OTF_WAIT_ALTITUDE:
115  if (isdigit((int)c) || (c == '-') || (c == '.')) {
116  otf_inp[otf_idx++] = c;
117  } else {
118  if ((otf_idx > 1) && (otf_idx < 9) && (c == OTF_LIMITER)) {
119  otf_inp[otf_idx] = 0;
120  altitude = (int32_t)(100. * atof(otf_inp));
121  otf_idx = 0;
122  otf_status++;
123  } else {
124  otf_status = OTF_UNINIT;
125  }
126  }
127  break;
128 
129  case OTF_WAIT_CHECKSUM:
130  if (isxdigit((int)c)) {
131  otf_inp[otf_idx++] = c;
132  } else {
133  if ((otf_idx == 2) && (c == OTF_END)) {
134  otf_inp[otf_idx] = 0;
135  checksum = strtol(otf_inp, NULL, 16);
136  otf_idx = 0;
137  int32_t foo = 0;
138  DOWNLINK_SEND_AEROPROBE(DefaultChannel, DefaultDevice, &counter, &course[0], &course[1], &course[2],
139  &altitude, &foo, &foo, &checksum);
140  }
141  otf_status = OTF_UNINIT;
142  }
143  break;
144 
145  default:
146  otf_status = OTF_UNINIT;
147  break;
148  }
149 }
150 
152 {
153 }
154 
156 {
157  while (MetBuffer()) {
158  uint8_t ch = MetGetch();
159  airspeed_otf_parse(ch);
160  }
161 }
162 
164 {
165 }
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define OTF_START
Definition: airspeed_otf.c:49
#define MetGetch()
void airspeed_otf_periodic(void)
Definition: airspeed_otf.c:163
#define OTF_WAIT_START
Definition: airspeed_otf.c:43
#define OTF_END
Definition: airspeed_otf.c:51
static int32_t altitude
Definition: airspeed_uADC.c:58
Device independent serial meteo code.
#define OTF_UNINIT
Definition: airspeed_otf.c:42
static uint8_t checksum
Definition: airspeed_uADC.c:60
#define OTF_WAIT_ANGLES
Definition: airspeed_otf.c:45
#define OTF_LIMITER
Definition: airspeed_otf.c:50
unsigned long uint32_t
Definition: types.h:18
signed short int16_t
Definition: types.h:17
int32_t counter
uint16_t foo
Definition: main_demo5.c:59
signed long int32_t
Definition: types.h:19
#define OTF_WAIT_CHECKSUM
Definition: airspeed_otf.c:47
unsigned char uint8_t
Definition: types.h:14
static int16_t course[3]
Definition: airspeed_uADC.c:57
void * _sbrk(int)
Definition: airspeed_otf.c:55
void airspeed_otf_parse(char c)
Definition: airspeed_otf.c:58
#define OTF_WAIT_ALTITUDE
Definition: airspeed_otf.c:46
void airspeed_otf_init(void)
Definition: airspeed_otf.c:151
void airspeed_otf_event(void)
Definition: airspeed_otf.c:155
#define OTF_WAIT_COUNTER
Definition: airspeed_otf.c:44
#define MetBuffer()