robot_activity_tutorials_minimal_node.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2018, University of Luxembourg
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of University of Luxembourg nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * Author: Maciej Zurad
00036  *********************************************************************/
00037 #include <ros/ros.h>
00038 #include <robot_activity/managed_robot_activity.h>
00039 
00040 #include <robot_activity_msgs/State.h>
00041 #include <std_srvs/Empty.h>
00042 
00043 namespace robot_activity_tutorials
00044 {
00045 
00046 class MyRobotActivity : public robot_activity::ManagedRobotActivity
00047 {
00048 public:
00049   /* Important to inherit the constructor */
00050   using ManagedRobotActivity::ManagedRobotActivity;
00051   ~MyRobotActivity()
00052   {
00053     ROS_DEBUG_STREAM(getNamespace() << " destructed!");
00054   }
00055 
00056 private:
00057   void onManagedCreate() override
00058   {
00059     subscriber_manager.subscribe("/heartbeat", 1,
00060                                  &MyRobotActivity::mySubscriberCallback, this);
00061 
00062     service_manager.advertiseService("test",
00063                                      &MyRobotActivity::myServiceCallback, this);
00064 
00065     auto timer = registerIsolatedTimer(
00066       std::bind(&MyRobotActivity::myTimerCallback, this), 0.5, true);
00067   };
00068   void onManagedTerminate() override {};
00069 
00070   bool onManagedConfigure() override { return true; };
00071   bool onManagedUnconfigure() override { return true; };
00072 
00073   bool onManagedStart() override { return true; };
00074   bool onManagedStop() override { return true; };
00075 
00076   bool onManagedResume() override { return true; };
00077   bool onManagedPause() override { return true; };
00078 
00079   void myTimerCallback()
00080   {
00081     ROS_INFO_STREAM(getNamespace() << " Timer Counter: " << counter);
00082     counter++;
00083     unsigned int seed = time(NULL);
00084     float r2 = rand_r(&seed) / (RAND_MAX / 0.10);
00085     ros::Duration(2.05 - r2).sleep();
00086   };
00087 
00088   void mySubscriberCallback(boost::shared_ptr<robot_activity_msgs::State const> msg)
00089   {
00090     ROS_INFO_STREAM(getNamespace() << " "
00091                     << msg->node_name << " is in " << unsigned(msg->state));
00092   };
00093 
00094   bool myServiceCallback(
00095     std_srvs::Empty::Request& request,
00096     std_srvs::Empty::Response& response)
00097   {
00098     ROS_INFO_STREAM(getNamespace() << " Service called, returning true");
00099     return true;
00100   };
00101 
00102   int counter = 0;
00103 };
00104 
00105 }  // namespace robot_activity_tutorials
00106 
00107 int main(int argc, char *argv[])
00108 {
00109   using robot_activity_tutorials::MyRobotActivity;
00110 
00111   MyRobotActivity node1(argc, argv, "first");
00112   node1.init().runAsync();
00113 
00114   MyRobotActivity node2(argc, argv, "second");
00115   node2.init().runAsync();
00116 
00117   ros::waitForShutdown();
00118   return 0;
00119 }


robot_activity_tutorials
Author(s): Maciej ZURAD
autogenerated on Thu Jun 6 2019 18:10:06