-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_client_template.cpp
78 lines (71 loc) · 3.02 KB
/
action_client_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
#include <ros/ros.h>
#include <actionlib/client/simple_action_client.h>
#include <actionlib/client/terminal_state.h>
#include <ros_template_programs/TimerAction.h>
#include <iostream>
namespace ros_template_programs {
class ActionClient {
private:
ros::NodeHandle nh_;
ros::NodeHandle pnh_;
actionlib::SimpleActionClient<ros_template_programs::TimerAction> act_clt_;
ros_template_programs::TimerGoal goal_;
bool action_executing_;
void sendGoal();
void doneCb( const actionlib::SimpleClientGoalState& state, const ros_template_programs::TimerResultConstPtr& result );
void activeCb( );
void feedbackCb( const ros_template_programs::TimerFeedbackConstPtr& feedback );
public:
ActionClient( );
void loopCallServer();
};
}
void ros_template_programs::ActionClient::sendGoal() {
act_clt_.sendGoal( goal_,
boost::bind( &ros_template_programs::ActionClient::doneCb, this, _1, _2 ),
boost::bind( &ros_template_programs::ActionClient::activeCb, this ),
boost::bind( &ros_template_programs::ActionClient::feedbackCb, this, _1 ) );
}
void ros_template_programs::ActionClient::doneCb(
const actionlib::SimpleClientGoalState& state,
const ros_template_programs::TimerResultConstPtr& result ) {
ROS_INFO( "[ %s ]\nFinished!!", ros::this_node::getName().c_str());
ROS_INFO( "[ %s ]\nTotal_time = %.2f", ros::this_node::getName().c_str(), result->total_time );
action_executing_ = false;
ros::Duration(1.0).sleep();
}
void ros_template_programs::ActionClient::activeCb( ) {
ROS_INFO( "[ %s ]\nGoal just went active...\n", ros::this_node::getName().c_str());
}
void ros_template_programs::ActionClient::feedbackCb(
const ros_template_programs::TimerFeedbackConstPtr& feedback ) {
ROS_INFO( "[ %s ]\nGot Feedback of Progress to Goal\nelapsed_time = %.2f\n", ros::this_node::getName().c_str(), feedback->elapsed_time );
}
ros_template_programs::ActionClient::ActionClient( ) : nh_(), pnh_("~"), act_clt_( "timer_action_server", true ) {
ROS_INFO( "[ %s ] Waiting For Server...", ros::this_node::getName().c_str() );
act_clt_.waitForServer();
ROS_INFO("[ %s ] Connect to the action server", ros::this_node::getName().c_str());
action_executing_ = false;
}
void ros_template_programs::ActionClient::loopCallServer( ) {
ros::Rate loop_rate(1);
double target_time = pnh_.param<double>("target_time", 10.0);
while(ros::ok()) {
if ( !action_executing_ ) {
goal_.target_time = target_time;
sendGoal();
ROS_INFO( "[ %s ] Set a new target_time = %.2f\n", ros::this_node::getName().c_str(), target_time );
action_executing_ = true;
}
ros::spinOnce();
loop_rate.sleep();
}
return;
}
int main(int argc, char *argv[]) {
ros::init(argc, argv, "action_client_template");
ros_template_programs::ActionClient act_clt;
act_clt.loopCallServer();
ros::spin();
return 0;
}