momentumopt
KinematicsState.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <array>
12 #include <vector>
13 #include <memory>
14 #include <iostream>
15 #include <Eigen/Eigen>
16 
18 
19 namespace momentumopt {
20 
23  {
24  public:
25  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
26 
27  public:
28  RobotPosture(int num_joints)
29  : num_joints_(num_joints),
30  base_position_(Eigen::Vector3d::Zero()),
31  joint_positions_(Eigen::VectorXd(num_joints).setZero()),
32  base_orientation_(Eigen::Quaternion<double>::Identity())
33  {}
34  ~RobotPosture(){}
35 
36  Eigen::Vector3d& basePosition() { return base_position_; }
37  Eigen::VectorXd& jointPositions() { return joint_positions_; }
38  Eigen::Quaternion<double>& baseOrientation() { return base_orientation_; }
39 
40  const Eigen::Vector3d& basePosition() const { return base_position_; }
41  const Eigen::VectorXd& jointPositions() const { return joint_positions_; }
42  const Eigen::Quaternion<double>& baseOrientation() const { return base_orientation_; }
43  const Eigen::Vector4d pyBaseOrientation() const { return Eigen::Vector4d(base_orientation_.x(), base_orientation_.y(), base_orientation_.z(), base_orientation_.w()); }
44 
45  void basePosition(const Eigen::Vector3d& base_position) { base_position_ = base_position; }
46  void jointPositions(const Eigen::VectorXd& joint_positions) { joint_positions_ = joint_positions; }
47  void baseOrientation(const Eigen::Quaternion<double>& base_orientation) { base_orientation_ = base_orientation; }
48  void pyBaseOrientation(const Eigen::Vector4d base_orientation) { base_orientation_ = Eigen::Quaternion<double>(base_orientation[3], base_orientation[0], base_orientation[1], base_orientation[2]); }
49 
50  Eigen::VectorXd generalizedJointPositions() const
51  {
52  Eigen::VectorXd generalized_joint_positions(num_joints_+7);
53  generalized_joint_positions.head(3) = base_position_;
54  generalized_joint_positions.tail(num_joints_) = joint_positions_;
55  generalized_joint_positions.segment<4>(3) = Eigen::Vector4d(base_orientation_.x(), base_orientation_.y(), base_orientation_.z(), base_orientation_.w());
56  return generalized_joint_positions;
57  }
58 
59  std::string toString() const;
60  friend std::ostream& operator<<(std::ostream &os, const RobotPosture& obj) { return os << obj.toString(); }
61 
62  private:
63  int num_joints_;
64  Eigen::Vector3d base_position_;
65  Eigen::VectorXd joint_positions_;
66  Eigen::Quaternion<double> base_orientation_;
67  };
68 
71  {
72  public:
73  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
74 
75  public:
76  RobotVelocity(int num_joints)
77  : num_joints_(num_joints),
78  generalized_joint_velocities_(Eigen::VectorXd(num_joints+6).setZero())
79  {}
80  ~RobotVelocity(){}
81 
82  Eigen::VectorXd& generalizedJointVelocities() { return generalized_joint_velocities_; }
83  Eigen::Ref<Eigen::Vector3d> baseLinearVelocity() { return generalized_joint_velocities_.segment<3>(0); }
84  Eigen::Ref<Eigen::Vector3d> baseAngularVelocity() { return generalized_joint_velocities_.segment<3>(3); }
85  Eigen::Ref<Eigen::VectorXd> jointVelocities() { return generalized_joint_velocities_.tail(num_joints_); }
86 
87  const Eigen::VectorXd& generalizedJointVelocities() const { return generalized_joint_velocities_; }
88  const Eigen::Ref<const Eigen::Vector3d> baseLinearVelocity() const { return generalized_joint_velocities_.segment<3>(0); }
89  const Eigen::Ref<const Eigen::Vector3d> baseAngularVelocity() const { return generalized_joint_velocities_.segment<3>(3); }
90  const Eigen::Ref<const Eigen::VectorXd> jointVelocities() const { return generalized_joint_velocities_.tail(num_joints_); }
91 
92  void generalizedJointVelocities(const Eigen::VectorXd& generalized_joint_velocities) { generalized_joint_velocities_ = generalized_joint_velocities; }
93  void jointVelocities(const Eigen::Ref<const Eigen::VectorXd> joint_velocities) { generalized_joint_velocities_.tail(num_joints_) = joint_velocities; }
94  void baseLinearVelocity(const Eigen::Ref< const Eigen::Vector3d> base_linear_velocity) { generalized_joint_velocities_.segment<3>(0) = base_linear_velocity; }
95  void baseAngularVelocity(const Eigen::Ref<const Eigen::Vector3d> base_angular_velocity) { generalized_joint_velocities_.segment<3>(3) = base_angular_velocity; }
96 
97  std::string toString() const;
98  friend std::ostream& operator<<(std::ostream &os, const RobotVelocity& obj) { return os << obj.toString(); }
99 
100  private:
101  int num_joints_;
102  Eigen::VectorXd generalized_joint_velocities_;
103  };
104 
107  {
108  public:
109  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
110 
111  public:
112  RobotAcceleration(int num_joints)
113  : num_joints_(num_joints),
114  generalized_joint_accelerations_(Eigen::VectorXd(num_joints+6).setZero())
115  {}
116  ~RobotAcceleration(){}
117 
118  Eigen::VectorXd& generalizedJointAccelerations() { return generalized_joint_accelerations_; }
119  Eigen::Ref<Eigen::Vector3d> baseLinearAcceleration() { return generalized_joint_accelerations_.segment<3>(0); }
120  Eigen::Ref<Eigen::Vector3d> baseAngularAcceleration() { return generalized_joint_accelerations_.segment<3>(3); }
121  Eigen::Ref<Eigen::VectorXd> jointAccelerations() { return generalized_joint_accelerations_.tail(num_joints_); }
122 
123  const Eigen::VectorXd& generalizedJointAccelerations() const { return generalized_joint_accelerations_; }
124  const Eigen::Ref<const Eigen::Vector3d> baseLinearAcceleration() const { return generalized_joint_accelerations_.segment<3>(0); }
125  const Eigen::Ref<const Eigen::Vector3d> baseAngularAcceleration() const { return generalized_joint_accelerations_.segment<3>(3); }
126  const Eigen::Ref<const Eigen::VectorXd> jointAccelerations() const { return generalized_joint_accelerations_.tail(num_joints_); }
127 
128  void generalizedJointAccelerations(const Eigen::VectorXd& generalized_joint_accelerations) { generalized_joint_accelerations_ = generalized_joint_accelerations; }
129  void jointAccelerations(const Eigen::Ref<const Eigen::VectorXd> joint_accelerations) { generalized_joint_accelerations_.tail(num_joints_) = joint_accelerations; }
130  void baseLinearAcceleration(const Eigen::Ref< const Eigen::Vector3d> base_linear_acceleration) { generalized_joint_accelerations_.segment<3>(0) = base_linear_acceleration; }
131  void baseAngularAcceleration(const Eigen::Ref<const Eigen::Vector3d> base_angular_acceleration) { generalized_joint_accelerations_.segment<3>(3) = base_angular_acceleration; }
132 
133  std::string toString() const;
134  friend std::ostream& operator<<(std::ostream &os, const RobotAcceleration& obj) { return os << obj.toString(); }
135 
136  private:
137  int num_joints_;
138  Eigen::VectorXd generalized_joint_accelerations_;
139  };
140 
147  {
148  public:
149  KinematicsState(int num_joints);
150  ~KinematicsState(){}
151 
152  // center of mass and momentum
153  Eigen::Vector3d& centerOfMass() { return com_; }
154  Eigen::Vector3d& linearMomentum() { return lmom_; }
155  Eigen::Vector3d& angularMomentum() { return amom_; }
156 
157  const Eigen::Vector3d& centerOfMass() const { return com_; }
158  const Eigen::Vector3d& linearMomentum() const { return lmom_; }
159  const Eigen::Vector3d& angularMomentum() const { return amom_; }
160 
161  void centerOfMass(const Eigen::Vector3d& com) { com_ = com; }
162  void linearMomentum(const Eigen::Vector3d& lmom) { lmom_ = lmom; }
163  void angularMomentum(const Eigen::Vector3d& amom) { amom_ = amom; }
164 
165  // robot joint variables
166  RobotPosture& robotPosture() { return robot_posture_; }
167  RobotVelocity& robotVelocity() { return robot_velocity_; }
168  RobotAcceleration& robotAcceleration() { return robot_acceleration_; }
169 
170  const RobotPosture& robotPosture() const { return robot_posture_; }
171  const RobotVelocity& robotVelocity() const { return robot_velocity_; }
172  const RobotAcceleration& robotAcceleration() const { return robot_acceleration_; }
173 
174  void robotPosture(const RobotPosture& robot_posture) { robot_posture_ = robot_posture; }
175  void robotVelocity(const RobotVelocity& robot_velocity) { robot_velocity_ = robot_velocity; }
176  void robotAcceleration(const RobotAcceleration& robot_acceleration) { robot_acceleration_ = robot_acceleration; }
177 
178  // endeffector poses
179  Eigen::Vector3d& endeffectorPosition(int eff_id) { return eff_positions_[eff_id]; }
180  Eigen::Vector3d& endeffectorVelocity(int eff_id) { return eff_velocities_[eff_id]; }
181  Eigen::Vector3d& endeffectorAcceleration(int eff_id) { return eff_accelerations_[eff_id]; }
182  Eigen::Quaternion<double>& endeffectorOrientation(int eff_id) { return eff_orientations_[eff_id]; }
183 
184  const Eigen::Vector3d& endeffectorPosition(int eff_id) const { return eff_positions_[eff_id]; }
185  const Eigen::Vector3d& endeffectorVelocity(int eff_id) const { return eff_velocities_[eff_id]; }
186  const Eigen::Vector3d& endeffectorAcceleration(int eff_id) const { return eff_accelerations_[eff_id]; }
187  const Eigen::Quaternion<double>& endeffectorOrientation(int eff_id) const { return eff_orientations_[eff_id]; }
188 
189  const std::vector<Eigen::Vector3d>& pyEndeffectorPositions() const { return eff_positions_; }
190  void pyEndeffectorPositions(const std::vector<Eigen::Vector3d>& eff_positions) { eff_positions_ = eff_positions; }
191 
192  // Helper functions
193  std::string toString() const;
194  const int numJoints() const { return num_joints_; }
195  friend std::ostream& operator<<(std::ostream &os, const KinematicsState& obj) { return os << obj.toString(); }
196 
197  private:
198  RobotPosture robot_posture_;
199  RobotVelocity robot_velocity_;
200  RobotAcceleration robot_acceleration_;
201 
202  int num_joints_;
203  Eigen::Vector3d com_, lmom_, amom_;
204  std::vector<Eigen::Quaternion<double>> eff_orientations_;
205  std::vector<Eigen::Vector3d> eff_positions_, eff_velocities_, eff_accelerations_;
206  };
207 
212  {
213  public:
214  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
215 
216  public:
218  ~KinematicsSequence(){}
219 
220  void clean() { kinematics_sequence_.clear(); }
221  void resize(int num_timesteps, int num_joints);
222  int size() const { return kinematics_sequence_.size(); }
223 
224  const std::vector<KinematicsState>& kinematicsSequence() const { return kinematics_sequence_; }
225  void kinematicsSequence(const std::vector<KinematicsState>& kinematics_sequence) { kinematics_sequence_ = kinematics_sequence; }
226 
227  KinematicsState& kinematicsState(int time_id) { return kinematics_sequence_[time_id]; }
228  const KinematicsState& kinematicsState(int time_id) const { return kinematics_sequence_[time_id]; }
229 
230  std::string toString() const;
231  friend std::ostream& operator<<(std::ostream &os, const KinematicsSequence& obj) { return os << obj.toString(); }
232 
233  private:
234  std::vector<KinematicsState> kinematics_sequence_;
235  };
236 
237 }
Definition: KinematicsState.hpp:106
This class is a container for a sequence of kinematic states.
Definition: KinematicsState.hpp:211
Definition: KinematicsState.hpp:22
Definition: KinematicsState.hpp:70
This class is a container for all variables required to define a kinematics state: joint posture...
Definition: KinematicsState.hpp:146