libs/range_sensor/c/samples/gd_scan.c
00001 
00013 #include "urg_ctrl.h"
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 
00017 
00018 static void urg_exit(urg_t *urg, const char *message)
00019 {
00020   printf("%s: %s\n", message, urg_error(urg));
00021   urg_disconnect(urg);
00022 
00023 #ifdef MSC
00024   getchar();
00025 #endif
00026   exit(1);
00027 }
00028 
00029 
00030 int main(int argc, char *argv[])
00031 {
00032 #ifdef WINDOWS_OS
00033   const char device[] = "COM3"; /* For Windows */
00034 #else
00035   const char device[] = "/dev/ttyACM0"; /* For Linux */
00036 #endif
00037 
00038   int data_max;
00039   long *data;
00040   int timestamp;
00041   int ret;
00042   int n;
00043   int i;
00044 
00045   /* Connection */
00046   urg_t urg;
00047   urg_initialize(&urg);
00048   ret = urg_connect(&urg, device, 115200);
00049   if (ret < 0) {
00050     urg_exit(&urg, "urg_connect()");
00051   }
00052 
00053   /* Reserve for reception data */
00054   data_max = urg_dataMax(&urg);
00055   data = (long*)malloc(sizeof(long) * data_max);
00056   if (data == NULL) {
00057     perror("malloc");
00058     exit(1);
00059   }
00060 
00061   /* Request for GD data */
00062   ret = urg_requestData(&urg, URG_GD, URG_FIRST, URG_LAST);
00063   if (ret < 0) {
00064     urg_exit(&urg, "urg_requestData()");
00065   }
00066 
00067   /* Reception */
00068   n = urg_receiveData(&urg, data, data_max);
00069   printf("# n = %d\n", n);
00070   if (n < 0) {
00071     urg_exit(&urg, "urg_receiveData()");
00072   }
00073 
00074   /* Display */
00075   timestamp = urg_recentTimestamp(&urg);
00076   printf("# timestamp: %d\n", timestamp);
00077   for (i = 0; i < n; ++i) {
00078     /*Neglect the distance less than  urg_minDistance()  */
00079     printf("%d %ld, ", i, data[i]);
00080   }
00081   printf("\n");
00082 
00083   urg_disconnect(&urg);
00084   free(data);
00085 
00086 #ifdef MSC
00087   getchar();
00088 #endif
00089 
00090   return 0;
00091 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator