Control Libraries 6.3.4
Loading...
Searching...
No Matches
JointPositions.cpp
1#include "state_representation/space/joint/JointPositions.hpp"
2
3#include "state_representation/exceptions/EmptyStateException.hpp"
4
5using namespace state_representation::exceptions;
6
7namespace state_representation {
9 this->set_type(StateType::JOINT_POSITIONS);
10}
11
12JointPositions::JointPositions(const std::string& robot_name, unsigned int nb_joints) :
13 JointState(robot_name, nb_joints) {
14 this->set_type(StateType::JOINT_POSITIONS);
15}
16
17JointPositions::JointPositions(const std::string& robot_name, const Eigen::VectorXd& positions) :
18 JointState(robot_name, positions.size()) {
19 this->set_type(StateType::JOINT_POSITIONS);
20 this->set_positions(positions);
21}
22
23JointPositions::JointPositions(const std::string& robot_name, const std::vector<std::string>& joint_names) :
24 JointState(robot_name, joint_names) {
25 this->set_type(StateType::JOINT_POSITIONS);
26}
27
29 const std::string& robot_name, const std::vector<std::string>& joint_names, const Eigen::VectorXd& positions
30) : JointState(robot_name, joint_names) {
31 this->set_type(StateType::JOINT_POSITIONS);
32 this->set_positions(positions);
33}
34
36 // set all the state variables to 0 except positions
37 this->set_type(StateType::JOINT_POSITIONS);
38 this->set_zero();
39 this->set_positions(state.get_positions());
40 this->set_empty(state.is_empty());
41}
42
44 JointPositions(static_cast<const JointState&>(positions)) {}
45
47 JointPositions(std::chrono::seconds(1) * velocities) {}
48
49JointPositions JointPositions::Zero(const std::string& robot_name, unsigned int nb_joints) {
50 return JointState::Zero(robot_name, nb_joints);
51}
52
53JointPositions JointPositions::Zero(const std::string& robot_name, const std::vector<std::string>& joint_names) {
54 return JointState::Zero(robot_name, joint_names);
55}
56
57JointPositions JointPositions::Random(const std::string& robot_name, unsigned int nb_joints) {
58 return JointPositions(robot_name, Eigen::VectorXd::Random(nb_joints));
59}
60
61JointPositions JointPositions::Random(const std::string& robot_name, const std::vector<std::string>& joint_names) {
62 return JointPositions(robot_name, joint_names, Eigen::VectorXd::Random(joint_names.size()));
63}
64
66 this->JointState::operator+=(positions);
67 return (*this);
68}
69
71 return this->JointState::operator+(positions);
72}
73
75 this->JointState::operator-=(positions);
76 return (*this);
77}
78
80 return this->JointState::operator-(positions);
81}
82
84 this->JointState::operator*=(lambda);
85 return (*this);
86}
87
89 return this->JointState::operator*(lambda);
90}
91
92JointPositions& JointPositions::operator*=(const Eigen::ArrayXd& lambda) {
93 this->multiply_state_variable(lambda, JointStateVariable::POSITIONS);
94 return (*this);
95}
96
97JointPositions JointPositions::operator*(const Eigen::ArrayXd& lambda) const {
98 JointPositions result(*this);
99 result *= lambda;
100 return result;
101}
102
103JointPositions& JointPositions::operator*=(const Eigen::MatrixXd& lambda) {
104 this->multiply_state_variable(lambda, JointStateVariable::POSITIONS);
105 return (*this);
106}
107
108JointPositions JointPositions::operator*(const Eigen::MatrixXd& lambda) const {
109 JointPositions result(*this);
110 result *= lambda;
111 return result;
112}
113
115 this->JointState::operator/=(lambda);
116 return (*this);
117}
118
120 return this->JointState::operator/(lambda);
121}
122
123JointVelocities JointPositions::operator/(const std::chrono::nanoseconds& dt) const {
124 if (this->is_empty()) { throw EmptyStateException(this->get_name() + " state is empty"); }
125 // operations
126 JointVelocities velocities(this->get_name(), this->get_names());
127 // convert the period to a double with the second as reference
128 double period = dt.count();
129 period /= 1e9;
130 // divide the positions by this period value and assign it as velocities
131 velocities.set_velocities(this->get_positions() / period);
132 return velocities;
133}
134
136 JointPositions result(*this);
137 return result;
138}
139
140Eigen::VectorXd JointPositions::data() const {
141 return this->get_positions();
142}
143
144void JointPositions::set_data(const Eigen::VectorXd& data) {
145 this->set_positions(data);
146}
147
148void JointPositions::set_data(const std::vector<double>& data) {
149 this->set_positions(Eigen::VectorXd::Map(data.data(), data.size()));
150}
151
152void JointPositions::clamp(double max_absolute_value, double noise_ratio) {
153 this->clamp_state_variable(max_absolute_value, JointStateVariable::POSITIONS, noise_ratio);
154}
155
156JointPositions JointPositions::clamped(double max_absolute_value, double noise_ratio) const {
157 JointPositions result(*this);
158 result.clamp(max_absolute_value, noise_ratio);
159 return result;
160}
161
162void JointPositions::clamp(const Eigen::ArrayXd& max_absolute_value_array, const Eigen::ArrayXd& noise_ratio_array) {
163 this->clamp_state_variable(max_absolute_value_array, JointStateVariable::POSITIONS, noise_ratio_array);
164}
165
167 const Eigen::ArrayXd& max_absolute_value_array, const Eigen::ArrayXd& noise_ratio_array
168) const {
169 JointPositions result(*this);
170 result.clamp(max_absolute_value_array, noise_ratio_array);
171 return result;
172}
173
174std::ostream& operator<<(std::ostream& os, const JointPositions& positions) {
175 if (positions.is_empty()) {
176 os << "Empty JointPositions";
177 } else {
178 os << positions.get_name() << " JointPositions" << std::endl;
179 os << "names: [";
180 for (auto& n: positions.get_names()) { os << n << ", "; }
181 os << "]" << std::endl;
182 os << "positions: [";
183 for (unsigned int i = 0; i < positions.get_size(); ++i) { os << positions.get_positions()(i) << ", "; }
184 os << "]";
185 }
186 return os;
187}
188
189JointPositions operator*(double lambda, const JointPositions& positions) {
190 JointPositions result(positions);
191 result *= lambda;
192 return result;
193}
194
195JointPositions operator*(const Eigen::ArrayXd& lambda, const JointPositions& positions) {
196 JointPositions result(positions);
197 result *= lambda;
198 return result;
199}
200
201JointPositions operator*(const Eigen::MatrixXd& lambda, const JointPositions& positions) {
202 JointPositions result(positions);
203 result *= lambda;
204 return result;
205}
206}// namespace state_representation
Class to define a positions of the joints.
void clamp(double max_absolute_value, double noise_ratio=0.)
Clamp inplace the magnitude of the positions to the value in argument.
static JointPositions Zero(const std::string &robot_name, unsigned int nb_joints)
Constructor for the zero JointPositions.
Eigen::VectorXd data() const override
Returns the positions data as an Eigen vector.
JointPositions & operator*=(double lambda)
Overload the *= operator with a double gain.
virtual void set_data(const Eigen::VectorXd &data) override
Set the positions data from an Eigen vector.
JointPositions & operator-=(const JointPositions &positions)
Overload the -= operator.
JointPositions & operator+=(const JointPositions &positions)
Overload the += operator.
JointPositions operator/(double lambda) const
Overload the / operator with a scalar.
JointPositions clamped(double max_absolute_value, double noise_ratio=0.) const
Return the position clamped to the value in argument.
static JointPositions Random(const std::string &robot_name, unsigned int nb_joints)
Constructor for the random JointPositions.
JointPositions operator+(const JointPositions &positions) const
Overload the + operator.
friend JointPositions operator*(double lambda, const JointPositions &positions)
Overload the * operator with a scalar.
JointPositions copy() const
Return a copy of the JointPositions.
JointPositions operator-(const JointPositions &positions) const
Overload the - operator.
JointPositions & operator/=(double lambda)
Overload the /= operator with a scalar.
Class to define a state in joint space.
Definition: JointState.hpp:36
JointState operator+(const JointState &state) const
Overload the + operator.
Definition: JointState.cpp:231
void set_positions(const Eigen::VectorXd &positions)
Setter of the positions attribute.
Definition: JointState.cpp:99
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
unsigned int get_size() const
Getter of the size from the attributes.
Definition: JointState.hpp:656
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
JointState & operator+=(const JointState &state)
Overload the += operator.
Definition: JointState.cpp:214
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
JointState & operator/=(double lambda)
Overload the /= operator with a scalar.
Definition: JointState.cpp:319
friend JointState operator*(double lambda, const JointState &state)
Overload the * operator with a scalar.
Definition: JointState.cpp:443
const std::vector< std::string > & get_names() const
Getter of the names attribute.
Definition: JointState.hpp:660
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
Class to define velocities of the joints.
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