Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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 <ctype.h>
34 #include "mcu_periph/uart.h"
35 #include "messages.h"
37 
38 #include "met_module.h"
39 #include "airspeed_otf.h"
40 
41 #ifndef DOWNLINK_DEVICE
42 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
43 #endif
44 
45 #define OTF_UNINIT 0x00
46 #define OTF_WAIT_START OTF_UNINIT
47 #define OTF_WAIT_COUNTER 0x01
48 #define OTF_WAIT_ANGLES 0x02
49 #define OTF_WAIT_ALTITUDE 0x03
50 #define OTF_WAIT_CHECKSUM 0x04
51 
52 #define OTF_START 0x0A
53 #define OTF_LIMITER ','
54 #define OTF_END 0x0D
55 
56 /* workaround for newlib */
57 void* _sbrk(int);
58 void* _sbrk(int a) {return 0;}
59 
60 /* airspeed_otf_parse */
62 {
63  static unsigned char otf_status = OTF_UNINIT, otf_idx = 0, otf_crs_idx;
64  static char otf_inp[64];
65  static unsigned int counter;
66  static short course[3];
67  static unsigned int altitude;
68  static unsigned char checksum;
69 
70  switch (otf_status) {
71 
72  case OTF_WAIT_START:
73  if (c == OTF_START) {
74  otf_status++;
75  otf_idx = 0;
76  } else {
77  otf_status = OTF_UNINIT;
78  }
79  break;
80 
81  case OTF_WAIT_COUNTER:
82  if (isdigit((int)c)) {
83  if (otf_idx == 0) {
84 //FIXME otf_timestamp = getclock();
85  }
86  otf_inp[otf_idx++] = c;
87  } else {
88  if ((otf_idx == 5) && (c == OTF_LIMITER)) {
89  otf_inp[otf_idx] = 0;
90  counter = atoi(otf_inp);
91  otf_idx = 0;
92  otf_crs_idx = 0;
93  otf_status++;
94  } else {
95  otf_status = OTF_UNINIT;
96  }
97  }
98  break;
99 
100  case OTF_WAIT_ANGLES:
101  if (isdigit((int)c) || (c == '-') || (c == '.')) {
102  otf_inp[otf_idx++] = c;
103  } else {
104  if ((otf_idx > 1) && (otf_idx < 9) && (c == OTF_LIMITER)) {
105  otf_inp[otf_idx] = 0;
106  course[otf_crs_idx] = (int16_t) (100. * atof(otf_inp));
107  otf_idx = 0;
108  if (otf_crs_idx++ == 2) {
109  otf_status++;
110  }
111  } else {
112  otf_status = OTF_UNINIT;
113  }
114  }
115  break;
116 
117  case OTF_WAIT_ALTITUDE:
118  if (isdigit((int)c) || (c == '-') || (c == '.')) {
119  otf_inp[otf_idx++] = c;
120  } else {
121  if ((otf_idx > 1) && (otf_idx < 9) && (c == OTF_LIMITER)) {
122  otf_inp[otf_idx] = 0;
123  altitude = (int32_t) (100. * atof(otf_inp));
124  otf_idx = 0;
125  otf_status++;
126  } else {
127  otf_status = OTF_UNINIT;
128  }
129  }
130  break;
131 
132  case OTF_WAIT_CHECKSUM:
133  if (isxdigit((int)c)) {
134  otf_inp[otf_idx++] = c;
135  } else {
136  if ((otf_idx == 2) && (c == OTF_END)) {
137  otf_inp[otf_idx] = 0;
138  checksum = strtol(otf_inp, NULL, 16);
139  otf_idx = 0;
140  DOWNLINK_SEND_FLOW_AP_OTF(DefaultChannel, DefaultDevice, &counter, &course[0], &course[1], &course[2], &altitude, &checksum);
141  }
142  otf_status = OTF_UNINIT;
143  }
144  break;
145 
146  default:
147  otf_status = OTF_UNINIT;
148  break;
149  }
150 }
151 
152 void airspeed_otf_init(void) {
153 }
154 
155 void airspeed_otf_event(void) {
156  while (MetLink(ChAvailable())) {
157  uint8_t ch = MetLink(Getch());
158  airspeed_otf_parse(ch);
159  }
160 }
161 
163 }
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define OTF_START
Definition: airspeed_otf.c:52
void airspeed_otf_periodic(void)
Definition: airspeed_otf.c:162
#define OTF_WAIT_START
Definition: airspeed_otf.c:46
#define OTF_END
Definition: airspeed_otf.c:54
void * _sbrk(int)
Definition: airspeed_otf.c:58
Device independent serial meteo code.
#define OTF_UNINIT
Definition: airspeed_otf.c:45
#define OTF_WAIT_ANGLES
Definition: airspeed_otf.c:48
#define OTF_LIMITER
Definition: airspeed_otf.c:53
signed short int16_t
Definition: types.h:17
signed long int32_t
Definition: types.h:19
#define OTF_WAIT_CHECKSUM
Definition: airspeed_otf.c:50
unsigned char uint8_t
Definition: types.h:14
static struct point c
Definition: discsurvey.c:39
void airspeed_otf_parse(char c)
Definition: airspeed_otf.c:61
#define OTF_WAIT_ALTITUDE
Definition: airspeed_otf.c:49
void airspeed_otf_init(void)
Definition: airspeed_otf.c:152
void airspeed_otf_event(void)
Definition: airspeed_otf.c:155
#define OTF_WAIT_COUNTER
Definition: airspeed_otf.c:47
#define MetLink(_x)
Definition: met_module.h:42