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
|
/**
* The deamon app only briefly exists to start
* the background job. The stack size assigned in the
* Makefile does only apply to this management task.
*
* The actual stack size should be set in the call
* to task_create().
* http://nuttx.org/Documentation/NuttxUserGuide.html#taskcreate
*/
#include <nuttx/config.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>
#include <float.h>
#include <nuttx/sched.h>
#include <sys/prctl.h>
#include <termios.h>
#include <errno.h>
#include <limits.h>
#include <math.h>
#include <uORB/uORB.h>
#include <drivers/drv_hrt.h>
#include <drivers/drv_range_finder.h>
#include <systemlib/perf_counter.h>
#include <systemlib/systemlib.h>
#include <systemlib/err.h>
#include <poll.h>
/**
* Minimal and maximal distances are get from
* http://www.maxbotix.com/documents/XL-MaxSonar-EZ_Datasheet.pdf
*/
#define MINIMAL_DISTANCE 20
#define MAXIMAL_DISTANCE 700
/**
* Experimentally found scale value
*/
#define DISTANCE_SCALE 0.01022f
__EXPORT int mb1230serial_main(int argc, char *argv[]);
int mb1230serial_thread_main(int argc, char *argv[]);
static bool thread_should_run = true; /**< Daemon running flag */
static bool thread_running = false; /**< Daemon status flag */
static int daemon_task; /**< Handle of daemon task / thread */
/**
* Print the correct usage.
*/
void
usage(const char *reason)
{
if (reason)
fprintf(stderr, "%s\n", reason);
fprintf(stderr, "usage: mb1230serial {start device_path | stop}\n\n");
exit(1);
}
int mb1230serial_main(int argc, char *argv[])
{
if (argc < 1)
usage("missing command");
if (!strcmp(argv[1], "start"))
{
if (thread_running)
{
warnx("already running\n");
/* this is not an error */
exit(0);
}
if (argc < 3)
{
usage("missing device path");
}
thread_should_run = true;
daemon_task = task_spawn_cmd("mb1230serial",
SCHED_DEFAULT,
SCHED_PRIORITY_MAX - 5,
2000,
mb1230serial_thread_main,
(argv) ? (const char **)&argv[2] : (const char **)NULL);
exit(0);
}
if (!strcmp(argv[1], "stop"))
{
thread_should_run = false;
exit(0);
}
usage("unrecognized command");
exit(1);
}
void cfsetraw(struct termios *termios_s)
{
termios_s->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
termios_s->c_oflag &= ~OPOST;
termios_s->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
termios_s->c_cflag &= ~(CSIZE | PARENB | CSTOPB);
termios_s->c_cflag |= CS8 | CLOCAL;
}
void setup_serial(int fd)
{
struct termios options;
int termios_state;
if ((termios_state = tcgetattr(fd, &options)) < 0)
{
close(fd);
err(-1, "ERR GET CONF: %d\n", termios_state);
}
cfsetraw(&options);
cfsetspeed(&options, B9600);
if ((termios_state = tcsetattr(fd, TCSANOW, &options)) < 0)
{
close(fd);
err(-1, "ERR SET CONF: %d\n", termios_state);
}
}
int mb1230serial_thread_main(int argc, char *argv[])
{
warnx("opening port %s", argv[1]);
int serial_fd = open(argv[1], O_RDONLY | O_NOCTTY);
if (serial_fd < 0)
{
err(1, "failed to open port: %s", argv[1]);
}
setup_serial(serial_fd);
char R = 0;
char raw_data[3];
int nread = 0;
int distance;
struct range_finder_report data = {
.error_count = 0,
.type = RANGE_FINDER_TYPE_ULTRASONIC,
.minimum_distance = MINIMAL_DISTANCE,
.maximum_distance = MAXIMAL_DISTANCE,
.valid = 1
};
orb_advert_t range_finder_pub = -1;
thread_running = true;
while (thread_should_run)
{
/*This runs at the rate of the sensors */
struct pollfd fds[] = {
{ .fd = serial_fd, .events = POLLIN }
};
/* wait for a sensor update, check for exit condition */
int ret = poll(fds, sizeof(fds) / sizeof(fds[0]), 1000);
if (ret < 0)
{
/* poll error, ignore */
}
else if (ret == 0)
{
/* no return value, ignore */
}
else
{
if (fds[0].revents & POLLIN)
{
if (R != 'R')
{
read(serial_fd, &R, sizeof(R));
}
else
{
nread += read(serial_fd, &raw_data[nread], sizeof(raw_data) - nread);
if (nread == 3)
{
nread = 0;
R = 0;
distance =
(raw_data[0] - '0') * 100 +
(raw_data[1] - '0') * 10 +
(raw_data[2] - '0');
if (distance >= MINIMAL_DISTANCE && distance <= MAXIMAL_DISTANCE) {
data.timestamp = hrt_absolute_time();
data.distance = (float)distance * DISTANCE_SCALE;
//printf("%llu: %.3f\n", data.timestamp, data.distance);
if (range_finder_pub > 0) {
orb_publish(ORB_ID(sensor_range_finder), range_finder_pub, &data);
} else {
range_finder_pub = orb_advertise(ORB_ID(sensor_range_finder), &data);
}
}
}
}
}
}
}
warnx("exiting");
thread_running = false;
fflush(stdout);
return 0;
}
|