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