pr2_gripper_event_detector_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_event_detector_action.cpp - action server for the pr2_gripper_event_detector
35 // action command
36 
37 #include "ros/ros.h"
38 
40 #include <pr2_gripper_sensor_msgs/PR2GripperEventDetectorAction.h>
41 #include <pr2_gripper_sensor_msgs/PR2GripperEventDetectorData.h>
42 #include <pr2_gripper_sensor_msgs/PR2GripperEventDetectorCommand.h>
43 
44 
46 {
47 private:
50 public:
52  node_(n),
53  action_server_(node_, "event_detector",
54  boost::bind(&Pr2GripperEventDetector::goalCB, this, _1),
55  boost::bind(&Pr2GripperEventDetector::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::PR2GripperEventDetectorCommand>("event_detector", 1);
64  node_.subscribe("event_detector_state", 1, &Pr2GripperEventDetector::controllerStateCB, this);
65 
67 
68  }
69 
71  {
75  }
76 
77 private:
78 
85 
87  GoalHandle active_goal_;
89 
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  // Sends command to trigger controller
139  pub_controller_command_.publish(gh.getGoal()->command);
140 
141  last_movement_time_ = ros::Time::now();
142  action_start_time = ros::Time::now();
143  }
144 
145  void cancelCB(GoalHandle gh)
146  {
147 
148  if (active_goal_ == gh)
149  {
150  // Stops the controller
152  {
153 
154  }
155 
156  // Marks the current goal as canceled.
157  active_goal_.setCanceled();
158  has_active_goal_ = false;
159  }
160  }
161 
162 
163  pr2_gripper_sensor_msgs::PR2GripperEventDetectorDataConstPtr last_controller_state_;
164  void controllerStateCB(const pr2_gripper_sensor_msgs::PR2GripperEventDetectorDataConstPtr &msg)
165  {
166  last_controller_state_ = msg;
167  ros::Time now = ros::Time::now();
168 
169  if (!has_active_goal_)
170  return;
171 
172  pr2_gripper_sensor_msgs::PR2GripperEventDetectorFeedback feedback;
173  feedback.data = *msg;
174 
175  pr2_gripper_sensor_msgs::PR2GripperEventDetectorResult result;
176  result.data = *msg;
177 
178  // check if our success-condition is met, but only listen to feedback messages
179  // that were published some dT after the action was started
180  double dT = 0.1;
181  if(feedback.data.trigger_conditions_met && (feedback.data.stamp.toSec() > action_start_time.toSec()+dT ))
182  {
183  ROS_INFO("Event Detector Triggered: slip %d, acceleration %d",feedback.data.slip_event,feedback.data.acceleration_event);
184  active_goal_.setSucceeded(result);
185  has_active_goal_ = false;
186  }
187 
188  active_goal_.publishFeedback(feedback);
189  }
190 };
191 
192 
193 int main(int argc, char** argv)
194 {
195  ros::init(argc, argv, "find_contact_node");
196  ros::NodeHandle node;
197  Pr2GripperEventDetector jte(node);
198 
199  ros::spin();
200 
201  return 0;
202 }
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())
void stop()
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
boost::shared_ptr< const Goal > getGoal() const
actionlib::ActionServer< pr2_gripper_sensor_msgs::PR2GripperEventDetectorAction > GAS
#define ROS_WARN(...)
void setCanceled(const Result &result=Result(), const std::string &text=std::string(""))
pr2_gripper_sensor_msgs::PR2GripperEventDetectorDataConstPtr last_controller_state_
ROSCPP_DECL void spin(Spinner &spinner)
void setAccepted(const std::string &text=std::string(""))
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 watchdog(const ros::TimerEvent &e)
void setAborted(const Result &result=Result(), const std::string &text=std::string(""))
static Time now()
void controllerStateCB(const pr2_gripper_sensor_msgs::PR2GripperEventDetectorDataConstPtr &msg)
int main(int argc, char **argv)


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