dock_server.cpp
Go to the documentation of this file.
1 #include "dock_server.h"
2 
3 iqr::DockServer::DockServer(ros::NodeHandle &nh, ros::NodeHandle &private_nh, const std::string & server_name)
4  : action_name_(server_name),
5  nh_(nh),
6  private_nh_(private_nh),
7  perception_(private_nh),
8  move_base_client_("move_base", true),
9  actionlib::ActionServer<caster_app::DockAction>(private_nh, server_name,
10  boost::bind(&DockServer::GoalCallback, this, _1),
11  boost::bind(&DockServer::CancelCallback, this, _1),
12  false) {
13  cmd_pub_ = nh_.advertise<geometry_msgs::Twist>("cmd_vel", 1000);
14 
15  private_nh_.param<float>("dock/dock_speed", dock_speed_, 0.05);
16  private_nh_.param<float>("dock/dock_distance", dock_distance_, 1.0);
17  private_nh_.param<std::string>("map_frame", map_frame_, "map");
18  private_nh_.param<std::string>("odom_frame", odom_frame_, "odom");
19  private_nh_.param<std::string>("base_frame", base_frame_, "base_footprint");
20 
21  ROS_INFO_STREAM("param: dock_spped " << dock_speed_ << ", dock_distance " << dock_distance_);
22  ROS_INFO_STREAM("param: map_frame " << map_frame_ << ", odom_frame " << odom_frame_ << ", base_frame " << base_frame_);
23 
24  dock_ready_pose_.position.x = 20.8905636619;
25  dock_ready_pose_.position.y = 14.6787757997;
26  dock_ready_pose_.position.z = 0.0;
27  dock_ready_pose_.orientation.x = 0.0;
28  dock_ready_pose_.orientation.y = 0.0;
29  dock_ready_pose_.orientation.z = -0.0289632634423;
30  dock_ready_pose_.orientation.w = 0.999580476685;
31 
32  docked_ = false;
33 }
34 
36  ROS_INFO("waitting for move_base action server...");
37  // move_base_client_.waitForServer();
38  ROS_INFO("move_base action server connected.");
39  start();
40 }
41 
43  float delta = 0;
44 
45  tf::StampedTransform current_pose, last_pose;
46 
47  geometry_msgs::Twist cmd;
48  cmd.linear.x = dock_speed_ * -1.0;
49 
50  tf::StampedTransform robot_pose;
51  try{
53  ros::Time(0), last_pose);
54  }
55  catch (tf::TransformException ex){
56  ROS_ERROR("%s",ex.what());
57  ros::Duration(1.0).sleep();
58  }
59 
60  while(delta < dock_distance_) {
61  cmd_pub_.publish(cmd);
62 
63  try{
65  ros::Time(0), current_pose);
66  }
67  catch (tf::TransformException ex){
68  ROS_ERROR("%s",ex.what());
69  ros::Duration(1.0).sleep();
70  }
71 
72  delta = current_pose.getOrigin().distance(last_pose.getOrigin());
73  // ROS_INFO("delta: %f", delta);
74  feedback_.dock_feedback = boost::str(boost::format("Moving to Dock, %dm left") % (dock_distance_-delta));
76 
77  ros::Duration(0.05).sleep();
78  ros::spinOnce();
79  }
80 
81  feedback_.dock_feedback = "Stop on DockReady";
83  // ROS_INFO("stop robot");
84 
85  cmd.linear.x = 0;
86  cmd_pub_.publish(cmd);
87 
88  docked_ = false;
89  goal_.setSucceeded(caster_app::DockResult(), "Undocked");
90  ROS_INFO("Undocked");
91 }
92 
94  float delta = 0;
95 
96  tf::StampedTransform current_pose, last_pose;
97 
98  geometry_msgs::Twist cmd;
99  cmd.linear.x = dock_speed_;
100 
101  tf::StampedTransform robot_pose;
102  try{
104  ros::Time(0), last_pose);
105  }
106  catch (tf::TransformException ex){
107  ROS_ERROR("%s",ex.what());
108  ros::Duration(1.0).sleep();
109  }
110 
111  while(delta < dock_distance_) {
112  cmd_pub_.publish(cmd);
113 
114  try{
116  ros::Time(0), current_pose);
117  }
118  catch (tf::TransformException ex){
119  ROS_ERROR("%s",ex.what());
120  ros::Duration(1.0).sleep();
121  }
122 
123  delta = current_pose.getOrigin().distance(last_pose.getOrigin());
124  // ROS_INFO("delta: %f", delta);
125  feedback_.dock_feedback = boost::str(boost::format("Moving to Dock, %dm left") % (dock_distance_-delta));
127 
128  ros::Duration(0.05).sleep();
129  ros::spinOnce();
130  }
131 
132  feedback_.dock_feedback = "Stop on Dock";
134  // ROS_INFO("stop robot");
135 
136  cmd.linear.x = 0;
137  cmd_pub_.publish(cmd);
138 
139  docked_ = true;
140  goal_.setSucceeded(caster_app::DockResult(), "Docked");
141  ROS_INFO("Docked");
142 }
143 
145  move_base_msgs::MoveBaseGoal mb_goal;
146  mb_goal.target_pose.header.stamp = ros::Time::now();
147  mb_goal.target_pose.header.frame_id = map_frame_;
148  mb_goal.target_pose.pose = dock_ready_pose_;
149 
150  move_base_client_.sendGoal(mb_goal,
151  boost::bind(&iqr::DockServer::MovebaseDoneCallback, this, _1, _2),
153  boost::bind(&iqr::DockServer::MovebaseFeedbackCallback, this, _1));
154 }
155 
156 void iqr::DockServer::MovebaseDoneCallback(const actionlib::SimpleClientGoalState& state, const move_base_msgs::MoveBaseResultConstPtr& result) {
157  // ROS_INFO("move_base finished in state [%s]", state.toString().c_str());
158 
159  caster_app::DockFeedback feedback;
160  feedback.dock_feedback = "DockReady arrived";
161  goal_.publishFeedback(feedback);
162 
163  ros::Duration(2.0).sleep();
164  MoveToDock();
165 }
166 
167 void iqr::DockServer::MovebaseFeedbackCallback(const move_base_msgs::MoveBaseFeedbackConstPtr& feedback) {
168  // ROS_INFO_STREAM("Get move_base feedback" << ", x:" << feedback->base_position.pose.position.x << ", y:" << feedback->base_position.pose.position.y);
169 
170  tf::StampedTransform robot_pose;
171  try{
173  ros::Time(0), robot_pose);
174  }
175  catch (tf::TransformException ex){
176  ROS_ERROR("%s",ex.what());
177  ros::Duration(1.0).sleep();
178  }
179 
180  tf::Vector3 dock_ready_pos(dock_ready_pose_.position.x, dock_ready_pose_.position.y, dock_ready_pose_.position.z);
181 
182  caster_app::DockFeedback ca_feedback;
183  ca_feedback.dock_feedback = boost::str(boost::format("Moving to DockReady, %dm left") % dock_ready_pos.distance(robot_pose.getOrigin()));
184  goal_.publishFeedback(ca_feedback);
185 }
186 
188  // ROS_INFO("GoalCallback");
189  goal_ = gh;
190  caster_app::DockGoal goal = *goal_.getGoal();
191 
192  switch (goal.dock) {
193  case true:
194  if (docked_ == true) {
195  ROS_WARN("rejected, robot has already docked");
196  goal_.setRejected(caster_app::DockResult(), "already docked");
197  } else {
198  ROS_INFO("Docking");
199  goal_.setAccepted("Docking");
200  MoveToDockReady();
201  }
202  break;
203  case false:
204  if(docked_ == false) {
205  /* test */
206  // Start perception
207  // geometry_msgs::PoseStamped dock_pose;
208  // dock_pose.header.frame_id = "base_footprint";
209  // dock_pose.pose.position.x = 1.0;
210  // dock_pose.pose.orientation.z = 1.0;
211 
212  // perception_.start(dock_pose);
213 
214  // For timeout calculation
215  // initDockTimeout();
216 
217  // Get initial dock pose.
218  // geometry_msgs::PoseStamped dock_pose_base_link;
219  // while (!perception_.getPose(dock_pose_base_link, "base_footprint"))
220  // {
221  // // Wait for perception to get its first pose estimate.
222  // // if (!continueDocking(result))
223  // // {
224  // // ROS_DEBUG_NAMED("autodock_dock_callback",
225  // // "Docking failed: Initial dock not found.");
226  // // break;
227  // // }
228  // ros::spinOnce();
229  // }
230  /* test */
231  ROS_WARN("rejected, robot is not on charging");
232  goal_.setRejected(caster_app::DockResult(), "robot is not on charging");
233  } else {
234  ROS_INFO("Start undock");
235  goal_.setAccepted("Start undock");
236  UnDock();
237  }
238  break;
239  default:
240  ROS_WARN_NAMED(action_name_, "unknown dock data type, should be true or false");
241  break;
242  }
243 }
244 
246  ROS_INFO("%s cancel has been called", action_name_.c_str());
247 
249 }
const char *const *argv double delta
void publishFeedback(const Feedback &feedback)
void publish(const boost::shared_ptr< M > &message) const
#define ROS_WARN_NAMED(name,...)
void MovebaseFeedbackCallback(const move_base_msgs::MoveBaseFeedbackConstPtr &feedback)
caster_app::DockFeedback feedback_
Definition: dock_server.h:31
string cmd
std::string base_frame_
Definition: dock_server.h:46
bool sleep() const
void GoalCallback(GoalHandle gh)
void CancelCallback(GoalHandle gh)
void setRejected(const Result &result=Result(), const std::string &text=std::string(""))
boost::shared_ptr< const Goal > getGoal() const
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
#define ROS_WARN(...)
void setAccepted(const std::string &text=std::string(""))
boost::function< void()> SimpleActiveCallback
actionlib::SimpleActionClient< move_base_msgs::MoveBaseAction > move_base_client_
Definition: dock_server.h:37
ros::NodeHandle private_nh_
Definition: dock_server.h:26
std::string map_frame_
Definition: dock_server.h:44
void setSucceeded(const Result &result=Result(), const std::string &text=std::string(""))
GoalHandle goal_
Definition: dock_server.h:28
std::string odom_frame_
Definition: dock_server.h:45
std::string action_name_
Definition: dock_server.h:23
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void MoveToDockReady()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
float dock_distance_
Definition: dock_server.h:43
ros::NodeHandle nh_
Definition: dock_server.h:25
void sendGoal(const Goal &goal, SimpleDoneCallback done_cb=SimpleDoneCallback(), SimpleActiveCallback active_cb=SimpleActiveCallback(), SimpleFeedbackCallback feedback_cb=SimpleFeedbackCallback())
tf::TransformListener tf_listener_
Definition: dock_server.h:33
#define ROS_INFO_STREAM(args)
geometry_msgs::Pose dock_ready_pose_
Definition: dock_server.h:48
void MovebaseDoneCallback(const actionlib::SimpleClientGoalState &state, const move_base_msgs::MoveBaseResultConstPtr &result)
static Time now()
DockServer(ros::NodeHandle &nh, ros::NodeHandle &private_nh, const std::string &server_name)
Definition: dock_server.cpp:3
ros::Publisher cmd_pub_
Definition: dock_server.h:35
ROSCPP_DECL void spinOnce()
#define ROS_ERROR(...)


caster_app
Author(s): Ye Tian
autogenerated on Wed Dec 18 2019 03:34:44