Control Libraries 6.3.4
Loading...
Searching...
No Matches
CartesianState.hpp
1#pragma once
2
3#include "state_representation/space/SpatialState.hpp"
4#include "state_representation/exceptions/IncompatibleSizeException.hpp"
5
6namespace state_representation {
7class CartesianState;
8
14enum class CartesianStateVariable {
15 POSITION,
16 ORIENTATION,
17 POSE,
18 LINEAR_VELOCITY,
19 ANGULAR_VELOCITY,
20 TWIST,
21 LINEAR_ACCELERATION,
22 ANGULAR_ACCELERATION,
23 ACCELERATION,
24 FORCE,
25 TORQUE,
26 WRENCH,
27 ALL
28};
29
38double dist(
39 const CartesianState& s1, const CartesianState& s2,
40 const CartesianStateVariable& state_variable_type = CartesianStateVariable::ALL
41);
42
48private:
49 Eigen::Vector3d position_;
50 Eigen::Quaterniond orientation_;
51 Eigen::Vector3d linear_velocity_;
52 Eigen::Vector3d angular_velocity_;
53 Eigen::Vector3d linear_acceleration_;
54 Eigen::Vector3d angular_acceleration_;
55 Eigen::Vector3d force_;
56 Eigen::Vector3d torque_;
57
62 void set_all_state_variables(const Eigen::VectorXd& new_values);
63
69 void set_state_variable(Eigen::Vector3d& state_variable, const Eigen::Vector3d& new_value);
70
76 void set_state_variable(Eigen::Vector3d& state_variable, const std::vector<double>& new_value);
77
84 void set_state_variable(
85 Eigen::Vector3d& linear_state_variable, Eigen::Vector3d& angular_state_variable,
86 const Eigen::Matrix<double, 6, 1>& new_value
87 );
88
89protected:
95 Eigen::VectorXd get_state_variable(const CartesianStateVariable& state_variable_type) const;
96
102 void set_state_variable(const Eigen::VectorXd& new_value, const CartesianStateVariable& state_variable_type);
103
104public:
108 explicit CartesianState();
109
115 explicit CartesianState(const std::string& name, const std::string& reference = "world");
116
120 CartesianState(const CartesianState& state) = default;
121
128 static CartesianState Identity(const std::string& name, const std::string& reference = "world");
129
136 static CartesianState Random(const std::string& name, const std::string& reference = "world");
137
143 friend void swap(CartesianState& state1, CartesianState& state2);
144
151
155 const Eigen::Vector3d& get_position() const;
156
160 const Eigen::Quaterniond& get_orientation() const;
161
166 Eigen::Vector4d get_orientation_coefficients() const;
167
173 Eigen::Matrix<double, 7, 1> get_pose() const;
174
179 Eigen::Matrix4d get_transformation_matrix() const;
180
184 const Eigen::Vector3d& get_linear_velocity() const;
185
189 const Eigen::Vector3d& get_angular_velocity() const;
190
194 Eigen::Matrix<double, 6, 1> get_twist() const;
195
199 const Eigen::Vector3d& get_linear_acceleration() const;
200
204 const Eigen::Vector3d& get_angular_acceleration() const;
205
209 Eigen::Matrix<double, 6, 1> get_acceleration() const;
210
214 const Eigen::Vector3d& get_force() const;
215
219 const Eigen::Vector3d& get_torque() const;
220
224 Eigen::Matrix<double, 6, 1> get_wrench() const;
225
229 void set_position(const Eigen::Vector3d& position);
230
234 void set_position(const std::vector<double>& position);
235
239 void set_position(const double& x, const double& y, const double& z);
240
244 void set_orientation(const Eigen::Quaterniond& orientation);
245
251 void set_orientation(const Eigen::Vector4d& orientation);
252
258 void set_orientation(const std::vector<double>& orientation);
259
263 void set_orientation(const double& w, const double& x, const double& y, const double& z);
264
270 void set_pose(const Eigen::Vector3d& position, const Eigen::Quaterniond& orientation);
271
277 void set_pose(const Eigen::Matrix<double, 7, 1>& pose);
278
284 void set_pose(const std::vector<double>& pose);
285
289 void set_linear_velocity(const Eigen::Vector3d& linear_velocity);
290
294 void set_linear_velocity(const std::vector<double>& linear_velocity);
295
299 void set_linear_velocity(const double& x, const double& y, const double& z);
300
304 void set_angular_velocity(const Eigen::Vector3d& angular_velocity);
305
309 void set_angular_velocity(const std::vector<double>& angular_velocity);
310
314 void set_angular_velocity(const double& x, const double& y, const double& z);
315
319 void set_twist(const Eigen::Matrix<double, 6, 1>& twist);
320
324 void set_twist(const std::vector<double>& twist);
325
329 void set_linear_acceleration(const Eigen::Vector3d& linear_acceleration);
330
334 void set_linear_acceleration(const std::vector<double>& linear_acceleration);
335
339 void set_linear_acceleration(const double& x, const double& y, const double& z);
340
344 void set_angular_acceleration(const Eigen::Vector3d& angular_acceleration);
345
349 void set_angular_acceleration(const std::vector<double>& angular_acceleration);
350
354 void set_angular_acceleration(const double& x, const double& y, const double& z);
355
359 void set_acceleration(const Eigen::Matrix<double, 6, 1>& acceleration);
360
364 void set_acceleration(const std::vector<double>& acceleration);
365
369 void set_force(const Eigen::Vector3d& force);
370
374 void set_force(const std::vector<double>& force);
375
379 void set_force(const double& x, const double& y, const double& z);
380
384 void set_torque(const Eigen::Vector3d& torque);
385
389 void set_torque(const std::vector<double>& torque);
390
394 void set_torque(const double& x, const double& y, const double& z);
395
399 void set_wrench(const Eigen::Matrix<double, 6, 1>& wrench);
400
404 void set_wrench(const std::vector<double>& wrench);
405
409 void initialize() override;
410
414 void set_zero();
415
423 void clamp_state_variable(double max_norm, const CartesianStateVariable& state_variable_type, double noise_ratio = 0);
424
429 CartesianState copy() const;
430
436 virtual Eigen::VectorXd data() const;
437
442 Eigen::ArrayXd array() const;
443
449 virtual void set_data(const Eigen::VectorXd& data) override;
450
456 virtual void set_data(const std::vector<double>& data) override;
457
464
470 CartesianState operator*(const CartesianState& state) const;
471
477 CartesianState& operator*=(double lambda);
478
484 CartesianState operator*(double lambda) const;
485
491 CartesianState& operator/=(double lambda);
492
498 CartesianState operator/(double lambda) const;
499
506
512 CartesianState operator+(const CartesianState& state) const;
513
520
526 CartesianState operator-(const CartesianState& state) const;
527
532 CartesianState inverse() const;
533
541 double dist(
542 const CartesianState& state, const CartesianStateVariable& state_variable_type = CartesianStateVariable::ALL
543 ) const;
544
550 virtual std::vector<double>
551 norms(const CartesianStateVariable& state_variable_type = CartesianStateVariable::ALL) const;
552
557 void normalize(const CartesianStateVariable& state_variable_type = CartesianStateVariable::ALL);
558
564 CartesianState normalized(const CartesianStateVariable& state_variable_type = CartesianStateVariable::ALL) const;
565
572 friend std::ostream& operator<<(std::ostream& os, const CartesianState& state);
573
579 friend CartesianState operator*(double lambda, const CartesianState& state);
580
589 friend double dist(
590 const CartesianState& s1, const CartesianState& s2, const CartesianStateVariable& state_variable_type
591 );
592
597 std::vector<double> to_std_vector() const;
598};
599
600inline void swap(CartesianState& state1, CartesianState& state2) {
601 swap(static_cast<SpatialState&>(state1), static_cast<SpatialState&>(state2));
602 std::swap(state1.position_, state2.position_);
603 std::swap(state1.orientation_, state2.orientation_);
604 std::swap(state1.linear_velocity_, state2.linear_velocity_);
605 std::swap(state1.angular_velocity_, state2.angular_velocity_);
606 std::swap(state1.linear_acceleration_, state2.linear_acceleration_);
607 std::swap(state1.angular_acceleration_, state2.angular_acceleration_);
608 std::swap(state1.force_, state2.force_);
609 std::swap(state1.torque_, state2.torque_);
610}
611
613 CartesianState tmp(state);
614 swap(*this, tmp);
615 return *this;
616}
617
618inline Eigen::VectorXd CartesianState::get_state_variable(const CartesianStateVariable& state_variable_type) const {
619 switch (state_variable_type) {
620 case CartesianStateVariable::POSITION:
621 return this->get_position();
622
623 case CartesianStateVariable::ORIENTATION:
624 return this->get_orientation_coefficients();
625
626 case CartesianStateVariable::POSE:
627 return this->get_pose();
628
629 case CartesianStateVariable::LINEAR_VELOCITY:
630 return this->get_linear_velocity();
631
632 case CartesianStateVariable::ANGULAR_VELOCITY:
633 return this->get_angular_velocity();
634
635 case CartesianStateVariable::TWIST:
636 return this->get_twist();
637
638 case CartesianStateVariable::LINEAR_ACCELERATION:
639 return this->get_linear_acceleration();
640
641 case CartesianStateVariable::ANGULAR_ACCELERATION:
642 return this->get_angular_acceleration();
643
644 case CartesianStateVariable::ACCELERATION:
645 return this->get_acceleration();
646
647 case CartesianStateVariable::FORCE:
648 return this->get_force();
649
650 case CartesianStateVariable::TORQUE:
651 return this->get_torque();
652
653 case CartesianStateVariable::WRENCH:
654 return this->get_wrench();
655
656 case CartesianStateVariable::ALL:
657 Eigen::VectorXd all_fields(25);
658 all_fields << this->get_pose(), this->get_twist(), this->get_acceleration(), this->get_wrench();
659 return all_fields;
660 }
661 // this never goes here but is compulsory to avoid a warning
662 return Eigen::Vector3d::Zero();
663}
664
665inline void CartesianState::set_all_state_variables(const Eigen::VectorXd& new_values) {
666 if (new_values.size() != 25) {
668 "Input is of incorrect size: expected 25, given " + std::to_string(new_values.size()));
669 }
670 this->set_pose(new_values.segment(0, 7));
671 this->set_twist(new_values.segment(7, 6));
672 this->set_acceleration(new_values.segment(13, 6));
673 this->set_wrench(new_values.segment(19, 6));
674}
675
676inline void CartesianState::set_state_variable(Eigen::Vector3d& state_variable, const Eigen::Vector3d& new_value) {
677 this->set_filled();
678 state_variable = new_value;
679}
680
681inline void CartesianState::set_state_variable(Eigen::Vector3d& state_variable, const std::vector<double>& new_value) {
682 if (new_value.size() != 3) {
683 throw exceptions::IncompatibleSizeException(
684 "Input vector is of incorrect size: expected 3, given " + std::to_string(new_value.size()));
685 }
686 this->set_state_variable(state_variable, Eigen::Vector3d::Map(new_value.data(), new_value.size()));
687}
688
689inline void CartesianState::set_state_variable(
690 Eigen::Vector3d& linear_state_variable, Eigen::Vector3d& angular_state_variable,
691 const Eigen::Matrix<double, 6, 1>& new_value
692) {
693 this->set_state_variable(linear_state_variable, new_value.head(3));
694 this->set_state_variable(angular_state_variable, new_value.tail(3));
695}
696
697inline void CartesianState::set_state_variable(
698 const Eigen::VectorXd& new_value, const CartesianStateVariable& state_variable_type
699) {
700 switch (state_variable_type) {
701 case CartesianStateVariable::POSITION:
702 this->set_position(new_value);
703 break;
704
705 case CartesianStateVariable::ORIENTATION:
706 this->set_orientation(new_value);
707 break;
708
709 case CartesianStateVariable::POSE:
710 this->set_pose(new_value);
711 break;
712
713 case CartesianStateVariable::LINEAR_VELOCITY:
714 this->set_linear_velocity(new_value);
715 break;
716
717 case CartesianStateVariable::ANGULAR_VELOCITY:
718 this->set_angular_velocity(new_value);
719 break;
720
721 case CartesianStateVariable::TWIST:
722 this->set_twist(new_value);
723 break;
724
725 case CartesianStateVariable::LINEAR_ACCELERATION:
726 this->set_linear_acceleration(new_value);
727 break;
728
729 case CartesianStateVariable::ANGULAR_ACCELERATION:
730 this->set_angular_acceleration(new_value);
731 break;
732
733 case CartesianStateVariable::ACCELERATION:
734 this->set_acceleration(new_value);
735 break;
736
737 case CartesianStateVariable::FORCE:
738 this->set_force(new_value);
739 break;
740
741 case CartesianStateVariable::TORQUE:
742 this->set_torque(new_value);
743 break;
744
745 case CartesianStateVariable::WRENCH:
746 this->set_wrench(new_value);
747 break;
748
749 case CartesianStateVariable::ALL:
750 this->set_pose(new_value.segment(0, 7));
751 this->set_twist(new_value.segment(7, 6));
752 this->set_acceleration(new_value.segment(13, 6));
753 this->set_wrench(new_value.segment(19, 6));
754 break;
755 }
756}
757
758inline std::vector<double> CartesianState::to_std_vector() const {
759 Eigen::VectorXd data = this->data();
760 return std::vector<double>(data.data(), data.data() + data.size());
761}
762}// namespace state_representation
Class to represent a state in Cartesian space.
void set_zero()
Set the State to a zero value.
const Eigen::Vector3d & get_force() const
Getter of the force attribute.
void set_acceleration(const Eigen::Matrix< double, 6, 1 > &acceleration)
Setter of the linear and angular acceleration from a 6d acceleration vector.
CartesianState inverse() const
Compute the inverse of the current CartesianState.
Eigen::ArrayXd array() const
Return the data vector as an Eigen Array.
const Eigen::Vector3d & get_torque() const
Getter of the torque attribute.
static CartesianState Random(const std::string &name, const std::string &reference="world")
Constructor for a random state.
void set_orientation(const Eigen::Quaterniond &orientation)
Setter of the orientation.
friend std::ostream & operator<<(std::ostream &os, const CartesianState &state)
Overload the ostream operator for printing.
const Eigen::Vector3d & get_linear_velocity() const
Getter of the linear velocity attribute.
void set_angular_acceleration(const Eigen::Vector3d &angular_acceleration)
Setter of the angular velocity attribute.
const Eigen::Vector3d & get_position() const
Getter of the position attribute.
void normalize(const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL)
Normalize inplace the state at the state variable given in argument (default is full state)
CartesianState operator-(const CartesianState &state) const
Overload the - operator.
virtual std::vector< double > norms(const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL) const
Compute the norms of the state variable specified by the input type (default is full state)
void set_force(const Eigen::Vector3d &force)
Setter of the force attribute.
Eigen::Matrix< double, 6, 1 > get_twist() const
Getter of the 6d twist from linear and angular velocity attributes.
CartesianState normalized(const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL) const
Compute the normalized state at the state variable given in argument (default is full state)
friend double dist(const CartesianState &s1, const CartesianState &s2, const CartesianStateVariable &state_variable_type)
compute the distance between two CartesianStates
static CartesianState Identity(const std::string &name, const std::string &reference="world")
Constructor for the identity CartesianState (identity pose and 0 for the rest)
CartesianState & operator/=(double lambda)
Overload the /= operator with a scalar.
const Eigen::Vector3d & get_linear_acceleration() const
Getter of the linear acceleration attribute.
Eigen::Matrix4d get_transformation_matrix() const
Getter of a pose from position and orientation attributes.
void set_linear_acceleration(const Eigen::Vector3d &linear_acceleration)
Setter of the linear acceleration attribute.
void clamp_state_variable(double max_norm, const CartesianStateVariable &state_variable_type, double noise_ratio=0)
Clamp inplace the norm of the a specific state variable.
Eigen::Matrix< double, 7, 1 > get_pose() const
Getter of a pose from position and orientation attributes.
void set_position(const Eigen::Vector3d &position)
Setter of the position.
void set_wrench(const Eigen::Matrix< double, 6, 1 > &wrench)
Setter of the force and torque from a 6d wrench vector.
const Eigen::Quaterniond & get_orientation() const
Getter of the orientation attribute.
Eigen::Matrix< double, 6, 1 > get_wrench() const
Getter of the 6d wrench from force and torque attributes.
Eigen::VectorXd get_state_variable(const CartesianStateVariable &state_variable_type) const
Getter of the variable value corresponding to the input.
void set_twist(const Eigen::Matrix< double, 6, 1 > &twist)
Setter of the linear and angular velocities from a 6d twist vector.
CartesianState & operator+=(const CartesianState &state)
Overload the += operator.
CartesianState & operator=(const CartesianState &state)
Copy assignment operator that have to be defined to the custom assignment operator.
virtual Eigen::VectorXd data() const
Return the data as the concatenation of all the state variables in a single vector.
const Eigen::Vector3d & get_angular_velocity() const
Getter of the angular velocity attribute.
void set_pose(const Eigen::Vector3d &position, const Eigen::Quaterniond &orientation)
Setter of the pose from both position and orientation.
CartesianState copy() const
Return a copy of the CartesianState.
CartesianState & operator-=(const CartesianState &state)
Overload the -= operator.
void set_torque(const Eigen::Vector3d &torque)
Setter of the torque attribute.
virtual void set_data(const Eigen::VectorXd &data) override
Set the data of the state from all the state variables in a single Eigen vector.
Eigen::Vector4d get_orientation_coefficients() const
Getter of the orientation attribute as Vector4d of coefficients Beware, quaternion coefficients are r...
CartesianState & operator*=(const CartesianState &state)
Overload the *= operator with another state by deriving the equations of motions.
const Eigen::Vector3d & get_angular_acceleration() const
Getter of the angular acceleration attribute.
CartesianState(const CartesianState &state)=default
Copy constructor of a CartesianState.
CartesianState operator/(double lambda) const
Overload the / operator with a scalar.
void initialize() override
Initialize the CartesianState to a zero value.
std::vector< double > to_std_vector() const
Return the state as a std vector.
void set_linear_velocity(const Eigen::Vector3d &linear_velocity)
Setter of the linear velocity attribute.
void set_angular_velocity(const Eigen::Vector3d &angular_velocity)
Setter of the angular velocity attribute.
friend CartesianState operator*(double lambda, const CartesianState &state)
Overload the * operator with a scalar.
Eigen::Matrix< double, 6, 1 > get_acceleration() const
Getter of the 6d acceleration from linear and angular acceleration attributes.
friend void swap(CartesianState &state1, CartesianState &state2)
Swap the values of the two CartesianState.
CartesianState operator+(const CartesianState &state) const
Overload the + operator.
void set_filled()
Setter of the empty attribute to false and also reset the timestamp.
Definition: State.cpp:31
Core state variables and objects.
double dist(const CartesianState &s1, const CartesianState &s2, const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL)
compute the distance between two CartesianStates
void swap(CartesianState &state1, CartesianState &state2)