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/IMUThread.h"
00029 #include "rtabmap/core/IMU.h"
00030 #include <rtabmap/utilite/UTimer.h>
00031 #include <rtabmap/utilite/ULogger.h>
00032 #include <rtabmap/utilite/UConversion.h>
00033
00034 namespace rtabmap
00035 {
00036
00037 IMUThread::IMUThread(int rate, const Transform & localTransform) :
00038 rate_(rate),
00039 localTransform_(localTransform),
00040 captureDelay_(0.0),
00041 previousStamp_(0.0)
00042 {
00043 }
00044
00045 IMUThread::~IMUThread()
00046 {
00047 imuFile_.close();
00048 }
00049
00050 bool IMUThread::init(const std::string & path)
00051 {
00052 imuFile_.close();
00053 captureDelay_ = 0.0;
00054 previousStamp_ = 0.0;
00055
00056
00057 std::string line;
00058 imuFile_.open(path.c_str());
00059 if (!imuFile_.good()) {
00060 UERROR("no imu file found at %s",path.c_str());
00061 return false;
00062 }
00063 int number_of_lines = 0;
00064 while (std::getline(imuFile_, line))
00065 ++number_of_lines;
00066 printf("No. IMU measurements: %d\n", number_of_lines-1);
00067 if (number_of_lines - 1 <= 0) {
00068 UERROR("no imu messages present in %s", path.c_str());
00069 return false;
00070 }
00071
00072 imuFile_.clear();
00073 imuFile_.seekg(0, std::ios::beg);
00074 std::getline(imuFile_, line);
00075
00076 return true;
00077 }
00078
00079 void IMUThread::setRate(int rate)
00080 {
00081 rate_ = rate;
00082 }
00083
00084 void IMUThread::mainLoopBegin()
00085 {
00086 ULogger::registerCurrentThread("IMU");
00087 frameRateTimer_.start();
00088 }
00089
00090 void IMUThread::mainLoop()
00091 {
00092 UTimer totalTime;
00093 UDEBUG("");
00094
00095 if(rate_>0 || captureDelay_)
00096 {
00097 double delay = rate_>0?1000.0/double(rate_):1000.0f*captureDelay_;
00098 int sleepTime = delay - 1000.0f*frameRateTimer_.getElapsedTime();
00099 if(sleepTime > 2)
00100 {
00101 uSleep(sleepTime-2);
00102 }
00103
00104
00105 delay/=1000.0;
00106 while(frameRateTimer_.getElapsedTime() < delay-0.000001)
00107 {
00108
00109 }
00110
00111 frameRateTimer_.start();
00112 }
00113 captureDelay_ = 0.0;
00114
00115 std::string line;
00116 if (std::getline(imuFile_, line))
00117 {
00118 std::stringstream stream(line);
00119 std::string s;
00120 std::getline(stream, s, ',');
00121 std::string nanoseconds = s.substr(s.size() - 9, 9);
00122 std::string seconds = s.substr(0, s.size() - 9);
00123
00124 cv::Vec3d gyr;
00125 for (int j = 0; j < 3; ++j) {
00126 std::getline(stream, s, ',');
00127 gyr[j] = uStr2Double(s);
00128 }
00129
00130 cv::Vec3d acc;
00131 for (int j = 0; j < 3; ++j) {
00132 std::getline(stream, s, ',');
00133 acc[j] = uStr2Double(s);
00134 }
00135
00136 double stamp = double(uStr2Int(seconds)) + double(uStr2Int(nanoseconds))*1e-9;
00137 if(previousStamp_>0 && stamp > previousStamp_)
00138 {
00139 captureDelay_ = stamp - previousStamp_;
00140 }
00141 previousStamp_ = stamp;
00142
00143 IMU imu(gyr, cv::Mat(), acc, cv::Mat(), localTransform_);
00144 this->post(new IMUEvent(imu, stamp));
00145 }
00146 else if(!this->isKilled())
00147 {
00148 UWARN("no more imu data...");
00149 this->kill();
00150 this->post(new IMUEvent());
00151 }
00152 }
00153
00154 }