Control Libraries 6.3.4
Loading...
Searching...
No Matches
CartesianState.cpp
1#include "state_representation/space/cartesian/CartesianState.hpp"
2#include "state_representation/exceptions/EmptyStateException.hpp"
3#include "state_representation/exceptions/IncompatibleReferenceFramesException.hpp"
4#include "state_representation/exceptions/NotImplementedException.hpp"
5
6using namespace state_representation::exceptions;
7
8namespace state_representation {
10 this->initialize();
11}
12
13CartesianState::CartesianState(const std::string& name, const std::string& reference) :
14 SpatialState(StateType::CARTESIAN_STATE, name, reference) {
15 this->initialize();
16}
17
19 this->State::initialize();
20 this->set_zero();
21}
22
24 this->position_.setZero();
25 this->orientation_.setIdentity();
26 this->linear_velocity_.setZero();
27 this->angular_velocity_.setZero();
28 this->linear_acceleration_.setZero();
29 this->angular_acceleration_.setZero();
30 this->force_.setZero();
31 this->torque_.setZero();
32}
33
34CartesianState CartesianState::Identity(const std::string& name, const std::string& reference) {
35 CartesianState identity = CartesianState(name, reference);
36 // as opposed to the constructor specify this state to be filled
37 identity.set_filled();
38 return identity;
39}
40
41CartesianState CartesianState::Random(const std::string& name, const std::string& reference) {
42 CartesianState random = CartesianState(name, reference);
43 // set all the state variables to random
44 random.set_state_variable(Eigen::VectorXd::Random(25), CartesianStateVariable::ALL);
45 return random;
46}
47
48const Eigen::Vector3d& CartesianState::get_position() const {
49 return this->position_;
50}
51
52const Eigen::Quaterniond& CartesianState::get_orientation() const {
53 return this->orientation_;
54}
55
57 return Eigen::Vector4d(
58 this->get_orientation().w(), this->get_orientation().x(), this->get_orientation().y(),
59 this->get_orientation().z());
60}
61
62Eigen::Matrix<double, 7, 1> CartesianState::get_pose() const {
63 Eigen::Matrix<double, 7, 1> pose;
64 pose << this->get_position(), this->get_orientation_coefficients();
65 return pose;
66}
67
69 Eigen::Matrix4d pose;
70 pose << this->orientation_.toRotationMatrix(), this->position_, 0., 0., 0., 1;
71 return pose;
72}
73
74const Eigen::Vector3d& CartesianState::get_linear_velocity() const {
75 return this->linear_velocity_;
76}
77
78const Eigen::Vector3d& CartesianState::get_angular_velocity() const {
79 return this->angular_velocity_;
80}
81
82Eigen::Matrix<double, 6, 1> CartesianState::get_twist() const {
83 Eigen::Matrix<double, 6, 1> twist;
84 twist << this->get_linear_velocity(), this->get_angular_velocity();
85 return twist;
86}
87
88const Eigen::Vector3d& CartesianState::get_linear_acceleration() const {
89 return this->linear_acceleration_;
90}
91
92const Eigen::Vector3d& CartesianState::get_angular_acceleration() const {
93 return this->angular_acceleration_;
94}
95
96Eigen::Matrix<double, 6, 1> CartesianState::get_acceleration() const {
97 Eigen::Matrix<double, 6, 1> acceleration;
98 acceleration << this->get_linear_acceleration(), this->get_angular_acceleration();
99 return acceleration;
100}
101
102const Eigen::Vector3d& CartesianState::get_force() const {
103 return this->force_;
104}
105
106const Eigen::Vector3d& CartesianState::get_torque() const {
107 return this->torque_;
108}
109
110Eigen::Matrix<double, 6, 1> CartesianState::get_wrench() const {
111 Eigen::Matrix<double, 6, 1> wrench;
112 wrench << this->get_force(), this->get_torque();
113 return wrench;
114}
115
116void CartesianState::set_position(const Eigen::Vector3d& position) {
117 this->set_state_variable(this->position_, position);
118}
119
120void CartesianState::set_position(const std::vector<double>& position) {
121 this->set_state_variable(this->position_, position);
122}
123
124void CartesianState::set_position(const double& x, const double& y, const double& z) {
125 this->set_position(Eigen::Vector3d(x, y, z));
126}
127
128void CartesianState::set_orientation(const Eigen::Quaterniond& orientation) {
129 this->set_filled();
130 this->orientation_ = orientation.normalized();
131}
132
133void CartesianState::set_orientation(const Eigen::Vector4d& orientation) {
134 this->set_orientation(Eigen::Quaterniond(orientation(0), orientation(1), orientation(2), orientation(3)));
135}
136
137void CartesianState::set_orientation(const std::vector<double>& orientation) {
138 if (orientation.size() != 4) {
139 throw exceptions::IncompatibleSizeException("The input vector is not of size 4 required for orientation");
140 }
141 this->set_orientation(Eigen::Vector4d::Map(orientation.data(), orientation.size()));
142}
143
144void CartesianState::set_orientation(const double& w, const double& x, const double& y, const double& z) {
145 this->set_orientation(Eigen::Vector4d(w, x, y, z));
146}
147
148void CartesianState::set_pose(const Eigen::Vector3d& position, const Eigen::Quaterniond& orientation) {
149 this->set_position(position);
150 this->set_orientation(orientation);
151}
152
153void CartesianState::set_pose(const Eigen::Matrix<double, 7, 1>& pose) {
154 this->set_position(pose.head(3));
155 this->set_orientation(pose.tail(4));
156}
157
158void CartesianState::set_pose(const std::vector<double>& pose) {
159 if (pose.size() != 7) {
160 throw exceptions::IncompatibleSizeException("The input vector is not of size 7 required for pose");
161 }
162 this->set_position(std::vector<double>(pose.begin(), pose.begin() + 3));
163 this->set_orientation(std::vector<double>(pose.begin() + 3, pose.end()));
164}
165
166void CartesianState::set_linear_velocity(const Eigen::Vector3d& linear_velocity) {
167 this->set_state_variable(this->linear_velocity_, linear_velocity);
168}
169
170void CartesianState::set_linear_velocity(const std::vector<double>& linear_velocity) {
171 this->set_state_variable(this->linear_velocity_, linear_velocity);
172}
173
174void CartesianState::set_linear_velocity(const double& x, const double& y, const double& z) {
175 this->set_linear_velocity(Eigen::Vector3d(x, y, z));
176}
177
178void CartesianState::set_angular_velocity(const Eigen::Vector3d& angular_velocity) {
179 this->set_state_variable(this->angular_velocity_, angular_velocity);
180}
181
182void CartesianState::set_angular_velocity(const std::vector<double>& angular_velocity) {
183 this->set_state_variable(this->angular_velocity_, angular_velocity);
184}
185
186void CartesianState::set_angular_velocity(const double& x, const double& y, const double& z) {
187 this->set_angular_velocity(Eigen::Vector3d(x, y, z));
188}
189
190void CartesianState::set_twist(const Eigen::Matrix<double, 6, 1>& twist) {
191 this->set_state_variable(this->linear_velocity_, this->angular_velocity_, twist);
192}
193
194void CartesianState::set_twist(const std::vector<double>& twist) {
195 if (twist.size() != 6) {
196 throw exceptions::IncompatibleSizeException("The input vector is not of size 6 required for twist");
197 }
198 this->set_linear_velocity(std::vector<double>(twist.begin(), twist.begin() + 3));
199 this->set_angular_velocity(std::vector<double>(twist.begin() + 3, twist.end()));
200}
201
202void CartesianState::set_linear_acceleration(const Eigen::Vector3d& linear_acceleration) {
203 this->set_state_variable(this->linear_acceleration_, linear_acceleration);
204}
205
206void CartesianState::set_linear_acceleration(const std::vector<double>& linear_acceleration) {
207 this->set_state_variable(this->linear_acceleration_, linear_acceleration);
208}
209
210void CartesianState::set_linear_acceleration(const double& x, const double& y, const double& z) {
211 this->set_linear_acceleration(Eigen::Vector3d(x, y, z));
212}
213
214void CartesianState::set_angular_acceleration(const Eigen::Vector3d& angular_acceleration) {
215 this->set_state_variable(this->angular_acceleration_, angular_acceleration);
216}
217
218void CartesianState::set_angular_acceleration(const std::vector<double>& angular_acceleration) {
219 this->set_state_variable(this->angular_acceleration_, angular_acceleration);
220}
221
222void CartesianState::set_angular_acceleration(const double& x, const double& y, const double& z) {
223 this->set_angular_acceleration(Eigen::Vector3d(x, y, z));
224}
225
226void CartesianState::set_acceleration(const Eigen::Matrix<double, 6, 1>& acceleration) {
227 this->set_state_variable(this->linear_acceleration_, this->angular_acceleration_, acceleration);
228}
229
230void CartesianState::set_acceleration(const std::vector<double>& acceleration) {
231 if (acceleration.size() != 6) {
232 throw exceptions::IncompatibleSizeException("The input vector is not of size 6 required for acceleration");
233 }
234 this->set_linear_acceleration(std::vector<double>(acceleration.begin(), acceleration.begin() + 3));
235 this->set_angular_acceleration(std::vector<double>(acceleration.begin() + 3, acceleration.end()));
236}
237
238void CartesianState::set_force(const Eigen::Vector3d& force) {
239 this->set_state_variable(this->force_, force);
240}
241
242void CartesianState::set_force(const std::vector<double>& force) {
243 this->set_state_variable(this->force_, force);
244}
245
246void CartesianState::set_force(const double& x, const double& y, const double& z) {
247 this->set_force(Eigen::Vector3d(x, y, z));
248}
249
250void CartesianState::set_torque(const Eigen::Vector3d& torque) {
251 this->set_state_variable(this->torque_, torque);
252}
253
254void CartesianState::set_torque(const std::vector<double>& torque) {
255 this->set_state_variable(this->torque_, torque);
256}
257
258void CartesianState::set_torque(const double& x, const double& y, const double& z) {
259 this->set_torque(Eigen::Vector3d(x, y, z));
260}
261
262void CartesianState::set_wrench(const Eigen::Matrix<double, 6, 1>& wrench) {
263 this->set_state_variable(this->force_, this->torque_, wrench);
264}
265
266void CartesianState::set_wrench(const std::vector<double>& wrench) {
267 if (wrench.size() != 6) {
268 throw exceptions::IncompatibleSizeException("The input vector is not of size 6 required for wrench");
269 }
270 this->set_force(std::vector<double>(wrench.begin(), wrench.begin() + 3));
271 this->set_torque(std::vector<double>(wrench.begin() + 3, wrench.end()));
272}
273
275 // sanity check
276 if (this->is_empty()) {
277 throw EmptyStateException(this->get_name() + " state is empty");
278 }
279 // operation
280 this->set_position(lambda * this->get_position());
281 // calculate the scaled rotation as a displacement from identity
282 Eigen::Quaterniond w = math_tools::log(this->get_orientation());
283 // calculate the orientation corresponding to the scaled velocity
284 this->set_orientation(math_tools::exp(w, lambda / 2.));
285 // calculate the other vectors normally
286 this->set_twist(lambda * this->get_twist());
287 this->set_acceleration(lambda * this->get_acceleration());
288 this->set_wrench(lambda * this->get_wrench());
289 return (*this);
290}
291
293 CartesianState result(*this);
294 result *= lambda;
295 return result;
296}
297
299 if (std::abs(lambda) < std::numeric_limits<double>::min()) {
300 throw std::runtime_error("Division by zero is not allowed");
301 }
302 lambda = 1.0 / lambda;
303 return this->operator*=(lambda);
304}
305
307 CartesianState result(*this);
308 result /= lambda;
309 return result;
310}
311
313 CartesianState result(*this);
314 return result;
315}
316
317Eigen::VectorXd CartesianState::data() const {
318 return this->get_state_variable(CartesianStateVariable::ALL);
319}
320
321void CartesianState::set_data(const Eigen::VectorXd& data) {
322 this->set_all_state_variables(data);
323}
324
325void CartesianState::set_data(const std::vector<double>& data) {
326 this->set_all_state_variables(Eigen::VectorXd::Map(data.data(), data.size()));
327}
328
329Eigen::ArrayXd CartesianState::array() const {
330 return this->data().array();
331}
332
334 // sanity check
335 if (this->is_empty()) {
336 throw EmptyStateException(this->get_name() + " state is empty");
337 }
338 if (state.is_empty()) {
339 throw EmptyStateException(state.get_name() + " state is empty");
340 }
341 if (this->get_name() != state.get_reference_frame()) {
342 throw IncompatibleReferenceFramesException("Expected " + this->get_name() + ", got " + state.get_reference_frame());
343 }
344 this->set_name(state.get_name());
345 // intermediate variables for f_S_b
346 Eigen::Vector3d f_P_b = this->get_position();
347 Eigen::Quaterniond f_R_b = this->get_orientation();
348 Eigen::Vector3d f_v_b = this->get_linear_velocity();
349 Eigen::Vector3d f_omega_b = this->get_angular_velocity();
350 Eigen::Vector3d f_a_b = this->get_linear_acceleration();
351 Eigen::Vector3d f_alpha_b = this->get_angular_acceleration();
352 // intermediate variables for b_S_c
353 Eigen::Vector3d b_P_c = state.get_position();
354 Eigen::Quaterniond b_R_c =
355 (this->get_orientation().dot(state.get_orientation()) > 0) ? state.get_orientation() : Eigen::Quaterniond(
356 -state.get_orientation().coeffs());
357 Eigen::Vector3d b_v_c = state.get_linear_velocity();
358 Eigen::Vector3d b_omega_c = state.get_angular_velocity();
359 Eigen::Vector3d b_a_c = state.get_linear_acceleration();
360 Eigen::Vector3d b_alpha_c = state.get_angular_acceleration();
361 // pose
362 this->set_position(f_P_b + f_R_b * b_P_c);
363 this->set_orientation(f_R_b * b_R_c);
364 // twist
365 this->set_linear_velocity(f_v_b + f_R_b * b_v_c + f_omega_b.cross(f_R_b * b_P_c));
366 this->set_angular_velocity(f_omega_b + f_R_b * b_omega_c);
367 // acceleration
369 f_a_b + f_R_b * b_a_c + f_alpha_b.cross(f_R_b * b_P_c) + 2 * f_omega_b.cross(f_R_b * b_v_c)
370 + f_omega_b.cross(f_omega_b.cross(f_R_b * b_P_c)));
371 this->set_angular_acceleration(f_alpha_b + f_R_b * b_alpha_c + f_omega_b.cross(f_R_b * b_omega_c));
372 // wrench
373 //TODO
374 return (*this);
375}
376
378 CartesianState result(*this);
379 result *= state;
380 return result;
381}
382
384 // sanity check
385 if (this->is_empty()) {
386 throw EmptyStateException(this->get_name() + " state is empty");
387 }
388 if (state.is_empty()) {
389 throw EmptyStateException(state.get_name() + " state is empty");
390 }
391 if (!(this->get_reference_frame() == state.get_reference_frame())) {
392 throw IncompatibleReferenceFramesException("The two states do not have the same reference frame");
393 }
394 // operation on pose
395 this->set_position(this->get_position() + state.get_position());
396 // specific operation on quaternion using Hamilton product
397 Eigen::Quaterniond orientation =
398 (this->get_orientation().dot(state.get_orientation()) > 0) ? state.get_orientation() : Eigen::Quaterniond(
399 -state.get_orientation().coeffs());
400 this->set_orientation(this->get_orientation() * orientation);
401 // operation on twist
402 this->set_twist(this->get_twist() + state.get_twist());
403 // operation on acceleration
404 this->set_acceleration(this->get_acceleration() + state.get_acceleration());
405 // operation on wrench
406 this->set_wrench(this->get_wrench() + state.get_wrench());
407 return (*this);
408}
409
411 CartesianState result(*this);
412 result += state;
413 return result;
414}
415
417 // sanity check
418 if (this->is_empty()) {
419 throw EmptyStateException(this->get_name() + " state is empty");
420 }
421 if (state.is_empty()) {
422 throw EmptyStateException(state.get_name() + " state is empty");
423 }
424 if (!(this->get_reference_frame() == state.get_reference_frame())) {
425 throw IncompatibleReferenceFramesException("The two states do not have the same reference frame");
426 }
427 // operation on pose
428 this->set_position(this->get_position() - state.get_position());
429 // specific operation on quaternion using Hamilton product
430 Eigen::Quaterniond orientation =
431 (this->get_orientation().dot(state.get_orientation()) > 0) ? state.get_orientation() : Eigen::Quaterniond(
432 -state.get_orientation().coeffs());
433 this->set_orientation(this->get_orientation() * orientation.conjugate());
434 // operation on twist
435 this->set_twist(this->get_twist() - state.get_twist());
436 // operation on acceleration
437 this->set_acceleration(this->get_acceleration() - state.get_acceleration());
438 // operation on wrench
439 this->set_wrench(this->get_wrench() - state.get_wrench());
440 return (*this);
441}
442
444 CartesianState result(*this);
445 result -= state;
446 return result;
447}
448
450 CartesianState result(*this);
451 // inverse name and reference frame
452 std::string ref = result.get_reference_frame();
453 result.set_reference_frame(result.get_name());
454 result.set_name(ref);
455 // intermediate variables for f_S_b
456 Eigen::Vector3d f_P_b = this->get_position();
457 Eigen::Quaterniond f_R_b = this->get_orientation();
458 Eigen::Vector3d f_v_b = this->get_linear_velocity();
459 Eigen::Vector3d f_omega_b = this->get_angular_velocity();
460 Eigen::Vector3d f_a_b = this->get_linear_acceleration();
461 Eigen::Vector3d f_alpha_b = this->get_angular_acceleration();
462 // computation for b_S_f
463 Eigen::Quaterniond b_R_f = f_R_b.conjugate();
464 Eigen::Vector3d b_P_f = b_R_f * (-f_P_b);
465 Eigen::Vector3d b_v_f = b_R_f * (-f_v_b);
466 Eigen::Vector3d b_omega_f = b_R_f * (-f_omega_b);
467 Eigen::Vector3d b_a_f = b_R_f * f_a_b; // not sure if minus is needed
468 Eigen::Vector3d b_alpha_f = b_R_f * f_alpha_b;// no minus for sure
469 // wrench
470 //TODO
471 // collect the results
472 result.set_position(b_P_f);
473 result.set_orientation(b_R_f);
474 result.set_linear_velocity(b_v_f);
475 result.set_angular_velocity(b_omega_f);
476 result.set_linear_acceleration(b_a_f);
477 result.set_angular_acceleration(b_alpha_f);
478 return result;
479}
480
482 double max_norm, const CartesianStateVariable& state_variable_type, double noise_ratio
483) {
484 if (state_variable_type == CartesianStateVariable::ORIENTATION || state_variable_type == CartesianStateVariable::POSE
485 || state_variable_type == CartesianStateVariable::ALL) {
486 throw NotImplementedException("clamp_state_variable is not implemented for this CartesianStateVariable");
487 }
488 Eigen::VectorXd state_variable_value = this->get_state_variable(state_variable_type);
489 if (noise_ratio != 0 && state_variable_value.norm() < noise_ratio * max_norm) {
490 // apply a dead zone
491 state_variable_value.setZero();
492 } else if (state_variable_value.norm() > max_norm) {
493 // clamp the values to their maximum amplitude provided
494 state_variable_value = max_norm * state_variable_value.normalized();
495 }
496 this->set_state_variable(state_variable_value, state_variable_type);
497}
498
499double CartesianState::dist(const CartesianState& state, const CartesianStateVariable& state_variable_type) const {
500 // sanity check
501 if (this->is_empty()) {
502 throw EmptyStateException(this->get_name() + " state is empty");
503 }
504 if (state.is_empty()) {
505 throw EmptyStateException(state.get_name() + " state is empty");
506 }
507 if (!(this->get_reference_frame() == state.get_reference_frame())) {
508 throw IncompatibleReferenceFramesException("The two states do not have the same reference frame");
509 }
510 // calculation
511 double result = 0;
512 if (state_variable_type == CartesianStateVariable::POSITION || state_variable_type == CartesianStateVariable::POSE
513 || state_variable_type == CartesianStateVariable::ALL) {
514 result += (this->get_position() - state.get_position()).norm();
515 }
516 if (state_variable_type == CartesianStateVariable::ORIENTATION || state_variable_type == CartesianStateVariable::POSE
517 || state_variable_type == CartesianStateVariable::ALL) {
518 // https://math.stackexchange.com/questions/90081/quaternion-distance for orientation
519 double inner_product = this->get_orientation().dot(state.get_orientation());
520 double argument = 2 * inner_product * inner_product - 1;
521 result += acos(std::min(1.0, std::max(-1.0, argument)));
522 }
523 if (state_variable_type == CartesianStateVariable::LINEAR_VELOCITY
524 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
525 result += (this->get_linear_velocity() - state.get_linear_velocity()).norm();
526 }
527 if (state_variable_type == CartesianStateVariable::ANGULAR_VELOCITY
528 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
529 result += (this->get_angular_velocity() - state.get_angular_velocity()).norm();
530 }
531 if (state_variable_type == CartesianStateVariable::LINEAR_ACCELERATION
532 || state_variable_type == CartesianStateVariable::ACCELERATION
533 || state_variable_type == CartesianStateVariable::ALL) {
534 result += (this->get_linear_acceleration() - state.get_linear_acceleration()).norm();
535 }
536 if (state_variable_type == CartesianStateVariable::ANGULAR_ACCELERATION
537 || state_variable_type == CartesianStateVariable::ACCELERATION
538 || state_variable_type == CartesianStateVariable::ALL) {
539 result += (this->get_angular_acceleration() - state.get_angular_acceleration()).norm();
540 }
541 if (state_variable_type == CartesianStateVariable::FORCE || state_variable_type == CartesianStateVariable::WRENCH
542 || state_variable_type == CartesianStateVariable::ALL) {
543 result += (this->get_force() - state.get_force()).norm();
544 }
545 if (state_variable_type == CartesianStateVariable::TORQUE || state_variable_type == CartesianStateVariable::WRENCH
546 || state_variable_type == CartesianStateVariable::ALL) {
547 result += (this->get_torque() - state.get_torque()).norm();
548 }
549 return result;
550}
551
552std::vector<double> CartesianState::norms(const CartesianStateVariable& state_variable_type) const {
553 // compute the norms for each independent state variable
554 std::vector<double> norms;
555 if (state_variable_type == CartesianStateVariable::POSITION || state_variable_type == CartesianStateVariable::POSE
556 || state_variable_type == CartesianStateVariable::ALL) {
557 norms.push_back(this->get_position().norm());
558 }
559 if (state_variable_type == CartesianStateVariable::ORIENTATION || state_variable_type == CartesianStateVariable::POSE
560 || state_variable_type == CartesianStateVariable::ALL) {
561 norms.push_back(this->get_orientation().norm());
562 }
563 if (state_variable_type == CartesianStateVariable::LINEAR_VELOCITY
564 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
565 norms.push_back(this->get_linear_velocity().norm());
566 }
567 if (state_variable_type == CartesianStateVariable::ANGULAR_VELOCITY
568 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
569 norms.push_back(this->get_angular_velocity().norm());
570 }
571 if (state_variable_type == CartesianStateVariable::LINEAR_ACCELERATION
572 || state_variable_type == CartesianStateVariable::ACCELERATION
573 || state_variable_type == CartesianStateVariable::ALL) {
574 norms.push_back(this->get_linear_acceleration().norm());
575 }
576 if (state_variable_type == CartesianStateVariable::ANGULAR_ACCELERATION
577 || state_variable_type == CartesianStateVariable::ACCELERATION
578 || state_variable_type == CartesianStateVariable::ALL) {
579 norms.push_back(this->get_angular_acceleration().norm());
580 }
581 if (state_variable_type == CartesianStateVariable::FORCE || state_variable_type == CartesianStateVariable::WRENCH
582 || state_variable_type == CartesianStateVariable::ALL) {
583 norms.push_back(this->get_force().norm());
584 }
585 if (state_variable_type == CartesianStateVariable::TORQUE || state_variable_type == CartesianStateVariable::WRENCH
586 || state_variable_type == CartesianStateVariable::ALL) {
587 norms.push_back(this->get_torque().norm());
588 }
589 return norms;
590}
591
592void CartesianState::normalize(const CartesianStateVariable& state_variable_type) {
593 if (state_variable_type == CartesianStateVariable::POSITION || state_variable_type == CartesianStateVariable::POSE
594 || state_variable_type == CartesianStateVariable::ALL) {
595 this->position_.normalize();
596 }
597 // there shouldn't be a need to renormalize orientation as it is already normalized
598 if (state_variable_type == CartesianStateVariable::ORIENTATION || state_variable_type == CartesianStateVariable::POSE
599 || state_variable_type == CartesianStateVariable::ALL) {
600 this->orientation_.normalize();
601 }
602 if (state_variable_type == CartesianStateVariable::LINEAR_VELOCITY
603 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
604 this->linear_velocity_.normalize();
605 }
606 if (state_variable_type == CartesianStateVariable::ANGULAR_VELOCITY
607 || state_variable_type == CartesianStateVariable::TWIST || state_variable_type == CartesianStateVariable::ALL) {
608 this->angular_velocity_.normalize();
609 }
610 if (state_variable_type == CartesianStateVariable::LINEAR_ACCELERATION
611 || state_variable_type == CartesianStateVariable::ACCELERATION
612 || state_variable_type == CartesianStateVariable::ALL) {
613 this->linear_acceleration_.normalize();
614 }
615 if (state_variable_type == CartesianStateVariable::ANGULAR_ACCELERATION
616 || state_variable_type == CartesianStateVariable::ACCELERATION
617 || state_variable_type == CartesianStateVariable::ALL) {
618 this->angular_acceleration_.normalize();
619 }
620 if (state_variable_type == CartesianStateVariable::FORCE || state_variable_type == CartesianStateVariable::WRENCH
621 || state_variable_type == CartesianStateVariable::ALL) {
622 this->force_.normalize();
623 }
624 if (state_variable_type == CartesianStateVariable::TORQUE || state_variable_type == CartesianStateVariable::WRENCH
625 || state_variable_type == CartesianStateVariable::ALL) {
626 this->torque_.normalize();
627 }
628}
629
630CartesianState CartesianState::normalized(const CartesianStateVariable& state_variable_type) const {
631 CartesianState result(*this);
632 result.normalize(state_variable_type);
633 return result;
634}
635
636std::ostream& operator<<(std::ostream& os, const CartesianState& state) {
637 if (state.is_empty()) {
638 os << "Empty CartesianState";
639 } else {
640 os << state.get_name() << " CartesianState expressed in " << state.get_reference_frame() << " frame" << std::endl;
641 os << "position: (" << state.position_(0) << ", ";
642 os << state.position_(1) << ", ";
643 os << state.position_(2) << ")" << std::endl;
644 os << "orientation: (" << state.orientation_.w() << ", ";
645 os << state.orientation_.x() << ", ";
646 os << state.orientation_.y() << ", ";
647 os << state.orientation_.z() << ")";
648 Eigen::AngleAxisd axis_angle(state.orientation_);
649 os << " <=> theta: " << axis_angle.angle() << ", ";
650 os << "axis: (" << axis_angle.axis()(0) << ", ";
651 os << axis_angle.axis()(1) << ", ";
652 os << axis_angle.axis()(2) << ")" << std::endl;
653 os << "linear velocity: (" << state.linear_velocity_(0) << ", ";
654 os << state.linear_velocity_(1) << ", ";
655 os << state.linear_velocity_(2) << ")" << std::endl;
656 os << "angular velocity: (" << state.angular_velocity_(0) << ", ";
657 os << state.angular_velocity_(1) << ", ";
658 os << state.angular_velocity_(2) << ")" << std::endl;
659 os << "linear acceleration: (" << state.linear_acceleration_(0) << ", ";
660 os << state.linear_acceleration_(1) << ", ";
661 os << state.linear_acceleration_(2) << ")" << std::endl;
662 os << "angular acceleration: (" << state.angular_acceleration_(0) << ", ";
663 os << state.angular_acceleration_(1) << ", ";
664 os << state.angular_acceleration_(2) << ")" << std::endl;
665 os << "force: (" << state.force_(0) << ", ";
666 os << state.force_(1) << ", ";
667 os << state.force_(2) << ")" << std::endl;
668 os << "torque: (" << state.torque_(0) << ", ";
669 os << state.torque_(1) << ", ";
670 os << state.torque_(2) << ")";
671 }
672 return os;
673}
674
675CartesianState operator*(double lambda, const CartesianState& state) {
676 return state * lambda;
677}
678
679double dist(const CartesianState& s1, const CartesianState& s2, const CartesianStateVariable& state_variable_type) {
680 return s1.dist(s2, state_variable_type);
681}
682}// namespace state_representation
Class to represent a state in Cartesian space.
void set_zero()
Set the State to a zero value.
const Eigen::Vector3d & get_force() const
Getter of the force attribute.
void set_acceleration(const Eigen::Matrix< double, 6, 1 > &acceleration)
Setter of the linear and angular acceleration from a 6d acceleration vector.
CartesianState inverse() const
Compute the inverse of the current CartesianState.
Eigen::ArrayXd array() const
Return the data vector as an Eigen Array.
const Eigen::Vector3d & get_torque() const
Getter of the torque attribute.
static CartesianState Random(const std::string &name, const std::string &reference="world")
Constructor for a random state.
void set_orientation(const Eigen::Quaterniond &orientation)
Setter of the orientation.
const Eigen::Vector3d & get_linear_velocity() const
Getter of the linear velocity attribute.
void set_angular_acceleration(const Eigen::Vector3d &angular_acceleration)
Setter of the angular velocity attribute.
const Eigen::Vector3d & get_position() const
Getter of the position attribute.
void normalize(const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL)
Normalize inplace the state at the state variable given in argument (default is full state)
CartesianState operator-(const CartesianState &state) const
Overload the - operator.
virtual std::vector< double > norms(const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL) const
Compute the norms of the state variable specified by the input type (default is full state)
void set_force(const Eigen::Vector3d &force)
Setter of the force attribute.
Eigen::Matrix< double, 6, 1 > get_twist() const
Getter of the 6d twist from linear and angular velocity attributes.
CartesianState normalized(const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL) const
Compute the normalized state at the state variable given in argument (default is full state)
friend double dist(const CartesianState &s1, const CartesianState &s2, const CartesianStateVariable &state_variable_type)
compute the distance between two CartesianStates
static CartesianState Identity(const std::string &name, const std::string &reference="world")
Constructor for the identity CartesianState (identity pose and 0 for the rest)
CartesianState & operator/=(double lambda)
Overload the /= operator with a scalar.
const Eigen::Vector3d & get_linear_acceleration() const
Getter of the linear acceleration attribute.
Eigen::Matrix4d get_transformation_matrix() const
Getter of a pose from position and orientation attributes.
void set_linear_acceleration(const Eigen::Vector3d &linear_acceleration)
Setter of the linear acceleration attribute.
void clamp_state_variable(double max_norm, const CartesianStateVariable &state_variable_type, double noise_ratio=0)
Clamp inplace the norm of the a specific state variable.
Eigen::Matrix< double, 7, 1 > get_pose() const
Getter of a pose from position and orientation attributes.
void set_position(const Eigen::Vector3d &position)
Setter of the position.
void set_wrench(const Eigen::Matrix< double, 6, 1 > &wrench)
Setter of the force and torque from a 6d wrench vector.
const Eigen::Quaterniond & get_orientation() const
Getter of the orientation attribute.
Eigen::Matrix< double, 6, 1 > get_wrench() const
Getter of the 6d wrench from force and torque attributes.
Eigen::VectorXd get_state_variable(const CartesianStateVariable &state_variable_type) const
Getter of the variable value corresponding to the input.
void set_twist(const Eigen::Matrix< double, 6, 1 > &twist)
Setter of the linear and angular velocities from a 6d twist vector.
CartesianState & operator+=(const CartesianState &state)
Overload the += operator.
virtual Eigen::VectorXd data() const
Return the data as the concatenation of all the state variables in a single vector.
const Eigen::Vector3d & get_angular_velocity() const
Getter of the angular velocity attribute.
void set_pose(const Eigen::Vector3d &position, const Eigen::Quaterniond &orientation)
Setter of the pose from both position and orientation.
CartesianState copy() const
Return a copy of the CartesianState.
CartesianState & operator-=(const CartesianState &state)
Overload the -= operator.
void set_torque(const Eigen::Vector3d &torque)
Setter of the torque attribute.
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.
Eigen::Vector4d get_orientation_coefficients() const
Getter of the orientation attribute as Vector4d of coefficients Beware, quaternion coefficients are r...
CartesianState & operator*=(const CartesianState &state)
Overload the *= operator with another state by deriving the equations of motions.
const Eigen::Vector3d & get_angular_acceleration() const
Getter of the angular acceleration attribute.
CartesianState operator/(double lambda) const
Overload the / operator with a scalar.
double dist(const CartesianState &state, const CartesianStateVariable &state_variable_type=CartesianStateVariable::ALL) const
Compute the distance to another state as the sum of distances between each features.
void initialize() override
Initialize the CartesianState to a zero value.
void set_linear_velocity(const Eigen::Vector3d &linear_velocity)
Setter of the linear velocity attribute.
void set_angular_velocity(const Eigen::Vector3d &angular_velocity)
Setter of the angular velocity attribute.
friend CartesianState operator*(double lambda, const CartesianState &state)
Overload the * operator with a scalar.
Eigen::Matrix< double, 6, 1 > get_acceleration() const
Getter of the 6d acceleration from linear and angular acceleration attributes.
CartesianState operator+(const CartesianState &state) const
Overload the + operator.
virtual void set_reference_frame(const std::string &reference_frame)
Setter of the reference frame.
const std::string & get_reference_frame() const
Getter of the reference frame as const reference.
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
virtual void set_name(const std::string &name)
Setter of the name.
Definition: State.cpp:52
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
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)
std::ostream & operator<<(std::ostream &os, const Ellipsoid &ellipsoid)
Definition: Ellipsoid.cpp:185
StateType
The class types inheriting from State.
Definition: StateType.hpp:13