Control Libraries 6.3.4
Loading...
Searching...
No Matches
Jacobian.hpp
1#pragma once
2
3#include "state_representation/State.hpp"
4#include "state_representation/exceptions/IncompatibleSizeException.hpp"
5#include "state_representation/space/joint/JointTorques.hpp"
6#include "state_representation/space/joint/JointVelocities.hpp"
7#include "state_representation/space/cartesian/CartesianTwist.hpp"
8#include "state_representation/space/cartesian/CartesianWrench.hpp"
9
10namespace state_representation {
11class CartesianTwist;
12class CartesianWrench;
13class JointVelocities;
14class JointTorques;
15
20class Jacobian : public State {
21private:
22 std::vector<std::string> joint_names_;
23 std::string frame_;
24 std::string reference_frame_;
25 unsigned int rows_;
26 unsigned int cols_;
27 Eigen::MatrixXd data_;
28
29public:
33 Jacobian();
34
42 Jacobian(const std::string& robot_name,
43 unsigned int nb_joints,
44 const std::string& frame,
45 const std::string& reference_frame = "world");
46
54 Jacobian(const std::string& robot_name,
55 const std::vector<std::string>& joint_names,
56 const std::string& frame,
57 const std::string& reference_frame = "world");
58
66 Jacobian(const std::string& robot_name,
67 const std::string& frame,
68 const Eigen::MatrixXd& data,
69 const std::string& reference_frame = "world");
70
79 Jacobian(const std::string& robot_name,
80 const std::vector<std::string>& joint_names,
81 const std::string& frame,
82 const Eigen::MatrixXd& data,
83 const std::string& reference_frame = "world");
84
88 Jacobian(const Jacobian& jacobian);
89
98 static Jacobian Random(const std::string& robot_name,
99 unsigned int nb_joints,
100 const std::string& frame,
101 const std::string& reference_frame = "world");
102
111 static Jacobian Random(const std::string& robot_name,
112 const std::vector<std::string>& joint_names,
113 const std::string& frame,
114 const std::string& reference_frame = "world");
115
121 friend void swap(Jacobian& jacobian1, Jacobian& jacobian2);
122
128 Jacobian& operator=(const Jacobian& jacobian);
129
134 unsigned int rows() const;
135
141 Eigen::VectorXd row(unsigned int index) const;
142
147 unsigned int cols() const;
148
154 Eigen::VectorXd col(unsigned int index) const;
155
159 const std::vector<std::string>& get_joint_names() const;
160
164 void set_joint_names(unsigned int nb_joints);
165
169 void set_joint_names(const std::vector<std::string>& joint_names);
170
174 const std::string& get_frame() const;
175
179 const std::string& get_reference_frame() const;
180
188 void set_reference_frame(const CartesianPose& reference_frame);
189
193 const Eigen::MatrixXd& data() const;
194
198 void set_data(const Eigen::MatrixXd& data) override;
199
204 bool is_compatible(const State& state) const override;
205
209 void initialize() override;
210
215 Jacobian transpose() const;
216
223 Jacobian inverse() const;
224
229 Jacobian pseudoinverse() const;
230
236 Eigen::MatrixXd operator*(const Eigen::MatrixXd& matrix) const;
237
243 Eigen::MatrixXd operator*(const Jacobian& jacobian) const;
244
251
257 JointVelocities operator*(const CartesianTwist& twist) const;
258
264 JointTorques operator*(const CartesianWrench& wrench) const;
265
271 Eigen::MatrixXd solve(const Eigen::MatrixXd& matrix) const;
272
278 JointVelocities solve(const CartesianTwist& twist) const;
279
286 double& operator()(unsigned int row, unsigned int col);
287
294 const double& operator()(unsigned int row, unsigned int col) const;
295
300 Jacobian copy() const;
301
308 friend std::ostream& operator<<(std::ostream& os, const Jacobian& jacobian);
309
317 friend Jacobian operator*(const CartesianPose& pose, const Jacobian& jacobian);
318
325 friend Eigen::MatrixXd operator*(const Eigen::MatrixXd& matrix, const Jacobian& jacobian);
326};
327
328inline void swap(Jacobian& jacobian1, Jacobian& jacobian2) {
329 swap(static_cast<State&>(jacobian1), static_cast<State&>(jacobian2));
330 std::swap(jacobian1.joint_names_, jacobian2.joint_names_);
331 std::swap(jacobian1.frame_, jacobian2.frame_);
332 std::swap(jacobian1.reference_frame_, jacobian2.reference_frame_);
333 std::swap(jacobian1.cols_, jacobian2.cols_);
334 std::swap(jacobian1.rows_, jacobian2.rows_);
335 std::swap(jacobian1.data_, jacobian2.data_);
336}
337
338inline Jacobian& Jacobian::operator=(const Jacobian& jacobian) {
339 Jacobian tmp(jacobian);
340 swap(*this, tmp);
341 return *this;
342}
343
344inline unsigned int Jacobian::rows() const {
345 return this->rows_;
346}
347
348inline unsigned int Jacobian::cols() const {
349 return this->cols_;
350}
351
352inline Eigen::VectorXd Jacobian::row(unsigned int index) const {
353 return this->data_.row(index);
354}
355
356inline Eigen::VectorXd Jacobian::col(unsigned int index) const {
357 return this->data_.col(index);
358}
359
360inline const std::vector<std::string>& Jacobian::get_joint_names() const {
361 return this->joint_names_;
362}
363
364inline void Jacobian::set_joint_names(unsigned int nb_joints) {
365 if (this->joint_names_.size() != nb_joints) {
366 throw exceptions::IncompatibleSizeException("Input number of joints is of incorrect size, expected "
367 + std::to_string(this->joint_names_.size())
368 + " got " + std::to_string(nb_joints));
369 }
370 for (unsigned int i = 0; i < nb_joints; ++i) {
371 this->joint_names_[i] = "joint" + std::to_string(i);
372 }
373}
374
375inline void Jacobian::set_joint_names(const std::vector<std::string>& joint_names) {
376 if (this->joint_names_.size() != joint_names.size()) {
377 throw exceptions::IncompatibleSizeException("Input vector of joint names is of incorrect size, expected "
378 + std::to_string(this->joint_names_.size())
379 + " got " + std::to_string(joint_names.size()));
380 }
381 this->joint_names_ = joint_names;
382}
383
384inline const std::string& Jacobian::get_frame() const {
385 return this->frame_;
386}
387
388inline const std::string& Jacobian::get_reference_frame() const {
389 return this->reference_frame_;
390}
391
392inline const Eigen::MatrixXd& Jacobian::data() const {
393 return this->data_;
394}
395
396inline void Jacobian::set_data(const Eigen::MatrixXd& data) {
397 if (this->rows() != data.rows() || this->cols() != data.cols()) {
398 throw exceptions::IncompatibleSizeException("Input matrix is of incorrect size, expected "
399 + std::to_string(this->rows_) + "x" + std::to_string(this->cols_)
400 + " got " + std::to_string(data.rows()) + "x"
401 + std::to_string(data.cols()));
402 }
403 this->set_filled();
404 this->data_ = data;
405}
406
407inline double& Jacobian::operator()(unsigned int row, unsigned int col) {
408 if (row > this->rows_) {
409 throw std::out_of_range("Given row is out of range: number of rows is " + std::to_string(this->rows_));
410 }
411 if (col > this->cols_) {
412 throw std::out_of_range("Given column is out of range: number of columns is " + std::to_string(this->cols_));
413 }
414 return this->data_(row, col);
415}
416
417inline const double& Jacobian::operator()(unsigned int row, unsigned int col) const {
418 if (row > this->rows_) {
419 throw std::out_of_range("Given row is out of range: number of rows is " + std::to_string(this->rows_));
420 }
421 if (col > this->cols_) {
422 throw std::out_of_range("Given column is out of range: number of columns is " + std::to_string(this->cols_));
423 }
424 return this->data_(row, col);
425}
426}// namespace state_representation
Class to define CartesianPose in cartesian space as 3D position and quaternion based orientation.
Class to define twist in cartesian space as 3D linear and angular velocity vectors.
Class to define wrench in cartesian space as 3D force and torque vectors.
Class to define a robot Jacobian matrix.
Definition: Jacobian.hpp:20
friend void swap(Jacobian &jacobian1, Jacobian &jacobian2)
Swap the values of the two Jacobian.
Definition: Jacobian.hpp:328
Jacobian copy() const
Return a copy of the JointPositions.
Definition: Jacobian.cpp:258
const std::vector< std::string > & get_joint_names() const
Getter of the joint_names attribute.
Definition: Jacobian.hpp:360
Eigen::VectorXd row(unsigned int index) const
Accessor of the row data at given index.
Definition: Jacobian.hpp:352
const Eigen::MatrixXd & data() const
Getter of the data attribute.
Definition: Jacobian.hpp:392
Jacobian pseudoinverse() const
Return the pseudoinverse of the Jacobian matrix.
Definition: Jacobian.cpp:157
unsigned int cols() const
Getter of the number of columns attribute.
Definition: Jacobian.hpp:348
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...
Definition: Jacobian.cpp:230
const std::string & get_reference_frame() const
Getter of the reference_frame attribute.
Definition: Jacobian.hpp:388
unsigned int rows() const
Getter of the number of rows attribute.
Definition: Jacobian.hpp:344
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...
Definition: Jacobian.cpp:133
const std::string & get_frame() const
Getter of the frame attribute.
Definition: Jacobian.hpp:384
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...
Definition: Jacobian.cpp:288
Jacobian inverse() const
Return the inverse of the Jacobian matrix If the matrix is not invertible, an error is thrown advisin...
Definition: Jacobian.cpp:145
void set_joint_names(unsigned int nb_joints)
Setter of the joint_names attribute from the number of joints.
Definition: Jacobian.hpp:364
Jacobian & operator=(const Jacobian &jacobian)
Copy assignment operator that have to be defined to the custom assignment operator.
Definition: Jacobian.hpp:338
void set_data(const Eigen::MatrixXd &data) override
Setter of the data attribute.
Definition: Jacobian.hpp:396
bool is_compatible(const State &state) const override
Check if the Jacobian matrix is compatible for operations with the state given as argument.
Definition: Jacobian.cpp:88
Jacobian()
Empty constructor for a Jacobian.
Definition: Jacobian.cpp:7
Jacobian transpose() const
Return the transpose of the Jacobian matrix.
Definition: Jacobian.cpp:137
void initialize() override
Initialize the matrix to a zero value.
Definition: Jacobian.cpp:64
double & operator()(unsigned int row, unsigned int col)
Overload the () operator in a non const fashion to modify the value at given (row,...
Definition: Jacobian.hpp:407
Eigen::VectorXd col(unsigned int index) const
Accessor of the column data at given index.
Definition: Jacobian.hpp:356
friend std::ostream & operator<<(std::ostream &os, const Jacobian &jacobian)
Overload the ostream operator for printing.
Definition: Jacobian.cpp:263
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.
Definition: Jacobian.cpp:70
Class to define torques of the joints.
Class to define velocities of the joints.
Abstract class to represent a state.
Definition: State.hpp:25
void set_filled()
Setter of the empty attribute to false and also reset the timestamp.
Definition: State.cpp:31
Core state variables and objects.
void swap(CartesianState &state1, CartesianState &state2)