3#include "state_representation/State.hpp"
4#include "state_representation/exceptions/IncompatibleSizeException.hpp"
6using namespace state_representation::exceptions;
17 POSITIONS, VELOCITIES, ACCELERATIONS, TORQUES, ALL
38 std::vector<std::string> names_;
39 Eigen::VectorXd positions_;
40 Eigen::VectorXd velocities_;
41 Eigen::VectorXd accelerations_;
42 Eigen::VectorXd torques_;
48 Eigen::VectorXd get_all_state_variables()
const;
55 void set_state_variable(Eigen::VectorXd& state_variable,
const Eigen::VectorXd& new_value);
62 void set_state_variable(Eigen::VectorXd& state_variable,
const std::vector<double>& new_value);
68 void set_all_state_variables(
const Eigen::VectorXd& new_values);
97 void set_state_variable(
const Eigen::VectorXd& new_value,
const JointStateVariable& state_variable_type);
110 explicit JointState(
const std::string& robot_name,
unsigned int nb_joints = 0);
117 explicit JointState(
const std::string& robot_name,
const std::vector<std::string>& joint_names);
131 static JointState Zero(
const std::string& robot_name,
unsigned int nb_joints);
139 static JointState Zero(
const std::string& robot_name,
const std::vector<std::string>& joint_names);
147 static JointState Random(
const std::string& robot_name,
unsigned int nb_joints);
155 static JointState Random(
const std::string& robot_name,
const std::vector<std::string>& joint_names);
181 const std::vector<std::string>&
get_names()
const;
193 void set_names(
const std::vector<std::string>& names);
216 double get_position(
const std::string& joint_name)
const;
243 void set_position(
double position,
const std::string& joint_name);
250 void set_position(
double position,
unsigned int joint_index);
264 double get_velocity(
const std::string& joint_name)
const;
291 void set_velocity(
double velocity,
const std::string& joint_name);
298 void set_velocity(
double velocity,
unsigned int joint_index);
360 double get_torque(
const std::string& joint_name)
const;
368 double get_torque(
unsigned int joint_index)
const;
380 void set_torques(
const std::vector<double>& torques);
387 void set_torque(
double torque,
const std::string& joint_name);
394 void set_torque(
double torque,
unsigned int joint_index);
420 double max_absolute_value,
const JointStateVariable& state_variable_type,
double noise_ratio = 0
432 const Eigen::ArrayXd& max_absolute_value_array,
const JointStateVariable& state_variable_type,
433 const Eigen::ArrayXd& noise_ratio_array
447 virtual Eigen::VectorXd
data()
const;
454 virtual void set_data(
const Eigen::VectorXd&
data)
override;
461 virtual void set_data(
const std::vector<double>&
data)
override;
467 Eigen::ArrayXd
array()
const;
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_);
613inline Eigen::VectorXd JointState::get_all_state_variables()
const {
614 Eigen::VectorXd all_fields(this->
get_size() * 4);
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()));
626 state_variable = new_value;
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()));
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()));
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()));
647 compatible = compatible && (this->names_.size() ==
dynamic_cast<const JointState&
>(state).names_.size());
649 for (
unsigned int i = 0; i < this->names_.size(); ++i) {
650 compatible = (compatible && this->names_[i] ==
dynamic_cast<const JointState&
>(state).names_[i]);
657 return this->names_.size();
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));
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);
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()));
682 this->names_ = names;
686 switch (state_variable_type) {
687 case JointStateVariable::POSITIONS:
690 case JointStateVariable::VELOCITIES:
693 case JointStateVariable::ACCELERATIONS:
696 case JointStateVariable::TORQUES:
699 case JointStateVariable::ALL:
700 return this->get_all_state_variables();
703 return Eigen::Vector3d::Zero();
706inline void JointState::set_state_variable(
709 switch (state_variable_type) {
710 case JointStateVariable::POSITIONS:
714 case JointStateVariable::VELOCITIES:
718 case JointStateVariable::ACCELERATIONS:
722 case JointStateVariable::TORQUES:
726 case JointStateVariable::ALL:
727 this->set_all_state_variables(new_value);
733 Eigen::VectorXd
data = this->
data();
734 return std::vector<double>(
data.data(),
data.data() +
data.size());
Class to define a state in joint space.
const Eigen::VectorXd & get_torques() const
Getter of the torques attribute.
JointState operator+(const JointState &state) const
Overload the + operator.
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.
void set_accelerations(const Eigen::VectorXd &accelerations)
Setter of the accelerations attribute.
double get_torque(const std::string &joint_name) const
Get the torque of a joint by its name, if it exists.
void set_positions(const Eigen::VectorXd &positions)
Setter of the positions attribute.
static JointState Random(const std::string &robot_name, unsigned int nb_joints)
Constructor for a random JointState.
JointState()
Empty constructor for a JointState.
void set_names(unsigned int nb_joints)
Setter of the names attribute from the number of joints.
JointState & operator=(const JointState &state)
Copy assignment operator that have to be defined to the custom assignment operator.
JointState & operator*=(double lambda)
Overload the *= operator with a double gain.
void set_zero()
Set the JointState to a zero value.
const Eigen::VectorXd & get_velocities() const
Getter of the velocities attribute.
unsigned int get_size() const
Getter of the size from the attributes.
Eigen::VectorXd get_state_variable(const JointStateVariable &state_variable_type) const
Getter of the variable value corresponding to the input.
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.
void set_acceleration(double acceleration, const std::string &joint_name)
Set the acceleration of a joint by its name.
JointState operator-(const JointState &state) const
Overload the - operator.
std::vector< double > to_std_vector() const
Return the joint state as a std vector of floats.
static JointState Zero(const std::string &robot_name, unsigned int nb_joints)
Constructor for a zero JointState.
Eigen::ArrayXd array() const
Returns the data vector as an Eigen Array.
JointState & operator+=(const JointState &state)
Overload the += operator.
unsigned int get_joint_index(const std::string &joint_name) const
Get joint index by the name of the joint, if it exists.
JointState copy() const
Return a copy of the JointState.
double get_acceleration(const std::string &joint_name) const
Get the acceleration of a joint by its name, if it exists.
void set_torques(const Eigen::VectorXd &torques)
Setter of the torques attribute.
void set_position(double position, const std::string &joint_name)
Set the position of a joint by its name.
bool is_compatible(const State &state) const
Check if the state is compatible for operations with the state given as argument.
double get_velocity(const std::string &joint_name) const
Get the velocity of a joint by its name, if it exists.
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.
friend std::ostream & operator<<(std::ostream &os, const JointState &state)
Overload the ostream operator for printing.
JointState & operator-=(const JointState &state)
Overload the -= operator.
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.
void set_torque(double torque, const std::string &joint_name)
Set the torque of a joint by its name.
JointState & operator/=(double lambda)
Overload the /= operator with a scalar.
const Eigen::VectorXd & get_accelerations() const
Getter of the accelerations attribute.
friend JointState operator*(double lambda, const JointState &state)
Overload the * operator with a scalar.
JointState(const JointState &state)=default
Copy constructor of a JointState.
void initialize()
Initialize the State to a zero value.
const std::vector< std::string > & get_names() const
Getter of the names attribute.
double get_position(const std::string &joint_name) const
Get the position of a joint by its name, if it exists.
void set_velocity(double velocity, const std::string &joint_name)
Set the velocity of a joint by its name.
const Eigen::VectorXd & get_positions() const
Getter of the positions attribute.
void set_velocities(const Eigen::VectorXd &velocities)
Setter of the velocities attribute.
JointState operator/(double lambda) const
Overload the / operator with a scalar.
virtual Eigen::VectorXd data() const
Returns the data as the concatenation of all the state variables in a single vector.
friend void swap(JointState &state1, JointState &state2)
Swap the values of the two JointState.
Abstract class to represent a state.
void set_filled()
Setter of the empty attribute to false and also reset the timestamp.
virtual bool is_compatible(const State &state) const
Check if the state is compatible for operations with the state given as argument.
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...
void swap(CartesianState &state1, CartesianState &state2)