Control Libraries 6.3.4
Loading...
Searching...
No Matches
JointState.hpp
1#pragma once
2
3#include "state_representation/State.hpp"
4#include "state_representation/exceptions/IncompatibleSizeException.hpp"
5
6using namespace state_representation::exceptions;
7
8namespace state_representation {
9class JointState;
10
17 POSITIONS, VELOCITIES, ACCELERATIONS, TORQUES, ALL
18};
19
28double dist(
29 const JointState& s1, const JointState& s2, const JointStateVariable& state_variable_type = JointStateVariable::ALL
30);
31
36class JointState : public State {
37private:
38 std::vector<std::string> names_;
39 Eigen::VectorXd positions_;
40 Eigen::VectorXd velocities_;
41 Eigen::VectorXd accelerations_;
42 Eigen::VectorXd torques_;
43
48 Eigen::VectorXd get_all_state_variables() const;
49
55 void set_state_variable(Eigen::VectorXd& state_variable, const Eigen::VectorXd& new_value);
56
62 void set_state_variable(Eigen::VectorXd& state_variable, const std::vector<double>& new_value);
63
68 void set_all_state_variables(const Eigen::VectorXd& new_values);
69
70protected:
76 void multiply_state_variable(const Eigen::ArrayXd& lambda, const JointStateVariable& state_variable_type);
77
83 void multiply_state_variable(const Eigen::MatrixXd& lambda, const JointStateVariable& state_variable_type);
84
90 Eigen::VectorXd get_state_variable(const JointStateVariable& state_variable_type) const;
91
97 void set_state_variable(const Eigen::VectorXd& new_value, const JointStateVariable& state_variable_type);
98
99public:
103 explicit JointState();
104
110 explicit JointState(const std::string& robot_name, unsigned int nb_joints = 0);
111
117 explicit JointState(const std::string& robot_name, const std::vector<std::string>& joint_names);
118
123 JointState(const JointState& state) = default;
124
131 static JointState Zero(const std::string& robot_name, unsigned int nb_joints);
132
139 static JointState Zero(const std::string& robot_name, const std::vector<std::string>& joint_names);
140
147 static JointState Random(const std::string& robot_name, unsigned int nb_joints);
148
155 static JointState Random(const std::string& robot_name, const std::vector<std::string>& joint_names);
156
162 friend void swap(JointState& state1, JointState& state2);
163
169 JointState& operator=(const JointState& state);
170
175 unsigned int get_size() const;
176
181 const std::vector<std::string>& get_names() const;
182
187 void set_names(unsigned int nb_joints);
188
193 void set_names(const std::vector<std::string>& names);
194
202 unsigned int get_joint_index(const std::string& joint_name) const;
203
208 const Eigen::VectorXd& get_positions() const;
209
216 double get_position(const std::string& joint_name) const;
217
224 double get_position(unsigned int joint_index) const;
225
230 void set_positions(const Eigen::VectorXd& positions);
231
236 void set_positions(const std::vector<double>& positions);
237
243 void set_position(double position, const std::string& joint_name);
244
250 void set_position(double position, unsigned int joint_index);
251
256 const Eigen::VectorXd& get_velocities() const;
257
264 double get_velocity(const std::string& joint_name) const;
265
272 double get_velocity(unsigned int joint_index) const;
273
278 void set_velocities(const Eigen::VectorXd& velocities);
279
284 void set_velocities(const std::vector<double>& velocities);
285
291 void set_velocity(double velocity, const std::string& joint_name);
292
298 void set_velocity(double velocity, unsigned int joint_index);
299
304 const Eigen::VectorXd& get_accelerations() const;
305
312 double get_acceleration(const std::string& joint_name) const;
313
320 double get_acceleration(unsigned int joint_index) const;
321
326 void set_accelerations(const Eigen::VectorXd& accelerations);
327
332 void set_accelerations(const std::vector<double>& accelerations);
333
339 void set_acceleration(double acceleration, const std::string& joint_name);
340
346 void set_acceleration(double acceleration, unsigned int joint_index);
347
352 const Eigen::VectorXd& get_torques() const;
353
360 double get_torque(const std::string& joint_name) const;
361
368 double get_torque(unsigned int joint_index) const;
369
374 void set_torques(const Eigen::VectorXd& torques);
375
380 void set_torques(const std::vector<double>& torques);
381
387 void set_torque(double torque, const std::string& joint_name);
388
394 void set_torque(double torque, unsigned int joint_index);
395
400 bool is_compatible(const State& state) const;
401
405 void initialize();
406
410 void set_zero();
411
420 double max_absolute_value, const JointStateVariable& state_variable_type, double noise_ratio = 0
421 );
422
432 const Eigen::ArrayXd& max_absolute_value_array, const JointStateVariable& state_variable_type,
433 const Eigen::ArrayXd& noise_ratio_array
434 );
435
440 JointState copy() const;
441
447 virtual Eigen::VectorXd data() const;
448
454 virtual void set_data(const Eigen::VectorXd& data) override;
455
461 virtual void set_data(const std::vector<double>& data) override;
462
467 Eigen::ArrayXd array() const;
468
474 JointState& operator+=(const JointState& state);
475
481 JointState operator+(const JointState& state) const;
482
488 JointState& operator-=(const JointState& state);
489
495 JointState operator-(const JointState& state) const;
496
502 JointState& operator*=(double lambda);
503
509 JointState operator*(double lambda) const;
510
516 JointState& operator*=(const Eigen::ArrayXd& lambda);
517
523 JointState operator*(const Eigen::ArrayXd& lambda) const;
524
530 JointState& operator*=(const Eigen::MatrixXd& lambda);
531
537 JointState operator*(const Eigen::MatrixXd& lambda) const;
538
544 JointState& operator/=(double lambda);
545
551 JointState operator/(double lambda) const;
552
560 double dist(const JointState& state, const JointStateVariable& state_variable_type = JointStateVariable::ALL) const;
561
568 friend std::ostream& operator<<(std::ostream& os, const JointState& state);
569
575 friend JointState operator*(double lambda, const JointState& state);
576
582 friend JointState operator*(const Eigen::ArrayXd& lambda, const JointState& state);
583
589 friend JointState operator*(const Eigen::MatrixXd& lambda, const JointState& state);
590
595 std::vector<double> to_std_vector() const;
596};
597
598inline void swap(JointState& state1, JointState& state2) {
599 swap(static_cast<State&>(state1), static_cast<State&>(state2));
600 std::swap(state1.names_, state2.names_);
601 std::swap(state1.positions_, state2.positions_);
602 std::swap(state1.velocities_, state2.velocities_);
603 std::swap(state1.accelerations_, state2.accelerations_);
604 std::swap(state1.torques_, state2.torques_);
605}
606
608 JointState tmp(state);
609 swap(*this, tmp);
610 return *this;
611}
612
613inline Eigen::VectorXd JointState::get_all_state_variables() const {
614 Eigen::VectorXd all_fields(this->get_size() * 4);
615 all_fields << this->get_positions(), this->get_velocities(), this->get_accelerations(), this->get_torques();
616 return all_fields;
617}
618
619inline void JointState::set_state_variable(Eigen::VectorXd& state_variable, const Eigen::VectorXd& new_value) {
620 if (new_value.size() != this->get_size()) {
622 "Input vector is of incorrect size: expected " + std::to_string(this->get_size()) + ", given "
623 + std::to_string(new_value.size()));
624 }
625 this->set_filled();
626 state_variable = new_value;
627}
628
629inline void JointState::set_state_variable(Eigen::VectorXd& state_variable, const std::vector<double>& new_value) {
630 this->set_state_variable(state_variable, Eigen::VectorXd::Map(new_value.data(), new_value.size()));
631}
632
633inline void JointState::set_all_state_variables(const Eigen::VectorXd& new_values) {
634 if (new_values.size() != 4 * this->get_size()) {
636 "Input is of incorrect size: expected " + std::to_string(this->get_size()) + ", given "
637 + std::to_string(new_values.size()));
638 }
639 this->set_positions(new_values.segment(0, this->get_size()));
640 this->set_velocities(new_values.segment(this->get_size(), this->get_size()));
641 this->set_accelerations(new_values.segment(2 * this->get_size(), this->get_size()));
642 this->set_torques(new_values.segment(3 * this->get_size(), this->get_size()));
643}
644
645inline bool JointState::is_compatible(const State& state) const {
646 bool compatible = this->State::is_compatible(state);
647 compatible = compatible && (this->names_.size() == dynamic_cast<const JointState&>(state).names_.size());
648 if (compatible) {
649 for (unsigned int i = 0; i < this->names_.size(); ++i) {
650 compatible = (compatible && this->names_[i] == dynamic_cast<const JointState&>(state).names_[i]);
651 }
652 }
653 return compatible;
654}
655
656inline unsigned int JointState::get_size() const {
657 return this->names_.size();
658}
659
660inline const std::vector<std::string>& JointState::get_names() const {
661 return this->names_;
662}
663
664inline void JointState::set_names(unsigned int nb_joints) {
665 if (this->get_size() != nb_joints) {
667 "Input number of joints is of incorrect size, expected " + std::to_string(this->get_size()) + " got "
668 + std::to_string(nb_joints));
669 }
670 this->names_.resize(nb_joints);
671 for (unsigned int i = 0; i < nb_joints; ++i) {
672 this->names_[i] = "joint" + std::to_string(i);
673 }
674}
675
676inline void JointState::set_names(const std::vector<std::string>& names) {
677 if (this->get_size() != names.size()) {
679 "Input number of joints is of incorrect size, expected " + std::to_string(this->get_size()) + " got "
680 + std::to_string(names.size()));
681 }
682 this->names_ = names;
683}
684
685inline Eigen::VectorXd JointState::get_state_variable(const JointStateVariable& state_variable_type) const {
686 switch (state_variable_type) {
687 case JointStateVariable::POSITIONS:
688 return this->get_positions();
689
690 case JointStateVariable::VELOCITIES:
691 return this->get_velocities();
692
693 case JointStateVariable::ACCELERATIONS:
694 return this->get_accelerations();
695
696 case JointStateVariable::TORQUES:
697 return this->get_torques();
698
699 case JointStateVariable::ALL:
700 return this->get_all_state_variables();
701 }
702 // this never goes here but is compulsory to avoid a warning
703 return Eigen::Vector3d::Zero();
704}
705
706inline void JointState::set_state_variable(
707 const Eigen::VectorXd& new_value, const JointStateVariable& state_variable_type
708) {
709 switch (state_variable_type) {
710 case JointStateVariable::POSITIONS:
711 this->set_positions(new_value);
712 break;
713
714 case JointStateVariable::VELOCITIES:
715 this->set_velocities(new_value);
716 break;
717
718 case JointStateVariable::ACCELERATIONS:
719 this->set_accelerations(new_value);
720 break;
721
722 case JointStateVariable::TORQUES:
723 this->set_torques(new_value);
724 break;
725
726 case JointStateVariable::ALL:
727 this->set_all_state_variables(new_value);
728 break;
729 }
730}
731
732inline std::vector<double> JointState::to_std_vector() const {
733 Eigen::VectorXd data = this->data();
734 return std::vector<double>(data.data(), data.data() + data.size());
735}
736}// namespace state_representation
Class to define a state in joint space.
Definition: JointState.hpp:36
const Eigen::VectorXd & get_torques() const
Getter of the torques attribute.
Definition: JointState.cpp:182
JointState operator+(const JointState &state) const
Overload the + operator.
Definition: JointState.cpp:231
double dist(const JointState &state, const JointStateVariable &state_variable_type=JointStateVariable::ALL) const
Compute the distance to another state as the sum of distances between each attribute.
Definition: JointState.cpp:389
void set_accelerations(const Eigen::VectorXd &accelerations)
Setter of the accelerations attribute.
Definition: JointState.cpp:163
double get_torque(const std::string &joint_name) const
Get the torque of a joint by its name, if it exists.
Definition: JointState.cpp:186
void set_positions(const Eigen::VectorXd &positions)
Setter of the positions attribute.
Definition: JointState.cpp:99
static JointState Random(const std::string &robot_name, unsigned int nb_joints)
Constructor for a random JointState.
Definition: JointState.cpp:64
JointState()
Empty constructor for a JointState.
Definition: JointState.cpp:16
void set_names(unsigned int nb_joints)
Setter of the names attribute from the number of joints.
Definition: JointState.hpp:664
JointState & operator=(const JointState &state)
Copy assignment operator that have to be defined to the custom assignment operator.
Definition: JointState.hpp:607
JointState & operator*=(double lambda)
Overload the *= operator with a double gain.
Definition: JointState.cpp:260
void set_zero()
Set the JointState to a zero value.
Definition: JointState.cpp:43
const Eigen::VectorXd & get_velocities() const
Getter of the velocities attribute.
Definition: JointState.cpp:118
unsigned int get_size() const
Getter of the size from the attributes.
Definition: JointState.hpp:656
Eigen::VectorXd get_state_variable(const JointStateVariable &state_variable_type) const
Getter of the variable value corresponding to the input.
Definition: JointState.hpp:685
void clamp_state_variable(double max_absolute_value, const JointStateVariable &state_variable_type, double noise_ratio=0)
Clamp inplace the magnitude of the a specific joint state variable.
Definition: JointState.cpp:379
void set_acceleration(double acceleration, const std::string &joint_name)
Set the acceleration of a joint by its name.
Definition: JointState.cpp:171
JointState operator-(const JointState &state) const
Overload the - operator.
Definition: JointState.cpp:254
std::vector< double > to_std_vector() const
Return the joint state as a std vector of floats.
Definition: JointState.hpp:732
static JointState Zero(const std::string &robot_name, unsigned int nb_joints)
Constructor for a zero JointState.
Definition: JointState.cpp:50
Eigen::ArrayXd array() const
Returns the data vector as an Eigen Array.
Definition: JointState.cpp:346
JointState & operator+=(const JointState &state)
Overload the += operator.
Definition: JointState.cpp:214
unsigned int get_joint_index(const std::string &joint_name) const
Get joint index by the name of the joint, if it exists.
Definition: JointState.cpp:78
JointState copy() const
Return a copy of the JointState.
Definition: JointState.cpp:329
double get_acceleration(const std::string &joint_name) const
Get the acceleration of a joint by its name, if it exists.
Definition: JointState.cpp:154
void set_torques(const Eigen::VectorXd &torques)
Setter of the torques attribute.
Definition: JointState.cpp:195
void set_position(double position, const std::string &joint_name)
Set the position of a joint by its name.
Definition: JointState.cpp:107
bool is_compatible(const State &state) const
Check if the state is compatible for operations with the state given as argument.
Definition: JointState.hpp:645
double get_velocity(const std::string &joint_name) const
Get the velocity of a joint by its name, if it exists.
Definition: JointState.cpp:122
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.
Definition: JointState.cpp:338
friend std::ostream & operator<<(std::ostream &os, const JointState &state)
Overload the ostream operator for printing.
Definition: JointState.cpp:415
JointState & operator-=(const JointState &state)
Overload the -= operator.
Definition: JointState.cpp:237
void multiply_state_variable(const Eigen::ArrayXd &lambda, const JointStateVariable &state_variable_type)
Proxy function that multiply the specified state variable by an array of gain.
Definition: JointState.cpp:268
void set_torque(double torque, const std::string &joint_name)
Set the torque of a joint by its name.
Definition: JointState.cpp:203
JointState & operator/=(double lambda)
Overload the /= operator with a scalar.
Definition: JointState.cpp:319
const Eigen::VectorXd & get_accelerations() const
Getter of the accelerations attribute.
Definition: JointState.cpp:150
friend JointState operator*(double lambda, const JointState &state)
Overload the * operator with a scalar.
Definition: JointState.cpp:443
JointState(const JointState &state)=default
Copy constructor of a JointState.
void initialize()
Initialize the State to a zero value.
Definition: JointState.cpp:31
const std::vector< std::string > & get_names() const
Getter of the names attribute.
Definition: JointState.hpp:660
double get_position(const std::string &joint_name) const
Get the position of a joint by its name, if it exists.
Definition: JointState.cpp:90
void set_velocity(double velocity, const std::string &joint_name)
Set the velocity of a joint by its name.
Definition: JointState.cpp:139
const Eigen::VectorXd & get_positions() const
Getter of the positions attribute.
Definition: JointState.cpp:86
void set_velocities(const Eigen::VectorXd &velocities)
Setter of the velocities attribute.
Definition: JointState.cpp:131
JointState operator/(double lambda) const
Overload the / operator with a scalar.
Definition: JointState.cpp:323
virtual Eigen::VectorXd data() const
Returns the data as the concatenation of all the state variables in a single vector.
Definition: JointState.cpp:334
friend void swap(JointState &state1, JointState &state2)
Swap the values of the two JointState.
Definition: JointState.hpp:598
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
virtual bool is_compatible(const State &state) const
Check if the state is compatible for operations with the state given as argument.
Definition: State.cpp:56
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
JointStateVariable
Enum representing all the fields (positions, velocities, accelerations and torques) of the JointState...
Definition: JointState.hpp:16
void swap(CartesianState &state1, CartesianState &state2)