Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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 }