From 8c4fce3654bbf4cb31314f1fb596f4fd17772589 Mon Sep 17 00:00:00 2001 From: Thomas Gubler Date: Thu, 22 Jan 2015 09:30:43 +0100 Subject: multiplatform: better publisher base class --- src/examples/publisher/publisher_example.h | 3 +- src/platforms/px4_nodehandle.h | 14 +++++----- src/platforms/px4_publisher.h | 44 ++++++++++++++++++++---------- 3 files changed, 39 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/examples/publisher/publisher_example.h b/src/examples/publisher/publisher_example.h index 304ecef47..4ff59a226 100644 --- a/src/examples/publisher/publisher_example.h +++ b/src/examples/publisher/publisher_example.h @@ -37,6 +37,7 @@ * * @author Thomas Gubler */ +#pragma once #include class PublisherExample { @@ -48,5 +49,5 @@ public: int main(); protected: px4::NodeHandle _n; - px4::Publisher * _rc_channels_pub; + px4::Publisher * _rc_channels_pub; }; diff --git a/src/platforms/px4_nodehandle.h b/src/platforms/px4_nodehandle.h index 8fafa168a..a25d57845 100644 --- a/src/platforms/px4_nodehandle.h +++ b/src/platforms/px4_nodehandle.h @@ -124,12 +124,12 @@ public: * Advertise topic */ template - Publisher* advertise() + Publisher* advertise() { ros::Publisher ros_pub = ros::NodeHandle::advertisedata())>::type &>(T::handle(), kQueueSizeDefault); - Publisher *pub = new Publisher(ros_pub); - _pubs.push_back(pub); - return pub; + PublisherROS *pub = new PublisherROS(ros_pub); + _pubs.push_back((PublisherBase*)pub); + return (Publisher*)pub; } /** @@ -236,16 +236,16 @@ public: * Advertise topic */ template - Publisher *advertise() + Publisher *advertise() { //XXX // uORB::PublicationBase * uorb_pub = new uORB::PublicationBase((new T())->handle()); uORB::PublicationBase * uorb_pub = new uORB::PublicationBase(T::handle()); - Publisher *pub = new Publisher(uorb_pub); + PublisherUORB *pub = new PublisherUORB(uorb_pub); _pubs.add(pub); - return pub; + return (Publisher*)pub; } /** diff --git a/src/platforms/px4_publisher.h b/src/platforms/px4_publisher.h index 7195777f5..afedbbee7 100644 --- a/src/platforms/px4_publisher.h +++ b/src/platforms/px4_publisher.h @@ -61,29 +61,44 @@ public: ~PublisherBase() {}; }; +/** + * Publisher base class, templated with the message type + * */ +template +class __EXPORT Publisher +{ +public: + Publisher() {}; + ~Publisher() {}; + + virtual int publish(const T &msg) = 0; +}; + #if defined(__PX4_ROS) -class Publisher : - public PublisherBase +template +class PublisherROS : + public Publisher { public: /** * Construct Publisher by providing a ros::Publisher * @param ros_pub the ros publisher which will be used to perform the publications */ - Publisher(ros::Publisher ros_pub) : - PublisherBase(), + PublisherROS(ros::Publisher ros_pub) : + Publisher(), _ros_pub(ros_pub) {} - ~Publisher() {}; + ~PublisherROS() {}; /** Publishes msg * @param msg the message which is published to the topic */ - template - int publish(T &msg) { + int publish(const T &msg) + { _ros_pub.publish(msg.data()); - return 0;} + return 0; + } private: ros::Publisher _ros_pub; /**< Handle to the ros publisher */ }; @@ -104,8 +119,9 @@ public: virtual void update() = 0; }; -class __EXPORT Publisher : - public PublisherBase, +template +class __EXPORT PublisherUORB : + public Publisher, public PublisherNode { @@ -113,17 +129,17 @@ public: /** * Construct Publisher by providing orb meta data */ - Publisher(uORB::PublicationBase * uorb_pub) : - PublisherBase(), + PublisherUORB(uORB::PublicationBase * uorb_pub) : + Publisher(), + PublisherNode(), _uorb_pub(uorb_pub) {} - ~Publisher() {}; + ~PublisherUORB() {}; /** Publishes msg * @param msg the message which is published to the topic */ - template int publish(const T &msg) { _uorb_pub->update((void *)&(msg.data())); -- cgit v1.2.3