TFMINI.cpp
7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/****************************************************************************
*
* Copyright (c) 2017-2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include "TFMINI.hpp"
#include <lib/drivers/device/Device.hpp>
#include <fcntl.h>
TFMINI::TFMINI(const char *port, uint8_t rotation) :
ScheduledWorkItem(MODULE_NAME, px4::serial_port_to_wq(port)),
_px4_rangefinder(0, rotation)
{
// store port name
strncpy(_port, port, sizeof(_port) - 1);
// enforce null termination
_port[sizeof(_port) - 1] = '\0';
device::Device::DeviceId device_id;
device_id.devid_s.devtype = DRV_DIST_DEVTYPE_TFMINI;
device_id.devid_s.bus_type = device::Device::DeviceBusType_SERIAL;
uint8_t bus_num = atoi(&_port[strlen(_port) - 1]); // Assuming '/dev/ttySx'
if (bus_num < 10) {
device_id.devid_s.bus = bus_num;
}
_px4_rangefinder.set_device_id(device_id.devid);
_px4_rangefinder.set_rangefinder_type(distance_sensor_s::MAV_DISTANCE_SENSOR_LASER);
}
TFMINI::~TFMINI()
{
// make sure we are truly inactive
stop();
perf_free(_sample_perf);
perf_free(_comms_errors);
}
int
TFMINI::init()
{
int32_t hw_model = 1; // only one model so far...
switch (hw_model) {
case 1: // TFMINI (12m, 100 Hz)
// Note:
// Sensor specification shows 0.3m as minimum, but in practice
// 0.3 is too close to minimum so chattering of invalid sensor decision
// is happening sometimes. this cause EKF to believe inconsistent range readings.
// So we set 0.4 as valid minimum.
_px4_rangefinder.set_min_distance(0.4f);
_px4_rangefinder.set_max_distance(12.0f);
_px4_rangefinder.set_fov(math::radians(1.15f));
break;
default:
PX4_ERR("invalid HW model %d.", hw_model);
return -1;
}
// status
int ret = 0;
do { // create a scope to handle exit conditions using break
// open fd
_fd = ::open(_port, O_RDWR | O_NOCTTY);
if (_fd < 0) {
PX4_ERR("Error opening fd");
return -1;
}
// baudrate 115200, 8 bits, no parity, 1 stop bit
unsigned speed = B115200;
termios uart_config{};
int termios_state{};
tcgetattr(_fd, &uart_config);
// clear ONLCR flag (which appends a CR for every LF)
uart_config.c_oflag &= ~ONLCR;
// set baud rate
if ((termios_state = cfsetispeed(&uart_config, speed)) < 0) {
PX4_ERR("CFG: %d ISPD", termios_state);
ret = -1;
break;
}
if ((termios_state = cfsetospeed(&uart_config, speed)) < 0) {
PX4_ERR("CFG: %d OSPD\n", termios_state);
ret = -1;
break;
}
if ((termios_state = tcsetattr(_fd, TCSANOW, &uart_config)) < 0) {
PX4_ERR("baud %d ATTR", termios_state);
ret = -1;
break;
}
uart_config.c_cflag |= (CLOCAL | CREAD); // ignore modem controls
uart_config.c_cflag &= ~CSIZE;
uart_config.c_cflag |= CS8; // 8-bit characters
uart_config.c_cflag &= ~PARENB; // no parity bit
uart_config.c_cflag &= ~CSTOPB; // only need 1 stop bit
uart_config.c_cflag &= ~CRTSCTS; // no hardware flowcontrol
// setup for non-canonical mode
uart_config.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
uart_config.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
uart_config.c_oflag &= ~OPOST;
// fetch bytes as they become available
uart_config.c_cc[VMIN] = 1;
uart_config.c_cc[VTIME] = 1;
if (_fd < 0) {
PX4_ERR("FAIL: laser fd");
ret = -1;
break;
}
} while (0);
// close the fd
::close(_fd);
_fd = -1;
if (ret == PX4_OK) {
start();
}
return ret;
}
int
TFMINI::collect()
{
perf_begin(_sample_perf);
// clear buffer if last read was too long ago
int64_t read_elapsed = hrt_elapsed_time(&_last_read);
// the buffer for read chars is buflen minus null termination
char readbuf[sizeof(_linebuf)] {};
unsigned readlen = sizeof(readbuf) - 1;
int ret = 0;
float distance_m = -1.0f;
// Check the number of bytes available in the buffer
int bytes_available = 0;
::ioctl(_fd, FIONREAD, (unsigned long)&bytes_available);
if (!bytes_available) {
perf_end(_sample_perf);
return 0;
}
// parse entire buffer
const hrt_abstime timestamp_sample = hrt_absolute_time();
do {
// read from the sensor (uart buffer)
ret = ::read(_fd, &readbuf[0], readlen);
if (ret < 0) {
PX4_ERR("read err: %d", ret);
perf_count(_comms_errors);
perf_end(_sample_perf);
// only throw an error if we time out
if (read_elapsed > (kCONVERSIONINTERVAL * 2)) {
/* flush anything in RX buffer */
tcflush(_fd, TCIFLUSH);
return ret;
} else {
return -EAGAIN;
}
}
_last_read = hrt_absolute_time();
// parse buffer
for (int i = 0; i < ret; i++) {
tfmini_parse(readbuf[i], _linebuf, &_linebuf_index, &_parse_state, &distance_m);
}
// bytes left to parse
bytes_available -= ret;
} while (bytes_available > 0);
// no valid measurement after parsing buffer
if (distance_m < 0.0f) {
perf_end(_sample_perf);
return -EAGAIN;
}
// publish most recent valid measurement from buffer
_px4_rangefinder.update(timestamp_sample, distance_m);
perf_end(_sample_perf);
return PX4_OK;
}
void
TFMINI::start()
{
// schedule a cycle to start things (the sensor sends at 100Hz, but we run a bit faster to avoid missing data)
ScheduleOnInterval(7_ms);
}
void
TFMINI::stop()
{
ScheduleClear();
}
void
TFMINI::Run()
{
// fds initialized?
if (_fd < 0) {
// open fd
_fd = ::open(_port, O_RDWR | O_NOCTTY);
}
// perform collection
if (collect() == -EAGAIN) {
// reschedule to grab the missing bits, time to transmit 9 bytes @ 115200 bps
ScheduleClear();
ScheduleOnInterval(7_ms, 87 * 9);
return;
}
}
void
TFMINI::print_info()
{
printf("Using port '%s'\n", _port);
perf_print_counter(_sample_perf);
perf_print_counter(_comms_errors);
}