Control Libraries 7.4.0
Loading...
Searching...
No Matches
Dissipative.cpp
1#include "controllers/impedance/Dissipative.hpp"
2
3#include "controllers/exceptions/NotImplementedException.hpp"
4
5using namespace state_representation;
6
7namespace controllers::impedance {
8
9template<class S>
12 "compute_orthonormal_basis(desired_velocity) not implemented for this input class");
13}
14
15template<>
16Eigen::MatrixXd Dissipative<CartesianState>::compute_orthonormal_basis(const CartesianState& desired_velocity) {
17 double tolerance = 1e-4;
18 Eigen::MatrixXd updated_basis = Eigen::MatrixXd::Zero(this->dimensions_, this->dimensions_);
19 switch (this->computational_space_) {
20 case ComputationalSpaceType::LINEAR: {
21 const Eigen::Vector3d& linear_velocity = desired_velocity.get_linear_velocity();
22 // only update the damping if the commanded linear velocity is non-null
23 if (linear_velocity.norm() < tolerance) {
24 updated_basis.topLeftCorner<3, 3>() = Eigen::Matrix3d::Identity();
25 return updated_basis;
26 }
27 //return only the linear block
28 updated_basis.topLeftCorner<3, 3>() =
29 Dissipative<CartesianState>::orthonormalize_basis(this->basis_.topLeftCorner<3, 3>(), linear_velocity);
30 break;
31 }
32 case ComputationalSpaceType::ANGULAR: {
33 const Eigen::Vector3d& angular_velocity = desired_velocity.get_angular_velocity();
34 // only update the damping if the commanded angular velocity is non-null
35 if (angular_velocity.norm() < tolerance) {
36 updated_basis.bottomRightCorner<3, 3>() = Eigen::Matrix3d::Identity();
37 return updated_basis;
38 }
39 // return only the angular block
40 updated_basis.bottomRightCorner<3, 3>() = Dissipative<CartesianState>::orthonormalize_basis(
41 this->basis_.bottomRightCorner<3, 3>(), angular_velocity);
42 break;
43 }
44 case ComputationalSpaceType::DECOUPLED_TWIST: {
45 // compute per block
46 bool updated = false;
47 const Eigen::Vector3d& linear_velocity = desired_velocity.get_linear_velocity();
48 const Eigen::Vector3d& angular_velocity = desired_velocity.get_angular_velocity();
49 if (linear_velocity.norm() > tolerance) {
50 updated_basis.block<3, 3>(0, 0) =
51 Dissipative<CartesianState>::orthonormalize_basis(this->basis_.topLeftCorner<3, 3>(), linear_velocity);
52 updated = true;
53 }
54 if (angular_velocity.norm() > tolerance) {
55 updated_basis.block<3, 3>(3, 3) = Dissipative<CartesianState>::orthonormalize_basis(
56 this->basis_.bottomRightCorner<3, 3>(), angular_velocity);
57 updated = true;
58 }
59 // at least the linear or angular parts have been updated
60 if (!updated) {
61 return Eigen::MatrixXd::Identity(this->dimensions_, this->dimensions_);
62 }
63 break;
64 }
65 case ComputationalSpaceType::FULL: {
66 // only update the damping if the commanded velocity is non null
67 if (desired_velocity.get_twist().norm() < tolerance) {
68 return Eigen::MatrixXd::Identity(this->dimensions_, this->dimensions_);
69 }
70 // return the full damping matrix
71 updated_basis = Dissipative<CartesianState>::orthonormalize_basis(this->basis_, desired_velocity.get_twist());
72 break;
73 }
74 }
75 return updated_basis;
76}
77
78template<>
79Eigen::MatrixXd Dissipative<JointState>::compute_orthonormal_basis(const JointState& desired_velocity) {
80 if (desired_velocity.get_size() != this->dimensions_) {
82 "The input state is of incorrect dimensions, expected " + std::to_string(this->dimensions_) + " got "
83 + std::to_string(desired_velocity.get_size()));
84 }
85 double tolerance = 1e-4;
86 // only update the damping if the commanded velocity is non-null
87 if (desired_velocity.get_velocities().norm() < tolerance) {
88 return Eigen::MatrixXd::Identity(this->dimensions_, this->dimensions_);
89 }
90 // return the full damping matrix
91 return Dissipative<JointState>::orthonormalize_basis(this->basis_, desired_velocity.get_velocities());
92}
93}// namespace controllers
static Eigen::MatrixXd orthonormalize_basis(const Eigen::MatrixXd &basis, const Eigen::VectorXd &main_eigenvector)
Orthornormalize the basis matrix given in input wrt the main engenvector.
Eigen::MatrixXd compute_orthonormal_basis(const S &desired_velocity)
Compute the orthonormal basis based on the desired velocity input.
Class to represent a state in Cartesian space.
const Eigen::Vector3d & get_linear_velocity() const
Getter of the linear velocity attribute.
Eigen::Matrix< double, 6, 1 > get_twist() const
Getter of the 6d twist from linear and angular velocity attributes.
const Eigen::Vector3d & get_angular_velocity() const
Getter of the angular velocity attribute.
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.
Core state variables and objects.