libs/range_sensor/c/samples/md_scan.c
00001 
00013 #include "urg_ctrl.h"
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <assert.h>
00017 
00018 
00019 static void urg_exit(urg_t *urg, const char *message)
00020 {
00021   printf("%s: %s\n", message, urg_error(urg));
00022   urg_disconnect(urg);
00023 
00024 #ifdef MSC
00025   getchar();
00026 #endif
00027   exit(1);
00028 }
00029 
00030 
00032 int main(int argc, char *argv[])
00033 {
00034   enum {
00035     CaptureTimes = 10,
00036   };
00037 
00038 #ifdef WINDOWS_OS
00039   const char device[] = "COM3"; /* For Windows */
00040 #else
00041   const char device[] = "/dev/ttyACM0"; /* For Linux */
00042 #endif
00043 
00044   int data_max;
00045   long* data;
00046   int timestamp = -1;
00047   int previous_timestamp;
00048   int remain_times;
00049   //int scan_msec;
00050   urg_parameter_t parameter;
00051   int ret;
00052   int n;
00053   int i;
00054   urg_t urg;
00055 
00056   /* Connection */
00057   urg_initialize(&urg);
00058   ret = urg_connect(&urg, device, 115200);
00059   if (ret < 0) {
00060     urg_exit(&urg, "urg_connect()");
00061     exit(1);
00062   }
00063 
00064   /* Reserve for receive buffer */
00065   data_max = urg_dataMax(&urg);
00066   data = (long*)malloc(sizeof(long) * data_max);
00067   if (data == NULL) {
00068     fprintf(stderr, "data_max: %d\n", data_max);
00069     perror("data buffer");
00070     exit(1);
00071   }
00072   urg_parameters(&urg, &parameter);
00073   //scan_msec = urg_scanMsec(&urg);
00074 
00075   /* Request for MD data */
00076   /* To get data continuously for more than 100 times, set capture times equal
00077      to infinity times(UrgInfinityTimes) */
00078   /* urg_setCaptureTimes(&urg, UrgInfinityTimes); */
00079   assert(CaptureTimes < 100);
00080   urg_setCaptureTimes(&urg, CaptureTimes);
00081 
00082   /* Request for data */
00083   ret = urg_requestData(&urg, URG_MD, URG_FIRST, URG_LAST);
00084   if (ret < 0) {
00085     urg_exit(&urg, "urg_requestData()");
00086   }
00087 
00088   for (i = 0; i < CaptureTimes; ++i) {
00089     /* Reception */
00090     n = urg_receiveData(&urg, data, data_max);
00091     printf("n = %d\n", n);
00092     if (n < 0) {
00093       urg_exit(&urg, "urg_receiveData()");
00094     } else if (n == 0) {
00095       printf("n == 0\n");
00096       --i;
00097       continue;
00098     }
00099 
00100     /* Display the front data with timestamp */
00101     /* Delay in reception of data at PC causes URG to discard the data which
00102        cannot be transmitted. This may  results in remain_times to become
00103        discontinuous */
00104     previous_timestamp = timestamp;
00105     timestamp = urg_recentTimestamp(&urg);
00106     remain_times = urg_remainCaptureTimes(&urg);
00107 
00108     /* Neglect the distance data if it is less than urg_minDistance() */
00109     printf("%d/%d: %ld [mm], %d [msec], (%d)\n",
00110            remain_times, CaptureTimes, data[parameter.area_front_], timestamp,
00111            timestamp - previous_timestamp);
00112 
00113     printf("%d, %d\n", i, remain_times);
00114 
00115     if (remain_times <= 0) {
00116       break;
00117     }
00118   }
00119 
00120   urg_disconnect(&urg);
00121   free(data);
00122 
00123 #ifdef MSC
00124   getchar();
00125 #endif
00126 
00127   return 0;
00128 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator