Control Libraries 6.3.4
All Classes Namespaces Functions Variables Typedefs Enumerations Friends Pages
Impedance.hpp
1#pragma once
2
3#include <eigen3/Eigen/Core>
4#include <eigen3/Eigen/Dense>
5
6#include "controllers/IController.hpp"
7#include "state_representation/parameters/Parameter.hpp"
8#include "state_representation/State.hpp"
9
10namespace controllers::impedance {
11
17template<class S>
18class Impedance : public IController<S> {
19public:
20
26 explicit Impedance(unsigned int dimensions = 6);
27
33 explicit Impedance(
34 const std::list<std::shared_ptr<state_representation::ParameterInterface>>& parameters,
35 unsigned int dimensions = 6
36 );
37
41 S compute_command(const S& command_state, const S& feedback_state) override;
42
43protected:
44
45 void clamp_force(Eigen::VectorXd& force);
46
51 void validate_and_set_parameter(const std::shared_ptr<state_representation::ParameterInterface>& parameter) override;
52
58 Eigen::MatrixXd
59 gain_matrix_from_parameter(const std::shared_ptr<state_representation::ParameterInterface>& parameter);
60
61 std::shared_ptr<state_representation::Parameter<Eigen::MatrixXd>>
63 std::shared_ptr<state_representation::Parameter<Eigen::MatrixXd>>
65 std::shared_ptr<state_representation::Parameter<Eigen::MatrixXd>>
67 std::shared_ptr<state_representation::Parameter<Eigen::VectorXd>>
69
70 const unsigned int dimensions_;
71};
72
73template<class S>
74Impedance<S>::Impedance(unsigned int dimensions) :
75 stiffness_(
76 state_representation::make_shared_parameter<Eigen::MatrixXd>(
77 "stiffness", Eigen::MatrixXd::Identity(dimensions, dimensions))), damping_(
78 state_representation::make_shared_parameter<Eigen::MatrixXd>(
79 "damping", Eigen::MatrixXd::Identity(dimensions, dimensions))), inertia_(
80 state_representation::make_shared_parameter<Eigen::MatrixXd>(
81 "inertia", Eigen::MatrixXd::Identity(dimensions, dimensions))), force_limit_(
82 state_representation::make_shared_parameter<Eigen::VectorXd>(
83 "force_limit", Eigen::VectorXd::Zero(dimensions))), dimensions_(dimensions) {
84 this->parameters_.insert(std::make_pair("stiffness", stiffness_));
85 this->parameters_.insert(std::make_pair("damping", damping_));
86 this->parameters_.insert(std::make_pair("inertia", inertia_));
87 this->parameters_.insert(std::make_pair("force_limit", inertia_));
88}
89
90template<class S>
92 const std::list<std::shared_ptr<state_representation::ParameterInterface>>& parameters, unsigned int dimensions
93) :
94 Impedance(dimensions) {
95 this->set_parameters(parameters);
96}
97
98template<class S>
99void Impedance<S>::clamp_force(Eigen::VectorXd& force) {
100 auto limit = this->force_limit_->get_value();
101 for (std::size_t index = 0; index < this->dimensions_; ++index) {
102 if (limit(index) > 0.0 && abs(force(index)) > limit(index)) {
103 force(index) = force(index) > 0.0 ? limit(index) : -limit(index);
104 }
105 }
106}
107
108template<class S>
110 const std::shared_ptr<state_representation::ParameterInterface>& parameter
111) {
112 if (parameter->get_name() == "stiffness") {
113 this->stiffness_->set_value(this->gain_matrix_from_parameter(parameter));
114 } else if (parameter->get_name() == "damping") {
115 this->damping_->set_value(this->gain_matrix_from_parameter(parameter));
116 } else if (parameter->get_name() == "inertia") {
117 this->inertia_->set_value(this->gain_matrix_from_parameter(parameter));
118 } else if (parameter->get_name() == "force_limit") {
119 auto limit_matrix = this->gain_matrix_from_parameter(parameter);
120 this->force_limit_->set_value(limit_matrix.diagonal());
121 } else {
123 "No parameter with name '" + parameter->get_name() + "' found"
124 );
125 }
126}
127
128template<class S>
130 const std::shared_ptr<state_representation::ParameterInterface>& parameter
131) {
132 Eigen::MatrixXd matrix;
133 if (parameter->get_parameter_type() == state_representation::ParameterType::DOUBLE) {
134 auto gain = std::static_pointer_cast<state_representation::Parameter<double>>(parameter);
135 matrix = gain->get_value() * Eigen::MatrixXd::Identity(this->dimensions_, this->dimensions_);
136 } else if (parameter->get_parameter_type() == state_representation::ParameterType::DOUBLE_ARRAY) {
137 auto gain = std::static_pointer_cast<state_representation::Parameter<std::vector<double>>>(parameter);
138 if (gain->get_value().size() != this->dimensions_) {
140 "The provided diagonal coefficients do not match the dimensionality of the controller ("
141 + std::to_string(this->dimensions_) + ")");
142 }
143 Eigen::VectorXd diagonal = Eigen::VectorXd::Map(gain->get_value().data(), this->dimensions_);
144 matrix = diagonal.asDiagonal();
145 } else if (parameter->get_parameter_type() == state_representation::ParameterType::VECTOR) {
146 auto gain = std::static_pointer_cast<state_representation::Parameter<Eigen::VectorXd>>(parameter);
147 if (gain->get_value().size() != this->dimensions_) {
149 "The provided diagonal coefficients do not match the dimensionality of the controller ("
150 + std::to_string(this->dimensions_) + ")");
151 }
152 Eigen::VectorXd diagonal = gain->get_value();
153 matrix = diagonal.asDiagonal();
154 } else if (parameter->get_parameter_type() == state_representation::ParameterType::MATRIX) {
155 auto gain = std::static_pointer_cast<state_representation::Parameter<Eigen::MatrixXd>>(parameter);
156 if (gain->get_value().rows() != this->dimensions_ || gain->get_value().cols() != this->dimensions_) {
157 auto dim = std::to_string(this->dimensions_);
159 "The provided matrix does not have the expected size (" + dim + "x" + dim + ")");
160 }
161 matrix = gain->get_value();
162 } else {
164 "Parameter " + parameter->get_name() + " has incorrect type");
165 }
166 return matrix;
167}
168
169}// namespace controllers
Abstract class to define a controller in a desired state type, such as joint or Cartesian spaces.
Definition: IController.hpp:26
Definition of an impedance controller in either joint or task space.
Definition: Impedance.hpp:18
Eigen::MatrixXd gain_matrix_from_parameter(const std::shared_ptr< state_representation::ParameterInterface > &parameter)
Convert a parameterized gain to a gain matrix.
Definition: Impedance.hpp:129
Impedance(unsigned int dimensions=6)
Base constructor.
Definition: Impedance.hpp:74
std::shared_ptr< state_representation::Parameter< Eigen::MatrixXd > > stiffness_
stiffness matrix of the controller associated to position
Definition: Impedance.hpp:62
std::shared_ptr< state_representation::Parameter< Eigen::VectorXd > > force_limit_
vector of force limits for each degree of freedom
Definition: Impedance.hpp:68
std::shared_ptr< state_representation::Parameter< Eigen::MatrixXd > > inertia_
inertia matrix of the controller associated to acceleration
Definition: Impedance.hpp:66
S compute_command(const S &command_state, const S &feedback_state) override
Compute the command output based on the commanded state and a feedback state To be redefined based on...
Definition: Impedance.cpp:12
Impedance(const std::list< std::shared_ptr< state_representation::ParameterInterface > > &parameters, unsigned int dimensions=6)
Constructor from an initial parameter list.
Definition: Impedance.hpp:91
std::shared_ptr< state_representation::Parameter< Eigen::MatrixXd > > damping_
damping matrix of the controller associated to velocity
Definition: Impedance.hpp:64
void validate_and_set_parameter(const std::shared_ptr< state_representation::ParameterInterface > &parameter) override
Validate and set parameters for damping, stiffness and inertia gain matrices.
Definition: Impedance.hpp:109
const unsigned int dimensions_
dimensionality of the control space and associated gain matrices
Definition: Impedance.hpp:70
void set_parameters(const ParameterInterfaceList &parameters)
Set parameters from a list of parameters.
ParameterInterfaceMap parameters_
map of parameters by name
Core state variables and objects.