Control Libraries 7.4.0
Loading...
Searching...
No Matches
JointPositions.cpp
1#include "state_representation/space/joint/JointPositions.hpp"
2
3#include "state_representation/exceptions/EmptyStateException.hpp"
4
5namespace state_representation {
6
7using namespace exceptions;
8
10 this->set_type(StateType::JOINT_POSITIONS);
11}
12
13JointPositions::JointPositions(const std::string& robot_name, unsigned int nb_joints) :
14 JointState(robot_name, nb_joints) {
15 this->set_type(StateType::JOINT_POSITIONS);
16}
17
18JointPositions::JointPositions(const std::string& robot_name, const Eigen::VectorXd& positions) :
19 JointState(robot_name, positions.size()) {
20 this->set_type(StateType::JOINT_POSITIONS);
21 this->set_positions(positions);
22}
23
24JointPositions::JointPositions(const std::string& robot_name, const std::vector<std::string>& joint_names) :
25 JointState(robot_name, joint_names) {
26 this->set_type(StateType::JOINT_POSITIONS);
27}
28
30 const std::string& robot_name, const std::vector<std::string>& joint_names, const Eigen::VectorXd& positions
31) : JointState(robot_name, joint_names) {
32 this->set_type(StateType::JOINT_POSITIONS);
33 this->set_positions(positions);
34}
35
37 this->set_type(StateType::JOINT_POSITIONS);
38 if (state) {
39 this->set_zero();
40 this->set_positions(state.get_positions());
41 }
42}
43
45 JointPositions(static_cast<const JointState&>(positions)) {}
46
48 JointPositions(std::chrono::seconds(1) * velocities) {}
49
50JointPositions JointPositions::Zero(const std::string& robot_name, unsigned int nb_joints) {
51 return JointState::Zero(robot_name, nb_joints);
52}
53
54JointPositions JointPositions::Zero(const std::string& robot_name, const std::vector<std::string>& joint_names) {
55 return JointState::Zero(robot_name, joint_names);
56}
57
58JointPositions JointPositions::Random(const std::string& robot_name, unsigned int nb_joints) {
59 return JointPositions(robot_name, Eigen::VectorXd::Random(nb_joints));
60}
61
62JointPositions JointPositions::Random(const std::string& robot_name, const std::vector<std::string>& joint_names) {
63 return JointPositions(robot_name, joint_names, Eigen::VectorXd::Random(joint_names.size()));
64}
65
66Eigen::VectorXd JointPositions::data() const {
67 return this->get_positions();
68}
69
70void JointPositions::set_data(const Eigen::VectorXd& data) {
71 this->set_positions(data);
72}
73
74void JointPositions::set_data(const std::vector<double>& data) {
75 this->set_positions(Eigen::VectorXd::Map(data.data(), data.size()));
76}
77
78void JointPositions::clamp(double max_absolute_value, double noise_ratio) {
79 this->clamp_state_variable(max_absolute_value, JointStateVariable::POSITIONS, noise_ratio);
80}
81
82void JointPositions::clamp(const Eigen::ArrayXd& max_absolute_value_array, const Eigen::ArrayXd& noise_ratio_array) {
83 this->clamp_state_variable(max_absolute_value_array, JointStateVariable::POSITIONS, noise_ratio_array);
84}
85
86JointPositions JointPositions::clamped(double max_absolute_value, double noise_ratio) const {
87 JointPositions result(*this);
88 result.clamp(max_absolute_value, noise_ratio);
89 return result;
90}
91
93 const Eigen::ArrayXd& max_absolute_value_array, const Eigen::ArrayXd& noise_ratio_array
94) const {
95 JointPositions result(*this);
96 result.clamp(max_absolute_value_array, noise_ratio_array);
97 return result;
98}
99
101 JointPositions result(*this);
102 return result;
103}
104
106 this->JointState::operator*=(lambda);
107 return (*this);
108}
109
111 return this->JointState::operator*(lambda);
112}
113
114JointPositions operator*(double lambda, const JointPositions& positions) {
115 JointPositions result(positions);
116 result *= lambda;
117 return result;
118}
119
120JointPositions operator*(const Eigen::MatrixXd& lambda, const JointPositions& positions) {
121 JointPositions result(positions);
122 result.multiply_state_variable(lambda, JointStateVariable::POSITIONS);
123 return result;
124}
125
127 this->JointState::operator/=(lambda);
128 return (*this);
129}
130
132 return this->JointState::operator/(lambda);
133}
134
135JointVelocities JointPositions::operator/(const std::chrono::nanoseconds& dt) const {
136 // operations
137 JointVelocities velocities(this->get_name(), this->get_names());
138 // convert the period to a double with the second as reference
139 double period = dt.count();
140 period /= 1e9;
141 // divide the positions by this period value and assign it as velocities
142 velocities.set_velocities(this->get_positions() / period);
143 return velocities;
144}
145
146JointPositions& JointPositions::operator+=(const JointPositions& positions) {
147 this->JointState::operator+=(positions);
148 return (*this);
149}
150
151JointPositions& JointPositions::operator+=(const JointState& state) {
152 this->JointState::operator+=(state);
153 return (*this);
154}
155
156JointPositions JointPositions::operator+(const JointPositions& positions) const {
157 return this->JointState::operator+(positions);
158}
159
160JointState JointPositions::operator+(const JointState& state) const {
161 return this->JointState::operator+(state);
162}
163
167
168JointPositions& JointPositions::operator-=(const JointPositions& positions) {
169 this->JointState::operator-=(positions);
170 return (*this);
171}
172
173JointPositions& JointPositions::operator-=(const JointState& state) {
174 this->JointState::operator-=(state);
175 return (*this);
176}
177
179 return this->JointState::operator-(positions);
180}
181
183 return this->JointState::operator-(state);
184}
185
186std::ostream& operator<<(std::ostream& os, const JointPositions& positions) {
187 os << positions.to_string();
188 return os;
189}
190}// namespace state_representation
Class to define 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 zero joint positions.
Eigen::VectorXd data() const override
Returns the positions data as an Eigen vector.
JointPositions & operator*=(double lambda)
Scale inplace by a scalar.
JointPositions operator-() const
Negate joint positions.
virtual void set_data(const Eigen::VectorXd &data) override
Set the positions data from an Eigen vector.
JointPositions operator/(double lambda) const
Scale joint positions by 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 random joint positions.
friend JointPositions operator*(double lambda, const JointPositions &positions)
Scale joint positions by a scalar.
JointPositions copy() const
Return a copy of the joint positions.
JointPositions & operator/=(double lambda)
Scale inplace by a scalar.
Class to define a state in joint space.
JointState operator+(const JointState &state) const
Add another joint sate.
void multiply_state_variable(const Eigen::MatrixXd &lambda, const JointStateVariable &state_variable_type)
Proxy function that scale the specified state variable by a matrix.
void set_positions(const Eigen::VectorXd &positions)
Setter of the positions attribute.
JointState & operator*=(double lambda)
Scale inplace by a scalar.
void set_zero()
Set the joint state to a zero value.
static JointState Zero(const std::string &robot_name, unsigned int nb_joints)
Constructor for a zero joint state.
JointState & operator+=(const JointState &state)
Add inplace another joint state.
std::string to_string() const override
Convert the state to its string representation.
JointState & operator-=(const JointState &state)
Compute inplace the difference with another joint state.
JointState & operator/=(double lambda)
Scale inplace by a scalar.
JointState operator-() const
Negate a joint state.
friend JointState operator*(double lambda, const JointState &state)
Scale a joint state by a scalar.
const std::vector< std::string > & get_names() const
Getter of the names attribute.
const Eigen::VectorXd & get_positions() const
Getter of the positions attribute.
void set_velocities(const Eigen::VectorXd &velocities)
Setter of the velocities attribute.
JointState operator/(double lambda) const
Scale a joint state by a scalar.
Class to define velocities of the joints.
const std::string & get_name() const
Getter of the name attribute.
Definition State.cpp:29
void set_type(const StateType &type)
Setter of the state type attribute.
Definition State.cpp:41
Core state variables and objects.
std::ostream & operator<<(std::ostream &os, const AnalogIOState &state)
CartesianAcceleration operator*(double lambda, const CartesianAcceleration &acceleration)