All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
gd_scan.c
1 
13 #include "urg_ctrl.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 
17 
18 static void urg_exit(urg_t *urg, const char *message)
19 {
20  printf("%s: %s\n", message, urg_error(urg));
21  urg_disconnect(urg);
22 
23 #ifdef MSC
24  getchar();
25 #endif
26  exit(1);
27 }
28 
29 
30 int main(int argc, char *argv[])
31 {
32  //const char device[] = "COM3"; /* Example for Windows */
33  const char device[] = "/dev/ttyACM0"; /* Example for Linux */
34 
35  int data_max;
36  long *data;
37  int timestamp;
38  int ret;
39  int n;
40  int i;
41 
42  /* Connection */
43  urg_t urg;
44  ret = urg_connect(&urg, device, 115200);
45  if (ret < 0) {
46  urg_exit(&urg, "urg_connect()");
47  }
48 
49  /* Reserve for reception data */
50  data_max = urg_dataMax(&urg);
51  data = (long*)malloc(sizeof(long) * data_max);
52  if (data == NULL) {
53  perror("malloc");
54  exit(1);
55  }
56 
57  /* Request for GD data */
59  if (ret < 0) {
60  urg_exit(&urg, "urg_requestData()");
61  }
62 
63  /* Reception */
64  n = urg_receiveData(&urg, data, data_max);
65  printf("# n = %d\n", n);
66  if (n < 0) {
67  urg_exit(&urg, "urg_receiveData()");
68  }
69 
70  /* Display */
71  timestamp = urg_recentTimestamp(&urg);
72  printf("# timestamp: %d\n", timestamp);
73  for (i = 0; i < n; ++i) {
74  /*Neglect the distance less than urg_minDistance() */
75  printf("%d %ld, ", i, data[i]);
76  }
77  printf("\n");
78 
79  urg_disconnect(&urg);
80  free(data);
81 
82  return 0;
83 }