IMUThread.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/IMUThread.h"
29 #include "rtabmap/core/IMU.h"
30 #include <rtabmap/utilite/UTimer.h>
33 
34 namespace rtabmap
35 {
36 
37 IMUThread::IMUThread(int rate, const Transform & localTransform) :
38  rate_(rate),
39  localTransform_(localTransform),
40  captureDelay_(0.0),
41  previousStamp_(0.0)
42 {
43 }
44 
46 {
47  imuFile_.close();
48 }
49 
50 bool IMUThread::init(const std::string & path)
51 {
52  imuFile_.close();
53  captureDelay_ = 0.0;
54  previousStamp_ = 0.0;
55 
56  // open the IMU file
57  std::string line;
58  imuFile_.open(path.c_str());
59  if (!imuFile_.good()) {
60  UERROR("no imu file found at %s",path.c_str());
61  return false;
62  }
63  int number_of_lines = 0;
64  while (std::getline(imuFile_, line))
65  ++number_of_lines;
66  printf("No. IMU measurements: %d\n", number_of_lines-1);
67  if (number_of_lines - 1 <= 0) {
68  UERROR("no imu messages present in %s", path.c_str());
69  return false;
70  }
71  // set reading position to second line
72  imuFile_.clear();
73  imuFile_.seekg(0, std::ios::beg);
74  std::getline(imuFile_, line);
75 
76  return true;
77 }
78 
79 void IMUThread::setRate(int rate)
80 {
81  rate_ = rate;
82 }
83 
85 {
88 }
89 
91 {
92  UTimer totalTime;
93  UDEBUG("");
94 
95  if(rate_>0 || captureDelay_)
96  {
97  double delay = rate_>0?1000.0/double(rate_):1000.0f*captureDelay_;
98  int sleepTime = delay - 1000.0f*frameRateTimer_.getElapsedTime();
99  if(sleepTime > 2)
100  {
101  uSleep(sleepTime-2);
102  }
103 
104  // Add precision at the cost of a small overhead
105  delay/=1000.0;
106  while(frameRateTimer_.getElapsedTime() < delay-0.000001)
107  {
108  //
109  }
110 
112  }
113  captureDelay_ = 0.0;
114 
115  std::string line;
116  if (std::getline(imuFile_, line))
117  {
118  std::stringstream stream(line);
119  std::string s;
120  std::getline(stream, s, ',');
121  std::string nanoseconds = s.substr(s.size() - 9, 9);
122  std::string seconds = s.substr(0, s.size() - 9);
123 
124  cv::Vec3d gyr;
125  for (int j = 0; j < 3; ++j) {
126  std::getline(stream, s, ',');
127  gyr[j] = uStr2Double(s);
128  }
129 
130  cv::Vec3d acc;
131  for (int j = 0; j < 3; ++j) {
132  std::getline(stream, s, ',');
133  acc[j] = uStr2Double(s);
134  }
135 
136  double stamp = double(uStr2Int(seconds)) + double(uStr2Int(nanoseconds))*1e-9;
137  if(previousStamp_>0 && stamp > previousStamp_)
138  {
139  captureDelay_ = stamp - previousStamp_;
140  }
141  previousStamp_ = stamp;
142 
143  IMU imu(gyr, cv::Mat(), acc, cv::Mat(), localTransform_);
144  this->post(new IMUEvent(imu, stamp));
145  }
146  else if(!this->isKilled())
147  {
148  UWARN("no more imu data...");
149  this->kill();
150  this->post(new IMUEvent());
151  }
152 }
153 
154 } // namespace rtabmap
int UTILITE_EXP uStr2Int(const std::string &str)
std::ifstream imuFile_
Definition: IMUThread.h:64
Definition: UTimer.h:46
bool init(const std::string &path)
Definition: IMUThread.cpp:50
double UTILITE_EXP uStr2Double(const std::string &str)
virtual ~IMUThread()
Definition: IMUThread.cpp:45
void kill()
Definition: UThread.cpp:48
double previousStamp_
Definition: IMUThread.h:67
GLM_FUNC_DECL genType e()
double captureDelay_
Definition: IMUThread.h:66
Some conversion functions.
void post(UEvent *event, bool async=true) const
double getElapsedTime()
Definition: UTimer.cpp:97
void setRate(int rate)
Definition: IMUThread.cpp:79
UTimer frameRateTimer_
Definition: IMUThread.h:65
static void registerCurrentThread(const std::string &name)
Definition: ULogger.cpp:218
virtual void mainLoop()
Definition: IMUThread.cpp:90
void start()
Definition: UTimer.cpp:87
virtual void mainLoopBegin()
Definition: IMUThread.cpp:84
void uSleep(unsigned int ms)
#define UDEBUG(...)
#define UERROR(...)
ULogger class and convenient macros.
#define UWARN(...)
IMUThread(int rate, const Transform &localTransform)
Definition: IMUThread.cpp:37
bool isKilled() const
Definition: UThread.cpp:255
Transform localTransform_
Definition: IMUThread.h:63


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:28