libs/range_sensor/c/samples/multi_urg.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 
00031 int main(int argc, char *argv[])
00032 {
00033   enum {
00034     Times = 10,
00035     Urgs = 2,
00036   };
00037 
00038   urg_t urg[Urgs];
00039   long *data[Urgs];
00040   int data_max[Urgs];
00041   int timestamp;
00042   int ret;
00043   int n;
00044   int i;
00045   int k;
00046 
00047 #ifdef WINDOWS_OS
00048   const char *devices[] = { "COM3", "COM4" }; /* For Windows */
00049 #else
00050   const char *devices[] = { "/dev/ttyACM0", "/dev/ttyACM1" }; /* For Linux */
00051 #endif
00052 
00053   /* Connection */
00054   for (i = 0; i < Urgs; ++i) {
00055     urg_initialize(&urg[i]);
00056     ret = urg_connect(&urg[i], devices[i], 115200);
00057     if (ret < 0) {
00058       urg_exit(&urg[i], "urg_connect()");
00059     }
00060     /* To clear existing MD command*/
00061     urg_laserOff(&urg[i]);
00062 
00063     /* It will become easy if some frames are skipped. */
00064     /* If specified skip is 2, then transferred data becomes half. */
00065     /* urg_setSkipLines(&urg[i], 2); */
00066 
00067     /* Reserve for receive buffer */
00068     data_max[i] = urg_dataMax(&urg[i]);
00069     data[i] = (long*)malloc(sizeof(long) * data_max[i]);
00070     if (data[i] == NULL) {
00071       perror("data buffer");
00072       exit(1);
00073     }
00074   }
00075 
00076   /* Request for MD data */
00077   for (i = 0; i < Urgs; ++i) {
00078     urg_setCaptureTimes(&urg[i], Times);
00079 
00080     /* Request for data */
00081     ret = urg_requestData(&urg[i], URG_MD, URG_FIRST, URG_LAST);
00082     if (ret < 0) {
00083       urg_exit(&urg[i], "urg_requestData()");
00084     }
00085   }
00086 
00087   for (k = 0; k < Times; ++k) {
00088     for (i = 0; i < Urgs; ++i) {
00089       /* Ends when data reception is completed */
00090       int remain_times = urg_remainCaptureTimes(&urg[i]);
00091       printf("    %d: ", i);
00092       printf("(%03d/%03d): ", remain_times, Times);
00093       if (remain_times <= 0) {
00094         printf("\n");
00095         continue;
00096       }
00097 
00098       /* Reception */
00099       n = urg_receiveData(&urg[i], data[i], data_max[i]);
00100       if (n < 0) {
00101         /* Continue processing, because there is chances of receiving the
00102            data next time. */
00103         printf("%s: %s\n", "urg_receiveData()", urg_error(urg));
00104 
00105       } else {
00106 
00107         /* Display */
00108         timestamp = urg_recentTimestamp(&urg[i]);
00109         printf("timestamp: %d, ", timestamp);
00110 #if 0
00111         {
00112           int j;
00113           for (j = 0; j < n; ++j) {
00114             /* Neglect if distance data is less than urg_minDistance() */
00115             printf("%d:%ld, ", j, data[i][j]);
00116           }
00117           printf("\n");
00118         }
00119 #endif
00120         printf("\n");
00121       }
00122     }
00123   }
00124 
00125   /* Disconnect */
00126   for (i = 0; i < Urgs; ++i) {
00127     urg_disconnect(&urg[i]);
00128     free(data[i]);
00129   }
00130 
00131 #ifdef MSC
00132   getchar();
00133 #endif
00134 
00135   return 0;
00136 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator