SickS300.cpp
Go to the documentation of this file.
00001 
00002 #include "cob_sick_s300/SickS300.hpp"
00003 namespace brics_oodl {
00004 
00005 SickS300::SickS300() {
00006   // Bouml preserved body begin 00020E67
00007 
00008   this->sickS300 = NULL;
00009   this->config = NULL;
00010   this->isConnected = false;
00011   this->newDataFlagOne = false;
00012   this->newDataFlagTwo = false;
00013   timestampBufferOne = 0;
00014   timeNowBufferOne = 0;
00015   timestampBufferTwo = 0;
00016   timeNowBufferTwo = 0;
00017 
00018   //assigning zeros to the two buffers to reserve the memory
00019   distanceBufferOne.assign(numberOfScanPoints, 0);
00020   angleBufferOne.assign(numberOfScanPoints, 0);
00021   intensityBufferOne.assign(numberOfScanPoints, 0);
00022 
00023   distanceBufferTwo.assign(numberOfScanPoints, 0);
00024   angleBufferTwo.assign(numberOfScanPoints, 0);
00025   intensityBufferTwo.assign(numberOfScanPoints, 0);
00026 
00027   // Bouml preserved body end 00020E67
00028 }
00029 
00030 SickS300::~SickS300() {
00031   // Bouml preserved body begin 00020EE7
00032   Errors er;
00033   this->close(er);
00034   if (this->config != NULL) {
00035     delete this->config;
00036   }
00037   // Bouml preserved body end 00020EE7
00038 }
00039 
00040 bool SickS300::open(Errors& error) {
00041   // Bouml preserved body begin 00021367
00042   if (this->isConnected) {
00043     return true;
00044   }
00045 
00046   if (this->config->devicePath == "") {
00047     error.addError("no_DevicePath", "the device path is not specified in the configuration");
00048     this->isConnected = false;
00049     return false;
00050   }
00051 
00052   {
00053     boost::mutex::scoped_lock lock_it(mutexSickS300);
00054 
00055     if (sickS300 != NULL) {
00056       error.addError("still_Connected", "a previous connection was not closed correctly please close it again.");
00057       this->isConnected = false;
00058       return false;
00059     }
00060 
00061     this->sickS300 = new ScannerSickS300();
00062 
00063   }
00064 
00065   unsigned int desired_baud = 500000;
00066 
00067   switch (this->config->baud) {
00068     case BAUD_9600:
00069       desired_baud = 9600;
00070       LOG(trace) << "using 9600 baut to comunicate to Sick S300";
00071       break;
00072     case BAUD_19200:
00073       desired_baud = 19200;
00074       LOG(trace) << "using 19200 baut to comunicate to Sick S300";
00075       break;
00076     case BAUD_38400:
00077       desired_baud = 38400;
00078       LOG(trace) << "using 38400 baut to comunicate to Sick S300";
00079       break;
00080     case BAUD_115200:
00081       desired_baud = 115200;
00082       LOG(trace) << "using 115200 baut to comunicate to Sick S300";
00083       break;
00084     case BAUD_500K:
00085       desired_baud = 500000;
00086       LOG(trace) << "using 500000 baut to comunicate to Sick S300";
00087       break;
00088     case BAUD_UNKNOWN:
00089       desired_baud = 0;
00090       break;
00091   }
00092 
00093   //Initialize the Sick S300
00094   try {
00095     {
00096       boost::mutex::scoped_lock lock_it(mutexSickS300);
00097       if (!sickS300->open(this->config->devicePath.c_str(), desired_baud, this->config->scannerID)) {
00098         throw "could not initilize Sick S300";
00099       }
00100       this->isConnected = true;
00101     }
00102     LOG(trace) << "connection to Sick S300 initialized";
00103 
00104     stopThread = false;
00105 
00106     threads.create_thread(boost::bind(&SickS300::receiveScan, this));
00107 
00108   } catch (...) {
00109     error.addError("Initialize_failed", "could not initilize Sick S300");
00110     {
00111       boost::mutex::scoped_lock lock_it(mutexSickS300);
00112       this->isConnected = false;
00113       delete sickS300;
00114       sickS300 = NULL;
00115     }
00116     return false;
00117   }
00118   return true;
00119   // Bouml preserved body end 00021367
00120 }
00121 
00122 bool SickS300::close(Errors& error) {
00123   // Bouml preserved body begin 00020F67
00124   stopThread = true;
00125 
00126   threads.join_all();
00127 
00128   {
00129     boost::mutex::scoped_lock lock_it(mutexSickS300);
00130     if (sickS300 != NULL) {
00131       LOG(trace) << "connection to Sick S300 closed";
00132       delete sickS300;
00133       sickS300 = NULL;
00134     }
00135     this->isConnected = false;
00136   }
00137   return true;
00138   // Bouml preserved body end 00020F67
00139 }
00140 
00141 bool SickS300::setConfiguration(const LaserScannerConfiguration& configuration, Errors& error) {
00142   // Bouml preserved body begin 00020FE7
00143 
00144   if (this->config != NULL) {
00145     delete this->config;
00146   }
00147   this->config = new LaserScannerConfiguration;
00148   *(this->config) = configuration;
00149 
00150   if (!this->open(error)) {
00151     return false;
00152   }
00153 
00154   //  error.addError("configuration_not_possible", "the configuration is not possible.");
00155   return true;
00156   // Bouml preserved body end 00020FE7
00157 }
00158 
00159 bool SickS300::getConfiguration(LaserScannerConfiguration& configuration, Errors& error) {
00160   // Bouml preserved body begin 000210E7
00161   if (!this->open(error)) {
00162     return false;
00163   }
00164   try {
00165     configuration.vendor = "SICK";
00166     configuration.product = "S300";
00167 
00168     configuration.scanAngleStart = -135.0 / 180.0 * M_PI * radian;
00169     configuration.scanAngleStop = 135.0 / 180.0 * M_PI * radian;
00170     configuration.scanResolution = ((-configuration.scanAngleStart) + configuration.scanAngleStop) / (double) numberOfScanPoints;
00171 
00172 
00173     LOG(trace) << "read Sick LMS configuration";
00174 
00175   } catch (...) {
00176     error.addError("unable_to_read_configuration", "could not get the configuration from the Sick S300");
00177     return false;
00178   }
00179 
00180   return true;
00181   // Bouml preserved body end 000210E7
00182 }
00183 
00184 bool SickS300::getData(std::vector< double >& ranges_, std::vector< double >& rangeAngles_, std::vector< double >& intensities_, unsigned int& timestamp_, unsigned int& timeNow_, Errors& error) {
00185 
00186   if (!this->open(error)) {
00187     return false;
00188   }
00189   try {
00190     if (newDataFlagOne == true) {
00191       {
00192         boost::mutex::scoped_lock dataMutex1(mutexData1);
00193         ranges_ = distanceBufferOne;
00194         rangeAngles_ = angleBufferOne;
00195         intensities_ = intensityBufferOne;
00196         timestamp_ = timestampBufferOne;
00197         timeNow_ = timeNowBufferOne;
00198       }
00199       newDataFlagOne = false;
00200 
00201     } else if (newDataFlagTwo == true) {
00202       {
00203         boost::mutex::scoped_lock dataMutex2(mutexData2);
00204         ranges_ = distanceBufferTwo;
00205         rangeAngles_ = angleBufferTwo;
00206         intensities_ = intensityBufferTwo;
00207         timestamp_ = timestampBufferTwo;
00208         timeNow_ = timeNowBufferTwo;
00209       }
00210       newDataFlagTwo = false;
00211     } else {
00212      // error.addError("unable_to_get_data", "could not get data from the Sick S300");
00213       return false;
00214     }
00215     // LOG(trace) << "receiving range and intensity scan from Sick S300";
00216 
00217   } catch (...) {
00218     error.addError("unable_to_get_data", "could not get data from the Sick S300");
00219     return false;
00220   }
00221 
00222   return true;
00223 }
00224 
00225 bool SickS300::resetDevice(Errors& error) {
00226   // Bouml preserved body begin 000212E7
00227   error.addError("unable_to_reset_sick_s300", "could not reset the Sick S300");
00228   return false;
00229   // Bouml preserved body end 000212E7
00230 }
00231 
00232 void SickS300::receiveScan() {
00233   // Bouml preserved body begin 000371F1
00234 
00235   bool returnValue = false;
00236 
00237   {
00238     boost::mutex::scoped_lock lock_it(mutexSickS300);
00239 
00240     while (!stopThread) {
00241 
00242       if (newDataFlagOne == false) {
00243         {
00244           boost::mutex::scoped_lock dataMutex1(mutexData1);
00245           returnValue = sickS300->getScan(distanceBufferOne, angleBufferOne, intensityBufferOne, timestampBufferOne, timeNowBufferOne);
00246         }
00247         if (returnValue) {
00248           newDataFlagOne = true;
00249           newDataFlagTwo = false;
00250         }
00251 
00252       } else if (newDataFlagTwo == false) {
00253         {
00254           boost::mutex::scoped_lock dataMutex2(mutexData2);
00255           returnValue = sickS300->getScan(distanceBufferTwo, angleBufferTwo, intensityBufferTwo, timestampBufferTwo, timeNowBufferTwo);
00256         }
00257         if (returnValue) {
00258           newDataFlagTwo = true;
00259           newDataFlagOne = false;
00260         }
00261       }
00262       boost::this_thread::sleep(boost::posix_time::milliseconds(timeTillNextPollForData));
00263     }
00264   }
00265   // Bouml preserved body end 000371F1
00266 }
00267 
00268 
00269 } // namespace brics_oodl


cob_sick_s300
Author(s): Florian Weisshardt
autogenerated on Sun Oct 5 2014 23:05:19