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>
17 #include <robottype.h>
18 #include <roboorte_robottype.h>
25 #define HOKUYO_DEVICE "/dev/ttyACM0" // /dev/ttyACM0
27 #define COMPILE_TIME_ASSERT(cond, msg) \
28 typedef char msg[(cond) ? 1 : -1]
30 COMPILE_TIME_ASSERT(HOKUYO_FINAL_MEASUREMENT - HOKUYO_INITIAL_MEASUREMENT == HOKUYO_ARRAY_SIZE,
31 wrong_size_of_hokuyo_scan_type);
38 struct robottype_orte_data orte;
40 int hokuyo_init(char *device)
43 ret = urg_connect(&urg, device, 115200);
45 error(0, errno, "hokuyo: urg_connect(%s) failed: %s errno",
46 device, urg_error(&urg));
51 /* Reserve for reception data */
52 scan_max = urg_dataMax(&urg);
53 scan = (long*)malloc(sizeof(long) * scan_max);
55 fprintf(stderr, "hokuyo: get_scan - malloc failed!\n");
62 static inline void next_period(struct timespec *next, long long interval_ns)
64 next->tv_nsec += interval_ns;
65 if (next->tv_nsec >= 1000000000) {
67 next->tv_nsec -= 1000000000;
71 static void sig_handler(int sig)
77 int main(int argc, char **argv)
80 urg_parameter_t parameter;
83 signal(SIGINT, sig_handler);
84 signal(SIGTERM, sig_handler);
86 ret = hokuyo_init(HOKUYO_DEVICE);
90 printf("Detected model: %s\n", urg_model(&urg));
91 printf("Distance min %ld mm, max %ld mm\n", urg_minDistance(&urg),
92 urg_maxDistance(&urg));
93 urg_parameters(&urg, ¶meter);
95 ret = robottype_roboorte_init(&orte);
97 fprintf(stderr, "robottype_roboorte_init failed\n");
99 /* Sleep in order to that not be respawn too fast by init */
104 robottype_publisher_hokuyo_scan_create(&orte, NULL, NULL);
106 /* Request for continuous scanning data */
107 urg_setCaptureTimes(&urg, UrgInfinityTimes);
108 ret = urg_requestData(&urg, URG_MD, URG_FIRST, URG_LAST);
110 fprintf(stderr, "hokuyo: get_scan - request data failed!\n");
112 /* Sleep in order to that not be respawn too fast by init */
118 clock_gettime(CLOCK_MONOTONIC, &next);
122 int average, errors[19];
124 memset(errors, 0, sizeof(errors));
126 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
128 next_period(&next, 100/*ms*/*1000*1000);
130 /* Receive actual data */
131 ret = urg_receiveData(&urg, scan, scan_max);
133 fprintf(stderr, "urg_receiveData failed\n");
136 /* for(i=0; i < scan_max ; i++) */
137 /* printf("[%d %ld] ", i, scan[i]); */
138 /* printf("\n%d\n", scan_max); */
140 for(i=0; i < HOKUYO_ARRAY_SIZE; i++) {
141 int idx = i + HOKUYO_INITIAL_MEASUREMENT;
144 // This is useful when you want to test which angle is scanning the robot inside
145 // ang = HOKUYO_INDEX_TO_RAD(i);
146 // if((ang<(-80.0/180.0*M_PI))||((ang>(75.0/180.0*M_PI))))
148 // orte.hokuyo_scan.data[i] = 0;
150 // orte.hokuyo_scan.data[i] = scan[idx];
152 orte.hokuyo_scan.data[i] = scan[idx];
154 average += scan[idx];
155 else if (scan[idx] > 0)
158 average /= HOKUYO_ARRAY_SIZE;
159 printf("Scan average: %d mm", average);
160 for (i = 0; i < 19; i++)
162 printf(", error %d: %dx", i, errors[i]);
164 ORTEPublicationSend(orte.publication_hokuyo_scan);
167 printf("Swithing off laser\n");
168 urg_disconnect(&urg);
170 robottype_roboorte_destroy(&orte);