pr2_gripper_forceServo_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_slipServo_action.cpp - action server for the
35 // pr2_gripper_slipServo action command
36 
37 #include "ros/ros.h"
38 
40 #include <pr2_gripper_sensor_msgs/PR2GripperForceServoAction.h>
41 #include <pr2_gripper_sensor_msgs/PR2GripperForceServoData.h>
42 #include <pr2_gripper_sensor_msgs/PR2GripperForceServoCommand.h>
43 #include <std_srvs/Empty.h>
44 
46 {
47 private:
50 public:
52  node_(n),
53  action_server_(node_, "force_servo",
54  boost::bind(&Pr2GripperForceServo::goalCB, this, _1),
55  boost::bind(&Pr2GripperForceServo::cancelCB, this, _1)),
56  has_active_goal_(false)
57  {
58  ros::NodeHandle pn("~");
59 
60  // we will listen for messages on "state"
62  node_.advertise<pr2_gripper_sensor_msgs::PR2GripperForceServoCommand>("force_servo", 1);
64  node_.subscribe("force_servo_state", 1, &Pr2GripperForceServo::controllerStateCB, this);
65 
67 
68  }
69 
71  {
75  }
76 
77 private:
78 
84 
86  bool firstDrop;
87  GoalHandle active_goal_;
89 
95 
96 
97 
98  void watchdog(const ros::TimerEvent &e)
99  {
100  ros::Time now = ros::Time::now();
101 
102  // Aborts the active goal if the controller does not appear to be active.
103  if (has_active_goal_)
104  {
105  bool should_abort = false;
107  {
108  should_abort = true;
109  ROS_WARN("Aborting goal because we have never heard a controller state message.");
110  }
111  else if ((now - last_controller_state_->stamp) > ros::Duration(5.0))
112  {
113  should_abort = true;
114  ROS_WARN("Aborting goal because we haven't heard from the controller in %.3lf seconds",
115  (now - last_controller_state_->stamp).toSec());
116  }
117 
118  if (should_abort)
119  {
120  // Marks the current goal as aborted.
121  active_goal_.setAborted();
122  has_active_goal_ = false;
123  }
124  }
125  }
126 
127  void goalCB(GoalHandle gh)
128  {
129  // Cancels the currently active goal.
130  if (has_active_goal_)
131  {
132  // Marks the current goal as canceled.
133  active_goal_.setCanceled();
134  has_active_goal_ = false;
135  }
136 
137  gh.setAccepted();
138  active_goal_ = gh;
139  has_active_goal_ = true;
140  firstDrop = true; // set our first drop flag
141  goal_received_ = ros::Time::now();
142 
143  // Sends the command along to the controller.
144  pub_controller_command_.publish(active_goal_.getGoal()->command);
145 
146  last_movement_time_ = ros::Time::now();
147  action_start_time = ros::Time::now();
148  }
149 
150  void cancelCB(GoalHandle gh)
151  {
152 
153  if (active_goal_ == gh)
154  {
155  // stop the real-time motor control
156  std_srvs::Empty::Request req;
157  std_srvs::Empty::Response resp;
158  if(ros::service::exists("stop_motor_output",true))
159  {
160  ROS_INFO("Stopping Motor Output");
161  ros::service::call("stop_motor_output",req,resp);
162  }
163 
164  // Marks the current goal as canceled.
165  active_goal_.setCanceled();
166  has_active_goal_ = false;
167  }
168  }
169 
170 
171  pr2_gripper_sensor_msgs::PR2GripperForceServoDataConstPtr last_controller_state_;
172  void controllerStateCB(const pr2_gripper_sensor_msgs::PR2GripperForceServoDataConstPtr &msg)
173  {
174  last_controller_state_ = msg;
175  ros::Time now = ros::Time::now();
176 
177  if (!has_active_goal_)
178  return;
179 
180  pr2_gripper_sensor_msgs::PR2GripperForceServoGoal goal;
181  goal.command = active_goal_.getGoal()->command;
182 
183  pr2_gripper_sensor_msgs::PR2GripperForceServoFeedback feedback;
184  feedback.data = *msg;
185 
186  pr2_gripper_sensor_msgs::PR2GripperForceServoResult result;
187  result.data = *msg;
188 
189 
190  // do not check until some dT has expired from message start
191  double dT = 0.1;
192  if(feedback.data.stamp.toSec() < action_start_time.toSec()+dT ){}
193 
194  // if we are actually in a force_servo control state
195  else if(feedback.data.rtstate.realtime_controller_state == 4)
196  {
197  // if the force servo achieved its goal
198  if ( feedback.data.force_achieved )
199  {
200  active_goal_.setSucceeded(result);
201  has_active_goal_ = false;
202  }
203 
204  }
205 
206  else
207  has_active_goal_ = false;
208 
209  active_goal_.publishFeedback(feedback);
210  }
211 };
212 
213 
214 int main(int argc, char** argv)
215 {
216  ros::init(argc, argv, "force_servo_node");
217  ros::NodeHandle node;
218  Pr2GripperForceServo jte(node);
219 
220  ros::spin();
221 
222  return 0;
223 }
void publishFeedback(const Feedback &feedback)
void publish(const boost::shared_ptr< M > &message) const
void controllerStateCB(const pr2_gripper_sensor_msgs::PR2GripperForceServoDataConstPtr &msg)
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)
int main(int argc, char **argv)
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
#define ROS_WARN(...)
void watchdog(const ros::TimerEvent &e)
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(""))
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(""))
Pr2GripperForceServo(ros::NodeHandle &n)
actionlib::ActionServer< pr2_gripper_sensor_msgs::PR2GripperForceServoAction > GAS
pr2_gripper_sensor_msgs::PR2GripperForceServoDataConstPtr last_controller_state_
static Time now()
ROSCPP_DECL bool exists(const std::string &service_name, bool print_failure_reason)
resp


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