1#include "state_representation/space/Jacobian.hpp"
3#include "state_representation/exceptions/EmptyStateException.hpp"
4#include "state_representation/exceptions/IncompatibleStatesException.hpp"
12 unsigned int nb_joints,
13 const std::string& frame,
14 const std::string& reference_frame) :
16 joint_names_(nb_joints),
18 reference_frame_(reference_frame),
26 const std::vector<std::string>& joint_names,
27 const std::string& frame,
28 const std::string& reference_frame) :
30 joint_names_(joint_names),
32 reference_frame_(reference_frame),
34 cols_(joint_names.size()) {
39 const std::string& frame,
40 const Eigen::MatrixXd& data,
41 const std::string& reference_frame) :
42 Jacobian(robot_name, data.cols(), frame, reference_frame) {
47 const std::vector<std::string>& joint_names,
48 const std::string& frame,
49 const Eigen::MatrixXd& data,
50 const std::string& reference_frame) :
51 Jacobian(robot_name, joint_names, frame, reference_frame) {
57 joint_names_(jacobian.joint_names_),
58 frame_(jacobian.frame_),
59 reference_frame_(jacobian.reference_frame_),
60 rows_(jacobian.rows_),
61 cols_(jacobian.cols()),
62 data_(jacobian.data_) {}
66 this->data_.resize(this->rows_, this->
cols());
67 this->data_.setZero();
71 unsigned int nb_joints,
72 const std::string& frame,
73 const std::string& reference_frame) {
74 Jacobian random(robot_name, nb_joints, frame, reference_frame);
75 random.
set_data(Eigen::MatrixXd::Random(random.rows_, random.cols_));
80 const std::vector<std::string>& joint_names,
81 const std::string& frame,
82 const std::string& reference_frame) {
83 Jacobian random(robot_name, joint_names, frame, reference_frame);
84 random.
set_data(Eigen::MatrixXd::Random(random.rows_, random.cols_));
89 bool compatible =
false;
90 switch (state.get_type()) {
91 case StateType::JACOBIAN:
96 for (
unsigned int i = 0; i < this->cols_; ++i) {
97 compatible = (compatible && this->joint_names_[i] ==
dynamic_cast<const Jacobian&
>(state).
get_joint_names()[i]);
104 case StateType::JOINT_STATE:
105 case StateType::JOINT_POSITIONS:
106 case StateType::JOINT_VELOCITIES:
107 case StateType::JOINT_ACCELERATIONS:
108 case StateType::JOINT_TORQUES:
113 for (
unsigned int i = 0; i < this->cols_; ++i) {
114 compatible = (compatible && this->joint_names_[i] ==
dynamic_cast<const JointState&
>(state).get_names()[i]);
118 case StateType::CARTESIAN_STATE:
119 case StateType::CARTESIAN_POSE:
120 case StateType::CARTESIAN_TWIST:
121 case StateType::CARTESIAN_ACCELERATION:
122 case StateType::CARTESIAN_WRENCH:
134 *
this = reference_frame * (*this);
139 result.cols_ = this->rows_;
140 result.rows_ = this->cols_;
146 if (this->rows_ != this->cols_) {
148 "The Jacobian matrix is not invertible, use the pseudoinverse function instead");
151 result.cols_ = this->cols_;
152 result.rows_ = this->rows_;
159 Eigen::MatrixXd pinv = this->
data().completeOrthogonalDecomposition().pseudoInverse();
160 result.cols_ = pinv.cols();
161 result.rows_ = pinv.rows();
170 if (matrix.rows() != this->cols_) {
172 + std::to_string(this->cols_) +
" rows, got " + std::to_string(matrix.rows()));
174 return this->
data() * matrix;
182 return (*
this) * jacobian.
data();
195 Eigen::Matrix<double, 6, 1> twist = (*this) * dq.
data();
196 CartesianTwist result(this->frame_, twist, this->reference_frame_);
210 Eigen::VectorXd joint_velocities = (*this) * twist.
data();
225 Eigen::VectorXd joint_torques = (*this) * wrench.
data();
234 if (this->rows_ != matrix.rows()) {
236 + std::to_string(this->rows_) +
" rows, got " + std::to_string(matrix.rows()));
238 return this->
data().colPivHouseholderQr().solve(matrix);
252 Eigen::VectorXd joint_velocities = this->
solve(twist.
data());
265 os <<
"Empty Jacobian";
267 os << jacobian.
get_name() <<
" Jacobian associated to " << jacobian.frame_;
268 os <<
", expressed in " << jacobian.reference_frame_ << std::endl;
269 os <<
"joint names: [";
271 os <<
"]" << std::endl;
272 os <<
"number of rows: " << jacobian.rows_ << std::endl;
273 os <<
"number of columns: " << jacobian.cols_ << std::endl;
274 for (
unsigned int i = 0; i < jacobian.rows_; ++i) {
275 os <<
"| " << jacobian(i, 0);
276 for (
unsigned int j = 1; j < jacobian.cols_; ++j) {
277 os <<
", " << jacobian(i, j);
280 if (i != jacobian.rows_ - 1) {
301 if (jacobian.rows_ != 6) {
303 "The Jacobian and the input CartesianPose are incompatible, the Jacobian has probably been transposed before");
307 for (
unsigned int i = 0; i < jacobian.cols_; ++i) {
309 result.data_.col(i).head(3) = pose.
get_orientation() * jacobian.data_.col(i).head(3);
311 result.data_.col(i).tail(3) = pose.
get_orientation() * jacobian.data_.col(i).tail(3);
323 if (matrix.cols() != jacobian.
rows()) {
326 return matrix * jacobian.
data();
Class to define CartesianPose in cartesian space as 3D position and quaternion based orientation.
Class to represent a state in Cartesian space.
const Eigen::Quaterniond & get_orientation() const
Getter of the orientation attribute.
Class to define twist in cartesian space as 3D linear and angular velocity vectors.
Eigen::VectorXd data() const override
Returns the twist data as an Eigen vector.
Class to define wrench in cartesian space as 3D force and torque vectors.
Eigen::VectorXd data() const override
Returns the wrench data as an Eigen vector.
Class to define a robot Jacobian matrix.
Jacobian copy() const
Return a copy of the JointPositions.
const std::vector< std::string > & get_joint_names() const
Getter of the joint_names attribute.
const Eigen::MatrixXd & data() const
Getter of the data attribute.
Jacobian pseudoinverse() const
Return the pseudoinverse of the Jacobian matrix.
unsigned int cols() const
Getter of the number of columns attribute.
Eigen::MatrixXd solve(const Eigen::MatrixXd &matrix) const
Solve the system X = inv(J)*M to obtain X which is more efficient than multiplying with the pseudo-in...
const std::string & get_reference_frame() const
Getter of the reference_frame attribute.
unsigned int rows() const
Getter of the number of rows attribute.
void set_reference_frame(const CartesianPose &reference_frame)
Setter of the reference_frame attribute from a CartesianPose Update the value of the data matrix acco...
const std::string & get_frame() const
Getter of the frame attribute.
friend Jacobian operator*(const CartesianPose &pose, const Jacobian &jacobian)
Overload the * operator with a CartesianPose on left side. This is equivalent to a changing of refere...
Jacobian inverse() const
Return the inverse of the Jacobian matrix If the matrix is not invertible, an error is thrown advisin...
void set_joint_names(unsigned int nb_joints)
Setter of the joint_names attribute from the number of joints.
void set_data(const Eigen::MatrixXd &data) override
Setter of the data attribute.
bool is_compatible(const State &state) const override
Check if the Jacobian matrix is compatible for operations with the state given as argument.
Jacobian()
Empty constructor for a Jacobian.
Jacobian transpose() const
Return the transpose of the Jacobian matrix.
void initialize() override
Initialize the matrix to a zero value.
static Jacobian Random(const std::string &robot_name, unsigned int nb_joints, const std::string &frame, const std::string &reference_frame="world")
Constructor for a random Jacobian.
Class to define a state in joint space.
unsigned int get_size() const
Getter of the size from the attributes.
Class to define torques of the joints.
Class to define velocities of the joints.
Eigen::VectorXd data() const override
Returns the velocities data as an Eigen vector.
const std::string & get_reference_frame() const
Getter of the reference frame as const reference.
Abstract class to represent a state.
virtual void initialize()
Initialize the State to a zero value.
const std::string & get_name() const
Getter of the name as const reference.
bool is_empty() const
Getter of the empty attribute.
Core state variables and objects.
CartesianAcceleration operator*(const CartesianState &state, const CartesianAcceleration &acceleration)
std::ostream & operator<<(std::ostream &os, const Ellipsoid &ellipsoid)
StateType
The class types inheriting from State.