rosrun actionlib_msgs genaction.py code example
Example: rosrun actionlib_msgs genaction.py
1
2
3
4
5 class FibonacciAction
6 {
7 protected:
8
9 ros::NodeHandle nh_;
10 actionlib::SimpleActionServer<learning_actionlib::FibonacciAction> as_;
11 std::string action_name_;
12
13 learning_actionlib::FibonacciFeedback feedback_;
14 learning_actionlib::FibonacciResult result_;
15
16 public:
17
18 FibonacciAction(std::string name) :
19 as_(nh_, name, boost::bind(&FibonacciAction::executeCB, this, _1), false),
20 action_name_(name)
21 {
22 as_.start();
23 }
24
25 ~FibonacciAction(void)
26 {
27 }
28
29 void executeCB(const learning_actionlib::FibonacciGoalConstPtr &goal)
30 {
31
32 ros::Rate r(1);
33 bool success = true;
34
35
36 feedback_.sequence.clear();
37 feedback_.sequence.push_back(0);
38 feedback_.sequence.push_back(1);
39
40
41 ROS_INFO("%s: Executing, creating fibonacci sequence of order %i with seeds %i, %i", action_name_.c_str(), goal->order, feedback_.sequence[0], feedback_.sequence[1]);
42
43
44 for(int i=1; i<=goal->order; i++)
45 {
46
47 if (as_.isPreemptRequested() || !ros::ok())
48 {
49 ROS_INFO("%s: Preempted", action_name_.c_str());
50
51 as_.setPreempted();
52 success = false;
53 break;
54 }
55 feedback_.sequence.push_back(feedback_.sequence[i] + feedback_.sequence[i-1]);
56
57 as_.publishFeedback(feedback_);
58
59 r.sleep();
60 }
61
62 if(success)
63 {
64 result_.sequence = feedback_.sequence;
65 ROS_INFO("%s: Succeeded", action_name_.c_str());
66
67 as_.setSucceeded(result_);
68 }
69 }
70
71
72 };
73
74
75 int main(int argc, char** argv)
76 {
77 ros::init(argc, argv, "fibonacci");
78
79 FibonacciAction fibonacci(ros::this_node::getName());
80 ros::spin();
81
82 return 0;
83 }