1#include "state_representation/space/cartesian/CartesianState.hpp"
2#include "state_representation/exceptions/EmptyStateException.hpp"
3#include "state_representation/exceptions/IncompatibleReferenceFramesException.hpp"
4#include "state_representation/exceptions/NotImplementedException.hpp"
6using namespace state_representation::exceptions;
24 this->position_.setZero();
25 this->orientation_.setIdentity();
26 this->linear_velocity_.setZero();
27 this->angular_velocity_.setZero();
28 this->linear_acceleration_.setZero();
29 this->angular_acceleration_.setZero();
30 this->force_.setZero();
31 this->torque_.setZero();
44 random.set_state_variable(Eigen::VectorXd::Random(25), CartesianStateVariable::ALL);
49 return this->position_;
53 return this->orientation_;
57 return Eigen::Vector4d(
63 Eigen::Matrix<double, 7, 1> pose;
70 pose << this->orientation_.toRotationMatrix(), this->position_, 0., 0., 0., 1;
75 return this->linear_velocity_;
79 return this->angular_velocity_;
83 Eigen::Matrix<double, 6, 1> twist;
89 return this->linear_acceleration_;
93 return this->angular_acceleration_;
97 Eigen::Matrix<double, 6, 1> acceleration;
107 return this->torque_;
111 Eigen::Matrix<double, 6, 1> wrench;
117 this->set_state_variable(this->position_, position);
121 this->set_state_variable(this->position_, position);
130 this->orientation_ = orientation.normalized();
134 this->
set_orientation(Eigen::Quaterniond(orientation(0), orientation(1), orientation(2), orientation(3)));
138 if (orientation.size() != 4) {
141 this->
set_orientation(Eigen::Vector4d::Map(orientation.data(), orientation.size()));
159 if (pose.size() != 7) {
162 this->
set_position(std::vector<double>(pose.begin(), pose.begin() + 3));
163 this->
set_orientation(std::vector<double>(pose.begin() + 3, pose.end()));
167 this->set_state_variable(this->linear_velocity_, linear_velocity);
171 this->set_state_variable(this->linear_velocity_, linear_velocity);
179 this->set_state_variable(this->angular_velocity_, angular_velocity);
183 this->set_state_variable(this->angular_velocity_, angular_velocity);
191 this->set_state_variable(this->linear_velocity_, this->angular_velocity_, twist);
195 if (twist.size() != 6) {
203 this->set_state_variable(this->linear_acceleration_, linear_acceleration);
207 this->set_state_variable(this->linear_acceleration_, linear_acceleration);
215 this->set_state_variable(this->angular_acceleration_, angular_acceleration);
219 this->set_state_variable(this->angular_acceleration_, angular_acceleration);
227 this->set_state_variable(this->linear_acceleration_, this->angular_acceleration_, acceleration);
231 if (acceleration.size() != 6) {
239 this->set_state_variable(this->force_, force);
243 this->set_state_variable(this->force_, force);
247 this->
set_force(Eigen::Vector3d(x, y, z));
251 this->set_state_variable(this->torque_, torque);
255 this->set_state_variable(this->torque_, torque);
263 this->set_state_variable(this->force_, this->torque_, wrench);
267 if (wrench.size() != 6) {
270 this->
set_force(std::vector<double>(wrench.begin(), wrench.begin() + 3));
271 this->
set_torque(std::vector<double>(wrench.begin() + 3, wrench.end()));
299 if (std::abs(lambda) < std::numeric_limits<double>::min()) {
300 throw std::runtime_error(
"Division by zero is not allowed");
302 lambda = 1.0 / lambda;
322 this->set_all_state_variables(
data);
326 this->set_all_state_variables(Eigen::VectorXd::Map(
data.data(),
data.size()));
330 return this->
data().array();
353 Eigen::Vector3d b_P_c = state.get_position();
354 Eigen::Quaterniond b_R_c =
356 -state.get_orientation().coeffs());
357 Eigen::Vector3d b_v_c = state.get_linear_velocity();
358 Eigen::Vector3d b_omega_c = state.get_angular_velocity();
359 Eigen::Vector3d b_a_c = state.get_linear_acceleration();
360 Eigen::Vector3d b_alpha_c = state.get_angular_acceleration();
369 f_a_b + f_R_b * b_a_c + f_alpha_b.cross(f_R_b * b_P_c) + 2 * f_omega_b.cross(f_R_b * b_v_c)
370 + f_omega_b.cross(f_omega_b.cross(f_R_b * b_P_c)));
397 Eigen::Quaterniond orientation =
399 -state.get_orientation().coeffs());
430 Eigen::Quaterniond orientation =
432 -state.get_orientation().coeffs());
463 Eigen::Quaterniond b_R_f = f_R_b.conjugate();
464 Eigen::Vector3d b_P_f = b_R_f * (-f_P_b);
465 Eigen::Vector3d b_v_f = b_R_f * (-f_v_b);
466 Eigen::Vector3d b_omega_f = b_R_f * (-f_omega_b);
467 Eigen::Vector3d b_a_f = b_R_f * f_a_b;
468 Eigen::Vector3d b_alpha_f = b_R_f * f_alpha_b;
482 double max_norm,
const CartesianStateVariable& state_variable_type,
double noise_ratio
484 if (state_variable_type == CartesianStateVariable::ORIENTATION || state_variable_type == CartesianStateVariable::POSE
485 || state_variable_type == CartesianStateVariable::ALL) {
489 if (noise_ratio != 0 && state_variable_value.norm() < noise_ratio * max_norm) {
491 state_variable_value.setZero();
492 }
else if (state_variable_value.norm() > max_norm) {
494 state_variable_value = max_norm * state_variable_value.normalized();
496 this->set_state_variable(state_variable_value, state_variable_type);
512 if (state_variable_type == CartesianStateVariable::POSITION || state_variable_type == CartesianStateVariable::POSE
513 || state_variable_type == CartesianStateVariable::ALL) {
516 if (state_variable_type == CartesianStateVariable::ORIENTATION || state_variable_type == CartesianStateVariable::POSE
517 || state_variable_type == CartesianStateVariable::ALL) {
520 double argument = 2 * inner_product * inner_product - 1;
521 result += acos(std::min(1.0, std::max(-1.0, argument)));
523 if (state_variable_type == CartesianStateVariable::LINEAR_VELOCITY
524 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
527 if (state_variable_type == CartesianStateVariable::ANGULAR_VELOCITY
528 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
531 if (state_variable_type == CartesianStateVariable::LINEAR_ACCELERATION
532 || state_variable_type == CartesianStateVariable::ACCELERATION
533 || state_variable_type == CartesianStateVariable::ALL) {
536 if (state_variable_type == CartesianStateVariable::ANGULAR_ACCELERATION
537 || state_variable_type == CartesianStateVariable::ACCELERATION
538 || state_variable_type == CartesianStateVariable::ALL) {
541 if (state_variable_type == CartesianStateVariable::FORCE || state_variable_type == CartesianStateVariable::WRENCH
542 || state_variable_type == CartesianStateVariable::ALL) {
545 if (state_variable_type == CartesianStateVariable::TORQUE || state_variable_type == CartesianStateVariable::WRENCH
546 || state_variable_type == CartesianStateVariable::ALL) {
554 std::vector<double>
norms;
555 if (state_variable_type == CartesianStateVariable::POSITION || state_variable_type == CartesianStateVariable::POSE
556 || state_variable_type == CartesianStateVariable::ALL) {
559 if (state_variable_type == CartesianStateVariable::ORIENTATION || state_variable_type == CartesianStateVariable::POSE
560 || state_variable_type == CartesianStateVariable::ALL) {
563 if (state_variable_type == CartesianStateVariable::LINEAR_VELOCITY
564 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
567 if (state_variable_type == CartesianStateVariable::ANGULAR_VELOCITY
568 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
571 if (state_variable_type == CartesianStateVariable::LINEAR_ACCELERATION
572 || state_variable_type == CartesianStateVariable::ACCELERATION
573 || state_variable_type == CartesianStateVariable::ALL) {
576 if (state_variable_type == CartesianStateVariable::ANGULAR_ACCELERATION
577 || state_variable_type == CartesianStateVariable::ACCELERATION
578 || state_variable_type == CartesianStateVariable::ALL) {
581 if (state_variable_type == CartesianStateVariable::FORCE || state_variable_type == CartesianStateVariable::WRENCH
582 || state_variable_type == CartesianStateVariable::ALL) {
585 if (state_variable_type == CartesianStateVariable::TORQUE || state_variable_type == CartesianStateVariable::WRENCH
586 || state_variable_type == CartesianStateVariable::ALL) {
593 if (state_variable_type == CartesianStateVariable::POSITION || state_variable_type == CartesianStateVariable::POSE
594 || state_variable_type == CartesianStateVariable::ALL) {
595 this->position_.normalize();
598 if (state_variable_type == CartesianStateVariable::ORIENTATION || state_variable_type == CartesianStateVariable::POSE
599 || state_variable_type == CartesianStateVariable::ALL) {
600 this->orientation_.normalize();
602 if (state_variable_type == CartesianStateVariable::LINEAR_VELOCITY
603 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
604 this->linear_velocity_.normalize();
606 if (state_variable_type == CartesianStateVariable::ANGULAR_VELOCITY
607 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
608 this->angular_velocity_.normalize();
610 if (state_variable_type == CartesianStateVariable::LINEAR_ACCELERATION
611 || state_variable_type == CartesianStateVariable::ACCELERATION
612 || state_variable_type == CartesianStateVariable::ALL) {
613 this->linear_acceleration_.normalize();
615 if (state_variable_type == CartesianStateVariable::ANGULAR_ACCELERATION
616 || state_variable_type == CartesianStateVariable::ACCELERATION
617 || state_variable_type == CartesianStateVariable::ALL) {
618 this->angular_acceleration_.normalize();
620 if (state_variable_type == CartesianStateVariable::FORCE || state_variable_type == CartesianStateVariable::WRENCH
621 || state_variable_type == CartesianStateVariable::ALL) {
622 this->force_.normalize();
624 if (state_variable_type == CartesianStateVariable::TORQUE || state_variable_type == CartesianStateVariable::WRENCH
625 || state_variable_type == CartesianStateVariable::ALL) {
626 this->torque_.normalize();
637 if (state.is_empty()) {
638 os <<
"Empty CartesianState";
640 os << state.get_name() <<
" CartesianState expressed in " << state.get_reference_frame() <<
" frame" << std::endl;
641 os <<
"position: (" << state.position_(0) <<
", ";
642 os << state.position_(1) <<
", ";
643 os << state.position_(2) <<
")" << std::endl;
644 os <<
"orientation: (" << state.orientation_.w() <<
", ";
645 os << state.orientation_.x() <<
", ";
646 os << state.orientation_.y() <<
", ";
647 os << state.orientation_.z() <<
")";
648 Eigen::AngleAxisd axis_angle(state.orientation_);
649 os <<
" <=> theta: " << axis_angle.angle() <<
", ";
650 os <<
"axis: (" << axis_angle.axis()(0) <<
", ";
651 os << axis_angle.axis()(1) <<
", ";
652 os << axis_angle.axis()(2) <<
")" << std::endl;
653 os <<
"linear velocity: (" << state.linear_velocity_(0) <<
", ";
654 os << state.linear_velocity_(1) <<
", ";
655 os << state.linear_velocity_(2) <<
")" << std::endl;
656 os <<
"angular velocity: (" << state.angular_velocity_(0) <<
", ";
657 os << state.angular_velocity_(1) <<
", ";
658 os << state.angular_velocity_(2) <<
")" << std::endl;
659 os <<
"linear acceleration: (" << state.linear_acceleration_(0) <<
", ";
660 os << state.linear_acceleration_(1) <<
", ";
661 os << state.linear_acceleration_(2) <<
")" << std::endl;
662 os <<
"angular acceleration: (" << state.angular_acceleration_(0) <<
", ";
663 os << state.angular_acceleration_(1) <<
", ";
664 os << state.angular_acceleration_(2) <<
")" << std::endl;
665 os <<
"force: (" << state.force_(0) <<
", ";
666 os << state.force_(1) <<
", ";
667 os << state.force_(2) <<
")" << std::endl;
668 os <<
"torque: (" << state.torque_(0) <<
", ";
669 os << state.torque_(1) <<
", ";
670 os << state.torque_(2) <<
")";
676 return state * lambda;
680 return s1.
dist(s2, state_variable_type);
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.
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
CartesianState()
Empty constructor.
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.
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 operator/(double lambda) const
Overload the / operator with a scalar.
double dist(const CartesianState &state, const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL) const
Compute the distance to another state as the sum of distances between each features.
void initialize() override
Initialize the CartesianState to a zero value.
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.
CartesianState operator+(const CartesianState &state) const
Overload the + operator.
virtual void set_reference_frame(const std::string &reference_frame)
Setter of the reference frame.
const std::string & get_reference_frame() const
Getter of the reference frame as const reference.
virtual void initialize()
Initialize the State to a zero value.
const std::string & get_name() const
Getter of the name as const reference.
virtual void set_name(const std::string &name)
Setter of the name.
void set_filled()
Setter of the empty attribute to false and also reset the timestamp.
bool is_empty() const
Getter of the empty attribute.
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
CartesianAcceleration operator*(const CartesianState &state, const CartesianAcceleration &acceleration)
std::ostream & operator<<(std::ostream &os, const Ellipsoid &ellipsoid)
StateType
The class types inheriting from State.