Control Libraries 6.3.4
Loading...
Searching...
No Matches
CartesianTwist.cpp
1#include "state_representation/space/cartesian/CartesianTwist.hpp"
2#include "state_representation/exceptions/EmptyStateException.hpp"
3
4using namespace state_representation::exceptions;
5
6namespace state_representation {
8 this->set_type(StateType::CARTESIAN_TWIST);
9}
10
11CartesianTwist::CartesianTwist(const std::string& name, const std::string& reference) :
12 CartesianState(name, reference) {
13 this->set_type(StateType::CARTESIAN_TWIST);
14}
15
17 const std::string& name, const Eigen::Vector3d& linear_velocity, const std::string& reference
18) : CartesianState(name, reference) {
19 this->set_type(StateType::CARTESIAN_TWIST);
20 this->set_linear_velocity(linear_velocity);
21}
22
24 const std::string& name, const Eigen::Vector3d& linear_velocity, const Eigen::Vector3d& angular_velocity,
25 const std::string& reference
26) : CartesianState(name, reference) {
27 this->set_type(StateType::CARTESIAN_TWIST);
28 this->set_linear_velocity(linear_velocity);
29 this->set_angular_velocity(angular_velocity);
30}
31
33 const std::string& name, const Eigen::Matrix<double, 6, 1>& twist, const std::string& reference
34) : CartesianState(name, reference) {
35 this->set_type(StateType::CARTESIAN_TWIST);
36 this->set_twist(twist);
37}
38
40 // set all the state variables to 0 except linear and angular velocities
41 this->set_type(StateType::CARTESIAN_TWIST);
42 this->set_zero();
43 this->set_twist(state.get_twist());
44 this->set_empty(state.is_empty());
45}
46
48 CartesianTwist(static_cast<const CartesianState&>(twist)) {}
49
50CartesianTwist::CartesianTwist(const CartesianPose& pose) : CartesianTwist(pose / std::chrono::seconds(1)) {}
51
53 CartesianTwist(acceleration * std::chrono::seconds(1)) {}
54
55CartesianTwist CartesianTwist::Zero(const std::string& name, const std::string& reference) {
56 return CartesianState::Identity(name, reference);
57}
58
59CartesianTwist CartesianTwist::Random(const std::string& name, const std::string& reference) {
60 // separating in the two lines in needed to avoid compilation error due to ambiguous constructor call
61 Eigen::Matrix<double, 6, 1> random = Eigen::Matrix<double, 6, 1>::Random();
62 return CartesianTwist(name, random, reference);
63}
64
66 this->CartesianState::operator+=(twist);
67 return (*this);
68}
69
71 return this->CartesianState::operator+(twist);
72}
73
75 this->CartesianState::operator-=(twist);
76 return (*this);
77}
78
80 return this->CartesianState::operator-(twist);
81}
82
83CartesianTwist& CartesianTwist::operator*=(double lambda) {
84 this->CartesianState::operator*=(lambda);
85 return (*this);
86}
87
89 return this->CartesianState::operator*(lambda);
90}
91
93 this->CartesianState::operator/=(lambda);
94 return (*this);
95}
96
98 return this->CartesianState::operator/(lambda);
99}
100
101CartesianTwist& CartesianTwist::operator*=(const Eigen::Matrix<double, 6, 6>& lambda) {
102 // sanity check
103 if (this->is_empty()) {
104 throw EmptyStateException(this->get_name() + " state is empty");
105 }
106 // operation
107 this->set_linear_velocity(lambda.block<3, 3>(0, 0) * this->get_linear_velocity());
108 this->set_angular_velocity(lambda.block<3, 3>(3, 3) * this->get_angular_velocity());
109 return (*this);
110}
111
112CartesianPose CartesianTwist::operator*(const std::chrono::nanoseconds& dt) const {
113 // sanity check
114 if (this->is_empty()) {
115 throw EmptyStateException(this->get_name() + " state is empty");
116 }
117 // operations
118 CartesianPose displacement(this->get_name(), this->get_reference_frame());
119 // convert the period to a double with the second as reference
120 double period = dt.count();
121 period /= 1e9;
122 // convert the velocities into a displacement
123 displacement.set_position(period * this->get_linear_velocity());
124 Eigen::Quaterniond angular_displacement = Eigen::Quaterniond::Identity();
125 double angular_norm = this->get_angular_velocity().norm();
126 if (angular_norm > 1e-4) {
127 double theta = angular_norm * period * 0.5;
128 angular_displacement.w() = cos(theta);
129 angular_displacement.vec() = this->get_angular_velocity() / angular_norm * sin(theta);
130 }
131 displacement.set_orientation(angular_displacement);
132 return displacement;
133}
134
135CartesianAcceleration CartesianTwist::operator/(const std::chrono::nanoseconds& dt) const {
136 if (this->is_empty()) {
137 throw EmptyStateException(this->get_name() + " state is empty");
138 }
139 CartesianAcceleration acceleration(this->get_name(), this->get_reference_frame());
140 // convert the period to a double with the second as reference
141 double period = dt.count();
142 period /= 1e9;
143 acceleration.set_linear_acceleration(this->get_linear_velocity() / period);
144 acceleration.set_angular_acceleration(this->get_angular_velocity() / period);
145 return acceleration;
146}
147
149 double max_linear, double max_angular, double linear_noise_ratio, double angular_noise_ratio
150) {
151 // clamp linear
152 this->clamp_state_variable(max_linear, CartesianStateVariable::LINEAR_VELOCITY, linear_noise_ratio);
153 // clamp angular
154 this->clamp_state_variable(max_angular, CartesianStateVariable::ANGULAR_VELOCITY, angular_noise_ratio);
155}
156
158 double max_linear, double max_angular, double linear_noise_ratio, double angular_noise_ratio
159) const {
160 CartesianTwist result(*this);
161 result.clamp(max_linear, max_angular, linear_noise_ratio, angular_noise_ratio);
162 return result;
163}
164
166 CartesianTwist result(*this);
167 return result;
168}
169
170Eigen::VectorXd CartesianTwist::data() const {
171 return this->get_twist();
172}
173
174void CartesianTwist::set_data(const Eigen::VectorXd& data) {
175 if (data.size() != 6) {
177 "Input is of incorrect size: expected 6, given " + std::to_string(data.size()));
178 }
179 this->set_twist(data);
180}
181
182void CartesianTwist::set_data(const std::vector<double>& data) {
183 this->set_data(Eigen::VectorXd::Map(data.data(), data.size()));
184}
185
187 return this->CartesianState::inverse();
188}
189
190std::ostream& operator<<(std::ostream& os, const CartesianTwist& twist) {
191 if (twist.is_empty()) {
192 os << "Empty CartesianTwist";
193 } else {
194 os << twist.get_name() << " CartesianTwist expressed in " << twist.get_reference_frame() << " frame" << std::endl;
195 os << "linear_velocity: (" << twist.get_linear_velocity()(0) << ", ";
196 os << twist.get_linear_velocity()(1) << ", ";
197 os << twist.get_linear_velocity()(2) << ")" << std::endl;
198 os << "angular_velocity: (" << twist.get_angular_velocity()(0) << ", ";
199 os << twist.get_angular_velocity()(1) << ", ";
200 os << twist.get_angular_velocity()(2) << ")";
201 }
202 return os;
203}
204
206 return state.operator*(twist);
207}
208
209CartesianTwist operator*(double lambda, const CartesianTwist& twist) {
210 return twist * lambda;
211}
212
213CartesianTwist operator*(const Eigen::Matrix<double, 6, 6>& lambda, const CartesianTwist& twist) {
214 CartesianTwist result(twist);
215 result *= lambda;
216 return result;
217}
218
219CartesianPose operator*(const std::chrono::nanoseconds& dt, const CartesianTwist& twist) {
220 return twist * dt;
221}
222}// namespace state_representation
Class to define acceleration in cartesian space as 3D linear and angular acceleration vectors.
Class to define CartesianPose in cartesian space as 3D position and quaternion based orientation.
Class to represent a state in Cartesian space.
void set_zero()
Set the State to a zero value.
CartesianState inverse() const
Compute the inverse of the current CartesianState.
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.
CartesianState operator-(const CartesianState &state) const
Overload the - operator.
Eigen::Matrix< double, 6, 1 > get_twist() const
Getter of the 6d twist from linear and angular velocity attributes.
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.
void set_linear_acceleration(const Eigen::Vector3d &linear_acceleration)
Setter of the linear acceleration attribute.
void set_position(const Eigen::Vector3d &position)
Setter of the position.
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.
const Eigen::Vector3d & get_angular_velocity() const
Getter of the angular velocity attribute.
CartesianState & operator-=(const CartesianState &state)
Overload the -= operator.
CartesianState & operator*=(const CartesianState &state)
Overload the *= operator with another state by deriving the equations of motions.
CartesianState operator/(double lambda) const
Overload the / operator with a scalar.
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.
CartesianState operator+(const CartesianState &state) const
Overload the + operator.
Class to define twist in cartesian space as 3D linear and angular velocity vectors.
friend CartesianTwist operator*(const CartesianState &state, const CartesianTwist &twist)
Overload the * operator with a CartesianState.
CartesianTwist & operator+=(const CartesianTwist &twist)
Overload the += operator.
CartesianTwist operator-(const CartesianTwist &twist) const
Overload the - operator with a twist.
Eigen::VectorXd data() const override
Returns the twist data as an Eigen vector.
CartesianTwist clamped(double max_linear, double max_angular, double noise_ratio=0, double angular_noise_ratio=0) const
Return the clamped twist.
static CartesianTwist Random(const std::string &name, const std::string &reference="world")
Constructor for a random twist.
CartesianTwist & operator-=(const CartesianTwist &twist)
Overload the -= operator.
static CartesianTwist Zero(const std::string &name, const std::string &reference="world")
Constructor for the zero twist.
void set_data(const Eigen::VectorXd &data) override
Set the twist data from an Eigen vector.
void clamp(double max_linear, double max_angular, double linear_noise_ratio=0, double angular_noise_ratio=0)
Clamp inplace the magnitude of the twist to the values in argument.
CartesianTwist operator+(const CartesianTwist &twist) const
Overload the + operator with a twist.
CartesianTwist copy() const
Return a copy of the CartesianTwist.
CartesianTwist operator/(double lambda) const
Overload the / operator with a scalar.
CartesianTwist inverse() const
Compute the inverse of the current CartesianTwist.
CartesianTwist & operator/=(double lambda)
Overload the /= operator with a scalar.
const std::string & get_reference_frame() const
Getter of the reference frame as const reference.
const std::string & get_name() const
Getter of the name as const reference.
Definition: State.cpp:48
void set_type(const StateType &type)
Override the state type.
Definition: State.cpp:89
bool is_empty() const
Getter of the empty attribute.
Definition: State.cpp:23
void set_empty(bool empty=true)
Setter of the empty attribute.
Definition: State.cpp:27
Core state variables and objects.
CartesianAcceleration operator*(const CartesianState &state, const CartesianAcceleration &acceleration)
std::ostream & operator<<(std::ostream &os, const Ellipsoid &ellipsoid)
Definition: Ellipsoid.cpp:185