Sarafun Behavior Trees package  1
Behavior trees for the SARAFun project
 All Data Structures Namespaces Functions Variables Enumerations Enumerator
sarafun_test_server.h
1 #ifndef __GENERIC_TEST_SERVER__
2 #define __GENERIC_TEST_SERVER__
3 #include <ros/ros.h>
4 #include <actionlib/server/simple_action_server.h>
5 
6 namespace sarafun {
12 template <class ActionClass, class ActionGoalConstPtr, class ActionFeedback,
13  class ActionResult>
14 class TestServer {
15 public:
16  TestServer(std::string node_name, std::string actionlib_name);
17  virtual ~TestServer() {}
18  void executeCB(const ActionGoalConstPtr &goal);
19 
20 protected:
26  virtual bool parseGoal(const ActionGoalConstPtr &goal) = 0;
27  std::string action_name_;
28 
29 private:
30  ros::NodeHandle nh_;
31  actionlib::SimpleActionServer<ActionClass> action_server_;
32  ActionFeedback feedback_;
33  ActionResult result_;
34 };
35 
36 template <class ActionClass, class ActionGoalConstPtr, class ActionFeedback,
37  class ActionResult>
38 TestServer<ActionClass, ActionGoalConstPtr, ActionFeedback,
39  ActionResult>::TestServer(std::string node_name,
40  std::string actionlib_name)
41  : action_server_(nh_, actionlib_name,
42  boost::bind(&TestServer::executeCB, this, _1), false),
43  action_name_(actionlib_name) {
44  action_server_.start();
45 
46  ROS_INFO("Started test server %s!", actionlib_name.c_str());
47 }
48 
49 template <class ActionClass, class ActionGoalConstPtr, class ActionFeedback,
50  class ActionResult>
51 void TestServer<ActionClass, ActionGoalConstPtr, ActionFeedback,
52  ActionResult>::executeCB(const ActionGoalConstPtr &goal) {
53  ROS_INFO("BT Test server %s has been called!", action_name_.c_str());
54  if (!parseGoal(goal)) {
55  action_server_.setPreempted();
56  return;
57  }
58 
59  ROS_INFO("Press 'p' for preempting this action, 'a' to abort, anything else "
60  "to succeed");
61  std::string input;
62  char key;
63  getline(std::cin, input);
64 
65  if (input.length() == 1) {
66  key = input[0];
67  } else {
68  key = 's';
69  }
70 
71  switch (key) {
72  case 'p':
73  ROS_WARN("Preempting!");
74  action_server_.setPreempted();
75  break;
76  case 'a':
77  ROS_ERROR("Aborting!");
78  action_server_.setAborted();
79  break;
80  default:
81  ROS_INFO("Succeeding!");
82  action_server_.setSucceeded();
83  break;
84  }
85 }
86 }
87 #endif
actionlib::SimpleActionServer< ActionClass > action_server_
TestServer(std::string node_name, std::string actionlib_name)
ActionFeedback feedback_
void executeCB(const ActionGoalConstPtr &goal)
virtual bool parseGoal(const ActionGoalConstPtr &goal)=0