-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathurg.c
265 lines (217 loc) · 7.54 KB
/
urg.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
// ===========================================================================
// 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.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;
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, sensor->version.command);
if (err < 0 ) return (err);
err = get_data(&sensor->serial, sensor->version.string);
if (err < 0 ) return (err);
err = get_data(&sensor->serial, sensor->version.vendor);
if (err < 0 ) return (err);
err = get_data(&sensor->serial, sensor->version.product);
if (err < 0 ) return (err);
err = get_data(&sensor->serial, sensor->version.firmware);
if (err < 0 ) return (err);
err = get_data(&sensor->serial, sensor->version.protocol);
if (err < 0 ) return (err);
err = get_data(&sensor->serial, sensor->version.serial);
return (err);
}
// ===========================================================================
// Main routine.
// ===========================================================================
int main(void)
{
int err;
sensor_t sensor;
const char *device = "/dev/ttyACM0";
long baud = 115200;
char data[64];
err = serial_open(&sensor.serial, device, baud);
if (err < 0)
{
printf("Error initialising port.\n");
}
err = get_version(&sensor, "Jaguar");
printf("Get version = %d\n", err);
if (err >= 0)
{
printf("Sensor Information.\n\n");
printf("\tVendor : %s\n", sensor.version.vendor);
printf("\tProduct : %s\n", sensor.version.product);
printf("\tFirmware : %s\n", sensor.version.firmware);
printf("\tProtocol : %s\n", sensor.version.protocol);
printf("\tSerial : %s\n", sensor.version.serial);
printf("\n");
}
err = serial_close(&sensor.serial);
if (err < 0)
{
printf("Error closing port.\n");
}
return (0);
}