1#include "state_representation/space/joint/JointState.hpp" 
    3#include "state_representation/exceptions/EmptyStateException.hpp" 
    4#include "state_representation/exceptions/JointNotFoundException.hpp" 
    5#include "state_representation/exceptions/IncompatibleStatesException.hpp" 
    9static void assert_index_in_range(
unsigned int joint_index, 
unsigned int size) {
 
   10  if (joint_index > size) {
 
   12        "Index '" + std::to_string(joint_index) + 
"' is out of range for joint state with size" + std::to_string(size));
 
   34  unsigned int size = this->names_.size();
 
   35  this->positions_.resize(size);
 
   36  this->velocities_.resize(size);
 
   37  this->accelerations_.resize(size);
 
   38  this->torques_.resize(size);
 
   44  this->positions_.setZero();
 
   45  this->velocities_.setZero();
 
   46  this->accelerations_.setZero();
 
   47  this->torques_.setZero();
 
   67  random.set_state_variable(Eigen::VectorXd::Random(random.
get_size() * 4), JointStateVariable::ALL);
 
   74  random.set_state_variable(Eigen::VectorXd::Random(random.
get_size() * 4), JointStateVariable::ALL);
 
   79  auto finder = std::find(this->names_.begin(), this->names_.end(), joint_name);
 
   80  if (finder == this->names_.end()) {
 
   81    throw JointNotFoundException(
"The joint with name '" + joint_name + 
"' could not be found in the joint state.");
 
   83  return std::distance(this->names_.begin(), finder);
 
   87  return this->positions_;
 
   95  assert_index_in_range(joint_index, this->
get_size());
 
   96  return this->positions_(joint_index);
 
  100  this->set_state_variable(this->positions_, positions);
 
  104  this->set_state_variable(this->positions_, positions);
 
  113  assert_index_in_range(joint_index, this->
get_size());
 
  115  this->positions_(joint_index) = position;
 
  119  return this->velocities_;
 
  127  assert_index_in_range(joint_index, this->
get_size());
 
  128  return this->velocities_(joint_index);
 
  132  this->set_state_variable(this->velocities_, velocities);
 
  136  this->set_state_variable(this->velocities_, velocities);
 
  145  assert_index_in_range(joint_index, this->
get_size());
 
  147  this->velocities_(joint_index) = velocity;
 
  151  return this->accelerations_;
 
  159  assert_index_in_range(joint_index, this->
get_size());
 
  160  return this->accelerations_(joint_index);
 
  164  this->set_state_variable(this->accelerations_, accelerations);
 
  168  this->set_state_variable(this->accelerations_, accelerations);
 
  173  this->accelerations_(this->
get_joint_index(joint_name)) = acceleration;
 
  177  assert_index_in_range(joint_index, this->
get_size());
 
  179  this->accelerations_(joint_index) = acceleration;
 
  183  return this->torques_;
 
  191  assert_index_in_range(joint_index, this->
get_size());
 
  192  return this->torques_(joint_index);
 
  196  this->set_state_variable(this->torques_, torques);
 
  200  this->set_state_variable(this->torques_, torques);
 
  209  assert_index_in_range(joint_index, this->
get_size());
 
  211  this->torques_(joint_index) = torque;
 
  224        "The two joint states are incompatible, check name, joint names and order or size" 
  227  this->set_all_state_variables(this->get_all_state_variables() + state.get_all_state_variables());
 
  247        "The two joint states are incompatible, check name, joint names and order or size" 
  250  this->set_all_state_variables(this->get_all_state_variables() - state.get_all_state_variables());
 
  264  this->set_all_state_variables(lambda * this->get_all_state_variables());
 
  270  int expected_size = state_variable.size();
 
  271  if (lambda.size() != expected_size) {
 
  273        "Gain matrix is of incorrect size: expected " + std::to_string(expected_size) + 
", given " 
  274            + std::to_string(lambda.size()));
 
  276  this->set_state_variable((lambda * state_variable.array()).matrix(), state_variable_type);
 
  281  int expected_size = state_variable.size();
 
  282  if (lambda.rows() != expected_size || lambda.cols() != expected_size) {
 
  284        "Gain matrix is of incorrect size: expected " + std::to_string(expected_size) + 
"x" 
  285            + std::to_string(expected_size) + 
", given " + std::to_string(lambda.rows()) + 
"x" 
  286            + std::to_string(lambda.cols()));
 
  288  this->set_state_variable(lambda * this->
get_state_variable(state_variable_type), state_variable_type);
 
  335  return this->get_all_state_variables();
 
  339  this->set_all_state_variables(
data);
 
  343  this->set_all_state_variables(Eigen::VectorXd::Map(
data.data(), 
data.size()));
 
  347  return this->
data().array();
 
  351    const Eigen::ArrayXd& max_absolute_value_array, 
const JointStateVariable& state_variable_type,
 
  352    const Eigen::ArrayXd& noise_ratio_array
 
  355  int expected_size = state_variable.size();
 
  356  if (max_absolute_value_array.size() != expected_size) {
 
  358        "Array of max values is of incorrect size: expected " + std::to_string(expected_size) + 
", given " 
  359            + std::to_string(max_absolute_value_array.size()));
 
  362  if (noise_ratio_array.size() != expected_size) {
 
  364        "Array of max values is of incorrect size: expected " + std::to_string(expected_size) + 
", given " 
  365            + std::to_string(noise_ratio_array.size()));
 
  367  for (
int i = 0; i < expected_size; ++i) {
 
  368    if (noise_ratio_array(i) != 0.0 && abs(state_variable(i)) < noise_ratio_array(i) * max_absolute_value_array(i)) {
 
  370      state_variable(i) = 0.0;
 
  371    } 
else if (abs(state_variable(i)) > max_absolute_value_array(i)) {
 
  373      state_variable(i) *= max_absolute_value_array(i) / abs(state_variable(i));
 
  376  this->set_state_variable(state_variable, state_variable_type);
 
  380    double max_absolute_value, 
const JointStateVariable& state_variable_type, 
double noise_ratio
 
  383  int expected_size = state_variable.size();
 
  385      max_absolute_value * Eigen::ArrayXd::Ones(expected_size), state_variable_type,
 
  386      noise_ratio * Eigen::ArrayXd::Ones(expected_size));
 
  395        "The two joint states are incompatible, check name, joint names and order or size" 
  400  if (state_variable_type == JointStateVariable::POSITIONS || state_variable_type == JointStateVariable::ALL) {
 
  403  if (state_variable_type == JointStateVariable::VELOCITIES || state_variable_type == JointStateVariable::ALL) {
 
  406  if (state_variable_type == JointStateVariable::ACCELERATIONS || state_variable_type == JointStateVariable::ALL) {
 
  409  if (state_variable_type == JointStateVariable::TORQUES || state_variable_type == JointStateVariable::ALL) {
 
  416  if (state.is_empty()) {
 
  417    os << 
"Empty " << state.get_name() << 
" JointState";
 
  419    os << state.get_name() << 
" JointState" << std::endl;
 
  421    for (
auto& n: state.names_) { os << n << 
", "; }
 
  422    os << 
"]" << std::endl;
 
  423    os << 
"positions: [";
 
  424    for (
unsigned int i = 0; i < state.positions_.size(); ++i) { os << state.positions_(i) << 
", "; }
 
  425    os << 
"]" << std::endl;
 
  426    os << 
"velocities: [";
 
  427    for (
unsigned int i = 0; i < state.velocities_.size(); ++i) { os << state.velocities_(i) << 
", "; }
 
  428    os << 
"]" << std::endl;
 
  429    os << 
"accelerations: [";
 
  430    for (
unsigned int i = 0; i < state.accelerations_.size(); ++i) { os << state.accelerations_(i) << 
", "; }
 
  431    os << 
"]" << std::endl;
 
  433    for (
unsigned int i = 0; i < state.torques_.size(); ++i) { os << state.torques_(i) << 
", "; }
 
  440  return s1.
dist(s2, state_variable_type);
 
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*=(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.
 
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.
 
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.
 
void initialize()
Initialize the State to a zero value.
 
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.
 
Abstract class to represent a state.
 
virtual void initialize()
Initialize the State to a zero value.
 
const std::string & get_name() const
Getter of the name as const reference.
 
void set_filled()
Setter of the empty attribute to false and also reset the timestamp.
 
bool is_empty() const
Getter 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 CartesianStates
 
CartesianAcceleration operator*(const CartesianState &state, const CartesianAcceleration &acceleration)
 
JointStateVariable
Enum representing all the fields (positions, velocities, accelerations and torques) of the JointState...
 
std::ostream & operator<<(std::ostream &os, const Ellipsoid &ellipsoid)
 
StateType
The class types inheriting from State.