00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _ROS_MSG_ACTIVITY_HPP_
00030 #define _ROS_MSG_ACTIVITY_HPP_
00031
00032 #include <rtt/Activity.hpp>
00033 #include <rtt/os/Mutex.hpp>
00034 #include <rtt/os/MutexLock.hpp>
00035 #include <boost/shared_ptr.hpp>
00036 #include <rtt/Logger.hpp>
00037
00038 #include <ros/ros.h>
00039
00040 namespace ros_integration{
00041 using namespace RTT;
00042
00047 struct RosPublisher
00048 {
00049 public:
00053 virtual void publish()=0;
00054 };
00055
00056
00065 class RosPublishActivity : public RTT::Activity {
00066 public:
00067 typedef boost::shared_ptr<RosPublishActivity> shared_ptr;
00068 private:
00069 typedef boost::weak_ptr<RosPublishActivity> weak_ptr;
00071 static weak_ptr ros_pub_act;
00072
00076 typedef std::map< RosPublisher*, bool> Publishers;
00077 Publishers publishers;
00078 os::Mutex map_lock;
00079
00080 RosPublishActivity( const std::string& name)
00081 : Activity(0, name)
00082 {
00083 Logger::In in("RosPublishActivity");
00084 log(Debug)<<"Creating RosPublishActivity"<<endlog();
00085 }
00086
00087 void loop(){
00088 os::MutexLock lock(map_lock);
00089 for(Publishers::const_iterator it = publishers.begin(); it != publishers.end(); ++it)
00090 if (it->second)
00091 it->first->publish();
00092 }
00093
00094 public:
00095
00103 static shared_ptr Instance() {
00104 shared_ptr ret = ros_pub_act.lock();
00105 if ( !ret ) {
00106 ret.reset(new RosPublishActivity("RosPublishActivity"));
00107 ros_pub_act = ret;
00108 ret->start();
00109 }
00110 return ret;
00111 }
00112
00113 void addPublisher(RosPublisher* pub) {
00114 os::MutexLock lock(map_lock);
00115 publishers[pub] = false;
00116 }
00117
00118 void removePublisher(RosPublisher* pub) {
00119 os::MutexLock lock(map_lock);
00120 publishers.erase(pub);
00121 }
00122
00128 bool requestPublish(RosPublisher* chan){
00129
00130 {
00131 os::MutexLock lock(map_lock);
00132 assert(publishers.find(chan) != publishers.end() );
00133 publishers.find(chan)->second = true;
00134 }
00135
00136 this->trigger();
00137 return true;
00138 }
00139 ~RosPublishActivity() {
00140 Logger::In in("RosPublishActivity");
00141 log(Info) << "RosPublishActivity cleans up: no more work."<<endlog();
00142 stop();
00143 }
00144
00145 };
00146 }
00147 #endif
00148