gripper_action_server.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
3 #include <xarm_gripper/MoveAction.h>
4 #include <xarm_ros_client.h>
5 
6 // Please run "export ROS_NAMESPACE=/xarm" first
7 
8 namespace xarm_control
9 {
11  {
12  protected:
13 
15  actionlib::SimpleActionServer<xarm_gripper::MoveAction> as_; // NodeHandle instance must be created before this line. Otherwise strange error occurs.
16  std::string action_name_;
17  // create messages that are used to published feedback/result
18  xarm_gripper::MoveFeedback feedback_;
19  xarm_gripper::MoveResult result_;
20 
22 
23  public:
24 
25  GripperAction(std::string name) :
26  as_(nh_, name, boost::bind(&GripperAction::executeCB, this, _1), false),
27  action_name_(name)
28  {
29  xarm.init(nh_);
30  as_.start();
31  }
32 
34  {
35  }
36 
37  void executeCB(const xarm_gripper::MoveGoalConstPtr &goal);
38 
39  };
40 
41  void GripperAction::executeCB(const xarm_gripper::MoveGoalConstPtr &goal)
42  {
43  ros::Rate r(10);
44  int ret = 0;
45  feedback_.current_pulse = 0;
46 
47  // publish info to the console for the user
48  ROS_INFO("Executing, creating MoveAction ");
49 
50  // start executing the action
51  if(xarm.gripperConfig(goal->pulse_speed))
52  {
53  ROS_INFO("%s: Aborted, not ready", action_name_.c_str());
54  as_.setAborted();
55  return;
56  }
57 
58  if(xarm.gripperMove(goal->target_pulse))
59  {
60  ROS_INFO("%s: Aborted, not ready", action_name_.c_str());
61  as_.setAborted();
62  return;
63  }
64 
65  float fdb_pulse = 0; int fdb_err = 0;
66  ret = xarm.getGripperState(&fdb_pulse, &fdb_err);
67 
68  while(!ret && fabs(fdb_pulse-goal->target_pulse)>10)
69  {
70 
71  feedback_.current_pulse = fdb_pulse;
73 
74  if (as_.isPreemptRequested() || !ros::ok())
75  {
76  ROS_INFO("%s: Preempted", action_name_.c_str());
77  // set the action state to preempted
78  xarm.gripperMove(fdb_pulse);
79  as_.setPreempted();
80  return;
81  }
82 
83  r.sleep();
84 
85  ret = xarm.getGripperState(&fdb_pulse, &fdb_err);
86 
87  }
88 
89  if(!ret)
90  {
91  result_.success = true;
92  result_.err_code = fdb_err;
93  ROS_INFO("%s: Succeeded, err_code: %d", action_name_.c_str(), fdb_err);
95  }
96 
97  else
98  {
99  result_.success = false;
100  result_.err_code = fdb_err;
101  ROS_INFO("%s: Failed, ret = %d, err_code: %d", action_name_.c_str(), ret, fdb_err);
102  as_.setAborted();
103  }
104  }
105 
106 }
107 
108 int main(int argc, char** argv)
109 {
110  ros::init(argc, argv, "xarm_gripper_node");
111 
112  xarm_control::GripperAction ga("gripper_move");
113 
114  ros::spin();
115 
116  return 0;
117 }
xarm_gripper::MoveResult result_
void publishFeedback(const FeedbackConstPtr &feedback)
void executeCB(const xarm_gripper::MoveGoalConstPtr &goal)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
ROSCPP_DECL void spin(Spinner &spinner)
int main(int argc, char **argv)
void setPreempted(const Result &result=Result(), const std::string &text=std::string(""))
int gripperConfig(float pulse_vel)
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
actionlib::SimpleActionServer< xarm_gripper::MoveAction > as_
bool sleep()
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
xarm_gripper::MoveFeedback feedback_
int getGripperState(float *curr_pulse, int *curr_err)
void init(ros::NodeHandle &nh)
r
int gripperMove(float pulse)


xarm_gripper
Author(s): jason_peng
autogenerated on Sat May 8 2021 02:51:44