Control Libraries 7.4.0
Loading...
Searching...
No Matches
JointState.hpp
1#pragma once
2
3#include "state_representation/State.hpp"
4#include "state_representation/exceptions/IncompatibleSizeException.hpp"
5
6namespace state_representation {
7
8class JointState;
9
16 POSITIONS, VELOCITIES, ACCELERATIONS, TORQUES, ALL
17};
18
27double dist(
28 const JointState& s1, const JointState& s2, const JointStateVariable& state_variable_type = JointStateVariable::ALL
29);
30
35class JointState : public State {
36public:
40 JointState();
41
47 explicit JointState(const std::string& robot_name, unsigned int nb_joints = 0);
48
54 JointState(const std::string& robot_name, const std::vector<std::string>& joint_names);
55
60 JointState(const JointState& state);
61
68 static JointState Zero(const std::string& robot_name, unsigned int nb_joints);
69
76 static JointState Zero(const std::string& robot_name, const std::vector<std::string>& joint_names);
77
84 static JointState Random(const std::string& robot_name, unsigned int nb_joints);
85
92 static JointState Random(const std::string& robot_name, const std::vector<std::string>& joint_names);
93
99 friend void swap(JointState& state1, JointState& state2);
100
106 JointState& operator=(const JointState& state);
107
111 unsigned int get_size() const;
112
117 const std::vector<std::string>& get_names() const;
118
125 unsigned int get_joint_index(const std::string& joint_name) const;
126
131 const Eigen::VectorXd& get_positions() const;
132
139 double get_position(const std::string& joint_name) const;
140
147 double get_position(unsigned int joint_index) const;
148
153 const Eigen::VectorXd& get_velocities() const;
154
161 double get_velocity(const std::string& joint_name) const;
162
169 double get_velocity(unsigned int joint_index) const;
170
175 const Eigen::VectorXd& get_accelerations() const;
176
183 double get_acceleration(const std::string& joint_name) const;
184
191 double get_acceleration(unsigned int joint_index) const;
192
197 const Eigen::VectorXd& get_torques() const;
198
205 double get_torque(const std::string& joint_name) const;
206
213 double get_torque(unsigned int joint_index) const;
214
219 virtual Eigen::VectorXd data() const;
220
225 Eigen::ArrayXd array() const;
226
231 void set_names(unsigned int nb_joints);
232
237 void set_names(const std::vector<std::string>& names);
238
243 void set_positions(const Eigen::VectorXd& positions);
244
249 void set_positions(const std::vector<double>& positions);
250
257 void set_position(double position, const std::string& joint_name);
258
265 void set_position(double position, unsigned int joint_index);
266
271 void set_velocities(const Eigen::VectorXd& velocities);
272
277 void set_velocities(const std::vector<double>& velocities);
278
285 void set_velocity(double velocity, const std::string& joint_name);
286
293 void set_velocity(double velocity, unsigned int joint_index);
294
299 void set_accelerations(const Eigen::VectorXd& accelerations);
300
305 void set_accelerations(const std::vector<double>& accelerations);
306
313 void set_acceleration(double acceleration, const std::string& joint_name);
314
321 void set_acceleration(double acceleration, unsigned int joint_index);
322
327 void set_torques(const Eigen::VectorXd& torques);
328
333 void set_torques(const std::vector<double>& torques);
334
341 void set_torque(double torque, const std::string& joint_name);
342
349 void set_torque(double torque, unsigned int joint_index);
350
355 virtual void set_data(const Eigen::VectorXd& data) override;
356
361 virtual void set_data(const std::vector<double>& data) override;
362
371 double max_absolute_value, const JointStateVariable& state_variable_type, double noise_ratio = 0
372 );
373
383 const Eigen::ArrayXd& max_absolute_value_array, const JointStateVariable& state_variable_type,
384 const Eigen::ArrayXd& noise_ratio_array
385 );
386
390 JointState copy() const;
391
399 double dist(const JointState& state, const JointStateVariable& state_variable_type = JointStateVariable::ALL) const;
400
409 friend double dist(const JointState& s1, const JointState& s2, const JointStateVariable& state_variable_type);
410
414 void reset() override;
415
420 bool is_incompatible(const State& state) const override;
421
425 void set_zero();
426
431 std::vector<double> to_std_vector() const;
432
439 JointState& operator*=(double lambda);
440
447 JointState operator*(double lambda) const;
448
456 friend JointState operator*(double lambda, const JointState& state);
457
464 friend JointState operator*(const Eigen::MatrixXd& lambda, const JointState& state);
465
472 JointState& operator/=(double lambda);
473
480 JointState operator/(double lambda) const;
481
487 JointState& operator+=(const JointState& state);
488
494 JointState operator+(const JointState& state) const;
495
500 JointState operator-() const;
501
507 JointState& operator-=(const JointState& state);
508
514 JointState operator-(const JointState& state) const;
515
522 friend std::ostream& operator<<(std::ostream& os, const JointState& state);
523
524protected:
530 void multiply_state_variable(const Eigen::MatrixXd& lambda, const JointStateVariable& state_variable_type);
531
537 Eigen::VectorXd get_state_variable(const JointStateVariable& state_variable_type) const;
538
544 void set_state_variable(const Eigen::VectorXd& new_value, const JointStateVariable& state_variable_type);
545
551 void set_state_variable(const std::vector<double>& new_value, const JointStateVariable& state_variable_type);
552
560 void set_state_variable(double new_value, unsigned int joint_index, const JointStateVariable& state_variable_type);
561
565 std::string to_string() const override;
566
567private:
568 std::vector<std::string> names_;
569 Eigen::VectorXd positions_;
570 Eigen::VectorXd velocities_;
571 Eigen::VectorXd accelerations_;
572 Eigen::VectorXd torques_;
573};
574
575inline void swap(JointState& state1, JointState& state2) {
576 swap(static_cast<State&>(state1), static_cast<State&>(state2));
577 std::swap(state1.names_, state2.names_);
578 std::swap(state1.positions_, state2.positions_);
579 std::swap(state1.velocities_, state2.velocities_);
580 std::swap(state1.accelerations_, state2.accelerations_);
581 std::swap(state1.torques_, state2.torques_);
582}
583}// namespace state_representation
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.
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.
friend std::ostream & operator<<(std::ostream &os, const JointState &state)
Overload the ostream operator for printing.
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.
Definition State.hpp:25
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.
void swap(AnalogIOState &state1, AnalogIOState &state2)
JointStateVariable
Enum representing all the fields (positions, velocities, accelerations and torques) of the JointState...