pr2_gripper_findContact_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_findContact_action.cpp - action server for sending
35 // findContact commands to the roobt
36 
37 #include "ros/ros.h"
38 
40 #include <pr2_gripper_sensor_msgs/PR2GripperFindContactAction.h>
41 #include <pr2_gripper_sensor_msgs/PR2GripperFindContactData.h>
42 #include <pr2_gripper_sensor_msgs/PR2GripperFindContactCommand.h>
43 #include <std_srvs/Empty.h>
44 
46 {
47 private:
50 public:
52  node_(n),
53  action_server_(node_, "find_contact",
54  boost::bind(&Pr2GripperFindContact::goalCB, this, _1),
55  boost::bind(&Pr2GripperFindContact::cancelCB, this, _1)),
56  has_active_goal_(false)
57  {
58  ros::NodeHandle pn("~");
59 
60  // we will listen for messages on "state" and send messages on "find_contact"
62  node_.advertise<pr2_gripper_sensor_msgs::PR2GripperFindContactCommand>("find_contact", 1);
64  node_.subscribe("contact_state", 1, &Pr2GripperFindContact::controllerStateCB, this);
65 
67 
68  }
69 
71  {
75  }
76 
77 private:
78 
84 
86  GoalHandle active_goal_;
88 
92 
93 
94  void watchdog(const ros::TimerEvent &e)
95  {
96  ros::Time now = ros::Time::now();
97 
98  // Aborts the active goal if the controller does not appear to be active.
99  if (has_active_goal_)
100  {
101  bool should_abort = false;
103  {
104  should_abort = true;
105  ROS_WARN("Aborting goal because we have never heard a controller state message.");
106  }
107  else if ((now - last_controller_state_->stamp) > ros::Duration(5.0))
108  {
109  should_abort = true;
110  ROS_WARN("Aborting goal because we haven't heard from the controller in %.3lf seconds",
111  (now - last_controller_state_->stamp).toSec());
112  }
113 
114  if (should_abort)
115  {
116  // Marks the current goal as aborted.
117  active_goal_.setAborted();
118  has_active_goal_ = false;
119  }
120  }
121  }
122 
123  void goalCB(GoalHandle gh)
124  {
125  // Cancels the currently active goal.
126  if (has_active_goal_)
127  {
128  // Marks the current goal as canceled.
129  active_goal_.setCanceled();
130  has_active_goal_ = false;
131  }
132 
133  gh.setAccepted();
134  active_goal_ = gh;
135  has_active_goal_ = true;
136  goal_received_ = ros::Time::now();
137 
138  // update our zero values for 0.25 seconds
139  if(active_goal_.getGoal()->command.zero_fingertip_sensors)
140  {
141  std_srvs::Empty::Request req;
142  std_srvs::Empty::Response resp;
143  if(ros::service::exists("zero_fingertip_sensors",true))
144  {
145  ROS_INFO("updating zeros!");
146  ros::service::call("zero_fingertip_sensors",req,resp);
147  }
148  }
149 
150  // Sends the command along to the controller.
151  pub_controller_command_.publish(active_goal_.getGoal()->command);
152 
153  last_movement_time_ = ros::Time::now();
154  action_start_time = ros::Time::now();
155  }
156 
157  void cancelCB(GoalHandle gh)
158  {
159 
160  if (active_goal_ == gh)
161  {
162  // stop the real-time motor control
163  std_srvs::Empty::Request req;
164  std_srvs::Empty::Response resp;
165  if(ros::service::exists("stop_motor_output",true))
166  {
167  ROS_INFO("Stopping Motor Output");
168  ros::service::call("stop_motor_output",req,resp);
169  }
170 
171  // Marks the current goal as canceled.
172  active_goal_.setCanceled();
173  has_active_goal_ = false;
174  }
175  }
176 
177 
178  pr2_gripper_sensor_msgs::PR2GripperFindContactDataConstPtr last_controller_state_;
179  void controllerStateCB(const pr2_gripper_sensor_msgs::PR2GripperFindContactDataConstPtr &msg)
180  {
181  last_controller_state_ = msg;
182  ros::Time now = ros::Time::now();
183 
184  if (!has_active_goal_)
185  return;
186 
187  pr2_gripper_sensor_msgs::PR2GripperFindContactGoal goal;
188  goal.command = active_goal_.getGoal()->command;
189 
190  pr2_gripper_sensor_msgs::PR2GripperFindContactFeedback feedback;
191  feedback.data = *msg;
192 
193  pr2_gripper_sensor_msgs::PR2GripperFindContactResult result;
194  result.data = *msg;
195 
196  // do not check until some dT has expired from message start
197  double dT = 0.2;
198  if(feedback.data.stamp.toSec() < action_start_time.toSec()+dT ){}
199 
200 
201  // if we are actually in a find_contact control state or positoin control state
202  else if(feedback.data.rtstate.realtime_controller_state == 5 || feedback.data.rtstate.realtime_controller_state == 3)
203  {
204  if(feedback.data.contact_conditions_met)
205  {
206  active_goal_.setSucceeded(result);
207  has_active_goal_ = false;
208  }
209  }
210 
211  else
212  has_active_goal_ = false;
213 
214  active_goal_.publishFeedback(feedback);
215  }
216 };
217 
218 
219 int main(int argc, char** argv)
220 {
221  ros::init(argc, argv, "find_contact_node");
222  ros::NodeHandle node;
223  Pr2GripperFindContact jte(node);
224 
225  ros::spin();
226 
227  return 0;
228 }
void publishFeedback(const Feedback &feedback)
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool call(const std::string &service_name, MReq &req, MRes &res)
void stop()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void watchdog(const ros::TimerEvent &e)
int main(int argc, char **argv)
boost::shared_ptr< const Goal > getGoal() const
#define ROS_WARN(...)
void setCanceled(const Result &result=Result(), const std::string &text=std::string(""))
ROSCPP_DECL void spin(Spinner &spinner)
void setAccepted(const std::string &text=std::string(""))
actionlib::ActionServer< pr2_gripper_sensor_msgs::PR2GripperFindContactAction > GAS
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
#define ROS_INFO(...)
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
static Time now()
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)
pr2_gripper_sensor_msgs::PR2GripperFindContactDataConstPtr last_controller_state_
resp
void controllerStateCB(const pr2_gripper_sensor_msgs::PR2GripperFindContactDataConstPtr &msg)


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