ros_plugin_demo
Demonstration of the ROS PlugInLib
 All Data Structures Namespaces Files Functions Variables Pages
main.cpp
2 #include <pluginlib/class_loader.h>
3 #include "geometry_msgs/PoseWithCovarianceStamped.h"
4 
5 #include <dynamic_reconfigure/server.h>
6 #include <plugin_demo_main/plugin_demoConfig.h>
7 
8 boost::shared_ptr<plugin_demo_interface_namespace::PathPlanner> plugin_node;
9 std::string plugin_name;
10 bool change_plugin, use_shared_pointer;
11 ros::Publisher setStartPub;
12 double delta_x, delta_y, delta_theta;
13 
14 void getStartCallback(const geometry_msgs::PoseWithCovarianceStamped event)
15 {
16  if (use_shared_pointer) //shared pointer demo
17  {
18  geometry_msgs::Pose2DPtr msg(new geometry_msgs::Pose2D);
19  msg->x = event.pose.pose.position.x;
20  msg->y = event.pose.pose.position.y;
21  msg->theta = plugin_node->getYawFromQuat(event.pose.pose.orientation);
22  setStartPub.publish(msg);
23  msg->x += delta_x;
24  msg->y += delta_y;
25  msg->theta += delta_theta;
26  } else {
27  geometry_msgs::Pose2D msg;
28  msg.x = event.pose.pose.position.x;
29  msg.y = event.pose.pose.position.y;
30  msg.theta = plugin_node->getYawFromQuat(event.pose.pose.orientation);
31  setStartPub.publish(msg);
32  msg.x += delta_x;
33  msg.y += delta_y;
34  msg.theta += delta_theta;
35  }
36 }
37 
38 void reconfigureCallback(plugin_demo_main::plugin_demoConfig &config, uint32_t level)
39 {
40  use_shared_pointer = config.use_shared_pointer;
41  delta_x = config.delta_x;
42  delta_y = config.delta_y;
43  delta_theta = config.delta_theta;
44 
45  if (config.plugin != plugin_name)
46  {
47  plugin_name = config.plugin;
48  change_plugin = true;
49  }
50 }
51 
52 int loadPlugin(pluginlib::ClassLoader<plugin_demo_interface_namespace::PathPlanner> *plug_in_loader, ros::NodeHandle *roshandle, std::string plugin_name)
53 {
54  ROS_INFO("[main] starting plugin \"%s\"", plugin_name.c_str());
55  try
56  {
57  plugin_node.reset();
58  plugin_node = plug_in_loader->createInstance(plugin_name.c_str());
59  plugin_node->initialize(*roshandle);
60  } catch(pluginlib::PluginlibException& ex) {
61  ROS_FATAL("[main] failed to load. Error: \"%s\"\nGoodbye.", ex.what());
62  return 1;
63  }
64  ROS_INFO("[main] module loaded");
65  return 0;
66 }
67 
68 int main(int argc, char **argv)
69 {
70  ros::init(argc, argv, "plugin_demo_path_planner");
71  ros::NodeHandle roshandle;
72 
73  dynamic_reconfigure::Server<plugin_demo_main::plugin_demoConfig> reconfServer;
74  dynamic_reconfigure::Server<plugin_demo_main::plugin_demoConfig>::CallbackType f;
75  f = boost::bind(&reconfigureCallback, _1, _2);
76  reconfServer.setCallback(f);
77 
78  ros::Subscriber newStartSub;
79  newStartSub = roshandle.subscribe("get_start", 10, &getStartCallback);
80  setStartPub = roshandle.advertise<geometry_msgs::Pose2D>("set_start", 10);
81  use_shared_pointer = false;
82 
83  //the plugin_name should include the namespace
84  ros::param::param<std::string>("~plugin", plugin_name, "plugin_demo_plugins_namespace::Direct");
85  change_plugin = false;
86 
87  pluginlib::ClassLoader<plugin_demo_interface_namespace::PathPlanner> plug_in_loader("plugin_demo_interface", "plugin_demo_interface_namespace::PathPlanner");
88 
89  loadPlugin(&plug_in_loader, &roshandle, plugin_name);
90  plugin_node->initialize(roshandle);
91 
92  ros::Rate freq(8.0);
93 
94  while (ros::ok())
95  {
96  if (change_plugin) {
97  geometry_msgs::Pose2D start, target;
98  plugin_node->getPoints(&start, &target);
99  loadPlugin(&plug_in_loader, &roshandle, plugin_name);
100  plugin_node->setPoints(&start, &target);
101  change_plugin = false;
102  }
103  ros::spinOnce();
104  freq.sleep();
105  }
106 
107  plugin_node.reset();
108 
109  return 0;
110 }
Contain the Interface class.