All Classes Namespaces Files Functions Variables Enumerations Enumerator
libs/range_sensor/simple_samples/connectById.cpp
00001 
00011 #include "UrgDevice.h"
00012 #include "UrgUtils.h"
00013 #include "Connection.h"
00014 #include "ConnectionUtils.h"
00015 #include <iostream>
00016 
00017 using namespace qrk;
00018 using namespace std;
00019 
00020 
00021 namespace
00022 {
00023   bool adjustById(UrgDevice urg[], long serials[], size_t n)
00024   {
00025     // Get serial ID
00026     vector<long> device_serials;
00027     for (size_t i = 0; i < n; ++i) {
00028       long serial_id = urgSerialId<UrgDevice>(&urg[i]);
00029       device_serials.push_back(serial_id);
00030     }
00031 
00032     // Sort urg object
00033     for (size_t i = 0; i < n; ++i) {
00034       long requested_serial = serials[i];
00035       if (device_serials[i] == requested_serial) {
00036         continue;
00037       }
00038 
00039       vector<long>::iterator p =
00040         find(device_serials.begin(), device_serials.end(), requested_serial);
00041       if (p == device_serials.end()) {
00042         return false;
00043       }
00044       size_t swap_index = p - device_serials.begin();
00045 
00046       swapConnection<UrgDevice>(urg[i], urg[swap_index]);
00047       swap(device_serials[i], device_serials[swap_index]);
00048     }
00049 
00050     return true;
00051   }
00052 }
00053 
00054 
00055 int main(int argc, char *argv[])
00056 {
00057   // serial ID of URG that wants to connect
00058   long serials[] = {
00059     710002,
00060     614967,
00061   };
00062 #ifdef WINDOWS_OS
00063   const char* devices[] = { "COM3", "COM4" };
00064 #else
00065   const char* devices[] = { "/dev/ttyACM0", "/dev/ttyACM1" };
00066 #endif
00067 
00068   UrgDevice urg[2];
00069 
00070   // Connection
00071   size_t n = sizeof(serials) / sizeof(serials[0]);
00072   for (size_t i = 0; i < n; ++i) {
00073     if (! urg[i].connect(devices[i])) {
00074       cout << "UrgDevice::connect: " << urg[i].what() << endl;
00075       exit(1);
00076     }
00077   }
00078 
00079   if (! adjustById(urg, serials, n)) {
00080     cout << "fail swapById()" << endl;
00081     exit(1);
00082   }
00083 
00084   // Display
00085   cout << "result" << endl;
00086   for (size_t i = 0; i < n; ++i) {
00087     cout << i << ", " << urgSerialId<UrgDevice>(&urg[i]) << endl;
00088   }
00089 
00090   for (size_t i = 0; i < n; ++i) {
00091     delete urg[i].connection();
00092     urg[i].setConnection(NULL);
00093   }
00094 
00095 #ifdef MSC
00096   getchar();
00097 #endif
00098 
00099   return 0;
00100 }