Control Libraries 6.3.4
Loading...
Searching...
No Matches
CartesianPose.cpp
1#include "state_representation/space/cartesian/CartesianPose.hpp"
2#include "state_representation/exceptions/EmptyStateException.hpp"
3#include "state_representation/exceptions/IncompatibleSizeException.hpp"
4
5using namespace state_representation::exceptions;
6
7namespace state_representation {
9 this->set_type(StateType::CARTESIAN_POSE);
10}
11
12CartesianPose::CartesianPose(const std::string& name, const std::string& reference) : CartesianState(name, reference) {
13 this->set_type(StateType::CARTESIAN_POSE);
14}
15
16CartesianPose::CartesianPose(const std::string& name, const Eigen::Vector3d& position, const std::string& reference) :
17 CartesianState(name, reference) {
18 this->set_type(StateType::CARTESIAN_POSE);
19 this->set_position(position);
20}
21
23 const std::string& name, double x, double y, double z, const std::string& reference
24) : CartesianState(name, reference) {
25 this->set_type(StateType::CARTESIAN_POSE);
26 this->set_position(x, y, z);
27}
28
30 const std::string& name, const Eigen::Quaterniond& orientation, const std::string& reference
31) : CartesianState(name, reference) {
32 this->set_type(StateType::CARTESIAN_POSE);
33 this->set_orientation(orientation);
34}
35
37 const std::string& name, const Eigen::Vector3d& position, const Eigen::Quaterniond& orientation,
38 const std::string& reference
39) : CartesianState(name, reference) {
40 this->set_type(StateType::CARTESIAN_POSE);
41 this->set_position(position);
42 this->set_orientation(orientation);
43}
44
46 // set all the state variables to 0 except position and orientation
47 this->set_type(StateType::CARTESIAN_POSE);
48 this->set_zero();
49 this->set_pose(state.get_pose());
50 this->set_empty(state.is_empty());
51}
52
53CartesianPose::CartesianPose(const CartesianPose& pose) : CartesianPose(static_cast<const CartesianState&>(pose)) {}
54
55CartesianPose::CartesianPose(const CartesianTwist& twist) : CartesianPose(std::chrono::seconds(1) * twist) {}
56
57CartesianPose CartesianPose::Identity(const std::string& name, const std::string& reference) {
58 return CartesianState::Identity(name, reference);
59}
60
61CartesianPose CartesianPose::Random(const std::string& name, const std::string& reference) {
62 return CartesianPose(name, Eigen::Vector3d::Random(), Eigen::Quaterniond::UnitRandom(), reference);
63}
64
65Eigen::Vector3d CartesianPose::operator*(const Eigen::Vector3d& vector) const {
66 return this->get_orientation() * vector + this->get_position();
67}
68
69CartesianPose& CartesianPose::operator*=(const CartesianPose& pose) {
71 return (*this);
72}
73
75 return this->CartesianState::operator*(pose);
76}
77
79 return this->CartesianState::operator*(state);
80}
81
83 return this->CartesianState::operator*(twist);
84}
85
87 return this->CartesianState::operator*(acceleration);
88}
89
91 return this->CartesianState::operator*(wrench);
92}
93
94CartesianPose& CartesianPose::operator*=(double lambda) {
95 this->CartesianState::operator*=(lambda);
96 return (*this);
97}
98
100 return this->CartesianState::operator*(lambda);
101}
102
104 this->CartesianState::operator/=(lambda);
105 return (*this);
106}
107
109 return this->CartesianState::operator/(lambda);
110}
111
113 this->CartesianState::operator+=(pose);
114 return (*this);
115}
116
118 return this->CartesianState::operator+(pose);
119}
120
122 this->CartesianState::operator-=(pose);
123 return (*this);
124}
125
127 return this->CartesianState::operator-(pose);
128}
129
130CartesianTwist CartesianPose::operator/(const std::chrono::nanoseconds& dt) const {
131 // sanity check
132 if (this->is_empty()) {
133 throw EmptyStateException(this->get_name() + " state is empty");
134 }
135 // operations
136 CartesianTwist twist(this->get_name(), this->get_reference_frame());
137 // convert the period to a double with the second as reference
138 double period = dt.count();
139 period /= 1e9;
140 // set linear velocity
141 twist.set_linear_velocity(this->get_position() / period);
142 // set angular velocity from the log of the quaternion error
143 Eigen::Quaterniond log_q = math_tools::log(this->get_orientation());
144 if (this->get_orientation().dot(log_q) < 0) {
145 log_q = Eigen::Quaterniond(-log_q.coeffs());
146 }
147 twist.set_angular_velocity(2 * log_q.vec() / period);
148 return twist;
149}
150
152 CartesianPose result(*this);
153 return result;
154}
155
156Eigen::VectorXd CartesianPose::data() const {
157 return this->get_pose();
158}
159
160void CartesianPose::set_data(const Eigen::VectorXd& data) {
161 if (data.size() != 7) {
163 "Input is of incorrect size: expected 7, given " + std::to_string(data.size()));
164 }
165 this->set_pose(data);
166}
167
168void CartesianPose::set_data(const std::vector<double>& data) {
169 this->set_data(Eigen::VectorXd::Map(data.data(), data.size()));
170}
171
173 return this->CartesianState::inverse();
174}
175
176std::ostream& operator<<(std::ostream& os, const CartesianPose& pose) {
177 if (pose.is_empty()) {
178 os << "Empty CartesianPose";
179 } else {
180 os << pose.get_name() << " CartesianPose expressed in " << pose.get_reference_frame() << " frame" << std::endl;
181 os << "position: (" << pose.get_position()(0) << ", ";
182 os << pose.get_position()(1) << ", ";
183 os << pose.get_position()(2) << ")" << std::endl;
184 os << "orientation: (" << pose.get_orientation().w() << ", ";
185 os << pose.get_orientation().x() << ", ";
186 os << pose.get_orientation().y() << ", ";
187 os << pose.get_orientation().z() << ")";
188 Eigen::AngleAxisd axis_angle(pose.get_orientation());
189 os << " <=> theta: " << axis_angle.angle() << ", ";
190 os << "axis: (" << axis_angle.axis()(0) << ", ";
191 os << axis_angle.axis()(1) << ", ";
192 os << axis_angle.axis()(2) << ")";
193 }
194 return os;
195}
196
198 return state.operator*(pose);
199}
200
201CartesianPose operator*(double lambda, const CartesianPose& pose) {
202 return pose * lambda;
203}
204}// 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.
void set_data(const Eigen::VectorXd &data) override
Set the pose data from an Eigen vector.
CartesianPose & operator-=(const CartesianPose &pose)
Overload the -= operator.
friend CartesianPose operator*(const CartesianState &state, const CartesianPose &pose)
Overload the * operator with a CartesianState.
CartesianPose operator-(const CartesianPose &pose) const
Overload the - operator with a pose.
CartesianPose & operator/=(double lambda)
Overload the /= operator with a scalar.
static CartesianPose Identity(const std::string &name, const std::string &reference="world")
Constructor for the identity pose.
CartesianPose & operator+=(const CartesianPose &pose)
Overload the += operator.
Eigen::VectorXd data() const override
Returns the pose data as an Eigen vector.
CartesianPose operator/(double lambda) const
Overload the / operator with a scalar.
CartesianPose copy() const
Return a copy of the CartesianPose.
CartesianPose operator+(const CartesianPose &pose) const
Overload the + operator with a pose.
CartesianPose inverse() const
Compute the inverse of the current CartesianPose.
static CartesianPose Random(const std::string &name, const std::string &reference="world")
Constructor for a random pose.
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_position() const
Getter of the position attribute.
CartesianState operator-(const CartesianState &state) const
Overload the - operator.
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.
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.
const Eigen::Quaterniond & get_orientation() const
Getter of the orientation attribute.
CartesianState & operator+=(const CartesianState &state)
Overload the += operator.
void set_pose(const Eigen::Vector3d &position, const Eigen::Quaterniond &orientation)
Setter of the pose from both position and orientation.
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.
Class to define wrench in cartesian space as 3D force and torque vectors.
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