joint_states_deflater.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2008, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 #include "joint_states_settler/joint_states_deflater.h"
00036 #include <ros/console.h>
00037 
00038 using namespace std;
00039 using namespace joint_states_settler;
00040 
00041 JointStatesDeflater::JointStatesDeflater()
00042 {
00043   mapping_.clear();
00044 }
00045 
00046 void JointStatesDeflater::setDeflationJointNames(std::vector<std::string> joint_names)
00047 {
00048   joint_names_ = joint_names;
00049   mapping_.resize(joint_names_.size());
00050 }
00051 
00052 void JointStatesDeflater::deflate(const sensor_msgs::JointStateConstPtr& joint_states, DeflatedJointStates& deflated_elem)
00053 {
00054   if (joint_states->name.size() != joint_states->position.size()){
00055     ROS_ERROR("JointStatesDeflater got invalid joint state message");
00056     return;
00057   }
00058 
00059   if (mapping_.size() != joint_names_.size())
00060     updateMapping(*joint_states);
00061 
00062   const unsigned int N = joint_names_.size();
00063 
00064   deflated_elem.channels_.resize(N);
00065 
00066   for (unsigned int i=0; i<N; i++)
00067   {
00068     if ( mapping_[i] >= joint_states->name.size() )
00069       updateMapping(*joint_states);
00070 
00071     if ( joint_states->name[mapping_[i]] != joint_names_[i])
00072       updateMapping(*joint_states);
00073 
00074     deflated_elem.header = joint_states->header;
00075     deflated_elem.channels_[i] = joint_states->position[mapping_[i]];
00076     deflated_elem.msg_ = joint_states;
00077   }
00078 }
00079 
00080 void JointStatesDeflater::prune(const sensor_msgs::JointState& joint_states, sensor_msgs::JointState& pruned_joint_states)
00081 {
00082   if (joint_states.name.size() !=  joint_states.position.size())
00083   {
00084     ROS_ERROR("JointStatesDeflater got invalid joint state message. name's size and position's size don't match.");
00085     return;
00086   }
00087 
00088   if (mapping_.size() != joint_names_.size())
00089     updateMapping(joint_states);
00090 
00091   const unsigned int N = joint_names_.size();
00092 
00093   // Initialize Structures
00094   pruned_joint_states.header.stamp = joint_states.header.stamp;
00095   pruned_joint_states.name.resize(N);
00096   pruned_joint_states.position.resize(N);
00097   pruned_joint_states.velocity.clear();
00098   pruned_joint_states.effort.clear();
00099 
00100   for (unsigned int i=0; i<N; i++)
00101   {
00102     if ( mapping_[i] >= joint_states.name.size() )
00103       updateMapping(joint_states);
00104 
00105     if ( joint_states.name[mapping_[i]] != joint_names_[i])
00106       updateMapping(joint_states);
00107 
00108     pruned_joint_states.name[i]     = joint_states.name[mapping_[i]];
00109     pruned_joint_states.position[i] = joint_states.position[mapping_[i]];
00110 
00111     // Currently not copying over velocity or effort. Maybe we should be doing this...
00112   }
00113 }
00114 
00115 void JointStatesDeflater::updateMapping(const sensor_msgs::JointState& joint_states)
00116 {
00117   ROS_DEBUG("Updating the JointStates mapping");
00118 
00119   const unsigned int N = joint_names_.size();
00120 
00121   mapping_.resize(N);
00122 
00123   for (unsigned int i=0; i<N; i++)
00124   {
00125     bool mapping_found = false;
00126     for (unsigned int j=0; j<joint_states.name.size(); j++)
00127     {
00128       if ( joint_names_[i] == joint_states.name[j])
00129       {
00130         mapping_[i] = j;
00131         mapping_found = true;
00132       }
00133     }
00134     ROS_ERROR_COND(!mapping_found, "Couldn't find mapping for [%s]", joint_names_[i].c_str());
00135   }
00136 }


joint_states_settler
Author(s): Vijay Pradeep, Eitan Marder-Eppstein
autogenerated on Wed Aug 26 2015 10:56:10