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 SensorData Camera::takeImage(CameraInfo * info)
00063 {
00064         bool warnFrameRateTooHigh = false;
00065         float actualFrameRate = 0;
00066         if(_imageRate>0)
00067         {
00068                 int sleepTime = (1000.0f/_imageRate - 1000.0f*_frameRateTimer->getElapsedTime());
00069                 if(sleepTime > 2)
00070                 {
00071                         uSleep(sleepTime-2);
00072                 }
00073                 else if(sleepTime < 0)
00074                 {
00075                         warnFrameRateTooHigh = true;
00076                         actualFrameRate = 1.0/(_frameRateTimer->getElapsedTime());
00077                 }
00078 
00079                 // Add precision at the cost of a small overhead
00080                 while(_frameRateTimer->getElapsedTime() < 1.0/double(_imageRate)-0.000001)
00081                 {
00082                         //
00083                 }
00084 
00085                 double slept = _frameRateTimer->getElapsedTime();
00086                 _frameRateTimer->start();
00087                 UDEBUG("slept=%fs vs target=%fs", slept, 1.0/double(_imageRate));
00088         }
00089 
00090         UTimer timer;
00091         SensorData data  = this->captureImage(info);
00092         double captureTime = timer.ticks();
00093         if(warnFrameRateTooHigh)
00094         {
00095                 UWARN("Camera: Cannot reach target image rate %f Hz, current rate is %f Hz and capture time = %f s.",
00096                                 _imageRate, actualFrameRate, captureTime);
00097         }
00098         else
00099         {
00100                 UDEBUG("Time capturing image = %fs", captureTime);
00101         }
00102         if(info)
00103         {
00104                 info->id = data.id();
00105                 info->stamp = data.stamp();
00106                 info->timeCapture = captureTime;
00107         }
00108         return data;
00109 }
00110 
00111 } // namespace rtabmap


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:15