state.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 Copyright (c) 2017, Alexander W. Winkler. All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 
00010 * Redistributions in binary form must reproduce the above copyright notice,
00011   this list of conditions and the following disclaimer in the documentation
00012   and/or other materials provided with the distribution.
00013 
00014 * Neither the name of the copyright holder nor the names of its
00015   contributors may be used to endorse or promote products derived from
00016   this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00021 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00022 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00024 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00025 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 ******************************************************************************/
00029 
00030 #ifndef _XPP_STATES_STATE_H_
00031 #define _XPP_STATES_STATE_H_
00032 
00033 #include <iostream>
00034 #include <Eigen/Dense>
00035 
00036 #include <xpp_states/cartesian_declarations.h>
00037 
00038 namespace xpp {
00039 
00040 class StateLin1d;
00041 class StateLin2d;
00042 class StateLin3d;
00043 class StateAng3d;
00044 class State3d;
00045 class State3dEuler;
00046 
00047 using Vector2d    = Eigen::Vector2d;
00048 using Vector3d    = Eigen::Vector3d;
00049 using Vector6d    = Eigen::Matrix<double,6,1>;
00050 using VectorXd    = Eigen::VectorXd;
00051 using Quaterniond = Eigen::Quaterniond;
00052 
00053 enum MotionDerivative { kPos=0, kVel, kAcc, kJerk };
00054 
00058 class StateLinXd {
00059 public:
00060   VectorXd p_, v_, a_; 
00061 
00068   explicit StateLinXd(int _dim = 0);
00069 
00078   explicit StateLinXd(const VectorXd& p, const VectorXd& v, const VectorXd& a);
00079 
00083   StateLinXd(const VectorXd& p);
00084   virtual ~StateLinXd() = default;
00085 
00091   const VectorXd GetByIndex(MotionDerivative deriv) const;
00092 
00098   VectorXd& GetByIndex(MotionDerivative deriv);
00099 
00103   bool operator==(const StateLinXd& other) const;
00104 
00108   bool operator!=(const StateLinXd& other) const;
00109 
00110   int kNumDim = 0;  
00111 };
00112 
00113 
00114 // some state classes of explicit dimensions for type safety as well as
00115 // conversions from the general base class.
00116 class StateLin1d  : public StateLinXd {
00117 public:
00118   StateLin1d() : StateLinXd(1) {};
00119   virtual ~StateLin1d() {};
00120 };
00121 
00122 class StateLin2d  : public StateLinXd {
00123 public:
00124   StateLin2d() : StateLinXd(2) {};
00125   virtual ~StateLin2d() {};
00126 };
00127 
00128 class StateLin3d : public StateLinXd {
00129 public:
00130   StateLin3d() : StateLinXd(3) {};
00131   StateLin3d(const StateLinXd&);
00132   virtual ~StateLin3d() {};
00133 
00137   StateLin2d Get2D() const;
00138 };
00139 
00140 
00147 class StateAng3d {
00148 public:
00149   Quaterniond q;  
00150   Vector3d    w;  
00151   Vector3d   wd;  
00152 
00163   explicit StateAng3d(Quaterniond _q = Quaterniond(1.0, 0.0, 0.0, 0.0),
00164                       Vector3d   _w  = Vector3d::Zero(),
00165                       Vector3d   _wd = Vector3d::Zero())
00166   : q(_q), w(_w), wd(_wd) {}
00167 };
00168 
00169 
00174 class State3d {
00175 public:
00176   StateLin3d lin; 
00177   StateAng3d ang; 
00178 
00179   Vector6d Get6dVel() const;
00180   Vector6d Get6dAcc() const;
00181 };
00182 
00187 class State3dEuler {
00188 public:
00189   StateLin3d lin; 
00190   StateLin3d ang; 
00191 };
00192 
00193 
00194 
00195 // Conversions between Euler angles and Quaternions
00206 static Vector3d GetEulerZYXAngles(const Quaterniond& q)
00207 {
00208   Vector3d yaw_pitch_roll = q.normalized().toRotationMatrix().eulerAngles(Z, Y, X);
00209   return yaw_pitch_roll.reverse();
00210 }
00211 
00219 static Quaterniond GetQuaternionFromEulerZYX(double yaw, double pitch, double roll)
00220 {
00221   using namespace Eigen;
00222   Quaterniond q;
00223 
00224   q =   AngleAxisd(yaw,   Vector3d::UnitZ())
00225       * AngleAxisd(pitch, Vector3d::UnitY())
00226       * AngleAxisd(roll,  Vector3d::UnitX());
00227 
00228   return q;
00229 }
00230 
00231 
00232 
00233 // convenience functions for easy readability
00234 inline std::ostream& operator<<(std::ostream& out, const StateLinXd& pos)
00235 {
00236   out << "p=" << pos.p_.transpose() << "  "
00237       << "v=" << pos.v_.transpose() << "  "
00238       << "a=" << pos.a_.transpose();
00239   return out;
00240 }
00241 
00242 inline StateLinXd operator+(const StateLinXd& lhs, const StateLinXd& rhs)
00243 {
00244   StateLinXd ret(lhs.kNumDim);
00245   ret.p_ = lhs.p_ + rhs.p_;
00246   ret.v_ = lhs.v_ + rhs.v_;
00247   ret.a_ = lhs.a_ + rhs.a_;
00248   return ret;
00249 }
00250 
00251 inline StateLinXd operator*(double mult, const StateLinXd& rhs)
00252 {
00253   StateLinXd ret(rhs.kNumDim);
00254   ret.p_ = mult * rhs.p_;
00255   ret.v_ = mult * rhs.v_;
00256   ret.a_ = mult * rhs.a_;
00257   return ret;
00258 }
00259 
00260 inline bool StateLinXd::operator==(const StateLinXd &other) const
00261 {
00262   bool all_equal = (p_==other.p_
00263                  && v_==other.v_
00264                  && a_==other.a_);
00265   return all_equal;
00266 }
00267 
00268 inline bool StateLinXd::operator!=(const StateLinXd &other) const
00269 {
00270   return !(*this == other);
00271 }
00272 
00273 inline std::ostream& operator<<(std::ostream& out, const StateAng3d& ori)
00274 {
00275   Vector3d rpy_rad;
00276   rpy_rad = GetEulerZYXAngles(ori.q);
00277   out << "rpy=" << rpy_rad.transpose() << "  "
00278       << "v="   << ori.w.transpose() << "  "
00279       << "a="   << ori.wd.transpose();
00280   return out;
00281 }
00282 
00283 inline std::ostream& operator<<(std::ostream& out, const State3d& pose)
00284 {
00285   out << "\tPos: " << pose.lin << "\n"
00286       << "\tOri: " << pose.ang;
00287   return out;
00288 }
00289 
00290 } // namespace xpp
00291 
00292 #endif // _XPP_STATES_STATE_H_


xpp_states
Author(s): Alexander W. Winkler
autogenerated on Fri Apr 5 2019 02:55:50