IMUCalibrator.cpp
Go to the documentation of this file.
00001 /*
00002  * IMUCalibrator.cpp
00003  *
00004  *  Created on: Aug 23, 2012
00005  *      Author: mriedel
00006  */
00007 
00008 #include "IMUCalibrator.hpp"
00009 
00010 #define GYRO_CENTER 512
00011 
00012 namespace TELEKYB_NAMESPACE {
00013 
00014 IMUCalibratorOptions::IMUCalibratorOptions()
00015         : OptionContainer("IMUCalibrator")
00016 {
00017         tDriftEstimTimeout = addBoundsOption<double>("tDriftEstimTimeout",
00018                         "Timeout for DriftEstim in seconds", 10.0, 0.1, 30.0, false, true);
00019         tDriftEstimVarianceThreshold = addBoundsOption<double>("tDriftEstimVarianceThreshold",
00020                         "Threshold for Drift Estimation Deltas", 1e-3, 1e-5, 20.0, false, false);
00021         tDriftEstimWindowSize = addBoundsOption<int>("tDriftEstimWindowSize",
00022                         "Window Size for Drift Estimation", 200, 10, 500, false, false);
00023         tDriftFilterGain = addOption<double>("tDriftFilterGain",
00024                         "Drift Filter Gain for Estimation", 5.0, false, true);
00025 }
00026 
00027 
00028 IMUCalibrator::IMUCalibrator() {
00029         driftEstimationDone[0] = false;
00030         driftEstimationDone[1] = false;
00031         driftEstimationDone[2] = false;
00032 
00033         driftEstimationActive = false;
00034 
00035 }
00036 
00037 IMUCalibrator::~IMUCalibrator() {
00038         // TODO Auto-generated destructor stub
00039 }
00040 
00041 
00042 bool IMUCalibrator::driftEstimation(Eigen::Vector3d& gyroOffsets) {
00043         gyroSteadyValues = Eigen::Vector3d::Constant(GYRO_CENTER);
00044         gyroSteadyValues += gyroOffsets;
00045 
00046 
00047         // loop
00048         bool returnValue = false;
00049 
00050 
00051         driftEstimationTimer.reset();
00052         driftEstimationActive = true;
00053         Timer timeoutTimer;
00054         while(timeoutTimer.getElapsed().toDSec() < options.tDriftEstimTimeout->getValue()) {
00055 
00056                 if (driftEstimationDone[0] && driftEstimationDone[1] && driftEstimationDone[2]) {
00057                         // all done
00058                         ROS_INFO("Successfully finished driftEstimation!");
00059                         returnValue = true;
00060                         break;
00061                 }
00062 
00063 
00064                 usleep(1000);
00065         }
00066 
00067         if (returnValue) {
00068                 gyroOffsets = gyroSteadyValues - Eigen::Vector3d::Constant(GYRO_CENTER);
00069 //              ROS_INFO_STREAM("Calculated Gyro Offsets: " << std::endl << gyroOffsets);
00070 //              sleep(2);
00071         }
00072 
00073         return returnValue;
00074 }
00075 
00076 void IMUCalibrator::processRawIMUData(const RawImuData& data) {
00077         if (!driftEstimationActive) {
00078                 return;
00079         }
00080 
00081         double elapsedTime = driftEstimationTimer.getElapsed().toDSec();
00082         driftEstimationTimer.reset();
00083 
00084         // active
00085         gyroSteadyValues(0) += options.tDriftFilterGain->getValue()*((double)data.gyroRoll - gyroSteadyValues(0))*elapsedTime;
00086         gyroSteadyValues(1) += options.tDriftFilterGain->getValue()*((double)data.gyroPitch - gyroSteadyValues(1))*elapsedTime;
00087         gyroSteadyValues(2) += options.tDriftFilterGain->getValue()*((double)data.gyroYaw - gyroSteadyValues(2))*elapsedTime;
00088 
00089         if (!driftEstimationDone[0]) {
00090                 driftEstimAccRoll( gyroSteadyValues(0) );
00091         }
00092         if (!driftEstimationDone[1]) {
00093                 driftEstimAccPitch( gyroSteadyValues(1) );
00094         }
00095         if (!driftEstimationDone[2]) {
00096                 driftEstimAccYaw( gyroSteadyValues(2) );
00097         }
00098 
00099         if ( (signed)count(driftEstimAccRoll) == options.tDriftEstimWindowSize->getValue()) {
00100                 //ROS_INFO("Variance Roll: %f", variance(driftEstimAccRoll));
00101 
00102                 if (variance(driftEstimAccRoll) < options.tDriftEstimVarianceThreshold->getValue()) {
00103                         ROS_INFO("Drift Estimation done: Roll = %f", mean(driftEstimAccRoll));
00104                         driftEstimationDone[0] = true;
00105                 }
00106 
00107                 // reset
00108                 driftEstimAccRoll = accumulator_set<double, stats<tag::variance(lazy)> >();
00109         }
00110 
00111         if ( (signed)count(driftEstimAccPitch) == options.tDriftEstimWindowSize->getValue()) {
00112                 //ROS_INFO("Variance Pitch: %f", variance(driftEstimAccPitch));
00113 
00114                 if (variance(driftEstimAccPitch) < options.tDriftEstimVarianceThreshold->getValue()) {
00115                         ROS_INFO("Drift Estimation done: Pitch = %f", mean(driftEstimAccPitch));
00116                         driftEstimationDone[1] = true;
00117                 }
00118 
00119                 // reset
00120                 driftEstimAccPitch = accumulator_set<double, stats<tag::variance(lazy)> >();
00121         }
00122 
00123         if ( (signed)count(driftEstimAccYaw) == options.tDriftEstimWindowSize->getValue()) {
00124                 //ROS_INFO("Variance Yaw: %f", variance(driftEstimAccYaw));
00125 
00126                 if (variance(driftEstimAccYaw) < options.tDriftEstimVarianceThreshold->getValue()) {
00127                         ROS_INFO("Drift Estimation done: Yaw = %f", mean(driftEstimAccYaw));
00128                         driftEstimationDone[2] = true;
00129                 }
00130 
00131                 // reset
00132                 driftEstimAccYaw = accumulator_set<double, stats<tag::variance(lazy)> >();
00133         }
00134 
00135 
00136 
00137         //ROS_INFO_STREAM("Vector: " << std::endl << gyroSteadyValues);
00138 
00139 }
00140 
00141 } /* namespace telekyb */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


tk_smurf_interface
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:14:12