3 @Brief: This is a demo program to obtain 10 scan data from URG-Series and save them into the file.
4 @Note : This sample source is a referece for the user when creating his/her program and does not gurantee for users program
5 @Note : Hokuyo Automatic Co., Ltd does not bear responsibility for any loss or damage caused due to this demo program.
6 @Note : Mention the publisher's name when distributing this source or its part to the third party
7 @Publisher HOKUYO AUTOMATIC CO.,LTD.
8 @author Tomoaki Yoshida
13 #include<sys/termios.h>
16 #include <lidar_params.h>
18 #include <robottype.h>
19 #include <roboorte_robottype.h>
26 #define HOKUYO_DEVICE "/dev/ttyACM0" // /dev/ttyACM0
28 #define COMPILE_TIME_ASSERT(cond, msg) \
29 typedef char msg[(cond) ? 1 : -1]
31 COMPILE_TIME_ASSERT(HOKUYO_FINAL_MEASUREMENT - HOKUYO_INITIAL_MEASUREMENT == HOKUYO_ARRAY_SIZE,
32 wrong_size_of_lidar_scan_type);
39 struct robottype_orte_data orte;
41 int hokuyo_init(char *device)
44 ret = urg_connect(&urg, device, 115200);
46 error(0, errno, "hokuyo: urg_connect(%s) failed: %s errno",
47 device, urg_error(&urg));
52 /* Reserve for reception data */
53 scan_max = urg_dataMax(&urg);
54 scan = (long*)malloc(sizeof(long) * scan_max);
56 fprintf(stderr, "hokuyo: get_scan - malloc failed!\n");
63 static inline void next_period(struct timespec *next, long long interval_ns)
65 next->tv_nsec += interval_ns;
66 if (next->tv_nsec >= 1000000000) {
68 next->tv_nsec -= 1000000000;
72 static void sig_handler(int sig)
78 int main(int argc, char **argv)
81 urg_parameter_t parameter;
84 signal(SIGINT, sig_handler);
85 signal(SIGTERM, sig_handler);
87 ret = hokuyo_init(HOKUYO_DEVICE);
91 printf("Detected model: %s\n", urg_model(&urg));
92 printf("Distance min %ld mm, max %ld mm\n", urg_minDistance(&urg),
93 urg_maxDistance(&urg));
94 urg_parameters(&urg, ¶meter);
96 ret = robottype_roboorte_init(&orte);
98 fprintf(stderr, "robottype_roboorte_init failed\n");
100 /* Sleep in order to that not be respawn too fast by init */
105 robottype_publisher_hokuyo_scan_create(&orte, NULL, NULL);
107 /* Request for continuous scanning data */
108 urg_setCaptureTimes(&urg, UrgInfinityTimes);
109 ret = urg_requestData(&urg, URG_MD, URG_FIRST, URG_LAST);
111 fprintf(stderr, "hokuyo: get_scan - request data failed!\n");
113 /* Sleep in order to that not be respawn too fast by init */
119 clock_gettime(CLOCK_MONOTONIC, &next);
123 int average, errors[19];
125 memset(errors, 0, sizeof(errors));
127 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
129 next_period(&next, 100/*ms*/*1000*1000);
131 /* Receive actual data */
132 ret = urg_receiveData(&urg, scan, scan_max);
134 fprintf(stderr, "urg_receiveData failed\n");
137 /* for(i=0; i < scan_max ; i++) */
138 /* printf("[%d %ld] ", i, scan[i]); */
139 /* printf("\n%d\n", scan_max); */
141 for(i=0; i < HOKUYO_ARRAY_SIZE; i++) {
142 int idx = i + HOKUYO_INITIAL_MEASUREMENT;
145 // This is useful when you want to test which angle is scanning the robot inside
146 // ang = HOKUYO_INDEX_TO_RAD(i);
147 // if((ang<(-80.0/180.0*M_PI))||((ang>(75.0/180.0*M_PI))))
149 // orte.hokuyo_scan.data[i] = 0;
151 // orte.hokuyo_scan.data[i] = scan[idx];
153 orte.hokuyo_scan.data[i] = scan[idx];
155 average += scan[idx];
156 else if (scan[idx] > 0)
159 average /= HOKUYO_ARRAY_SIZE;
160 printf("Scan average: %d mm", average);
161 for (i = 0; i < 19; i++)
163 printf(", error %d: %dx", i, errors[i]);
166 orte.hokuyo_scan.data_lenght = hokuyo_params.data_lenght;
167 orte.hokuyo_scan.lidar_type = hokuyo_params.type;
168 ORTEPublicationSend(orte.publication_hokuyo_scan);
171 printf("Swithing off laser\n");
172 urg_disconnect(&urg);
174 robottype_roboorte_destroy(&orte);