1#include "dynamical_systems/PointAttractor.hpp"
3#include "dynamical_systems/exceptions/NotImplementedException.hpp"
4#include "dynamical_systems/exceptions/EmptyAttractorException.hpp"
5#include "state_representation/exceptions/InvalidParameterException.hpp"
6#include "dynamical_systems/exceptions/IncompatibleSizeException.hpp"
7#include "state_representation/exceptions/EmptyStateException.hpp"
8#include "state_representation/exceptions/IncompatibleReferenceFramesException.hpp"
9#include "state_representation/exceptions/IncompatibleStatesException.hpp"
10#include "state_representation/space/cartesian/CartesianPose.hpp"
11#include "state_representation/space/cartesian/CartesianState.hpp"
12#include "state_representation/space/joint/JointPositions.hpp"
13#include "state_representation/space/joint/JointState.hpp"
22 gain_(make_shared_parameter<Eigen::MatrixXd>(
"gain", Eigen::MatrixXd::Identity(6, 6))) {
23 this->parameters_.insert(std::make_pair(
"attractor", attractor_));
24 this->parameters_.insert(std::make_pair(
"gain", gain_));
28PointAttractor<JointState>::PointAttractor() :
30 gain_(std::make_shared<
Parameter<Eigen::MatrixXd>>(
"gain")) {
31 this->parameters_.insert(std::make_pair(
"attractor", attractor_));
32 this->parameters_.insert(std::make_pair(
"gain", gain_));
44 auto attractor = this->get_parameter_value<JointState>(
"attractor");
45 if (attractor.is_empty()) {
48 bool compatible = (attractor.get_size() == state.get_size());
50 for (
unsigned int i = 0; i < attractor.get_size(); ++i) {
51 compatible = (compatible && attractor.get_names()[i] == state.get_names()[i]);
58void PointAttractor<S>::set_gain(
const std::shared_ptr<ParameterInterface>& parameter,
unsigned int expected_size) {
59 if (parameter->get_parameter_type() == ParameterType::DOUBLE) {
60 auto gain = parameter->get_parameter_value<
double>();
61 this->gain_->set_value(gain * Eigen::MatrixXd::Identity(expected_size, expected_size));
62 }
else if (parameter->get_parameter_type() == ParameterType::DOUBLE_ARRAY) {
63 auto gain = parameter->get_parameter_value<std::vector<double>>();
64 if (gain.size() != expected_size) {
66 "The provided diagonal coefficients do not correspond to the expected size of the attractor");
68 Eigen::VectorXd diagonal = Eigen::VectorXd::Map(gain.data(), expected_size);
69 this->gain_->set_value(diagonal.asDiagonal());
70 }
else if (parameter->get_parameter_type() == ParameterType::MATRIX) {
71 auto gain = parameter->get_parameter_value<Eigen::MatrixXd>();
72 if (gain.rows() != expected_size && gain.cols() != expected_size) {
74 "The provided gain matrix do not have the expected size (" + std::to_string(expected_size) +
"x"
75 + std::to_string(expected_size) +
")");
77 this->gain_->set_value(gain);
84void PointAttractor<S>::set_attractor(
const S&) {
89void PointAttractor<CartesianState>::set_attractor(
const CartesianState& attractor) {
93 if (this->get_base_frame().is_empty()) {
94 IDynamicalSystem<CartesianState>::set_base_frame(
101 "The reference frame of the attractor " + attractor.
get_name() +
" in frame "
102 + attractor.
get_reference_frame() +
" is incompatible with the base frame of the dynamical system "
103 + this->get_base_frame().get_name() +
" in frame " + this->get_base_frame().get_reference_frame() +
".");
105 this->attractor_->set_value(this->get_base_frame().inverse() * attractor);
107 this->attractor_->set_value(attractor);
112void PointAttractor<JointState>::set_attractor(
const JointState& attractor) {
116 this->attractor_->set_value(attractor);
117 if (this->gain_->get_value().size() == 0) {
118 this->gain_->set_value(Eigen::MatrixXd::Identity(attractor.
get_size(), attractor.
get_size()));
134 IDynamicalSystem<CartesianState>::set_base_frame(base_frame);
135 if (!this->attractor_->get_value().is_empty()) {
137 auto attractor = this->attractor_->get_value();
138 attractor.set_reference_frame(base_frame.
get_name());
139 this->set_attractor(attractor);
144void PointAttractor<CartesianState>::validate_and_set_parameter(
const std::shared_ptr<ParameterInterface>& parameter) {
145 if (parameter->get_name() ==
"attractor") {
146 if (parameter->get_parameter_state_type() == StateType::CARTESIAN_STATE) {
147 this->set_attractor(parameter->get_parameter_value<
CartesianState>());
148 }
else if (parameter->get_parameter_state_type() == StateType::CARTESIAN_POSE) {
149 this->set_attractor(parameter->get_parameter_value<
CartesianPose>());
151 }
else if (parameter->get_name() ==
"gain") {
152 this->set_gain(parameter, 6);
155 "No parameter with name '" + parameter->get_name() +
"' found");
160void PointAttractor<JointState>::validate_and_set_parameter(
const std::shared_ptr<ParameterInterface>& parameter) {
161 if (parameter->get_name() ==
"attractor") {
162 if (parameter->get_parameter_state_type() == StateType::JOINT_STATE) {
163 this->set_attractor(parameter->get_parameter_value<
JointState>());
164 }
else if (parameter->get_parameter_state_type() == StateType::JOINT_POSITIONS) {
165 this->set_attractor(parameter->get_parameter_value<
JointPositions>());
167 }
else if (parameter->get_name() ==
"gain") {
168 this->set_gain(parameter, this->attractor_->get_value().get_size());
171 "No parameter with name '" + parameter->get_name() +
"' found");
182 if (this->attractor_->get_value().is_empty()) {
186 twist *= this->gain_->get_value();
187 return CartesianTwist(state.get_name(), twist.
get_twist(), this->attractor_->get_value().get_reference_frame());
193 velocities *= this->gain_->get_value();
Abstract class for a dynamical system.
Represents a dynamical system to move towards an attractor.
PointAttractor()
Empty constructor.
Class to define CartesianPose in cartesian space as 3D position and quaternion based orientation.
Class to represent a state in Cartesian space.
Eigen::Matrix< double, 6, 1 > get_twist() const
Getter of the 6d twist from linear and angular velocity attributes.
static CartesianState Identity(const std::string &name, const std::string &reference="world")
Constructor for the identity CartesianState (identity pose and 0 for the rest)
Class to define twist in cartesian space as 3D linear and angular velocity vectors.
Class to define a positions of the joints.
Class to define a state in joint space.
const Eigen::VectorXd & get_velocities() const
Getter of the velocities attribute.
unsigned int get_size() const
Getter of the size from the attributes.
Class to define velocities of the joints.
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.
bool is_empty() const
Getter of the empty attribute.
Systems of equations relating state variables to their derivatives.
Core state variables and objects.