-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_server_template.cpp
79 lines (67 loc) · 2.79 KB
/
action_server_template.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include <ros_template_programs/TimerAction.h>
#include <iostream>
namespace ros_template_programs {
class ActionServer {
private:
ros::NodeHandle nh_;
ros::NodeHandle pnh_;
actionlib::SimpleActionServer<ros_template_programs::TimerAction> act_srv_;
ros_template_programs::TimerGoal goal_;
double init_time;
void goalCb() ;
void preemptCb();
void controlCb();
public:
ActionServer( );
};
}
void ros_template_programs::ActionServer::goalCb() {
goal_ = *act_srv_.acceptNewGoal();
ROS_INFO("[ %s ] Got a new target_time = %.2f", ros::this_node::getName().c_str(), goal_.target_time);
controlCb();
return;
}
void ros_template_programs::ActionServer::preemptCb() {
ROS_INFO("[ %s ] Preempted\n", ros::this_node::getName().c_str() );
ros_template_programs::TimerResult result;
result.total_time = -1.0;
act_srv_.setPreempted( result, "I got Preempted!" );
return;
}
void ros_template_programs::ActionServer::controlCb() {
if ( !act_srv_.isActive() || act_srv_.isPreemptRequested() ) return;
ROS_INFO( "[ %s ]\nStart Timer", ros::this_node::getName().c_str() );
ros_template_programs::TimerFeedback feedback;
ros_template_programs::TimerResult result;
double init_time = ros::Time::now().toSec();
double curt_time = ros::Time::now().toSec();
double target_time = goal_.target_time;
feedback.elapsed_time = curt_time - init_time;
ros::Rate loop_rate(2);
while ( feedback.elapsed_time < target_time ) {
curt_time = ros::Time::now().toSec();
feedback.elapsed_time = curt_time - init_time;
act_srv_.publishFeedback( feedback );
ROS_INFO( "[ %s ]\npublishFeedback\nelapsed_time = %.2f\n", ros::this_node::getName().c_str(), feedback.elapsed_time );
ros::spinOnce();
loop_rate.sleep();
}
result.total_time = feedback.elapsed_time;
ROS_INFO("[ %s ]\nIt's time to end\ntotal_time = %.2f\n", ros::this_node::getName().c_str(), result.total_time );
act_srv_.setSucceeded( result );
return;
}
ros_template_programs::ActionServer::ActionServer( ) : nh_(), pnh_("~"), act_srv_( nh_, "timer_action_server", false ) {
act_srv_.registerGoalCallback( boost::bind( &ros_template_programs::ActionServer::goalCb, this ) );
act_srv_.registerPreemptCallback ( boost::bind( &ros_template_programs::ActionServer::preemptCb, this ) );
act_srv_.start();
ROS_INFO( "[ %s ] Start the Server...\n", ros::this_node::getName().c_str() );
}
int main(int argc, char *argv[]) {
ros::init(argc, argv, "action_server_template");
ros_template_programs::ActionServer act_srv;
ros::spin();
return 0;
}