libs/range_sensor/c/samples/convert_2d.c
00001 
00011 #include <math.h>
00012 #include "urg_ctrl.h"
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 
00016 
00017 static void urg_exit(urg_t *urg, const char *message)
00018 {
00019   printf("%s: %s\n", message, urg_error(urg));
00020   urg_disconnect(urg);
00021 
00022 #ifdef MSC
00023   getchar();
00024 #endif
00025   exit(1);
00026 }
00027 
00028 
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   long *data = NULL;
00039   int data_max;
00040   int min_length = 0;
00041   int max_length = 0;
00042   int ret;
00043   int n;
00044   int i;
00045 
00046   /* Connection */
00047   urg_t urg;
00048   urg_initialize(&urg);
00049   ret = urg_connect(&urg, device, 115200);
00050   if (ret < 0) {
00051     urg_exit(&urg, "urg_connect()");
00052   }
00053 
00054   /* Reserve for Reception data */
00055   data_max = urg_dataMax(&urg);
00056   data = (long*)malloc(sizeof(long) * data_max);
00057   if (data == NULL) {
00058     perror("data buffer");
00059     exit(1);
00060   }
00061 
00062   /* Request for GD data */
00063   ret = urg_requestData(&urg, URG_GD, URG_FIRST, URG_LAST);
00064   if (ret < 0) {
00065     urg_exit(&urg, "urg_requestData()");
00066   }
00067 
00068   /* Reception */
00069   n = urg_receiveData(&urg, data, data_max);
00070   if (n < 0) {
00071     urg_exit(&urg, "urg_receiveData()");
00072   }
00073 
00074   /* Output as 2 dimensional data */
00075   /* Consider front of URG as positive direction of X axis */
00076   min_length = urg_minDistance(&urg);
00077   max_length = urg_maxDistance(&urg);
00078   for (i = 0; i < n; ++i) {
00079     int x, y;
00080     long length = data[i];
00081 
00082     /* Neglect the out of range values */
00083     if ((length <= min_length) || (length >= max_length)) {
00084       continue;
00085     }
00086 
00087     x = (int)(length * cos(urg_index2rad(&urg, i)));
00088     y = (int)(length * sin(urg_index2rad(&urg, i)));
00089 
00090     printf("%d\t%d\t# %d, %ld\n", x, y, i, length);
00091   }
00092 
00093   urg_disconnect(&urg);
00094   free(data);
00095 
00096 #ifdef MSC
00097   getchar();
00098 #endif
00099 
00100   return 0;
00101 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator