Control Libraries 6.3.4
Loading...
Searching...
No Matches
Impedance.cpp
1#include "controllers/impedance/Impedance.hpp"
2
3#include "controllers/exceptions/NotImplementedException.hpp"
4#include "state_representation/space/joint/JointState.hpp"
5#include "state_representation/space/cartesian/CartesianState.hpp"
6
7using namespace state_representation;
8
9namespace controllers::impedance {
10
11template<class S>
12S Impedance<S>::compute_command(const S&, const S&) {
13 throw exceptions::NotImplementedException("compute_command is not implemented for this state variable");
14}
15
16template<>
18 const CartesianState& command_state, const CartesianState& feedback_state
19) {
20 CartesianState state_error = command_state - feedback_state;
21 // compute the wrench using the formula W = I * acc_desired + K * e_pose + D * e_twist + e_wrench
22 CartesianState command(feedback_state.get_name(), feedback_state.get_reference_frame());
23 // compute force
24 Eigen::Vector3d position_control = this->stiffness_->get_value().topLeftCorner<3, 3>() * state_error.get_position()
25 + this->damping_->get_value().topLeftCorner<3, 3>() * state_error.get_linear_velocity()
26 + this->inertia_->get_value().topLeftCorner<3, 3>() * command_state.get_linear_acceleration();
27 Eigen::Vector3d commanded_force = position_control + state_error.get_force();
28
29 // compute torque (orientation requires special care)
30 Eigen::Vector3d orientation_control =
31 this->stiffness_->get_value().bottomRightCorner<3, 3>() * state_error.get_orientation().vec()
32 + this->damping_->get_value().bottomRightCorner<3, 3>() * state_error.get_angular_velocity()
33 + this->inertia_->get_value().bottomRightCorner<3, 3>() * command_state.get_angular_acceleration();
34 Eigen::Vector3d commanded_torque = orientation_control + state_error.get_torque();
35
36 Eigen::VectorXd wrench(6);
37 wrench << commanded_force, commanded_torque;
38 clamp_force(wrench);
39
40 command.set_wrench(wrench);
41 return command;
42}
43
44template<>
46 const JointState& command_state, const JointState& feedback_state
47) {
48 JointState state_error = command_state - feedback_state;
49 // compute the wrench using the formula T = I * acc_desired + K * e_pos + D * e_vel + e_tor
50 JointState command(feedback_state.get_name(), feedback_state.get_names());
51 // compute torques
52 Eigen::VectorXd state_control = this->stiffness_->get_value() * state_error.get_positions()
53 + this->damping_->get_value() * state_error.get_velocities()
54 + this->inertia_->get_value() * command_state.get_accelerations();
55 Eigen::VectorXd commanded_torques = state_control + state_error.get_torques();
56 clamp_force(commanded_torques);
57 command.set_torques(commanded_torques);
58 return command;
59}
60
61}// namespace controllers
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
Class to represent a state in Cartesian space.
const Eigen::Vector3d & get_force() const
Getter of the force attribute.
const Eigen::Vector3d & get_torque() const
Getter of the torque attribute.
const Eigen::Vector3d & get_linear_velocity() const
Getter of the linear velocity attribute.
const Eigen::Vector3d & get_position() const
Getter of the position attribute.
const Eigen::Vector3d & get_linear_acceleration() const
Getter of the linear acceleration attribute.
const Eigen::Quaterniond & get_orientation() const
Getter of the orientation attribute.
const Eigen::Vector3d & get_angular_velocity() const
Getter of the angular velocity attribute.
const Eigen::Vector3d & get_angular_acceleration() const
Getter of the angular acceleration attribute.
Class to define a state in joint space.
Definition: JointState.hpp:36
const Eigen::VectorXd & get_torques() const
Getter of the torques attribute.
Definition: JointState.cpp:182
const Eigen::VectorXd & get_velocities() const
Getter of the velocities attribute.
Definition: JointState.cpp:118
void set_torques(const Eigen::VectorXd &torques)
Setter of the torques attribute.
Definition: JointState.cpp:195
const Eigen::VectorXd & get_accelerations() const
Getter of the accelerations attribute.
Definition: JointState.cpp:150
const std::vector< std::string > & get_names() const
Getter of the names attribute.
Definition: JointState.hpp:660
const Eigen::VectorXd & get_positions() const
Getter of the positions attribute.
Definition: JointState.cpp:86
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.
Definition: State.cpp:48
Core state variables and objects.