]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
sick-tim3xx: Add data streaming to ORTE
authorMatous Pokorny <matous.pokorny@me.com>
Thu, 27 Sep 2012 18:57:51 +0000 (20:57 +0200)
committerMatous Pokorny <matous.pokorny@me.com>
Thu, 27 Sep 2012 18:57:51 +0000 (20:57 +0200)
src/sick-tim3xx/Makefile.omk
src/sick-tim3xx/sickd.c
src/types/robottype.idl
src/types/robottype.ortegen

index 039d3b892978faed4e8a8aa9509c04b9964f2b4c..49ee6b4898fdd22773af562d78571e51d869c430 100644 (file)
@@ -8,4 +8,7 @@ test_PROGRAMS = test_sick
 test_sick_SOURCES = tim3xx_example.c
 
 bin_PROGRAMS = sickd
-sickd_SOURCES = sickd.c
\ No newline at end of file
+sickd_SOURCES = sickd.c
+
+lib_LOADLIBES = roboorte robottype orte
+
index a336cb2da9e6428843632cdf581cf8d3e21c31ae..b3dc78ef5a97fdc61b13a62a984c67a74d5d2663 100644 (file)
@@ -4,23 +4,26 @@
 #include <libusb-1.0/libusb.h>
 #include <robottype.h>
 #include <roboorte_robottype.h>
+#include <signal.h>
+
+static unsigned short *dists = NULL;   // distance scan
+//static unsigned short *refls = NULL; // reflex instensity scan
+static int scan_max = 271;      // angle range 0 - 270, step is 1 degree
+int interrupt = 0;
 
-/**
- * Function prototypes.
- */
 ssize_t getSOPASDeviceList(libusb_context *ctx, uint16_t vendorID, uint16_t productID, libusb_device ***list);
 void freeSOPASDeviceList(libusb_device **list);
 void printUSBDeviceDetails(struct libusb_device_descriptor desc);
 void printUSBInterfaceDetails(libusb_device* device);
-
 void printSOPASDeviceInformation(ssize_t numberOfDevices, libusb_device** devices);
 int sendSOPASTelegram(libusb_device_handle* device_handle, const char* request, unsigned int timeout);
 int receiveSOPASTelegram(libusb_device_handle* device_handle, unsigned char* receiveBuffer, int receiveBufferSize, unsigned int timeout);
-int read_scan(libusb_device_handle* device_handle, unsigned short * ranges, unsigned short * intensities, int size);
+int read_scan(libusb_device_handle* device_handle, unsigned short* ranges, unsigned short* intensities, int size);
+static void sig_handler(int sig);
 
 int main(void) {
 
-    printf("SICK TIM3xx daemon\n\n");
+    printf("sickd: This daemon reads data from SICK TIM3xx and publics it on ORTE.\n\n");
 
     /*
      * Create and initialize a new LIBUSB session.
@@ -28,13 +31,13 @@ int main(void) {
     libusb_context *ctx = NULL;
     int result = libusb_init(&ctx);
     if (result != 0) {
-        fprintf(stderr,"LIBUSB - Initialization failed with the following error code: %i.\n", result);
+        fprintf(stderr,"sickd: libusb - Initialization failed with the following error code: %i.\n", result);
     }
 
     /*
      * Set the verbosity level to 3 as suggested in the documentation.
      */
-    libusb_set_debug(ctx, 3);
+    //libusb_set_debug(ctx, 3);
 
     /*
      * Get a list of all SICK TIM3xx devices connected to the USB bus.
@@ -46,7 +49,27 @@ int main(void) {
     int vendorID = 0x19A2; // SICK AG
     int deviceID = 0x5001; // TIM3XX
     ssize_t numberOfDevices = getSOPASDeviceList(ctx, vendorID, deviceID, &devices);
+    
+    // ORTE init 
+    struct robottype_orte_data orte;
+    
+    orte.strength = 1;
+
+    if(robottype_roboorte_init(&orte)) {
+           fprintf(stderr,"sickd: Roboorte initialization failed! Exiting...\n");
+           exit(-1);
+    }
+    else
+           printf("sickd: Roboorte OK\n");
 
+    // Creating ORTE publishers
+    robottype_publisher_sick_scan_create(&orte, NULL, NULL);
+    printf("sickd: Publisher OK\n");
+    
+    // Signal handler registration
+    signal(SIGINT, sig_handler);
+    signal(SIGTERM, sig_handler);
+    
     /*
      * If available, open the first SICK TIM3xx device.
      */
@@ -63,15 +86,15 @@ int main(void) {
         libusb_device_handle *device_handle;
         libusb_open(devices[0], &device_handle);
         if (device_handle == NULL) {
-            fprintf(stderr, "\nLIBUSB - Cannot open device\n");
+            fprintf(stderr, "sickd: LIBUSB - Cannot open device\n");
         } else {
-            printf("\nLIBUSB - Device opened\n");
+            printf("sickd: LIBUSB - Device opened\n");
         }
 
         if (libusb_kernel_driver_active(device_handle, 0) == 1) {
-            printf("LIBUSB - Kernel driver active\n");
+            printf("sickd: LIBUSB - Kernel driver active\n");
             if (libusb_detach_kernel_driver(device_handle, 0) == 0) {
-                printf("LIBUSB - Kernel driver detached!\n");
+                printf("sickd: LIBUSB - Kernel driver detached!\n");
             }
         }
 
@@ -80,9 +103,9 @@ int main(void) {
          */
         result = libusb_claim_interface(device_handle, 0);
         if (result < 0) {
-            fprintf(stderr, "LIBUSB - Cannot claim interface\n");
+            fprintf(stderr, "sickd: LIBUSB - Cannot claim interface\n");
         } else {
-            printf("LIBUSB - Claimed interface\n");
+            printf("sickd: LIBUSB - Claimed interface\n");
         }
 
         /*
@@ -103,25 +126,74 @@ int main(void) {
 //         
 //         printf("LIBUSB - Read data...  %s\n\n", receiveBuffer);
         
-        unsigned short ranges[271];
-        unsigned short intensities[271];
-        read_scan(device_handle, ranges, intensities, 271);
-        
-        printf("Ranges a intensities:\n");
-        int i;
-        for (i = 0; i < 271; i++)
-        {
-            printf("index: %d, range: %d, intensity: %d\n", i, ranges[i], intensities[i]);
-        };
+        /*
+       * Start streaming 'LMDscandata'.
+       */
+       const char scan_start_cmd[] = {"\x02sEN LMDscandata 1\x03\0"};
+       result = sendSOPASTelegram(device_handle, scan_start_cmd, 500);
+       if (result != 0)
+       {
+           fprintf(stderr, "sickd: SOPAS - Error starting to stream 'LMDscandata'.");
+           return EXIT_FAILURE; 
+       }
         
+        // Distance scan variable init
+       dists = (unsigned short*)malloc(sizeof(unsigned short) * scan_max);
+       if (dists == NULL) {
+               fprintf(stderr, "sickd: Malloc failed!\n");
+               return -1;
+       }
+       
+       // Reflex instensity scan variable init
+//     refls = (unsigned short*)malloc(sizeof(unsigned short) * scan_max);
+//     if (refls == NULL) {
+//             fprintf(stderr, "sickd: Malloc failed!\n");
+//             return -1;
+//     }
+       
+       while (!interrupt) {
+           //read_scan(device_handle, dists, refls, scan_max);
+           
+           // Read distances only
+           read_scan(device_handle, dists, NULL, scan_max);
+       
+           // Print measured data
+           printf("Ranges:\n");
+           
+           int j;
+           for (j = 0; j < scan_max; j++) {
+               //printf("index: %d, distance: %li, reflex intensity: %li\n", j, dists[j], refls[j]);
+                printf("index: %d, distance: %d\n", j, dists[j]);
+               orte.sick_scan.data[j] = dists[j];
+               /*if (refls) {
+                 orte.sick_scan.data[j + scan_max] = refls[j];
+               }*/  
+           };
+           
+           
+           // Publish on ORTE
+           ORTEPublicationSend(orte.publication_sick_scan);
+       }
+       
+       /*
+       * End streaming 'LMDscandata'.
+       */
+       const char scan_stop_cmd[] = {"\x02sEN LMDscandata 0\x03\0"};
+       result = sendSOPASTelegram(device_handle, scan_stop_cmd, 500);
+       if (result != 0)
+       {
+           fprintf(stderr, "sickd: SOPAS - Error ending to stream 'LMDscandata'.");
+           return EXIT_FAILURE;
+       }
+       
         /*
          * Release the interface
          */
         result = libusb_release_interface(device_handle, 0);
         if (result != 0) {
-            fprintf(stderr, "LIBUSB - Cannot Release Interface\n");
+            fprintf(stderr, "sickd: LIBUSB - Cannot Release Interface\n");
         }
-        printf("LIBUSB - Released Interface\n");
+        printf("sickd: LIBUSB - Released Interface\n");
 
         /*
          * Close the device handle.
@@ -130,8 +202,11 @@ int main(void) {
     }
     else
     {
-        fprintf(stderr, "Error, no SICK Tim3xx device found\n\n");
+        fprintf(stderr, "sickd: Error, no SICK Tim3xx device found\n\n");
     }
+    
+    // ORTE deinit
+    robottype_roboorte_destroy(&orte);
 
     /*
      * Free the list of the USB devices.
@@ -143,7 +218,7 @@ int main(void) {
      */
     libusb_exit(ctx);
 
-    printf("SICK TIM3xx daemon finished\n\n");
+    printf("sickd: SICK TIM3xx daemon finished\n\n");
 
     return EXIT_SUCCESS;
 }
@@ -152,24 +227,24 @@ int read_scan(libusb_device_handle* device_handle, unsigned short * ranges, unsi
 {
   int result = 0;
   unsigned char receiveBuffer[65536];
-  unsigned char receiveBufferCopy[65536]; // only for debugging
+  //unsigned char receiveBufferCopy[65536]; // only for debugging
   int actual_length = 0;
   static const size_t NUM_FIELDS = 580;
   char* fields[NUM_FIELDS];
   size_t count;
-  static unsigned int iteration_count = 0;
+  //static unsigned int iteration_count = 0;
 
   result = libusb_bulk_transfer(device_handle, (1 | LIBUSB_ENDPOINT_IN), receiveBuffer, 65535, &actual_length, 0);
   if (result != 0)
   {
     if (result == LIBUSB_ERROR_TIMEOUT)
     {
-      fprintf(stderr, "LIBUSB - Read Error: LIBUSB_ERROR_TIMEOUT.");
+      fprintf(stderr, "sickd: LIBUSB - Read Error: LIBUSB_ERROR_TIMEOUT.");
       return EXIT_SUCCESS; // return success to continue looping
     }
     else
     {
-      fprintf(stderr, "LIBUSB - Read Error: %i.", result);
+      fprintf(stderr, "sickd: LIBUSB - Read Error: %i.", result);
       return EXIT_FAILURE;
     }
   }
@@ -198,13 +273,13 @@ int read_scan(libusb_device_handle* device_handle, unsigned short * ranges, unsi
   if (count < NUM_FIELDS)
   {
     fprintf(stderr, 
-        "received less fields than expected fields (actual: %zu, expected: %zu), ignoring scan", count, NUM_FIELDS);
+        "sickd: Received less fields than expected fields (actual: %zu, expected: %zu), ignoring scan", count, NUM_FIELDS);
     // ROS_DEBUG("received message was: %s", receiveBufferCopy);
     return EXIT_SUCCESS; // return success to continue looping
   }
   else if (count > NUM_FIELDS)
   {
-    fprintf(stderr, "received more fields than expected (expected: %zu), ignoring scan", NUM_FIELDS);
+    fprintf(stderr, "sickd: Received more fields than expected (expected: %zu), ignoring scan", NUM_FIELDS);
     // ROS_DEBUG("received message was: %s", receiveBufferCopy);
     return EXIT_SUCCESS; // return success to continue looping
   }
@@ -298,7 +373,7 @@ int read_scan(libusb_device_handle* device_handle, unsigned short * ranges, unsi
   {
     unsigned short range;
     sscanf(fields[j + 26], "%hx", &range);
-    ranges[j - index_min] = range; // /1000.0;
+    ranges[j - index_min] = range; //distance in mm
   }
 
   // 297: Number of 8 bit channels (1)
@@ -505,4 +580,9 @@ int receiveSOPASTelegram(libusb_device_handle* device_handle, unsigned char* rec
     receiveBuffer[actual] = 0;
 
     return result;
+}
+
+static void sig_handler(int sig)
+{
+        interrupt = 1;
 }
\ No newline at end of file
index aea4f6641ecc2aef74611b5cc62f5a70f4dada43..cfe54ed8990d289129777ed70dc393adfd5f448d 100644 (file)
@@ -87,6 +87,10 @@ struct hokuyo_scan {
     unsigned short data[681];
 };
 
+struct sick_scan {
+    unsigned short data[271];
+};
+
 struct pos_short {
        unsigned short left;
        unsigned short right;
index 2bcf46984d37fae1b475e9fe709d489534ce3538..d8febe123a5e0a967a401739e9b58647bb6c4ebf 100644 (file)
@@ -11,6 +11,7 @@ type=robot_pos                topic=est_pos_odo
 type=robot_pos         topic=est_pos_indep_odo
 type=robot_pos         topic=est_pos_best
 type=hokuyo_scan       topic=hokuyo_scan       deadline=1
+type=sick_scan         topic=sick_scan         deadline=1
 type=odo_data          topic=odo_data          deadline=0.3
 type=motion_irc                topic=motion_irc        deadline=0.3
 type=motion_speed      topic=motion_speed      deadline=0.3    pubdelay=0.1