00001 00002 // Copyright (C) 2012, hiDOF INC. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are met: 00006 // * Redistributions of source code must retain the above copyright notice, 00007 // this list of conditions and the following disclaimer. 00008 // * Redistributions in binary form must reproduce the above copyright 00009 // notice, this list of conditions and the following disclaimer in the 00010 // documentation and/or other materials provided with the distribution. 00011 // * Neither the name of hiDOF Inc nor the names of its 00012 // contributors may be used to endorse or promote products derived from 00013 // this software without specific prior written permission. 00014 // 00015 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00016 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00017 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00019 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00020 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00021 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00023 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00024 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00025 // POSSIBILITY OF SUCH DAMAGE. 00027 00028 /* 00029 * Author: Wim Meeussen 00030 */ 00031 00032 00033 #include "joint_state_controller/joint_state_controller.h" 00034 00035 00036 00037 namespace joint_state_controller 00038 { 00039 00040 bool JointStateController::init(hardware_interface::JointStateInterface* hw, ros::NodeHandle &root_nh, ros::NodeHandle& controller_nh) 00041 { 00042 // get all joint names from the hardware interface 00043 const std::vector<std::string>& joint_names = hw->getNames(); 00044 for (unsigned i=0; i<joint_names.size(); i++) 00045 ROS_DEBUG("Got joint %s", joint_names[i].c_str()); 00046 00047 // get publishing period 00048 if (!controller_nh.getParam("publish_rate", publish_rate_)){ 00049 ROS_ERROR("Parameter 'publish_rate' not set"); 00050 return false; 00051 } 00052 00053 // realtime publisher 00054 realtime_pub_.reset(new realtime_tools::RealtimePublisher<sensor_msgs::JointState>(root_nh, "joint_states", 4)); 00055 00056 // get joints and allocate message 00057 for (unsigned i=0; i<joint_names.size(); i++){ 00058 joint_state_.push_back(hw->getHandle(joint_names[i])); 00059 realtime_pub_->msg_.name.push_back(joint_names[i]); 00060 realtime_pub_->msg_.position.push_back(0.0); 00061 realtime_pub_->msg_.velocity.push_back(0.0); 00062 realtime_pub_->msg_.effort.push_back(0.0); 00063 } 00064 00065 return true; 00066 } 00067 00068 void JointStateController::starting(const ros::Time& time) 00069 { 00070 // initialize time 00071 last_publish_time_ = time; 00072 } 00073 00074 void JointStateController::update(const ros::Time& time, const ros::Duration& period) 00075 { 00076 // limit rate of publishing 00077 if (publish_rate_ > 0.0 && last_publish_time_ + ros::Duration(1.0/publish_rate_) < time){ 00078 00079 // try to publish 00080 if (realtime_pub_->trylock()){ 00081 // we're actually publishing, so increment time 00082 last_publish_time_ = last_publish_time_ + ros::Duration(1.0/publish_rate_); 00083 00084 // populate joint state message 00085 realtime_pub_->msg_.header.stamp = time; 00086 for (unsigned i=0; i<joint_state_.size(); i++){ 00087 realtime_pub_->msg_.position[i] = joint_state_[i].getPosition(); 00088 realtime_pub_->msg_.velocity[i] = joint_state_[i].getVelocity(); 00089 realtime_pub_->msg_.effort[i] = joint_state_[i].getEffort(); 00090 } 00091 realtime_pub_->unlockAndPublish(); 00092 } 00093 } 00094 } 00095 00096 void JointStateController::stopping(const ros::Time& time) 00097 {} 00098 00099 } 00100 00101 00102 PLUGINLIB_EXPORT_CLASS( joint_state_controller::JointStateController, controller_interface::ControllerBase)