pr2_gripper_release_action.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * \author Joe Romano
30  */
31 
32 // @author: Joe Romano
33 // @email: joeromano@gmail.com
34 // @brief: pr2_gripper_release_action.cpp - action server for the
35 // pr2_gripper_release action command
36 
37 #include <ros/ros.h>
38 #include <pr2_controllers_msgs/Pr2GripperCommandAction.h>
39 #include <pr2_gripper_sensor_msgs/PR2GripperReleaseAction.h>
40 #include <pr2_gripper_sensor_msgs/PR2GripperEventDetectorAction.h>
41 #include <std_srvs/Empty.h>
44 
45 
47 // Our Action interface type, provided as a typedef for convenience
49 
50 
51 
52 class Gripper{
53 private:
56 
57 
58 public:
59  //Action client initialization
61 
62  //Initialize the client for the Action interface to the gripper controller
63  //and tell the action client that we want to spin a thread by default
64  gripper_client_ = new GripperClient("gripper_action", true);
65  event_client_ = new EventClient("event_detector",true);
66 
67 
68  //wait for the gripper action server to come up
69  while(!gripper_client_->waitForServer(ros::Duration(5.0))){
70  ROS_INFO("Waiting for the gripper_action action server to come up");
71  }
72 
73  while(!event_client_->waitForServer(ros::Duration(5.0))){
74  ROS_INFO("Waiting for the event_detector action server to come up");
75  }
76  }
77 
79  delete gripper_client_;
80  delete event_client_;
81  }
82 
83  //Open the gripper
84  void open(double position_open){
85 
86  pr2_controllers_msgs::Pr2GripperCommandGoal open;
87  open.command.position = position_open; // position open (9 cm)
88  open.command.max_effort = -1.0; // max force
89 
90  ROS_INFO("Sending open goal");
91  gripper_client_->sendGoal(open);
92  // do not wait, return right away so that the user knows contact was made
93  }
94 
95  //move into event_detector mode to drop an object
96  void place(int trigger_conditions, double acc_trigger, double slip_trigger){
97  pr2_gripper_sensor_msgs::PR2GripperEventDetectorGoal place_goal;
98  place_goal.command.trigger_conditions = trigger_conditions;
99  place_goal.command.acceleration_trigger_magnitude = acc_trigger; // set the contact acceleration to n m/s^2
100  place_goal.command.slip_trigger_magnitude = slip_trigger;
101 
102  ROS_INFO("Waiting for object placement contact...");
103  event_client_->sendGoal(place_goal);
104  event_client_->waitForResult();
106  {
107  ROS_INFO("Place Success");
108  ROS_INFO("cond met: %d, acc_met: %d, slip_met: %d", event_client_->getResult()->data.trigger_conditions_met, event_client_->getResult()->data.acceleration_event, event_client_->getResult()->data.slip_event);
109  }
110  else
111  ROS_INFO("Place Failure");
112  }
113 
114 
115 };
116 
117 
118 
120 {
121 protected:
122 
125  std::string action_name_;
126  // create messages that are used to published feedback/result
127  pr2_gripper_sensor_msgs::PR2GripperReleaseFeedback feedback_;
128  pr2_gripper_sensor_msgs::PR2GripperReleaseResult result_;
130 
131 public:
132 
133  PR2GripperReleaseAction(std::string name) :
134  as_(nh_, name, boost::bind(&PR2GripperReleaseAction::executeCB, this, _1)),
135  action_name_(name)
136  {
137  }
138 
140  {
141  }
142 
143  void executeCB(const pr2_gripper_sensor_msgs::PR2GripperReleaseGoalConstPtr &goal)
144  {
145  double position_open;
146  if(!nh_.getParam("position_open", position_open))
147  ROS_ERROR("No position_open given in namespace: '%s')", nh_.getNamespace().c_str());
148 
149  // helper variables
150  bool prempted = false;
151 
152  gripper.place(goal->command.event.trigger_conditions, goal->command.event.acceleration_trigger_magnitude, goal->command.event.slip_trigger_magnitude);
153 
154  // check that preempt has not been requested by the client
155  if (as_.isPreemptRequested() || !ros::ok())
156  {
157  ROS_INFO("%s: Preempted", action_name_.c_str());
158  // set the action state to preempted
159  as_.setPreempted();
160  prempted = true;
161  }
162  if(!prempted)
163  gripper.open(position_open);
164 
165 
166  result_.data.rtstate.realtime_controller_state = feedback_.data.rtstate.realtime_controller_state;
167  ROS_INFO("%s: Succeeded", action_name_.c_str());
168  // set the action state to succeeded
169  if(!prempted)
170  as_.setSucceeded(result_);
171  }
172 };
173 
174 
175 int main(int argc, char** argv)
176 {
177  ros::init(argc, argv, "release");
178 
179  PR2GripperReleaseAction Release("release");
180  ros::spin();
181 
182  return 0;
183 }
184 
bool waitForServer(const ros::Duration &timeout=ros::Duration(0, 0)) const
pr2_gripper_sensor_msgs::PR2GripperReleaseResult result_
void open(double position_open)
bool waitForResult(const ros::Duration &timeout=ros::Duration(0, 0))
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void place(int trigger_conditions, double acc_trigger, double slip_trigger)
int main(int argc, char **argv)
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
ROSCPP_DECL void spin(Spinner &spinner)
void setPreempted(const Result &result=Result(), const std::string &text=std::string(""))
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
const std::string & getNamespace() const
pr2_gripper_sensor_msgs::PR2GripperReleaseFeedback feedback_
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
actionlib::SimpleActionServer< pr2_gripper_sensor_msgs::PR2GripperReleaseAction > as_
bool getParam(const std::string &key, std::string &s) const
GripperClient * gripper_client_
EventClient * event_client_
SimpleClientGoalState getState() const
ResultConstPtr getResult() const
#define ROS_ERROR(...)
actionlib::SimpleActionClient< pr2_gripper_sensor_msgs::PR2GripperEventDetectorAction > EventClient
void executeCB(const pr2_gripper_sensor_msgs::PR2GripperReleaseGoalConstPtr &goal)
actionlib::SimpleActionClient< pr2_controllers_msgs::Pr2GripperCommandAction > GripperClient


pr2_gripper_sensor_action
Author(s): Joe Romano
autogenerated on Wed Apr 1 2020 03:58:26