PX4 Firmware
PX4 Autopilot Software http://px4.io
tfmini_parser.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file modified from sf0x_parser.cpp
36  * @author Lorenz Meier <lm@inf.ethz.ch>
37  * @author Chuong Nguyen <chnguye7@asu.edu>
38  * @author Ayush Gaud <ayush.gaud@gmail.com>
39  *
40  * Declarations of parser for the Benewake TFmini laser rangefinder series
41  */
42 
43 #include "tfmini_parser.h"
44 #include <string.h>
45 #include <stdlib.h>
46 
47 // #define TFMINI_DEBUG
48 
49 #ifdef TFMINI_DEBUG
50 #include <stdio.h>
51 
52 const char *parser_state[] = {
53  "0_UNSYNC",
54  "1_SYNC_1",
55  "2_SYNC_2",
56  "3_GOT_DIST_L",
57  "4_GOT_DIST_H",
58  "5_GOT_STRENGTH_L",
59  "6_GOT_STRENGTH_H",
60  "7_GOT_PRESERVED",
61  "8_GOT_QUALITY"
62  "9_GOT_CHECKSUM"
63 };
64 #endif
65 
66 int tfmini_parse(char c, char *parserbuf, unsigned *parserbuf_index, TFMINI_PARSE_STATE *state, float *dist)
67 {
68  int ret = -1;
69  //char *end;
70 
71  switch (*state) {
73  if (c == 'Y') {
75  parserbuf[*parserbuf_index] = c;
76  (*parserbuf_index)++;
77 
78  } else {
80  }
81 
82  break;
83 
85  if (c == 'Y') {
87  parserbuf[*parserbuf_index] = c;
88  (*parserbuf_index)++;
89  }
90 
91  break;
92 
94  if (c == 'Y') {
96  parserbuf[*parserbuf_index] = c;
97  (*parserbuf_index)++;
98 
99  } else {
101  *parserbuf_index = 0;
102  }
103 
104  break;
105 
108  parserbuf[*parserbuf_index] = c;
109  (*parserbuf_index)++;
110 
111  break;
112 
115  parserbuf[*parserbuf_index] = c;
116  (*parserbuf_index)++;
117 
118  break;
119 
122  parserbuf[*parserbuf_index] = c;
123  (*parserbuf_index)++;
124 
125  break;
126 
129  parserbuf[*parserbuf_index] = c;
130  (*parserbuf_index)++;
131 
132  break;
133 
136  parserbuf[*parserbuf_index] = c;
137  (*parserbuf_index)++;
138 
139  break;
140 
143  parserbuf[*parserbuf_index] = c;
144  (*parserbuf_index)++;
145 
146  break;
147 
149  // Find the checksum
150  unsigned char cksm = 0;
151 
152  for (int i = 0; i < 8; i++) {
153  cksm += parserbuf[i];
154  }
155 
156  if (c == cksm) {
157  parserbuf[*parserbuf_index] = '\0';
158  unsigned int t1 = parserbuf[2];
159  unsigned int t2 = parserbuf[3];
160  t2 <<= 8;
161  t2 += t1;
162  *dist = ((float)t2) / 100;
164  *parserbuf_index = 0;
165  ret = 0;
166 
167  } else {
169  *parserbuf_index = 0;
170  }
171 
172  break;
173  }
174 
175 #ifdef TFMINI_DEBUG
176  printf("state: TFMINI_PARSE_STATE%s\n", parser_state[*state]);
177 #endif
178 
179  return ret;
180 }
TFMINI_PARSE_STATE
Definition: tfmini_parser.h:59
static enum @74 state
int tfmini_parse(char c, char *parserbuf, unsigned *parserbuf_index, TFMINI_PARSE_STATE *state, float *dist)
static struct hrt_call t1
Definition: hrt_test.cpp:54
static crsf_parser_state_t parser_state
Definition: crsf.cpp:134
t2
Definition: calcH_YAW312.c:3