cob_hwinterface_topics.cpp
Go to the documentation of this file.
00001 #include <cob_hardware_interface/cob_hwinterface_topics.h>
00002 
00003 CobHWInterfaceTopics::CobHWInterfaceTopics()
00004 {
00005   //Get joint names from parameter server  
00006   std::string param="joint_names";
00007   XmlRpc::XmlRpcValue jointNames_XMLRPC;
00008   if (nh.hasParam(param))
00009   {
00010     nh.getParam(param, jointNames_XMLRPC);
00011   }
00012   else
00013   {
00014     ROS_ERROR("Parameter %s not set, shutting down node...", param.c_str());
00015     nh.shutdown();
00016   }
00017 
00018   for (int i=0; i<jointNames_XMLRPC.size(); i++)
00019   {
00020     joint_names.push_back(static_cast<std::string>(jointNames_XMLRPC[i]));  
00021     ROS_INFO("JointNames: %s", joint_names[i].c_str());
00022   }
00023    
00024   pos.resize(joint_names.size());
00025   vel.resize(joint_names.size());
00026   eff.resize(joint_names.size());
00027   cmd.resize(joint_names.size());
00028   
00029   //initialize the according subscriber and publisher here   
00030   cmd_vel_pub = nh.advertise<brics_actuator::JointVelocities>("command_vel_direct",1);
00031   jointstates_sub = nh.subscribe("/joint_states",1, &CobHWInterfaceTopics::jointstates_callback,this);
00032    
00033   for(unsigned int i=0; i<joint_names.size(); i++)
00034   {
00035     // connect and register the joint state interface
00036     hardware_interface::JointStateHandle state_handle(joint_names[i], &pos[i], &vel[i], &eff[i]);
00037     jnt_state_interface.registerHandle(state_handle);
00038     
00039     // connect and register the joint velocity interface
00040     hardware_interface::JointHandle vel_handle(jnt_state_interface.getHandle(joint_names[i]), &cmd[i]);
00041     jnt_vel_interface.registerHandle(vel_handle);
00042   }
00043 
00044   registerInterface(&jnt_state_interface);
00045   registerInterface(&jnt_vel_interface);
00046 }
00047 
00048 void CobHWInterfaceTopics::jointstates_callback(const sensor_msgs::JointState::ConstPtr& msg)
00049 {
00050   //boost::lock_guard<boost::mutex> guard(mtx_);
00051   for(unsigned int i=0; i<msg->name.size(); i++) 
00052   {
00053     for(unsigned int j=0; j<joint_names.size(); j++)
00054     {
00055       if(msg->name[i] == joint_names[j])
00056       {
00057         pos[j]=msg->position[i];
00058         vel[j]=msg->velocity[i];
00059         eff[j]=msg->effort[i];
00060         break;
00061       }
00062     }
00063   }
00064 }
00065 
00066 void CobHWInterfaceTopics::read()
00067 {
00068   //ROS_INFO("Reading Interface");
00069   //pos, vel, eff are updated!
00070   //boost::lock_guard<boost::mutex> guard(mtx_);
00071 }
00072 
00073 void CobHWInterfaceTopics::write()
00074 {
00075   //ROS_INFO("Writing Interface");
00076   //for(unsigned int k=0; k<cmd.size(); k++)
00077   //{
00078     //ROS_INFO("CMD[%d]: %f", k, cmd[k]);
00079   //}
00080   //here the current cmd values need to be propagated to the actual hardware
00081   brics_actuator::JointVelocities command;
00082   ros::Time update_time = ros::Time::now();
00083   
00084   //mtx_.lock();
00085   for (unsigned int i=0; i<joint_names.size(); i++) 
00086   {    
00087     brics_actuator::JointValue value;
00088     value.timeStamp = update_time;
00089     value.joint_uri = joint_names[i];
00090     value.unit = "rad";
00091     value.value=cmd[i];
00092     command.velocities.push_back(value);
00093   }
00094   //mtx_.unlock();
00095      
00096   cmd_vel_pub.publish(command);
00097   
00098   return;
00099 }


cob_hardware_interface
Author(s): Felix Messmer
autogenerated on Sun Mar 1 2015 13:56:44