Control Libraries 7.4.0
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
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
28 // compute torque (orientation requires special care)
29 if (state_error.get_orientation().w() < 0) {
30 state_error.set_orientation(state_error.get_orientation().conjugate());
31 }
32 Eigen::Vector3d orientation_control =
33 this->stiffness_->get_value().bottomRightCorner<3, 3>() * state_error.get_orientation().vec()
34 + this->damping_->get_value().bottomRightCorner<3, 3>() * state_error.get_angular_velocity()
35 + this->inertia_->get_value().bottomRightCorner<3, 3>() * command_state.get_angular_acceleration();
36
37 Eigen::VectorXd wrench(6);
38 wrench << position_control, orientation_control;
39 // if the 'feed_forward_force' parameter is set to true, also add the wrench error to the command
40 if (this->feed_forward_force_->get_value()) {
41 wrench += state_error.get_wrench();
42 }
43 clamp_force(wrench);
44
45 command.set_wrench(wrench);
46 return command;
47}
48
49template<>
51 const JointState& command_state, const JointState& feedback_state
52) {
53 JointState state_error = command_state - feedback_state;
54 // compute the wrench using the formula T = I * acc_desired + K * e_pos + D * e_vel
55 JointState command(feedback_state.get_name(), feedback_state.get_names());
56 // compute torques
57 Eigen::VectorXd torque_control = this->stiffness_->get_value() * state_error.get_positions()
58 + this->damping_->get_value() * state_error.get_velocities()
59 + this->inertia_->get_value() * command_state.get_accelerations();
60
61 // if the 'feed_forward_force' parameter is set to true, also add the torque error to the command
62 if (this->feed_forward_force_->get_value()) {
63 torque_control += state_error.get_torques();
64 }
65 clamp_force(torque_control);
66
67 command.set_torques(torque_control);
68 return command;
69}
70
71}// 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.
void set_orientation(const Eigen::Quaterniond &orientation)
Setter of the orientation.
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.
Eigen::Matrix< double, 6, 1 > get_wrench() const
Getter of the 6d wrench from force and torque attributes.
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.
const Eigen::VectorXd & get_torques() const
Getter of the torques attribute.
const Eigen::VectorXd & get_velocities() const
Getter of the velocities attribute.
const Eigen::VectorXd & get_accelerations() const
Getter of the accelerations attribute.
const std::vector< std::string > & get_names() const
Getter of the names attribute.
const Eigen::VectorXd & get_positions() const
Getter of the positions attribute.
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 attribute.
Definition State.cpp:29
Core state variables and objects.