00001 /* 00002 * Copyright (c) 2010, Robot Control and Pattern Recognition Group, Warsaw University of Technology. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Robot Control and Pattern Recognition Group, 00014 * Warsaw University of Technology nor the names of its contributors may 00015 * be used to endorse or promote products derived from this software 00016 * without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00021 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00022 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00028 * POSSIBILITY OF SUCH DAMAGE. 00029 */ 00030 00031 #include <rtt/os/TimeService.hpp> 00032 #include <rtt/Time.hpp> 00033 #include <ocl/Component.hpp> 00034 00035 #include "JointStatePublisher.hpp" 00036 00037 #include "xeno_clock.h" 00038 00039 JointStatePublisher::JointStatePublisher(const std::string& name) : 00040 RTT::TaskContext(name, PreOperational), msr_jnt_pos_port_("msrJntPos"), 00041 joint_state_port_("joints_state"), joint_names_prop("joint_names", 00042 "number of joints") 00043 { 00044 ports()->addPort(msr_jnt_pos_port_); 00045 ports()->addPort(joint_state_port_); 00046 00047 this->addProperty(joint_names_prop); 00048 } 00049 00050 JointStatePublisher::~JointStatePublisher() 00051 { 00052 } 00053 00054 bool JointStatePublisher::configureHook() 00055 { 00056 names_ = joint_names_prop.get(); 00057 number_of_joints_ = names_.size(); 00058 00059 joint_state_.name.resize(number_of_joints_); 00060 joint_state_.position.resize(number_of_joints_); 00061 joint_state_.velocity.resize(number_of_joints_); 00062 joint_state_.effort.resize(number_of_joints_); 00063 00064 for (unsigned int i = 0; i < number_of_joints_; i++) 00065 { 00066 joint_state_.name[i] = names_[i].c_str(); 00067 } 00068 00069 return true; 00070 } 00071 00072 void JointStatePublisher::updateHook() 00073 { 00074 if (msr_jnt_pos_port_.read(jnt_pos_) == RTT::NewData) 00075 { 00076 if (jnt_pos_.size() == number_of_joints_) 00077 { 00078 joint_state_.header.stamp = now(); 00079 for (unsigned int i = 0; i < number_of_joints_; i++) 00080 { 00081 joint_state_.position[i] = jnt_pos_[i]; 00082 // joint_state_.velocity[i] = servo_state_.states[i].velocity; 00083 // joint_state_.effort[i] = servo_state_.states[i].effort; 00084 } 00085 joint_state_port_.write(joint_state_); 00086 } 00087 else 00088 { 00089 RTT::Logger::log(RTT::Logger::Error) 00090 << "Received servo state have invalid size (received : " 00091 << jnt_pos_.size() << " expected : " << number_of_joints_ << " )" 00092 << RTT::endlog(); 00093 } 00094 } 00095 } 00096 00097 ORO_CREATE_COMPONENT( JointStatePublisher )