shadowhand_to_cybergrasp_remapper.cpp
Go to the documentation of this file.
00001 
00029 //ROS include
00030 #include <ros/ros.h>
00031 
00032 //generic include
00033 #include <string>
00034 
00035 #include <unistd.h>
00036 #include <math.h>
00037 
00038 //own .h
00039 #include "sr_remappers/shadowhand_to_cybergrasp_remapper.h"
00040 
00041 using namespace ros;
00042 
00043 namespace shadowhand_to_cybergrasp_remapper{
00044 
00045   ShadowhandToCybergraspRemapper::ShadowhandToCybergraspRemapper()
00046     : n_tilde("~"), publish_rate(0.0)
00047   {
00048     std::string searched_param;
00049 
00050     //load the calibration
00051     std::string path;
00052     n_tilde.searchParam("cybergrasp_calibration_path", searched_param);
00053     n_tilde.param(searched_param, path, std::string());
00054 
00055     calibration_parser = new CalibrationParser(path);
00056 
00057    //publish to cybergraspforces topic
00058     std::string prefix;
00059     n_tilde.searchParam("cybergrasp_prefix", searched_param);
00060     n_tilde.param(searched_param, prefix, std::string());
00061     std::string full_topic = prefix + "cybergraspforces";
00062 
00063     shadowhand_cybergrasp_pub = node.advertise<cybergrasp::cybergraspforces>(full_topic, 20);
00064 
00065     //subscribe to joint_states topic
00066     n_tilde.searchParam("joint_states_prefix", searched_param);
00067     n_tilde.param(searched_param, prefix, std::string());
00068 
00069     full_topic = prefix + "joint_states";
00070 
00071     shadowhand_jointstates_sub = node.subscribe(full_topic, 10, &ShadowhandToCybergraspRemapper::jointstatesCallback, this);
00072 
00073    }
00074 
00075   ShadowhandToCybergraspRemapper::~ShadowhandToCybergraspRemapper()
00076   {
00077     if( calibration_parser )
00078       delete calibration_parser;
00079   }
00080 
00087   void ShadowhandToCybergraspRemapper::jointstatesCallback(const sensor_msgs::JointStateConstPtr& msg)
00088   { 
00089     //read msg and remap the vector to the cybergrasp
00090     cybergrasp::cybergraspforces cybergrasp_msg;
00091 
00092     //std::vector<double> pos(5);
00093     /*std::vector<double> difference_target_position(24);
00094     for(unsigned int i = 0 ; i < difference_target_position.size() ; ++i)
00095       {
00096         //velocity contains the targets... not the proper way of doing that
00097         difference_target_position[i] = fabs(msg->position[i] - msg->velocity[i]);
00098       }
00099     */
00100     //    pos = calibration_parser->get_remapped_vector(difference_target_position);
00101     /*    pos = calibration_parser->get_remapped_vector(msg->effort);
00102 
00103     for( unsigned int i=0; i < pos.size(); ++i)
00104       {
00105         double tmp = fabs(pos[i]);
00106 
00107         ROS_ERROR("%f", tmp);
00108         if(tmp > 0.015)
00109           {
00110             ROS_ERROR("%d >0.015", i);
00111             cybergrasp_msg.forces[i] = (tmp-0.01)*7.0;
00112           }
00113         else
00114           cybergrasp_msg.forces[i] = 0.0;
00115       }
00116     */
00117 
00118     double average = 0.0;
00119 
00120     for( unsigned int i=0; i < msg->effort.size(); ++i)
00121       {
00122         average = msg->effort[i]*10.0;
00123       }
00124     
00125     average /= msg->effort.size();
00126 
00127     average /= msg->effort.size();
00128     average *= 1000.0;
00129 
00130     for( unsigned int i=0; i < 5; ++i)
00131       {
00132         cybergrasp_msg.forces[i] = 0.01;
00133       }
00134 
00135     //return the max of the read values on the sensor
00136     cybergrasp_msg.forces[1] = (average - 0.016)*2.0;
00137     //send vector to cybergrasp
00138     shadowhand_cybergrasp_pub.publish(cybergrasp_msg);
00139 
00140     //    sleep(2);
00141 
00142   }
00143 }//end namespace


sr_remappers
Author(s): Ugo Cupcic
autogenerated on Fri Jan 3 2014 12:05:01