ImpedanceProjection.cpp
Go to the documentation of this file.
00001 /*
00002  * ImpedanceProjection.cpp
00003  *
00004  *  Created on: 09-04-2012
00005  *      Author: konradb3
00006  */
00007 
00008 #include <rtt/Component.hpp>
00009 
00010 #include "ImpedanceProjection.h"
00011 #include <tf_conversions/tf_kdl.h>
00012 #include <Eigen/Dense>
00013 typedef Eigen::Matrix<double, 6, 1> Vector6d;
00014 typedef Eigen::Matrix<double, 7, 1> Vector7d;
00015 typedef Eigen::Matrix<double, 7, 7> Matrix7d;
00016 
00017 namespace lwr {
00018 
00019 ImpedanceProjector::ImpedanceProjector(const std::string& name) : TaskContext(name, PreOperational) {
00020   // command inputs
00021   this->addPort("CartesianImpedanceCommand", port_cartesian_impedance_command);
00022   this->addPort("ToolFrame", port_tool_frame);
00023   // meansurment inputs
00024   this->addEventPort("CommandPeriod", port_command_period);
00025   this->addPort("Jacobian", port_jacobian);
00026   this->addPort("JointPosition", port_jnt_pos_msr);
00027   // commands outputs
00028   this->addPort("JointEffortCommand", port_jnt_trq_cmd);
00029   this->addPort("FriJointImpedance", port_joint_impedance_command);
00030   this->addPort("JointPositionCommand", port_joint_position_command);
00031 
00032 }
00033 
00034 ImpedanceProjector::~ImpedanceProjector() {
00035 
00036 }
00037 
00038 bool ImpedanceProjector::configureHook() {
00039         return true;
00040 }
00041 
00042 void ImpedanceProjector::cleanupHook() {
00043 
00044 }
00045 
00046 bool ImpedanceProjector::startHook() {
00047 
00048         jnt_trq_cmd.resize(7);
00049 
00050         //set detault stiffness and damping
00051         cartesian_impedance_command.stiffness.force.x = 1500.0;
00052         cartesian_impedance_command.stiffness.force.y = 1500.0;
00053         cartesian_impedance_command.stiffness.force.z = 50.0;
00054 
00055         cartesian_impedance_command.stiffness.torque.x = 50.0;
00056         cartesian_impedance_command.stiffness.torque.y = 50.0;
00057         cartesian_impedance_command.stiffness.torque.z = 50.0;
00058 
00059         cartesian_impedance_command.damping.force.x = 0.8;
00060         cartesian_impedance_command.damping.force.y = 0.8;
00061         cartesian_impedance_command.damping.force.z = 0.2;
00062 
00063         cartesian_impedance_command.damping.torque.x = 0.80;
00064         cartesian_impedance_command.damping.torque.y = 0.80;
00065         cartesian_impedance_command.damping.torque.z = 0.80;
00066 
00067   jnt_pos_cmd.resize(7);
00068   jnt_pos.resize(7);
00069 
00070         if(port_jnt_pos_msr.read(jnt_pos) == RTT::NoData) {
00071           RTT::Logger::log(RTT::Logger::Error) << "No position data " << RTT::endlog();
00072                 return false;
00073         }
00074         
00075         for(size_t i = 0; i<7; i++) {
00076     jnt_pos_cmd[i] = jnt_pos[i];
00077   }
00078         
00079         if(port_command_period.read(dt) == RTT::NoData) {
00080           RTT::Logger::log(RTT::Logger::Error) << "No comand period data " << RTT::endlog();
00081                 return false;
00082         }
00083         
00084         geometry_msgs::Pose tool_frame_msg;
00085         
00086         tool_frame.M = KDL::Rotation::RPY(0.0, -1.57079633, 0.0);
00087   tool_frame.p = KDL::Vector(-0.115, 0.0, -0.055);
00088         
00089         if(port_tool_frame.read(tool_frame_msg) == RTT::NoData) {
00090           RTT::Logger::log(RTT::Logger::Error) << "No tool data " << RTT::endlog();
00091          // return false;
00092         }
00093 
00094         return true;
00095 }
00096 
00097 void ImpedanceProjector::stopHook() {
00098 
00099 }
00100 
00101 void ImpedanceProjector::updateHook() {
00102 
00103   Matrix7d Kj, Dj;
00104   Vector6d Kc, Dc;
00105   Vector7d rt, dq;
00106 
00107   port_command_period.read(dt);
00108   port_jacobian.read(jacobian);
00109   port_cartesian_impedance_command.read(cartesian_impedance_command);
00110   port_jnt_pos_msr.read(jnt_pos);
00111   port_joint_position_command.read(jnt_pos_cmd);
00112 
00113   jacobian.changeRefFrame(tool_frame);
00114 
00115   Kc(0) = cartesian_impedance_command.stiffness.force.x;
00116   Kc(1) = cartesian_impedance_command.stiffness.force.y;
00117   Kc(2) = cartesian_impedance_command.stiffness.force.z;
00118   
00119   Kc(3) = cartesian_impedance_command.stiffness.torque.x;
00120   Kc(4) = cartesian_impedance_command.stiffness.torque.y;
00121   Kc(5) = cartesian_impedance_command.stiffness.torque.z;
00122 
00123   Kj = jacobian.data.transpose() * Kc.asDiagonal() * jacobian.data;
00124 
00125   Dc(0) = cartesian_impedance_command.damping.force.x;
00126   Dc(1) = cartesian_impedance_command.damping.force.y;
00127   Dc(2) = cartesian_impedance_command.damping.force.z;
00128   
00129   Dc(3) = cartesian_impedance_command.damping.torque.x;
00130   Dc(4) = cartesian_impedance_command.damping.torque.y;
00131   Dc(5) = cartesian_impedance_command.damping.torque.z;
00132 
00133   Dj = jacobian.data.transpose() * Dc.asDiagonal() * jacobian.data;
00134 
00135 //  std::cout << std::endl;
00136 //  std::cout << std::endl;
00137 //  std::cout << Kj;
00138 //  std::cout << std::endl;
00139 //  std::cout << std::endl;
00140 
00141   for(size_t i = 0; i < 7; i++) {
00142     imp.stiffness[i] = Kj(i, i);
00143     imp.damping[i] = Dj(i, i);
00144     Kj(i, i) = 0.0;
00145     Dj(i, i) = 0.0;
00146   }
00147 
00148   for(size_t i = 0; i < 7; i++) {
00149     dq(i) = jnt_pos_cmd[i] - jnt_pos[i]; 
00150   }
00151 
00152   rt = Kj * dq;
00153 
00154 //  double s = sqrt((jacobian.data * jacobian.data.transpose() ).determinant());
00155 
00156 //  std::cout << "Manip index : " << s << std::endl;
00157 
00158   for(size_t i = 0; i < 7; i++) {
00159     if(rt(i) < 50.0) {
00160       jnt_trq_cmd[i] = rt(i);
00161     } else {
00162       RTT::Logger::log(RTT::Logger::Error) << "Torque limit " << RTT::endlog();
00163      // std::cout << std::endl;
00164      // std::cout << Kj;
00165      // std::cout << std::endl;
00166     } 
00167   }
00168 
00169         port_joint_impedance_command.write(imp);
00170   port_jnt_trq_cmd.write(jnt_trq_cmd);
00171 }
00172 
00173 }
00174 ORO_CREATE_COMPONENT(lwr::ImpedanceProjector)


lwr_impedance_controller
Author(s): Konrad Banachowicz
autogenerated on Mon Oct 6 2014 02:01:41