00001 //================================================================================================= 00002 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt 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 // * Redistributions of source code must retain the above copyright 00008 // notice, this list of conditions and the following disclaimer. 00009 // * Redistributions in binary form must reproduce the above copyright 00010 // notice, this list of conditions and the following disclaimer in the 00011 // documentation and/or other materials provided with the distribution. 00012 // * Neither the name of the Flight Systems and Automatic Control group, 00013 // TU Darmstadt, nor the names of its contributors may be used to 00014 // endorse or promote products derived from this software without 00015 // specific prior written permission. 00016 00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 //================================================================================================= 00028 00029 #ifndef GAZEBO_RTT_PLUGIN_GAZEBO_RTT_PUBLISHER_H 00030 #define GAZEBO_RTT_PLUGIN_GAZEBO_RTT_PUBLISHER_H 00031 00032 #include <ros/publisher.h> 00033 00034 #include <rtt/base/ChannelElement.hpp> 00035 #include <rtt/OutputPort.hpp> 00036 00037 namespace gazebo { 00038 00039 class RTTPublisher : public ros::Publisher 00040 { 00041 public: 00042 RTTPublisher() : connection_id(this) {} 00043 RTTPublisher(const ros::Publisher& pub) : ros::Publisher(pub), connection_id(this) {} 00044 00045 template <typename T> 00046 class PublisherChannelElement : public RTT::base::ChannelElement<T> 00047 { 00048 public: 00049 PublisherChannelElement(const RTTPublisher* publisher) : publisher(publisher) {} 00050 virtual ~PublisherChannelElement() {} 00051 00052 virtual bool write(typename RTT::base::ChannelElement<T>::param_t sample) { 00053 if (!publisher || !*publisher) return false; 00054 publisher->publish(sample); 00055 return true; 00056 } 00057 00058 private: 00059 const RTTPublisher* publisher; 00060 }; 00061 00062 class ConnID : public RTT::internal::ConnID 00063 { 00064 public: 00065 ConnID(const RTTPublisher* publisher) : publisher(publisher) {} 00066 virtual ~ConnID() {} 00067 00068 virtual bool isSameID(RTT::internal::ConnID const& id) const { 00069 const ConnID *other = dynamic_cast<const ConnID *>(&id); 00070 if (!other) return false; 00071 return other == this; 00072 } 00073 00074 virtual RTT::internal::ConnID* clone() const { 00075 return new ConnID(*this); 00076 } 00077 00078 private: 00079 const RTTPublisher* publisher; 00080 }; 00081 00082 template <typename T> 00083 bool connect(RTT::base::PortInterface *port) { 00084 RTT::OutputPort<T> *output_port = dynamic_cast<RTT::OutputPort<T> *>(port); 00085 if (!output_port) return false; 00086 00087 channel.reset(new PublisherChannelElement<T>(this)); 00088 return output_port->addConnection(&connection_id, channel, RTT::ConnPolicy()); 00089 } 00090 00091 private: 00092 ConnID connection_id; 00093 RTT::base::ChannelElementBase::shared_ptr channel; 00094 }; 00095 00096 } // namespace gazebo 00097 00098 #endif // GAZEBO_RTT_PLUGIN_GAZEBO_RTT_PUBLISHER_H