ethzasl-msf - Modular Sensor Fusion
Time delay compensated single and multi sensor fusion framework based on an EKF
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
eigen_utils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011-2012 Stephan Weiss, ASL, ETH Zurich, Switzerland
3  * You can contact the author at <stephan dot weiss at ieee dot org>
4  * Copyright (C) 2012-2013 Simon Lynen, ASL, ETH Zurich, Switzerland
5  * You can contact the author at <slynen at ethz dot ch>
6  * Copyright (c) 2012, Markus Achtelik, ASL, ETH Zurich, Switzerland
7  * You can contact the author at <acmarkus at ethz dot ch>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 #ifndef EIGEN_UTILS_H_
22 #define EIGEN_UTILS_H_
23 
24 #include <Eigen/Dense>
25 #include <Eigen/Geometry>
26 #include <iostream>
27 
29 template<class Derived>
30 inline Eigen::Matrix<typename Derived::Scalar, 3, 3> skew(
31  const Eigen::MatrixBase<Derived> & vec) {
32  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
33  return (Eigen::Matrix<typename Derived::Scalar, 3, 3>() << 0.0, -vec[2], vec[1],
34  vec[2], 0.0, -vec[0], -vec[1], vec[0], 0.0).finished();
35 }
36 
38 // integration with the JPL notation.
44 template<class Derived>
45 inline Eigen::Matrix<typename Derived::Scalar, 4, 4> omegaMatJPL(
46  const Eigen::MatrixBase<Derived> & vec) {
47  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
48  return (Eigen::Matrix<typename Derived::Scalar, 4, 4>() << 0, vec[2], -vec[1],
49  vec[0], -vec[2], 0, vec[0], vec[1], vec[1], -vec[0], 0, vec[2], -vec[0],
50  -vec[1], -vec[2], 0)
51  .finished();
52 }
53 
55 // integration with the Hamilton notation.
61 template<class Derived>
62 inline Eigen::Matrix<typename Derived::Scalar, 4, 4> omegaMatHamilton(
63  const Eigen::MatrixBase<Derived> & vec) {
64  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
65  return (Eigen::Matrix<typename Derived::Scalar, 4, 4>() << 0, -vec[2], vec[1],
66  vec[0], vec[2], 0, -vec[0], vec[1], -vec[1], vec[0], 0, vec[2], -vec[0],
67  -vec[1], -vec[2], 0).finished();
68 }
69 
71 
79 template<class Derived>
80 inline Eigen::Matrix<typename Derived::Scalar, 4, 3> xiMat(
81  const Eigen::MatrixBase<Derived> & q_vec) {
82  EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Derived, 4, 1);
83 
84  return (Eigen::Matrix<typename Derived::Scalar, 4, 3>() <<
85  // This is the Xi matrix ---
86  q_vec[3] * Eigen::Matrix<typename Derived::Scalar, 3, 3>::Identity()
87  + skew(q_vec.template head<3>()), -q_vec.template head<3>().transpose()
88  // ---
89  ).finished();
90  }
91 
93 template<class Derived>
94 Eigen::Quaternion<typename Derived::Scalar> quaternionFromSmallAngle(
95  const Eigen::MatrixBase<Derived> & theta) {
96  typedef typename Derived::Scalar Scalar;
97  EIGEN_STATIC_ASSERT_FIXED_SIZE (Derived);
98  EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived, 3);
99  const Scalar q_squared = theta.squaredNorm() / 4.0;
100 
101  if (q_squared < 1) {
102  return Eigen::Quaternion < Scalar
103  > (sqrt(1 - q_squared), theta[0] * 0.5, theta[1] * 0.5, theta[2] * 0.5);
104  } else {
105  const Scalar w = 1.0 / sqrt(1 + q_squared);
106  const Scalar f = w * 0.5;
107  return Eigen::Quaternion < Scalar
108  > (w, theta[0] * f, theta[1] * f, theta[2] * f);
109  }
110 }
111 
113 template<class D>
114 bool checkForNumeric(const Eigen::MatrixBase<D> & mat,
115  const std::string & info) {
116  enum {
117  rows = Eigen::MatrixBase < D > ::RowsAtCompileTime,
118  cols = Eigen::MatrixBase < D > ::ColsAtCompileTime
119  };
120 
121  for (int i = 0; i < rows; ++i) {
122  for (int j = 0; j < cols; ++j) {
123  if (std::isnan(mat(i, j))) {
124  std::cerr << "=== ERROR === " << info << ": NAN at index [" << i << ","
125  << j << "]" << std::endl;
126  return false;
127  }
128  if (std::isinf(mat(i, j))) {
129  std::cerr << "=== ERROR === " << info << ": INF at index [" << i << ","
130  << j << "]" << std::endl;
131  return false;
132  }
133  }
134  }
135  return true;
136 }
137 
138 #endif // EIGEN_UTILS_H_