nodelet.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, UC Regents
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the University of California nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 #include <ros/ros.h>
00035 #include <nodelet/nodelet.h>
00036 #include <pluginlib/class_list_macros.h>
00037 #include <std_msgs/String.h>
00038 
00039 namespace some_namespace
00040 {
00041 
00042 using std::string;
00043 
00044 class MyNodelet : public nodelet::Nodelet
00045 {
00046 private:
00047   ros::NodeHandle nh_priv;
00048   ros::NodeHandle nh;
00049   // Params
00050   bool enable_awesomeness; // enable cool stuff
00051   double awesome_factor; // [rad/s] amount of awesomeness
00052   // Publishers
00053   ros::Publisher awesome_pub;
00054   // Subscribers
00055   ros::Subscriber chatter_sub;
00056   // Timers
00057   ros::Timer output_timer;
00058   // Members
00059   string latest_chatter;
00060 
00061 public:
00062   MyNodelet() :
00063     enable_awesomeness(true), awesome_factor(1.57)
00064   {
00065   }
00066 private:
00067   void onInit()
00068   {
00069     nh = getMTNodeHandle();
00070     nh_priv = getMTPrivateNodeHandle();
00071 
00072     NODELET_INFO("onInit()");
00073 
00074     // Parameters
00075     nh_priv.param("enable_awesomeness", enable_awesomeness, enable_awesomeness);
00076     nh_priv.param("awesome_factor", awesome_factor, awesome_factor);
00077     // Publishers
00078     awesome_pub = nh_priv.advertise<std_msgs::String> ("awesome", 10);
00079     // Subscribers
00080     chatter_sub = nh.subscribe("chatter", 10, &MyNodelet::chatterCb, this);
00081     // Timers
00082     output_timer = nh.createTimer(ros::Duration(0.1), &MyNodelet::outputTimerCb, this);
00083   }
00084 
00085   void outputTimerCb(const ros::TimerEvent& e)
00086   {
00087     std_msgs::String output;
00088     std::ostringstream s;
00089     s << "foo bar baz " << awesome_factor;
00090     output.data = s.str();
00091     awesome_pub.publish(output);
00092   }
00093 
00094   void chatterCb(const std_msgs::StringConstPtr& chatter)
00095   {
00096     NODELET_INFO_STREAM("Heard chatter: " << chatter->data);
00097   }
00098 
00099 };
00100 PLUGINLIB_DECLARE_CLASS(some_namespace, MyNodelet, some_namespace::MyNodelet, nodelet::Nodelet)
00101 ;
00102 }


starmac_templates
Author(s): bouffard
autogenerated on Sun Jan 5 2014 11:39:05