Control Libraries 7.4.0
Loading...
Searching...
No Matches
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<bool>>
69 std::shared_ptr<state_representation::Parameter<Eigen::VectorXd>>
71
72 const unsigned int dimensions_;
73};
74
75template<class S>
76Impedance<S>::Impedance(unsigned int dimensions) :
77 stiffness_(
78 state_representation::make_shared_parameter<Eigen::MatrixXd>(
79 "stiffness", Eigen::MatrixXd::Identity(dimensions, dimensions))), damping_(
80 state_representation::make_shared_parameter<Eigen::MatrixXd>(
81 "damping", Eigen::MatrixXd::Identity(dimensions, dimensions))), inertia_(
82 state_representation::make_shared_parameter<Eigen::MatrixXd>(
83 "inertia", Eigen::MatrixXd::Identity(dimensions, dimensions))), feed_forward_force_(
84 state_representation::make_shared_parameter<bool>("feed_forward_force", false)), force_limit_(
85 state_representation::make_shared_parameter<Eigen::VectorXd>("force_limit")), dimensions_(dimensions) {
86 this->parameters_.insert(std::make_pair("stiffness", stiffness_));
87 this->parameters_.insert(std::make_pair("damping", damping_));
88 this->parameters_.insert(std::make_pair("inertia", inertia_));
89 this->parameters_.insert(std::make_pair("feed_forward_force", feed_forward_force_));
90 this->parameters_.insert(std::make_pair("force_limit", force_limit_));
91}
92
93template<class S>
95 const std::list<std::shared_ptr<state_representation::ParameterInterface>>& parameters, unsigned int dimensions
96) :
97 Impedance(dimensions) {
98 this->set_parameters(parameters);
99}
100
101template<class S>
102void Impedance<S>::clamp_force(Eigen::VectorXd& force) {
103 if (*this->force_limit_) {
104 force = force.cwiseMax(-this->force_limit_->get_value()).cwiseMin(this->force_limit_->get_value());
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() == "feed_forward_force") {
119 this->feed_forward_force_->set_value(parameter->get_parameter_value<bool>());
120 } else if (parameter->get_name() == "force_limit") {
121 if (parameter->get_parameter_type() == state_representation::ParameterType::MATRIX) {
123 "Parameter " + parameter->get_name() + " has incorrect type");
124 }
125 auto limit_matrix = this->gain_matrix_from_parameter(parameter);
126 this->force_limit_->set_value(limit_matrix.diagonal());
127 } else {
129 "No parameter with name '" + parameter->get_name() + "' found"
130 );
131 }
132}
133
134template<class S>
136 const std::shared_ptr<state_representation::ParameterInterface>& parameter
137) {
138 Eigen::MatrixXd matrix;
139 if (parameter->get_parameter_type() == state_representation::ParameterType::DOUBLE) {
140 auto gain = std::static_pointer_cast<state_representation::Parameter<double>>(parameter);
141 matrix = gain->get_value() * Eigen::MatrixXd::Identity(this->dimensions_, this->dimensions_);
142 } else if (parameter->get_parameter_type() == state_representation::ParameterType::DOUBLE_ARRAY) {
143 auto gain = std::static_pointer_cast<state_representation::Parameter<std::vector<double>>>(parameter);
144 if (gain->get_value().size() == 1) {
145 matrix = gain->get_value().at(0) * Eigen::MatrixXd::Identity(this->dimensions_, this->dimensions_);
146 } else if (gain->get_value().size() == this->dimensions_) {
147 Eigen::VectorXd diagonal = Eigen::VectorXd::Map(gain->get_value().data(), this->dimensions_);
148 matrix = diagonal.asDiagonal();
149 } else {
151 "The provided diagonal coefficients do not match the dimensionality of the controller ("
152 + std::to_string(this->dimensions_) + ")");
153 }
154 } else if (parameter->get_parameter_type() == state_representation::ParameterType::VECTOR) {
155 auto gain = std::static_pointer_cast<state_representation::Parameter<Eigen::VectorXd>>(parameter);
156 if (gain->get_value().size() == 1) {
157 matrix = gain->get_value()(0) * Eigen::MatrixXd::Identity(this->dimensions_, this->dimensions_);
158 } else if (gain->get_value().size() == this->dimensions_) {
159 matrix = gain->get_value().asDiagonal();
160 } else {
162 "The provided diagonal coefficients do not match the dimensionality of the controller ("
163 + std::to_string(this->dimensions_) + ")");
164 }
165 } else if (parameter->get_parameter_type() == state_representation::ParameterType::MATRIX) {
166 auto gain = std::static_pointer_cast<state_representation::Parameter<Eigen::MatrixXd>>(parameter);
167 if (gain->get_value().rows() != this->dimensions_ || gain->get_value().cols() != this->dimensions_) {
168 auto dim = std::to_string(this->dimensions_);
170 "The provided matrix does not have the expected size (" + dim + "x" + dim + ")");
171 }
172 matrix = gain->get_value();
173 } else {
175 "Parameter " + parameter->get_name() + " has incorrect type");
176 }
177 if ((matrix.array() < 0).any()) {
179 "Parameter " + parameter->get_name() + " cannot have negative elements");
180 }
181 return matrix;
182}
183
184}// namespace controllers
Abstract class to define a controller in a desired state type, such as joint or Cartesian spaces.
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.
Impedance(unsigned int dimensions=6)
Base constructor.
Definition Impedance.hpp:76
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< bool > > feed_forward_force_
flag to decide if force error should be passed on
Definition Impedance.hpp:68
std::shared_ptr< state_representation::Parameter< Eigen::VectorXd > > force_limit_
vector of force limits for each degree of freedom
Definition Impedance.hpp:70
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:94
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.
const unsigned int dimensions_
dimensionality of the control space and associated gain matrices
Definition Impedance.hpp:72
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.