Control Libraries 6.3.4
Loading...
Searching...
No Matches
JointState.cpp
1#include "state_representation/space/joint/JointState.hpp"
2
3#include "state_representation/exceptions/EmptyStateException.hpp"
4#include "state_representation/exceptions/JointNotFoundException.hpp"
5#include "state_representation/exceptions/IncompatibleStatesException.hpp"
6
7namespace state_representation {
8
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));
13 }
14}
15
17 this->initialize();
18}
19
20JointState::JointState(const std::string& robot_name, unsigned int nb_joints) :
21 State(StateType::JOINT_STATE, robot_name), names_(nb_joints) {
22 this->set_names(nb_joints);
23 this->initialize();
24}
25
26JointState::JointState(const std::string& robot_name, const std::vector<std::string>& joint_names) :
27 State(StateType::JOINT_STATE, robot_name), names_(joint_names) {
28 this->initialize();
29}
30
32 this->State::initialize();
33 // resize
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);
39 // set to zeros
40 this->set_zero();
41}
42
44 this->positions_.setZero();
45 this->velocities_.setZero();
46 this->accelerations_.setZero();
47 this->torques_.setZero();
48}
49
50JointState JointState::Zero(const std::string& robot_name, unsigned int nb_joints) {
51 JointState zero = JointState(robot_name, nb_joints);
52 // as opposed to the constructor specify this state to be filled
53 zero.set_filled();
54 return zero;
55}
56
57JointState JointState::Zero(const std::string& robot_name, const std::vector<std::string>& joint_names) {
58 JointState zero = JointState(robot_name, joint_names);
59 // as opposed to the constructor specify this state to be filled
60 zero.set_filled();
61 return zero;
62}
63
64JointState JointState::Random(const std::string& robot_name, unsigned int nb_joints) {
65 JointState random = JointState(robot_name, nb_joints);
66 // set all the state variables to random
67 random.set_state_variable(Eigen::VectorXd::Random(random.get_size() * 4), JointStateVariable::ALL);
68 return random;
69}
70
71JointState JointState::Random(const std::string& robot_name, const std::vector<std::string>& joint_names) {
72 JointState random = JointState(robot_name, joint_names);
73 // set all the state variables to random
74 random.set_state_variable(Eigen::VectorXd::Random(random.get_size() * 4), JointStateVariable::ALL);
75 return random;
76}
77
78unsigned int JointState::get_joint_index(const std::string& joint_name) const {
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.");
82 }
83 return std::distance(this->names_.begin(), finder);
84}
85
86const Eigen::VectorXd& JointState::get_positions() const {
87 return this->positions_;
88}
89
90double JointState::get_position(const std::string& joint_name) const {
91 return this->positions_(this->get_joint_index(joint_name));
92}
93
94double JointState::get_position(unsigned int joint_index) const {
95 assert_index_in_range(joint_index, this->get_size());
96 return this->positions_(joint_index);
97}
98
99void JointState::set_positions(const Eigen::VectorXd& positions) {
100 this->set_state_variable(this->positions_, positions);
101}
102
103void JointState::set_positions(const std::vector<double>& positions) {
104 this->set_state_variable(this->positions_, positions);
105}
106
107void JointState::set_position(double position, const std::string& joint_name) {
108 this->set_filled();
109 this->positions_(this->get_joint_index(joint_name)) = position;
110}
111
112void JointState::set_position(double position, unsigned int joint_index) {
113 assert_index_in_range(joint_index, this->get_size());
114 this->set_filled();
115 this->positions_(joint_index) = position;
116}
117
118const Eigen::VectorXd& JointState::get_velocities() const {
119 return this->velocities_;
120}
121
122double JointState::get_velocity(const std::string& joint_name) const {
123 return this->velocities_(this->get_joint_index(joint_name));
124}
125
126double JointState::get_velocity(unsigned int joint_index) const {
127 assert_index_in_range(joint_index, this->get_size());
128 return this->velocities_(joint_index);
129}
130
131void JointState::set_velocities(const Eigen::VectorXd& velocities) {
132 this->set_state_variable(this->velocities_, velocities);
133}
134
135void JointState::set_velocities(const std::vector<double>& velocities) {
136 this->set_state_variable(this->velocities_, velocities);
137}
138
139void JointState::set_velocity(double velocity, const std::string& joint_name) {
140 this->set_filled();
141 this->velocities_(this->get_joint_index(joint_name)) = velocity;
142}
143
144void JointState::set_velocity(double velocity, unsigned int joint_index) {
145 assert_index_in_range(joint_index, this->get_size());
146 this->set_filled();
147 this->velocities_(joint_index) = velocity;
148}
149
150const Eigen::VectorXd& JointState::get_accelerations() const {
151 return this->accelerations_;
152}
153
154double JointState::get_acceleration(const std::string& joint_name) const {
155 return this->accelerations_(this->get_joint_index(joint_name));
156}
157
158double JointState::get_acceleration(unsigned int joint_index) const {
159 assert_index_in_range(joint_index, this->get_size());
160 return this->accelerations_(joint_index);
161}
162
163void JointState::set_accelerations(const Eigen::VectorXd& accelerations) {
164 this->set_state_variable(this->accelerations_, accelerations);
165}
166
167void JointState::set_accelerations(const std::vector<double>& accelerations) {
168 this->set_state_variable(this->accelerations_, accelerations);
169}
170
171void JointState::set_acceleration(double acceleration, const std::string& joint_name) {
172 this->set_filled();
173 this->accelerations_(this->get_joint_index(joint_name)) = acceleration;
174}
175
176void JointState::set_acceleration(double acceleration, unsigned int joint_index) {
177 assert_index_in_range(joint_index, this->get_size());
178 this->set_filled();
179 this->accelerations_(joint_index) = acceleration;
180}
181
182const Eigen::VectorXd& JointState::get_torques() const {
183 return this->torques_;
184}
185
186double JointState::get_torque(const std::string& joint_name) const {
187 return this->torques_(this->get_joint_index(joint_name));
188}
189
190double JointState::get_torque(unsigned int joint_index) const {
191 assert_index_in_range(joint_index, this->get_size());
192 return this->torques_(joint_index);
193}
194
195void JointState::set_torques(const Eigen::VectorXd& torques) {
196 this->set_state_variable(this->torques_, torques);
197}
198
199void JointState::set_torques(const std::vector<double>& torques) {
200 this->set_state_variable(this->torques_, torques);
201}
202
203void JointState::set_torque(double torque, const std::string& joint_name) {
204 this->set_filled();
205 this->torques_(this->get_joint_index(joint_name)) = torque;
206}
207
208void JointState::set_torque(double torque, unsigned int joint_index) {
209 assert_index_in_range(joint_index, this->get_size());
210 this->set_filled();
211 this->torques_(joint_index) = torque;
212}
213
215 // sanity check
216 if (this->is_empty()) {
217 throw EmptyStateException(this->get_name() + " state is empty");
218 }
219 if (state.is_empty()) {
220 throw EmptyStateException(state.get_name() + " state is empty");
221 }
222 if (!this->is_compatible(state)) {
224 "The two joint states are incompatible, check name, joint names and order or size"
225 );
226 }
227 this->set_all_state_variables(this->get_all_state_variables() + state.get_all_state_variables());
228 return (*this);
229}
230
232 JointState result(*this);
233 result += state;
234 return result;
235}
236
238 // sanity check
239 if (this->is_empty()) {
240 throw EmptyStateException(this->get_name() + " state is empty");
241 }
242 if (state.is_empty()) {
243 throw EmptyStateException(state.get_name() + " state is empty");
244 }
245 if (!this->is_compatible(state)) {
247 "The two joint states are incompatible, check name, joint names and order or size"
248 );
249 }
250 this->set_all_state_variables(this->get_all_state_variables() - state.get_all_state_variables());
251 return (*this);
252}
253
255 JointState result(*this);
256 result -= state;
257 return result;
258}
259
261 if (this->is_empty()) {
262 throw EmptyStateException(this->get_name() + " state is empty");
263 }
264 this->set_all_state_variables(lambda * this->get_all_state_variables());
265 return (*this);
266}
267
268void JointState::multiply_state_variable(const Eigen::ArrayXd& lambda, const JointStateVariable& state_variable_type) {
269 Eigen::VectorXd state_variable = this->get_state_variable(state_variable_type);
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()));
275 }
276 this->set_state_variable((lambda * state_variable.array()).matrix(), state_variable_type);
277}
278
279void JointState::multiply_state_variable(const Eigen::MatrixXd& lambda, const JointStateVariable& state_variable_type) {
280 Eigen::VectorXd state_variable = this->get_state_variable(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()));
287 }
288 this->set_state_variable(lambda * this->get_state_variable(state_variable_type), state_variable_type);
289}
290
291JointState& JointState::operator*=(const Eigen::ArrayXd& lambda) {
292 this->multiply_state_variable(lambda, JointStateVariable::ALL);
293 return (*this);
294}
295
296JointState& JointState::operator*=(const Eigen::MatrixXd& lambda) {
297 this->multiply_state_variable(lambda, JointStateVariable::ALL);
298 return (*this);
299}
300
301JointState JointState::operator*(double lambda) const {
302 JointState result(*this);
303 result *= lambda;
304 return result;
305}
306
307JointState JointState::operator*(const Eigen::MatrixXd& lambda) const {
308 JointState result(*this);
309 result *= lambda;
310 return result;
311}
312
313JointState JointState::operator*(const Eigen::ArrayXd& lambda) const {
314 JointState result(*this);
315 result *= lambda;
316 return result;
317}
318
320 return JointState::operator*=(1 / lambda);
321}
322
323JointState JointState::operator/(double lambda) const {
324 JointState result(*this);
325 result /= lambda;
326 return result;
327}
328
330 JointState result(*this);
331 return result;
332}
333
334Eigen::VectorXd JointState::data() const {
335 return this->get_all_state_variables();
336}
337
338void JointState::set_data(const Eigen::VectorXd& data) {
339 this->set_all_state_variables(data);
340}
341
342void JointState::set_data(const std::vector<double>& data) {
343 this->set_all_state_variables(Eigen::VectorXd::Map(data.data(), data.size()));
344}
345
346Eigen::ArrayXd JointState::array() const {
347 return this->data().array();
348}
349
351 const Eigen::ArrayXd& max_absolute_value_array, const JointStateVariable& state_variable_type,
352 const Eigen::ArrayXd& noise_ratio_array
353) {
354 Eigen::VectorXd state_variable = this->get_state_variable(state_variable_type);
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()));
360 }
361
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()));
366 }
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)) {
369 // apply dead zone
370 state_variable(i) = 0.0;
371 } else if (abs(state_variable(i)) > max_absolute_value_array(i)) {
372 // clamp to max value
373 state_variable(i) *= max_absolute_value_array(i) / abs(state_variable(i));
374 }
375 }
376 this->set_state_variable(state_variable, state_variable_type);
377}
378
380 double max_absolute_value, const JointStateVariable& state_variable_type, double noise_ratio
381) {
382 Eigen::VectorXd state_variable = this->get_state_variable(state_variable_type);
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));
387}
388
389double JointState::dist(const JointState& state, const JointStateVariable& state_variable_type) const {
390 // sanity check
391 if (this->is_empty()) { throw EmptyStateException(this->get_name() + " state is empty"); }
392 if (state.is_empty()) { throw EmptyStateException(state.get_name() + " state is empty"); }
393 if (!this->is_compatible(state)) {
395 "The two joint states are incompatible, check name, joint names and order or size"
396 );
397 }
398 // calculation
399 double result = 0;
400 if (state_variable_type == JointStateVariable::POSITIONS || state_variable_type == JointStateVariable::ALL) {
401 result += (this->get_positions() - state.get_positions()).norm();
402 }
403 if (state_variable_type == JointStateVariable::VELOCITIES || state_variable_type == JointStateVariable::ALL) {
404 result += (this->get_velocities() - state.get_velocities()).norm();
405 }
406 if (state_variable_type == JointStateVariable::ACCELERATIONS || state_variable_type == JointStateVariable::ALL) {
407 result += (this->get_accelerations() - state.get_accelerations()).norm();
408 }
409 if (state_variable_type == JointStateVariable::TORQUES || state_variable_type == JointStateVariable::ALL) {
410 result += (this->get_torques() - state.get_torques()).norm();
411 }
412 return result;
413}
414
415std::ostream& operator<<(std::ostream& os, const JointState& state) {
416 if (state.is_empty()) {
417 os << "Empty " << state.get_name() << " JointState";
418 } else {
419 os << state.get_name() << " JointState" << std::endl;
420 os << "names: [";
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;
432 os << "torques: [";
433 for (unsigned int i = 0; i < state.torques_.size(); ++i) { os << state.torques_(i) << ", "; }
434 os << "]";
435 }
436 return os;
437}
438
439double dist(const JointState& s1, const JointState& s2, const JointStateVariable& state_variable_type) {
440 return s1.dist(s2, state_variable_type);
441}
442
443JointState operator*(double lambda, const JointState& state) {
444 if (state.is_empty()) { throw EmptyStateException(state.get_name() + " state is empty"); }
445 JointState result(state);
446 result *= lambda;
447 return result;
448}
449
450JointState operator*(const Eigen::MatrixXd& lambda, const JointState& state) {
451 JointState result(state);
452 result *= lambda;
453 return result;
454}
455
456JointState operator*(const Eigen::ArrayXd& lambda, const JointState& state) {
457 JointState result(state);
458 result *= lambda;
459 return result;
460}
461}// 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*=(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
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
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
void initialize()
Initialize the State to a zero value.
Definition: JointState.cpp:31
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
Abstract class to represent a state.
Definition: State.hpp:25
virtual void initialize()
Initialize the State to a zero value.
Definition: State.cpp:61
const std::string & get_name() const
Getter of the name as const reference.
Definition: State.cpp:48
void set_filled()
Setter of the empty attribute to false and also reset the timestamp.
Definition: State.cpp:31
bool is_empty() const
Getter of the empty attribute.
Definition: State.cpp:23
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...
Definition: JointState.hpp:16
std::ostream & operator<<(std::ostream &os, const Ellipsoid &ellipsoid)
Definition: Ellipsoid.cpp:185
StateType
The class types inheriting from State.
Definition: StateType.hpp:13