- 3.0.2 rigid body dynamics module.
JointState.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include <ct/core/core.h>
9 
10 namespace ct {
11 namespace rbd {
12 
20 template <size_t NJOINTS, typename SCALAR = double>
22 {
23 public:
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25 
26  enum NDOFS
27  {
28  NDOFS = NJOINTS
29  };
30 
31  typedef Eigen::Matrix<SCALAR, 2 * NJOINTS, 1> joint_state_vector_t;
32  typedef Eigen::Matrix<SCALAR, NJOINTS, 1> Position;
33  typedef Eigen::Matrix<SCALAR, NJOINTS, 1> Velocity;
34 
35  typedef Eigen::VectorBlock<joint_state_vector_t, NJOINTS> JointPositionBlock;
36  typedef Eigen::VectorBlock<const joint_state_vector_t, NJOINTS> JointPositionBlockConst;
37 
39  JointState(const joint_state_vector_t& state) : state_(state) {}
40  inline bool operator==(const JointState& rhs) const { return (this->state_ == rhs.state_); }
41  inline bool operator!=(const JointState<NJOINTS, SCALAR>& rhs) const { return (this->state_ != rhs.state_); }
43  {
44  return JointState<NJOINTS, SCALAR>(this->state_ + rhs.state_);
45  }
47  {
48  return JointState<NJOINTS, SCALAR>(this->state_ - rhs.state_);
49  }
50 
51  inline JointState<NJOINTS, SCALAR> operator*(const SCALAR& scalar) const
52  {
53  return JointState<NJOINTS, SCALAR>(this->state_ * scalar);
54  }
55  inline JointState<NJOINTS, SCALAR> operator/(const SCALAR& scalar) const
56  {
57  return JointState<NJOINTS, SCALAR>(this->state_ / scalar);
58  }
59 
60  inline bool operator<(const JointState<NJOINTS, SCALAR>& rhs) const
61  {
62  return (this->array() < rhs.array()).isConstant(1);
63  }
64 
65  inline bool operator>(const JointState<NJOINTS, SCALAR>& rhs) const
66  {
67  return (this->array() > rhs.array()).isConstant(1);
68  }
69 
70  bool isApprox(const JointState<NJOINTS, SCALAR>& rhs, const SCALAR& tol = 1e-10)
71  {
72  return state_.isApprox(rhs.state_, tol);
73  }
74 
76  JointPositionBlock getPositions() { return state_.template head<NJOINTS>(); }
77 
79  const JointPositionBlockConst getPositions() const { return state_.template head<NJOINTS>(); }
80 
81  SCALAR& getPosition(size_t i)
82  {
83  assert(i < NJOINTS && "Invalid joint index");
84  return state_(i);
85  }
86 
87  const SCALAR& getPosition(size_t i) const
88  {
89  assert(i < NJOINTS && "Invalid joint index");
90  return state_(i);
91  }
92 
94  template <typename T>
95  void toUniquePosition(T lowerLimitVec, double tolerance = 0.0)
96  {
97  assert(lowerLimitVec.size() == NJOINTS && "Wrong limit dimensions");
98  for (size_t i = 0; i < NJOINTS; ++i)
99  {
100  // compute the integer of multiple of 2*PI to substract
101  int k_lower = std::floor((getPosition(i) - (lowerLimitVec[i] - tolerance)) / (2 * M_PI));
102  int k_upper = std::floor((getPosition(i) - (lowerLimitVec[i] + tolerance)) / (2 * M_PI));
103 
104  if (std::abs(k_lower) <= std::abs(k_upper))
105  {
106  if (k_lower != 0)
107  getPosition(i) -= k_lower * 2 * M_PI;
108  }
109  else if (k_upper != 0)
110  getPosition(i) -= k_upper * 2 * M_PI;
111  }
112  }
113 
115  template <typename T>
116  bool checkPositionLimits(const T lowerLimit, const T upperLimit, const double tolerance = 0.0)
117  {
118  assert(lowerLimit.size() == NJOINTS && upperLimit.size() == NJOINTS && "Wrong limit dimensions");
119  for (size_t i = 0; i < NJOINTS; i++)
120  {
121  if ((std::abs(getPosition(i) - lowerLimit[i]) > tolerance && getPosition(i) < lowerLimit[i]) ||
122  (std::abs(getPosition(i) - upperLimit[i]) > tolerance && getPosition(i) > upperLimit[i]))
123  {
124  return false;
125  }
126  }
127  return true;
128  }
129 
131  JointPositionBlock getVelocities() { return state_.template tail<NJOINTS>(); }
132 
134  const JointPositionBlockConst getVelocities() const { return state_.template tail<NJOINTS>(); }
135 
136  SCALAR& getVelocity(size_t i)
137  {
138  assert(i < NJOINTS && "Invalid joint index");
139  return state_(NJOINTS + i);
140  }
141 
142  const SCALAR& getVelocity(size_t i) const
143  {
144  assert(i < NJOINTS && "Invalid joint index");
145  return state_(NJOINTS + i);
146  }
147 
149  template <typename T>
150  bool checkVelocityLimits(T limit)
151  {
152  assert(limit.size() == NJOINTS && "Wrong limit dimension");
153  for (size_t i = 0; i < NJOINTS; ++i)
154  if (std::abs(getVelocity(i)) > limit[i])
155  return false;
156  return true;
157  }
158 
159  joint_state_vector_t& toImplementation() { return state_; }
160 
161  const joint_state_vector_t& toImplementation() const { return state_; }
162 
164  void setZero() { state_.setZero(); }
165 
166  void setRandom() { state_.setRandom(); }
167 
168 protected:
169  joint_state_vector_t state_;
170 };
171 
172 } // namespace rbd
173 } // namespace ct
NDOFS
Definition: JointState.h:26
Eigen::VectorBlock< const joint_state_vector_t, NJOINTS > JointPositionBlockConst
Definition: JointState.h:36
JointState< NJOINTS, SCALAR > operator-(const JointState< NJOINTS, SCALAR > &rhs) const
Definition: JointState.h:46
const joint_state_vector_t & toImplementation() const
Definition: JointState.h:161
bool operator!=(const JointState< NJOINTS, SCALAR > &rhs) const
Definition: JointState.h:41
joint state and joint velocity
Definition: JointState.h:21
const SCALAR & getVelocity(size_t i) const
Definition: JointState.h:142
Eigen::VectorBlock< joint_state_vector_t, NJOINTS > JointPositionBlock
Definition: JointState.h:35
joint_state_vector_t state_
Definition: JointState.h:169
bool isApprox(const JointState< NJOINTS, SCALAR > &rhs, const SCALAR &tol=1e-10)
Definition: JointState.h:70
bool operator==(const JointState &rhs) const
Definition: JointState.h:40
CppAD::AD< CppAD::cg::CG< double > > SCALAR
bool checkVelocityLimits(T limit)
check joint velocity limits
Definition: JointState.h:150
Eigen::Matrix< SCALAR, NJOINTS, 1 > Position
Definition: JointState.h:32
JointPositionBlock getVelocities()
get joint velocity
Definition: JointState.h:131
Eigen::Matrix< SCALAR, NJOINTS, 1 > Velocity
Definition: JointState.h:33
JointState< NJOINTS, SCALAR > operator*(const SCALAR &scalar) const
Definition: JointState.h:51
for i
JointState(const joint_state_vector_t &state)
Definition: JointState.h:39
SCALAR & getVelocity(size_t i)
Definition: JointState.h:136
bool operator>(const JointState< NJOINTS, SCALAR > &rhs) const
Definition: JointState.h:65
void toUniquePosition(T lowerLimitVec, double tolerance=0.0)
normalize the joint state to be in the range [lowerLimitVec, lowerLimitVec + 2pi) ...
Definition: JointState.h:95
JointState()
Definition: JointState.h:38
const SCALAR & getPosition(size_t i) const
Definition: JointState.h:87
JointState< NJOINTS, SCALAR > operator/(const SCALAR &scalar) const
Definition: JointState.h:55
SCALAR & getPosition(size_t i)
Definition: JointState.h:81
JointState< NJOINTS, SCALAR > operator+(const JointState< NJOINTS, SCALAR > &rhs) const
Definition: JointState.h:42
joint_state_vector_t & toImplementation()
Definition: JointState.h:159
void setZero()
set states to zero
Definition: JointState.h:164
Eigen::Matrix< SCALAR, 2 *NJOINTS, 1 > joint_state_vector_t
Definition: JointState.h:31
const double tolerance
Definition: JointStateTest.cpp:15
JointPositionBlock getPositions()
get joint state
Definition: JointState.h:76
bool checkPositionLimits(const T lowerLimit, const T upperLimit, const double tolerance=0.0)
check joint position limits assuming limits and joint position are in the same range e...
Definition: JointState.h:116
const JointPositionBlockConst getVelocities() const
get constant joint velocity
Definition: JointState.h:134
const JointPositionBlockConst getPositions() const
get constant joint state
Definition: JointState.h:79
void setRandom()
Definition: JointState.h:166