00001 /* 00002 Copyright (c) 2010-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are met: 00007 * Redistributions of source code must retain the above copyright 00008 notice, this list of conditions and the following disclaimer. 00009 * Redistributions in binary form must reproduce the above copyright 00010 notice, this list of conditions and the following disclaimer in the 00011 documentation and/or other materials provided with the distribution. 00012 * Neither the name of the Universite de Sherbrooke nor the 00013 names of its contributors may be used to endorse or promote products 00014 derived from this software without specific prior written permission. 00015 00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY 00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00028 #include "rtabmap/core/OdometryThread.h" 00029 #include "rtabmap/core/Odometry.h" 00030 #include "rtabmap/core/OdometryInfo.h" 00031 #include "rtabmap/core/CameraEvent.h" 00032 #include "rtabmap/core/OdometryEvent.h" 00033 #include "rtabmap/utilite/ULogger.h" 00034 00035 namespace rtabmap { 00036 00037 OdometryThread::OdometryThread(Odometry * odometry) : 00038 _odometry(odometry), 00039 _resetOdometry(false) 00040 { 00041 UASSERT(_odometry != 0); 00042 } 00043 00044 OdometryThread::~OdometryThread() 00045 { 00046 this->unregisterFromEventsManager(); 00047 this->join(true); 00048 if(_odometry) 00049 { 00050 delete _odometry; 00051 } 00052 UDEBUG(""); 00053 } 00054 00055 void OdometryThread::handleEvent(UEvent * event) 00056 { 00057 if(this->isRunning()) 00058 { 00059 if(event->getClassName().compare("CameraEvent") == 0) 00060 { 00061 CameraEvent * cameraEvent = (CameraEvent*)event; 00062 if(cameraEvent->getCode() == CameraEvent::kCodeImageDepth) 00063 { 00064 this->addData(cameraEvent->data()); 00065 } 00066 else if(cameraEvent->getCode() == CameraEvent::kCodeNoMoreImages) 00067 { 00068 this->post(new CameraEvent()); // forward the event 00069 } 00070 } 00071 else if(event->getClassName().compare("OdometryResetEvent") == 0) 00072 { 00073 _resetOdometry = true; 00074 } 00075 } 00076 } 00077 00078 void OdometryThread::mainLoopKill() 00079 { 00080 _dataAdded.release(); 00081 } 00082 00083 //============================================================ 00084 // MAIN LOOP 00085 //============================================================ 00086 void OdometryThread::mainLoop() 00087 { 00088 if(_resetOdometry) 00089 { 00090 _odometry->reset(); 00091 _resetOdometry = false; 00092 } 00093 00094 SensorData data; 00095 getData(data); 00096 if(data.isValid()) 00097 { 00098 OdometryInfo info; 00099 Transform pose = _odometry->process(data, &info); 00100 data.setPose(pose, info.variance, info.variance); // a null pose notify that odometry could not be computed 00101 this->post(new OdometryEvent(data, info)); 00102 } 00103 } 00104 00105 void OdometryThread::addData(const SensorData & data) 00106 { 00107 if(dynamic_cast<OdometryMono*>(_odometry) == 0) 00108 { 00109 if(data.image().empty() || data.depthOrRightImage().empty() || data.fx() == 0.0f || data.fyOrBaseline() == 0.0f) 00110 { 00111 ULOGGER_ERROR("Missing some information (images empty or missing calibration)!?"); 00112 return; 00113 } 00114 } 00115 else 00116 { 00117 if(data.image().empty() || data.fx() == 0.0f || data.fyOrBaseline() == 0.0f) 00118 { 00119 ULOGGER_ERROR("Missing some information (image empty or missing calibration)!?"); 00120 return; 00121 } 00122 } 00123 00124 bool notify = true; 00125 _dataMutex.lock(); 00126 { 00127 notify = !_dataBuffer.isValid(); 00128 _dataBuffer = data; 00129 } 00130 _dataMutex.unlock(); 00131 00132 if(notify) 00133 { 00134 _dataAdded.release(); 00135 } 00136 } 00137 00138 void OdometryThread::getData(SensorData & data) 00139 { 00140 _dataAdded.acquire(); 00141 _dataMutex.lock(); 00142 { 00143 if(_dataBuffer.isValid()) 00144 { 00145 data = _dataBuffer; 00146 _dataBuffer = SensorData(); 00147 } 00148 } 00149 _dataMutex.unlock(); 00150 } 00151 00152 } // namespace rtabmap