-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurg-multi.c
354 lines (287 loc) · 10 KB
/
urg-multi.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// ===========================================================================
// Driver for Hokuyo URG-04LX-UG01 laser scanner.
// ===========================================================================
/*
Copyright 2017 Darren Faulke <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// ===========================================================================
#include "urg-multi.h"
#include <unistd.h> // UNIX standard function definitions.
#include <stdio.h> // Standard Input/Output definitions.
#include <stdlib.h>
#include <string.h> // String function definitions.
#include <stdint.h> // Standard type definitions.
#include <fcntl.h> // File control definitions.
#include <stdbool.h> // Boolean definitions.
#include <termios.h> // POSIX terminal control definitions.
// Commands ------------------------------------------------------------------
// ===========================================================================
// Clears serial port.
// ===========================================================================
void serial_flush(serial_t *serial)
{
tcdrain(serial->fd);
tcflush(serial->fd, TCIOFLUSH);
}
// ===========================================================================
// Sets serial baud rate.
// ===========================================================================
int serial_set_baud(serial_t *serial, long baud)
{
long baud_val;
switch (baud)
{
case 4800:
baud_val = B4800;
break;
case 9600:
baud_val = B9600;
break;
case 19200:
baud_val = B19200;
break;
case 38400:
baud_val = B38400;
break;
case 57600:
baud_val = B57600;
break;
case 115200:
baud_val = B115200;
break;
default:
return -1;
}
cfsetospeed(&serial->settings, baud_val);
cfsetispeed(&serial->settings, baud_val);
tcsetattr(serial->fd, TCSADRAIN, &serial->settings);
serial_flush(serial);
return (0);
}
// ===========================================================================
// Initialises serial port.
// ===========================================================================
int serial_open(serial_t *serial, const char *device, long baud)
{
int flags = 0;
int ret = 0;
serial->fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (serial->fd < 0)
{
perror("USB open");
return (-1);
}
flags = fcntl(serial->fd, F_GETFL, 0);
fcntl(serial->fd, F_SETFL, flags & ~O_NONBLOCK);
// Get current port options.
tcgetattr(serial->fd, &serial->settings);
// Set port options (lifted from the urg library source code).
serial->settings.c_iflag = 0;
serial->settings.c_oflag = 0;
serial->settings.c_cflag &= ~(CSIZE | PARENB | CSTOPB);
serial->settings.c_cflag |= CS8 | CREAD | CLOCAL;
serial->settings.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN);
serial->settings.c_cc[VMIN] = 0;
serial->settings.c_cc[VTIME] = 0;
ret = serial_set_baud(serial, baud);
return ret;
}
// ===========================================================================
// Closes serial port.
// ===========================================================================
int serial_close(serial_t *serial)
{
return close(serial->fd);
}
// ===========================================================================
// Writes command to port.
// ===========================================================================
int write_command(serial_t *serial, const char *data, int size)
{
return write(serial->fd, data, size);
}
// ===========================================================================
// Returns data sum.
// ===========================================================================
char get_data_sum(char *data)
{
uint8_t i;
uint16_t val;
uint8_t sum;
uint8_t len;
val = 0;
len = strlen(data);
for (i = 0; i < len; i++)
{
val += data[i];
}
sum = (val & 0x3f) + 0x30;
return (sum);
}
// ===========================================================================
// Returns data block from sensor.
// ===========================================================================
int get_data(serial_t *serial, char data[DATA_BLOCK_LEN])
{
char c;
int i;
i = 0;
while (read(serial->fd, &c, 1) > 0 && (c != STRING_LF))
{
data[i++] = c;
}
return (i-1);
}
// ===========================================================================
// Returns version information in version_t.
// ===========================================================================
int get_version(sensor_t *sensor, char string[16])
{
char cmd[DATA_CMD_LEN + DATA_STRING_LEN];
int err;
char cmd_ret[64] = "";
char str_ret[64] = "";
strcpy(cmd, CMD_GET_VERSION);
// strcat(buf, string); // Don't know why this kills the return data!
strcat(cmd, LF);
printf("Command = %s\n", CMD_GET_VERSION);
serial_flush(&sensor->serial);
err = write(sensor->serial.fd, cmd, strlen(cmd));
if (err < 0)
{
printf("Error writing command.\n");
perror("Write to port");
return (err);
}
usleep( 100000 ); // Definitely needs this!
/*
It would be better to have a routine that waits
for the port to be ready.
*/
err = get_data(&sensor->serial, cmd_ret); // Command echo.
if (err < 0 ) return (err);
err = get_data(&sensor->serial, str_ret); // String echo.
if (err < 0 ) return (err);
err = get_data(&sensor->serial, sensor->version.vendor);
if (err < 0 ) return (err);
printf("\tVendor = %s.\n", sensor->version.vendor);
err = get_data(&sensor->serial, sensor->version.product);
if (err < 0 ) return (err);
printf("\tProduct = %s.\n", sensor->version.product);
err = get_data(&sensor->serial, sensor->version.firmware);
if (err < 0 ) return (err);
printf("\tFirmware = %s.\n", sensor->version.firmware);
err = get_data(&sensor->serial, sensor->version.protocol);
if (err < 0 ) return (err);
printf("\tProtocol = %s.\n", sensor->version.protocol);
err = get_data(&sensor->serial, sensor->version.serial);
if (err < 0 ) return (err);
printf("\tSerial = %s.\n", sensor->version.serial);
printf("\n");
return 0;
}
// ===========================================================================
// Initialises sensor instance.
// ===========================================================================
int sensor_init(void)
//int sensor_init(sensor_t *sensor)
{
/*
Use this to allocate resources to each sensor instance.
For each instance, need to open unique tty port and assign id.
Currently only supports 1.
*/
static bool init = false; // 1st call.
static uint8_t index = 0; // Number of sensors initialised.
sensor_t *sensor_temp; // Copy of sensor instance.
int id = -1;
uint8_t i;
int err;
// Set all instances of sensor to NULL on first call.
if (!init)
for (i = 0; i < SENSORS_MAX; i++)
sensor[i] = NULL;
// Get next available ID.
for (i = 0; i < SENSORS_MAX; i++)
{
if (sensor[i] == NULL) // Not already initialised.
{
id = i;
i = SENSORS_MAX; // Break out of loop.
}
}
if (id < 0) return -1; // Didn't initialise!
// Allocate memory for sensor struct.
sensor_temp = malloc(sizeof(sensor));
// Return if unable to allocate memory.
if (sensor_temp == NULL) return -1;
const char *device = "/dev/ttyACM0"; // Serial port (assumed same for all).
long baud = BIT_RATE_0; // Initial baud setting.
// Not needed because not running anything on 1st init only!
if (!init)
{
init = true;
}
// Need to open next available USB /edv/ttyACMx port here.
// Create the instance.
sensor_temp->id = id;
// Allocate a port - not sure if this stays the same for multiple sensors.
err = serial_open(&sensor_temp->serial, device, baud);
if (err < 0)
{
printf("Error opening serial port on %s.\n", device);
return -1;
}
err = get_version(sensor_temp, "Jaguar");
if (err < 0)
{
printf("Error getting version info for sensor %d.\n", id);
return -1;
}
sensor[index] = sensor_temp;
index++;
return 0;
}
// ===========================================================================
// Main routine.
// ===========================================================================
int main(void)
{
int err;
uint8_t num_sensors = 1;
uint8_t i;
// Array of sensors.
// sensor_t *sensor[SENSORS_MAX];
for (i = 0; i < num_sensors; i++)
{
err = sensor_init();
if (err < 0)
{
printf("Couldn't initialise sensor.\n");
return -1;
}
}
// Print out information for each sensor.
for (i = 0; i < num_sensors; i++)
{
printf("Sensor ID = %d.\n\n", sensor[i]->id);
printf("\tSerial ID = %d.\n", sensor[i]->serial.fd);
printf("\tVendor = %s.\n", sensor[i]->version.vendor);
printf("\tProduct = %s.\n", sensor[i]->version.vendor);
printf("\tFirmware = %s.\n", sensor[i]->version.vendor);
printf("\tProtocol = %s.\n", sensor[i]->version.vendor);
printf("\tSerial = %s.\n", sensor[i]->version.vendor);
printf("\n");
}
return (0);
}