Camera.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include "rtabmap/core/Camera.h"
29 
32 #include <rtabmap/utilite/UStl.h>
34 #include <rtabmap/utilite/UFile.h>
36 #include <rtabmap/utilite/UTimer.h>
37 
38 #include <opencv2/imgproc/imgproc.hpp>
39 
40 #include <iostream>
41 #include <cmath>
42 
43 namespace rtabmap
44 {
45 
46 Camera::Camera(float imageRate, const Transform & localTransform) :
47  _imageRate(imageRate),
48  _localTransform(localTransform),
49  _targetImageSize(0,0),
50  _frameRateTimer(new UTimer()),
51  _seq(0)
52 {
53 }
54 
56 {
57  UDEBUG("");
58  delete _frameRateTimer;
59  UDEBUG("");
60 }
61 
63 {
65 }
66 
68 {
69  bool warnFrameRateTooHigh = false;
70  float actualFrameRate = 0;
71  if(_imageRate>0)
72  {
73  int sleepTime = (1000.0f/_imageRate - 1000.0f*_frameRateTimer->getElapsedTime());
74  if(sleepTime > 2)
75  {
76  uSleep(sleepTime-2);
77  }
78  else if(sleepTime < 0)
79  {
80  warnFrameRateTooHigh = true;
81  actualFrameRate = 1.0/(_frameRateTimer->getElapsedTime());
82  }
83 
84  // Add precision at the cost of a small overhead
85  while(_frameRateTimer->getElapsedTime() < 1.0/double(_imageRate)-0.000001)
86  {
87  //
88  }
89 
90  double slept = _frameRateTimer->getElapsedTime();
92  UDEBUG("slept=%fs vs target=%fs", slept, 1.0/double(_imageRate));
93  }
94 
95  UTimer timer;
96  SensorData data = this->captureImage(info);
97  double captureTime = timer.ticks();
98  if(warnFrameRateTooHigh)
99  {
100  UWARN("Camera: Cannot reach target image rate %f Hz, current rate is %f Hz and capture time = %f s.",
101  _imageRate, actualFrameRate, captureTime);
102  }
103  else
104  {
105  UDEBUG("Time capturing image = %fs", captureTime);
106  }
107  if(info)
108  {
109  info->id = data.id();
110  info->stamp = data.stamp();
111  info->timeCapture = captureTime;
112  }
113  return data;
114 }
115 
116 } // namespace rtabmap
Definition: UTimer.h:46
Camera(float imageRate=0, const Transform &localTransform=Transform::getIdentity())
Definition: Camera.cpp:46
void resetTimer()
Definition: Camera.cpp:62
Some conversion functions.
virtual ~Camera()
Definition: Camera.cpp:55
double getElapsedTime()
Definition: UTimer.cpp:90
virtual SensorData captureImage(CameraInfo *info=0)=0
SensorData takeImage(CameraInfo *info=0)
Definition: Camera.cpp:67
Wrappers of STL for convenient functions.
void start()
Definition: UTimer.cpp:80
int id() const
Definition: SensorData.h:152
double stamp() const
Definition: SensorData.h:154
UTimer * _frameRateTimer
Definition: Camera.h:89
void uSleep(unsigned int ms)
float _imageRate
Definition: Camera.h:86
#define UDEBUG(...)
#define UWARN(...)
double ticks()
Definition: UTimer.cpp:110


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:30