CameraThread.cpp
Go to the documentation of this file.
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/CameraThread.h"
00029 #include "rtabmap/core/Camera.h"
00030 #include "rtabmap/core/CameraRGBD.h"
00031 #include "rtabmap/core/CameraEvent.h"
00032 
00033 #include <rtabmap/utilite/UTimer.h>
00034 #include <rtabmap/utilite/ULogger.h>
00035 
00036 namespace rtabmap
00037 {
00038 
00039 // ownership transferred
00040 CameraThread::CameraThread(Camera * camera) :
00041                 _camera(camera),
00042                 _cameraRGBD(0),
00043                 _seq(0)
00044 {
00045         UASSERT(_camera != 0);
00046 }
00047 
00048 // ownership transferred
00049 CameraThread::CameraThread(CameraRGBD * camera) :
00050                 _camera(0),
00051                 _cameraRGBD(camera),
00052                 _seq(0)
00053 {
00054         UASSERT(_cameraRGBD != 0);
00055 }
00056 
00057 CameraThread::~CameraThread()
00058 {
00059         join(true);
00060         if(_camera)
00061         {
00062                 delete _camera;
00063         }
00064         if(_cameraRGBD)
00065         {
00066                 delete _cameraRGBD;
00067         }
00068 }
00069 
00070 void CameraThread::setImageRate(float imageRate)
00071 {
00072         if(_camera)
00073         {
00074                 _camera->setImageRate(imageRate);
00075         }
00076         if(_cameraRGBD)
00077         {
00078                 _cameraRGBD->setImageRate(imageRate);
00079         }
00080 }
00081 
00082 bool CameraThread::init()
00083 {
00084         if(!this->isRunning())
00085         {
00086                 _seq = 0;
00087                 if(_cameraRGBD)
00088                 {
00089                         return _cameraRGBD->init();
00090                 }
00091                 else
00092                 {
00093                         return _camera->init();
00094                 }
00095 
00096                 // Added sleep time to ignore first frames (which are darker)
00097                 uSleep(1000);
00098         }
00099         else
00100         {
00101                 UERROR("Cannot initialize the camera because it is already running...");
00102         }
00103         return false;
00104 }
00105 
00106 void CameraThread::mainLoop()
00107 {
00108         UTimer timer;
00109         UDEBUG("");
00110         cv::Mat rgb, depth;
00111         float fx = 0.0f;
00112         float fy = 0.0f;
00113         float cx = 0.0f;
00114         float cy = 0.0f;
00115         if(_cameraRGBD)
00116         {
00117                 _cameraRGBD->takeImage(rgb, depth, fx, fy, cx, cy);
00118         }
00119         else
00120         {
00121                 rgb = _camera->takeImage();
00122         }
00123 
00124         if(!rgb.empty() && !this->isKilled())
00125         {
00126                 if(_cameraRGBD)
00127                 {
00128                         SensorData data(rgb, depth, fx, fy, cx, cy, _cameraRGBD->getLocalTransform(), Transform(), 1, 1, ++_seq, UTimer::now());
00129                         this->post(new CameraEvent(data, _cameraRGBD->getSerial()));
00130                 }
00131                 else
00132                 {
00133                         this->post(new CameraEvent(rgb, ++_seq, UTimer::now()));
00134                 }
00135         }
00136         else if(!this->isKilled())
00137         {
00138                 if(_cameraRGBD)
00139                 {
00140                         UWARN("no more images...");
00141                 }
00142                 this->kill();
00143                 this->post(new CameraEvent());
00144         }
00145 }
00146 
00147 } // namespace rtabmap


rtabmap
Author(s): Mathieu Labbe
autogenerated on Fri Aug 28 2015 12:51:31