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