1#include "state_representation/space/joint/JointState.hpp"
3#include "state_representation/exceptions/EmptyStateException.hpp"
4#include "state_representation/exceptions/IncompatibleStatesException.hpp"
5#include "state_representation/exceptions/InvalidCastException.hpp"
6#include "state_representation/exceptions/JointNotFoundException.hpp"
10using namespace exceptions;
12static void assert_index_in_range(
unsigned int joint_index,
unsigned int size) {
13 if (joint_index > size) {
14 throw JointNotFoundException(
15 "Index '" + std::to_string(joint_index) +
"' is out of range for joint state with size" + std::to_string(size));
19static unsigned int get_state_variable_size_factor(
const JointStateVariable& state_variable_type) {
20 switch (state_variable_type) {
21 case JointStateVariable::POSITIONS:
22 case JointStateVariable::VELOCITIES:
23 case JointStateVariable::ACCELERATIONS:
24 case JointStateVariable::TORQUES:
26 case JointStateVariable::ALL:
34 this->
set_type(StateType::JOINT_STATE);
40 positions_(Eigen::VectorXd::Zero(nb_joints)),
41 velocities_(Eigen::VectorXd::Zero(nb_joints)),
42 accelerations_(Eigen::VectorXd::Zero(nb_joints)),
43 torques_(Eigen::VectorXd::Zero(nb_joints)) {
44 this->
set_type(StateType::JOINT_STATE);
95 switch (state_variable_type) {
96 case JointStateVariable::POSITIONS:
97 return this->positions_;
98 case JointStateVariable::VELOCITIES:
99 return this->velocities_;
100 case JointStateVariable::ACCELERATIONS:
101 return this->accelerations_;
102 case JointStateVariable::TORQUES:
103 return this->torques_;
104 case JointStateVariable::ALL: {
105 Eigen::VectorXd all_fields(this->
get_size() * 4);
106 all_fields << this->positions_, this->velocities_, this->accelerations_, this->torques_;
115 return this->names_.size();
123 auto finder = std::find(this->names_.begin(), this->names_.end(), joint_name);
124 if (finder == this->names_.end()) {
125 throw JointNotFoundException(
"The joint with name '" + joint_name +
"' could not be found in the joint state.");
127 return std::distance(this->names_.begin(), finder);
132 return this->positions_;
141 assert_index_in_range(joint_index, this->
get_size());
142 return this->positions_(joint_index);
147 return this->velocities_;
156 assert_index_in_range(joint_index, this->
get_size());
157 return this->velocities_(joint_index);
162 return this->accelerations_;
171 assert_index_in_range(joint_index, this->
get_size());
172 return this->accelerations_(joint_index);
177 return this->torques_;
186 assert_index_in_range(joint_index, this->
get_size());
187 return this->torques_(joint_index);
195 return this->
data().array();
201 this->
set_state_variable(Eigen::VectorXd::Map(new_value.data(), new_value.size()), state_variable_type);
205 auto expected_size = get_state_variable_size_factor(state_variable_type) * this->
get_size();
206 if (new_value.size() != expected_size) {
208 "Input is of incorrect size, expected " + std::to_string(expected_size) +
", got "
209 + std::to_string(new_value.size()));
211 switch (state_variable_type) {
212 case JointStateVariable::POSITIONS:
213 this->positions_ = new_value;
215 case JointStateVariable::VELOCITIES:
216 this->velocities_ = new_value;
218 case JointStateVariable::ACCELERATIONS:
219 this->accelerations_ = new_value;
221 case JointStateVariable::TORQUES:
222 this->torques_ = new_value;
224 case JointStateVariable::ALL: {
226 this->positions_ = new_value.head(size);
227 this->velocities_ = new_value.segment(size, size);
228 this->accelerations_ = new_value.segment(2 * size, size);
229 this->torques_ = new_value.tail(size);
237 double new_value,
unsigned int joint_index,
const JointStateVariable& state_variable_type
239 assert_index_in_range(joint_index, this->
get_size());
240 switch (state_variable_type) {
241 case JointStateVariable::POSITIONS:
242 this->positions_(joint_index) = new_value;
244 case JointStateVariable::VELOCITIES:
245 this->velocities_(joint_index) = new_value;
247 case JointStateVariable::ACCELERATIONS:
248 this->accelerations_(joint_index) = new_value;
250 case JointStateVariable::TORQUES:
251 this->torques_(joint_index) = new_value;
253 case JointStateVariable::ALL:
254 this->positions_(joint_index) = new_value;
255 this->velocities_(joint_index) = new_value;
256 this->accelerations_(joint_index) = new_value;
257 this->torques_(joint_index) = new_value;
263 if (this->
get_size() != nb_joints) {
265 "Input number of joints is of incorrect size, expected " + std::to_string(this->
get_size()) +
" got "
266 + std::to_string(nb_joints));
268 for (
unsigned int i = 0; i < nb_joints; ++i) {
269 this->names_[i] =
"joint" + std::to_string(i);
275 if (this->
get_size() != names.size()) {
277 "Input number of joints is of incorrect size, expected " + std::to_string(this->
get_size()) +
" got "
278 + std::to_string(names.size()));
280 this->names_ = names;
329 this->
set_state_variable(acceleration, joint_index, JointStateVariable::ACCELERATIONS);
357 const Eigen::ArrayXd& max_absolute_value_array,
const JointStateVariable& state_variable_type,
358 const Eigen::ArrayXd& noise_ratio_array
361 long expected_size = state_variable.size();
362 if (max_absolute_value_array.size() != expected_size) {
364 "Array of max values is of incorrect size: expected " + std::to_string(expected_size) +
", given "
365 + std::to_string(max_absolute_value_array.size()));
368 if (noise_ratio_array.size() != expected_size) {
370 "Array of max values is of incorrect size: expected " + std::to_string(expected_size) +
", given "
371 + std::to_string(noise_ratio_array.size()));
373 for (
int i = 0; i < expected_size; ++i) {
374 if (noise_ratio_array(i) != 0.0 && abs(state_variable(i)) < noise_ratio_array(i) * max_absolute_value_array(i)) {
376 state_variable(i) = 0.0;
377 }
else if (abs(state_variable(i)) > max_absolute_value_array(i)) {
379 state_variable(i) *= max_absolute_value_array(i) / abs(state_variable(i));
386 double max_absolute_value,
const JointStateVariable& state_variable_type,
double noise_ratio
389 long expected_size = state_variable.size();
391 max_absolute_value * Eigen::ArrayXd::Ones(expected_size), state_variable_type,
392 noise_ratio * Eigen::ArrayXd::Ones(expected_size));
403 "The two joint states are incompatible, check name, joint names and order or size"
408 if (state_variable_type == JointStateVariable::POSITIONS || state_variable_type == JointStateVariable::ALL) {
411 if (state_variable_type == JointStateVariable::VELOCITIES || state_variable_type == JointStateVariable::ALL) {
414 if (state_variable_type == JointStateVariable::ACCELERATIONS || state_variable_type == JointStateVariable::ALL) {
417 if (state_variable_type == JointStateVariable::TORQUES || state_variable_type == JointStateVariable::ALL) {
424 return s1.
dist(s2, state_variable_type);
434 auto other =
dynamic_cast<const JointState&
>(state);
435 if (this->names_.size() != other.names_.size()) {
438 for (
unsigned int i = 0; i < this->names_.size(); ++i) {
439 if (this->names_[i] != other.names_[i]) {
444 }
catch (
const std::bad_cast& ex) {
446 std::string(
"Could not cast the given object to a JointState: ") + ex.what());
452 this->positions_.setZero();
453 this->velocities_.setZero();
454 this->accelerations_.setZero();
455 this->torques_.setZero();
461 Eigen::VectorXd
data = this->
data();
462 return {data.data(),
data.data() +
data.size()};
467 long expected_size = state_variable.size();
468 if (lambda.rows() != expected_size || lambda.cols() != expected_size) {
470 "Gain matrix is of incorrect size: expected " + std::to_string(expected_size) +
"x"
471 + std::to_string(expected_size) +
", given " + std::to_string(lambda.rows()) +
"x"
472 + std::to_string(lambda.cols()));
513 "The two joint states are incompatible, check name, joint names and order or size"
517 this->
get_state_variable(JointStateVariable::ALL) + state.get_state_variable(JointStateVariable::ALL),
518 JointStateVariable::ALL
550 s << std::endl <<
"joint names: [";
551 for (
auto& n : this->
get_names()) { s << n <<
", "; }
556 if (this->
get_type() == StateType::JOINT_POSITIONS || this->
get_type() == StateType::JOINT_STATE) {
557 s << std::endl <<
"positions: [";
561 if (this->
get_type() == StateType::JOINT_VELOCITIES || this->
get_type() == StateType::JOINT_STATE) {
562 s << std::endl <<
"velocities: [";
566 if (this->
get_type() == StateType::JOINT_ACCELERATIONS || this->
get_type() == StateType::JOINT_STATE) {
567 s << std::endl <<
"accelerations: [";
571 if (this->
get_type() == StateType::JOINT_TORQUES || this->
get_type() == StateType::JOINT_STATE) {
572 s << std::endl <<
"torques: [";
573 for (
auto& t : this->
get_torques()) { s << t <<
", "; }
580 os << state.to_string();
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
Add another joint sate.
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.
void reset() override
Reset the object to a post-construction state.
void multiply_state_variable(const Eigen::MatrixXd &lambda, const JointStateVariable &state_variable_type)
Proxy function that scale the specified state variable by a matrix.
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 joint state.
JointState()
Empty constructor for a joint state.
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 has to be defined to the custom assignment operator.
JointState & operator*=(double lambda)
Scale inplace by a scalar.
void set_zero()
Set the joint state 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.
bool is_incompatible(const State &state) const override
Check if the joint state is incompatible for operations with the state given as argument.
Eigen::VectorXd get_state_variable(const JointStateVariable &state_variable_type) const
Getter of the variable value corresponding to the input.
void set_state_variable(const Eigen::VectorXd &new_value, const JointStateVariable &state_variable_type)
Setter 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.
std::vector< double > to_std_vector() const
Return the joint state as a std vector.
static JointState Zero(const std::string &robot_name, unsigned int nb_joints)
Constructor for a zero joint state.
Eigen::ArrayXd array() const
Returns the data vector as an Eigen array.
JointState & operator+=(const JointState &state)
Add inplace another joint state.
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 joint state.
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.
std::string to_string() const override
Convert the state to its string representation.
void set_position(double position, const std::string &joint_name)
Set the position of a joint by its name.
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.
JointState & operator-=(const JointState &state)
Compute inplace the difference with another joint state.
void set_torque(double torque, const std::string &joint_name)
Set the torque of a joint by its name.
friend double dist(const JointState &s1, const JointState &s2, const JointStateVariable &state_variable_type)
Compute the distance between two joint states.
JointState & operator/=(double lambda)
Scale inplace by a scalar.
const Eigen::VectorXd & get_accelerations() const
Getter of the accelerations attribute.
JointState operator-() const
Negate a joint state.
friend JointState operator*(double lambda, const JointState &state)
Scale a joint state by a scalar.
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
Scale a joint state by 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 joint states.
Abstract class to represent a state.
void reset_timestamp()
Reset the timestamp attribute to now.
const StateType & get_type() const
Getter of the type attribute.
void set_type(const StateType &type)
Setter of the state type attribute.
virtual std::string to_string() const
Convert the state to its string representation.
virtual void reset()
Reset the object to a post-construction state.
void assert_not_empty() const
Throw an exception if the state is empty.
bool is_empty() const
Getter of the empty attribute.
void set_empty(bool empty=true)
Setter of the empty attribute.
Exception that is thrown when a joint name or index is out of range.
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 Cartesian states.
std::ostream & operator<<(std::ostream &os, const AnalogIOState &state)
JointStateVariable
Enum representing all the fields (positions, velocities, accelerations and torques) of the JointState...
CartesianAcceleration operator*(double lambda, const CartesianAcceleration &acceleration)