IMUThread.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/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         // open the IMU file
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         // set reading position to second line
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                 // Add precision at the cost of a small overhead
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 } // namespace rtabmap


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:20