3#include <eigen3/Eigen/Core>
4#include <eigen3/Eigen/Dense>
6#include "controllers/IController.hpp"
7#include "state_representation/parameters/Parameter.hpp"
8#include "state_representation/State.hpp"
10namespace controllers::impedance {
34 const std::list<std::shared_ptr<state_representation::ParameterInterface>>& parameters,
35 unsigned int dimensions = 6
41 S
compute_command(
const S& command_state,
const S& feedback_state)
override;
45 void clamp_force(Eigen::VectorXd& force);
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>>
77 "stiffness", Eigen::MatrixXd::Identity(dimensions, dimensions))), damping_(
79 "damping", Eigen::MatrixXd::Identity(dimensions, dimensions))), inertia_(
81 "inertia", Eigen::MatrixXd::Identity(dimensions, dimensions))), force_limit_(
83 "force_limit", Eigen::VectorXd::Zero(dimensions))), dimensions_(dimensions) {
92 const std::list<std::shared_ptr<state_representation::ParameterInterface>>& parameters,
unsigned int dimensions
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);
110 const std::shared_ptr<state_representation::ParameterInterface>& parameter
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());
123 "No parameter with name '" + parameter->get_name() +
"' found"
130 const std::shared_ptr<state_representation::ParameterInterface>& parameter
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_) +
")");
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_) +
")");
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 +
")");
161 matrix = gain->get_value();
164 "Parameter " + parameter->get_name() +
" has incorrect type");
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.
Eigen::MatrixXd gain_matrix_from_parameter(const std::shared_ptr< state_representation::ParameterInterface > ¶meter)
Convert a parameterized gain to a gain matrix.
Impedance(unsigned int dimensions=6)
Base constructor.
std::shared_ptr< state_representation::Parameter< Eigen::MatrixXd > > stiffness_
stiffness matrix of the controller associated to position
std::shared_ptr< state_representation::Parameter< Eigen::VectorXd > > force_limit_
vector of force limits for each degree of freedom
std::shared_ptr< state_representation::Parameter< Eigen::MatrixXd > > inertia_
inertia matrix of the controller associated to acceleration
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...
Impedance(const std::list< std::shared_ptr< state_representation::ParameterInterface > > ¶meters, unsigned int dimensions=6)
Constructor from an initial parameter list.
std::shared_ptr< state_representation::Parameter< Eigen::MatrixXd > > damping_
damping matrix of the controller associated to velocity
void validate_and_set_parameter(const std::shared_ptr< state_representation::ParameterInterface > ¶meter) 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
void set_parameters(const ParameterInterfaceList ¶meters)
Set parameters from a list of parameters.
ParameterInterfaceMap parameters_
map of parameters by name
Core state variables and objects.