All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
md_scan.c
1 
13 #include "urg_ctrl.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <assert.h>
17 
18 
19 static void urg_exit(urg_t *urg, const char *message)
20 {
21  printf("%s: %s\n", message, urg_error(urg));
22  urg_disconnect(urg);
23 
24 #ifdef MSC
25  getchar();
26 #endif
27  exit(1);
28 }
29 
30 
32 int main(int argc, char *argv[])
33 {
34  enum {
35  CaptureTimes = 99, /* Data acquisition frequency */
36  };
37 
38  //const char device[] = "COM3"; /* Example for Windows */
39  const char device[] = "/dev/ttyACM0"; /* Example for Linux */
40 
41  int data_max;
42  long* data;
43  int timestamp;
44  int remain_times;
45  int scan_msec;
46  urg_parameter_t parameter;
47  int ret;
48  int n;
49  int i;
50 
51  /* Connection */
52  urg_t urg;
53  ret = urg_connect(&urg, device, 115200);
54  if (ret < 0) {
55  urg_exit(&urg, "urg_connect()");
56  exit(1);
57  }
58 
59  /* Reserve for receive buffer */
60  data_max = urg_dataMax(&urg);
61  data = (long*)malloc(sizeof(long) * data_max);
62  if (data == NULL) {
63  fprintf(stderr, "data_max: %d\n", data_max);
64  perror("data buffer");
65  exit(1);
66  }
67  urg_parameters(&urg, &parameter);
68  scan_msec = urg_scanMsec(&urg);
69 
70  /* Request for MD data */
71  /* To get data continuously for more than 100 times, set capture times equal to infinity times(UrgInfinityTimes) */
72  /* urg_setCaptureTimes(&urg, UrgInfinityTimes); */
73  assert(CaptureTimes < 100);
74  urg_setCaptureTimes(&urg, CaptureTimes);
75 
76  /* Request for data */
78  if (ret < 0) {
79  urg_exit(&urg, "urg_requestData()");
80  }
81 
82  /* Get data */
83  for (i = 0; i < CaptureTimes; ++i) {
84  /* Reception */
85  n = urg_receiveData(&urg, data, data_max);
86  if (n < 0) {
87  urg_exit(&urg, "urg_receiveData()");
88  }
89 
90  /* Display the front data with timestamp */
91  /*
92  Delay in reception of data at PC causes URG to discard the data which cannot be transmitted.
93  This may results in remain_times to become discontinuous
94  */
95  timestamp = urg_recentTimestamp(&urg);
96  remain_times = urg_remainCaptureTimes(&urg);
97 
98  /* Neglect the distance data if it is less than urg_minDistance() */
99  printf("%d/%d: %ld [mm], %d [msec]\n",
100  remain_times, CaptureTimes, data[parameter.area_front_], timestamp);
101 
102  if (remain_times <= 0) {
103  break;
104  }
105  }
106 
107  if (CaptureTimes > 99) {
108  /*It is necessary to explicitly stop the data acquisition to get data for more than 99 times */
109 
110  urg_laserOff(&urg);
111  }
112 
113  urg_disconnect(&urg);
114  free(data);
115 
116  return 0;
117 }