state.cpp
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
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 Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00028 
00029 #include <hector_pose_estimation/state.h>
00030 #include <hector_pose_estimation/substate.h>
00031 
00032 namespace hector_pose_estimation {
00033 
00034 State::State()
00035   : vector_(Dimension)
00036   , covariance_(Dimension)
00037   , base_(new SubState_<0>(*this))
00038 {
00039   reset();
00040 }
00041 
00042 State::State(const Vector &vector, const Covariance& covariance)
00043   : vector_(vector)
00044   , covariance_(covariance)
00045   , base_(new SubState_<0>(*this))
00046 {
00047   reset();
00048 }
00049 
00050 State::~State()
00051 {
00052 }
00053 
00054 void State::reset()
00055 {
00056   // reset state
00057   vector_.setZero();
00058   covariance_.setZero();
00059   orientation().w() = 1.0;
00060 
00061   // reset status flags
00062   system_status_ = 0;
00063   measurement_status_ = 0;
00064 
00065   // reset pseudo-states
00066   fake_rate_.resize(3,1);
00067   fake_rate_ << 0.0, 0.0, 0.0;
00068   fake_orientation_.resize(4,1);
00069   fake_orientation_ << 0.0, 0.0, 0.0, 1.0;
00070   fake_position_.resize(3,1);
00071   fake_position_ << 0.0, 0.0, 0.0;
00072   fake_velocity_.resize(3,1);
00073   fake_velocity_ << 0.0, 0.0, 0.0;
00074   fake_acceleration_.resize(3,1);
00075   fake_acceleration_ << 0.0, 0.0, 0.0;
00076 }
00077 
00078 bool State::valid() const {
00079   return (vector_ == vector_);
00080 }
00081 
00082 void State::updated()
00083 {
00084   normalize();
00085   P().symmetric();
00086 }
00087 
00088 State::RotationMatrix State::getRotationMatrix() const {
00089   RotationMatrix R;
00090   getRotationMatrix(R);
00091   return R;
00092 }
00093 
00094 void State::getRotationMatrix(RotationMatrix &R) const
00095 {
00096   ConstOrientationType q(getOrientation());
00097   R << (q.w()*q.w()+q.x()*q.x()-q.y()*q.y()-q.z()*q.z()), (2.0*q.x()*q.y()-2.0*q.w()*q.z()),                 (2.0*q.x()*q.z()+2.0*q.w()*q.y()),
00098        (2.0*q.x()*q.y()+2.0*q.w()*q.z()),                 (q.w()*q.w()-q.x()*q.x()+q.y()*q.y()-q.z()*q.z()), (2.0*q.y()*q.z()-2.0*q.w()*q.x()),
00099        (2.0*q.x()*q.z()-2.0*q.w()*q.y()),                 (2.0*q.y()*q.z()+2.0*q.w()*q.x()),                 (q.w()*q.w()-q.x()*q.x()-q.y()*q.y()+q.z()*q.z());
00100 }
00101 
00102 double State::getYaw() const
00103 {
00104   ConstOrientationType q(getOrientation());
00105   return atan2(2*q.x()*q.y() + 2*q.w()*q.z(), q.x()*q.x() + q.w()*q.w() - q.z()*q.z() - q.y()*q.y());
00106 }
00107 
00108 bool State::inSystemStatus(SystemStatus test_status) const {
00109   return (getSystemStatus() & test_status) == test_status;
00110 }
00111 
00112 bool State::setSystemStatus(SystemStatus new_status) {
00113   if (new_status == system_status_) return true;
00114 
00115   // iterate through StatusCallbacks
00116   for(std::vector<SystemStatusCallback>::const_iterator it = status_callbacks_.begin(); it != status_callbacks_.end(); ++it)
00117     if (!(*it)(new_status)) return false;
00118 
00119   SystemStatus set = new_status & ~system_status_;
00120   SystemStatus cleared = system_status_ & ~new_status;
00121   if (set)     ROS_INFO_STREAM("Set system status " << getSystemStatusString(new_status, set));
00122   if (cleared) ROS_INFO_STREAM("Cleared system status " << getSystemStatusString(cleared, cleared));
00123 
00124   system_status_ = new_status;
00125   return true;
00126 }
00127 
00128 bool State::setMeasurementStatus(SystemStatus new_measurement_status) {
00129   SystemStatus set = new_measurement_status & ~measurement_status_;
00130   SystemStatus cleared = measurement_status_ & ~new_measurement_status;
00131   if (set)     ROS_INFO_STREAM("Set measurement status " << getSystemStatusString(new_measurement_status, set));
00132   if (cleared) ROS_INFO_STREAM("Cleared measurement status " << getSystemStatusString(cleared, cleared));
00133 
00134   measurement_status_ = new_measurement_status;
00135   return true;
00136 }
00137 
00138 bool State::updateSystemStatus(SystemStatus set, SystemStatus clear) {
00139   return setSystemStatus((system_status_ & ~clear) | set);
00140 }
00141 
00142 bool State::updateMeasurementStatus(SystemStatus set, SystemStatus clear) {
00143   return setMeasurementStatus((measurement_status_ & ~clear) | set);
00144 }
00145 
00146 void State::addSystemStatusCallback(const SystemStatusCallback& callback) {
00147 //  for(std::vector<SystemStatusCallback>::const_iterator it = status_callbacks_.begin(); it != status_callbacks_.end(); ++it)
00148 //    if (*it == callback) return;
00149   status_callbacks_.push_back(callback);
00150 }
00151 
00152 double State::normalize() {
00153   double s = 1.0 / orientation().norm();
00154   orientation() = orientation() * s;
00155   return s;
00156 }
00157 
00158 template class SubState_<0>;
00159 
00160 } // namespace hector_pose_estimation


hector_pose_estimation_core
Author(s): Johannes Meyer
autogenerated on Mon Oct 6 2014 00:24:16