Camera.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2010-2016, 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/Camera.h"
00029 
00030 #include <rtabmap/utilite/UEventsManager.h>
00031 #include <rtabmap/utilite/UConversion.h>
00032 #include <rtabmap/utilite/UStl.h>
00033 #include <rtabmap/utilite/UConversion.h>
00034 #include <rtabmap/utilite/UFile.h>
00035 #include <rtabmap/utilite/UDirectory.h>
00036 #include <rtabmap/utilite/UTimer.h>
00037 
00038 #include <opencv2/imgproc/imgproc.hpp>
00039 
00040 #include <iostream>
00041 #include <cmath>
00042 
00043 namespace rtabmap
00044 {
00045 
00046 Camera::Camera(float imageRate, const Transform & localTransform) :
00047         _imageRate(imageRate),
00048         _localTransform(localTransform),
00049         _targetImageSize(0,0),
00050         _frameRateTimer(new UTimer()),
00051         _seq(0)
00052 {
00053 }
00054 
00055 Camera::~Camera()
00056 {
00057         UDEBUG("");
00058         delete _frameRateTimer;
00059         UDEBUG("");
00060 }
00061 
00062 void Camera::resetTimer()
00063 {
00064         _frameRateTimer->start();
00065 }
00066 
00067 SensorData Camera::takeImage(CameraInfo * info)
00068 {
00069         bool warnFrameRateTooHigh = false;
00070         float actualFrameRate = 0;
00071         if(_imageRate>0)
00072         {
00073                 int sleepTime = (1000.0f/_imageRate - 1000.0f*_frameRateTimer->getElapsedTime());
00074                 if(sleepTime > 2)
00075                 {
00076                         uSleep(sleepTime-2);
00077                 }
00078                 else if(sleepTime < 0)
00079                 {
00080                         warnFrameRateTooHigh = true;
00081                         actualFrameRate = 1.0/(_frameRateTimer->getElapsedTime());
00082                 }
00083 
00084                 // Add precision at the cost of a small overhead
00085                 while(_frameRateTimer->getElapsedTime() < 1.0/double(_imageRate)-0.000001)
00086                 {
00087                         //
00088                 }
00089 
00090                 double slept = _frameRateTimer->getElapsedTime();
00091                 _frameRateTimer->start();
00092                 UDEBUG("slept=%fs vs target=%fs", slept, 1.0/double(_imageRate));
00093         }
00094 
00095         UTimer timer;
00096         SensorData data  = this->captureImage(info);
00097         double captureTime = timer.ticks();
00098         if(warnFrameRateTooHigh)
00099         {
00100                 UWARN("Camera: Cannot reach target image rate %f Hz, current rate is %f Hz and capture time = %f s.",
00101                                 _imageRate, actualFrameRate, captureTime);
00102         }
00103         else
00104         {
00105                 UDEBUG("Time capturing image = %fs", captureTime);
00106         }
00107         if(info)
00108         {
00109                 info->id = data.id();
00110                 info->stamp = data.stamp();
00111                 info->timeCapture = captureTime;
00112         }
00113         return data;
00114 }
00115 
00116 } // namespace rtabmap


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:18