ServiceProvider.cpp
Go to the documentation of this file.
2 
3 namespace rsm {
4 
6  ros::NodeHandle private_nh("~");
7  private_nh.param<std::string>("robot_frame", _robot_frame, "/base_link");
8  private_nh.param<std::vector<std::string>>("waypoint_routines",
9  _waypoint_routines, std::vector<std::string>());
10 
11  ros::NodeHandle nh("rsm");
12  _add_waypoint_service = nh.advertiseService("addWaypoint",
14  _get_waypoints_service = nh.advertiseService("getWaypoints",
16  _move_waypoint_service = nh.advertiseService("moveWaypoint",
18  _remove_waypoint_service = nh.advertiseService("removeWaypoint",
20  _waypoint_visited_service = nh.advertiseService("waypointVisited",
22  _waypoint_unreachable_service = nh.advertiseService("waypointUnreachable",
24  _reset_waypoints_service = nh.advertiseService("resetWaypoints",
27  "setWaypointFollowingMode",
29  _set_waypoint_routine_service = nh.advertiseService("setWaypointRoutine",
31  _get_waypoint_routines_service = nh.advertiseService("getWaypointRoutines",
33  _waypoints_publisher = nh.advertise<rsm_msgs::WaypointArray>("waypoints",
34  10);
35 
36  _set_navigation_goal_service = nh.advertiseService("setNavigationGoal",
38  _get_navigation_goal_service = nh.advertiseService("getNavigationGoal",
41  "navigationGoalCompleted",
43 
44  _get_robot_pose_service = nh.advertiseService("getRobotPose",
46 
47  _set_exploration_mode_service = nh.advertiseService("setExplorationMode",
49  _get_exploration_mode_service = nh.advertiseService("getExplorationMode",
51  _exploration_mode_publisher = nh.advertise<std_msgs::Bool>(
52  "explorationMode", 1);
53  _exploration_goal_publisher = nh.advertise<rsm_msgs::GoalStatus>(
54  "explorationGoalStatus", 1);
55 
56  _set_reverse_mode_service = nh.advertiseService("setReverseMode",
58  _get_reverse_mode_service = nh.advertiseService("getReverseMode",
60  _set_navigation_to_reverse_client = nh.serviceClient<std_srvs::SetBool>(
61  "setNavigationToReverse");
62  _reverse_mode_publisher = nh.advertise<std_msgs::Bool>("reverseMode", 10);
63 
64  _waypoint_array.header.seq = 0;
65  _waypoint_array.header.stamp = ros::Time::now();
66  _waypoint_array.header.frame_id = "map";
67  _waypoint_array.mode = 0;
68  _waypoint_array.reverse = false;
69  _waypoint_array.waypoints_size = 0;
70 
71  _navigation_mode = -1;
72  _waypoint_position = -1;
73 
75 
76  _reverse_mode_active = false;
77 }
78 
80 
81 }
82 
88 }
89 
90 bool ServiceProvider::addWaypoint(rsm_msgs::AddWaypoint::Request &req,
91  rsm_msgs::AddWaypoint::Response &res) {
92  if (req.position >= 0 && req.position < _waypoint_array.waypoints_size) {
93  _waypoint_array.waypoints.insert(
94  _waypoint_array.waypoints.begin() + req.position, req.waypoint);
95  res.message = "Added waypoint successfully";
96  } else {
97  _waypoint_array.waypoints.push_back(req.waypoint);
98  res.message = "Added waypoint successfully at the end";
99  }
100  _waypoint_array.waypoints_size++;
101  res.success = 1;
102  return true;
103 }
104 
105 bool ServiceProvider::getWaypoints(rsm_msgs::GetWaypoints::Request &req,
106  rsm_msgs::GetWaypoints::Response &res) {
107  res.waypointArray = _waypoint_array;
108  return true;
109 }
110 
111 bool ServiceProvider::moveWaypoint(rsm_msgs::MoveWaypoint::Request &req,
112  rsm_msgs::MoveWaypoint::Response &res) {
113  if (req.position >= 0 && req.position < _waypoint_array.waypoints_size) {
114  _waypoint_array.waypoints[req.position].pose = req.pose;
115  res.success = 1;
116  res.message = "Moved waypoint successfully";
117  } else {
118  res.success = 0;
119  res.message = "Position out of waypoint array range";
120  }
121  return true;
122 }
123 
124 bool ServiceProvider::removeWaypoint(rsm_msgs::RemoveWaypoint::Request &req,
125  rsm_msgs::RemoveWaypoint::Response &res) {
126  if (req.position >= 0 && req.position < _waypoint_array.waypoints_size) {
127  _waypoint_array.waypoints.erase(
128  _waypoint_array.waypoints.begin() + req.position);
129  _waypoint_array.waypoints_size--;
130  res.success = 1;
131  res.message = "Removed waypoint successfully";
132  } else {
133  res.success = 0;
134  res.message = "Position out of waypoint array range";
135  }
136  return true;
137 }
138 
139 bool ServiceProvider::waypointVisited(rsm_msgs::WaypointVisited::Request &req,
140  rsm_msgs::WaypointVisited::Response &res) {
141  if (req.position >= 0 && req.position < _waypoint_array.waypoints_size) {
142  _waypoint_array.waypoints[req.position].visited = true;
143  res.success = 1;
144  res.message = "Waypoint visited";
145  } else {
146  res.success = 0;
147  res.message = "Position out of waypoint array range";
148  }
149  return true;
150 }
151 
153  rsm_msgs::WaypointUnreachable::Request &req,
154  rsm_msgs::WaypointUnreachable::Response &res) {
155  if (req.position >= 0 && req.position < _waypoint_array.waypoints_size) {
156  _waypoint_array.waypoints[req.position].unreachable = true;
157  res.success = 1;
158  res.message = "Waypoint unreachable";
159  } else {
160  res.success = 0;
161  res.message = "Position out of waypoint array range";
162  }
163  return true;
164 }
165 
166 bool ServiceProvider::resetWaypoints(std_srvs::Trigger::Request &req,
167  std_srvs::Trigger::Response &res) {
168  for (auto& it : _waypoint_array.waypoints) {
169  it.visited = false;
170  it.unreachable = false;
171  }
172  res.success = 1;
173  res.message = "Waypoints reset";
174  return true;
175 }
176 
178  rsm_msgs::SetWaypointFollowingMode::Request &req,
179  rsm_msgs::SetWaypointFollowingMode::Response &res) {
180  _waypoint_array.mode = req.mode;
181  _waypoint_array.reverse = req.reverse;
182  res.success = 1;
183  res.message = "Waypoint Following mode changed";
184  return true;
185 }
186 
188  rsm_msgs::SetWaypointRoutine::Request &req,
189  rsm_msgs::SetWaypointRoutine::Response &res) {
190  if (req.position >= 0 && req.position < _waypoint_array.waypoints_size) {
191  _waypoint_array.waypoints[req.position].routine = req.routine;
192  res.success = 1;
193  res.message = "Waypoint routine set";
194  } else {
195  res.success = 0;
196  res.message = "Position out of waypoint array range";
197  }
198  return true;
199 }
200 
202  rsm_msgs::GetWaypointRoutines::Request &req,
203  rsm_msgs::GetWaypointRoutines::Response &res) {
204  res.waypointRoutines = _waypoint_routines;
205  return true;
206 }
207 
210 }
211 
213  rsm_msgs::SetNavigationGoal::Request &req,
214  rsm_msgs::SetNavigationGoal::Response &res) {
215  _navigation_goal = req.goal;
216  _navigation_mode = req.navigationMode;
217  _waypoint_position = req.waypointPosition;
218  _routine = req.routine;
219 
221  _exploration_goal_completed_msg.goal_status = rsm_msgs::GoalStatus::ACTIVE;
222  res.success = 1;
223  res.message = "Navigation goal set";
224  return true;
225 }
226 
228  rsm_msgs::GetNavigationGoal::Request &req,
229  rsm_msgs::GetNavigationGoal::Response &res) {
230  res.goal = _navigation_goal;
231  res.navigationMode = _navigation_mode;
232  res.waypointPosition = _waypoint_position;
233  res.routine = _routine;
234  return true;
235 }
236 
238  rsm_msgs::GoalCompleted::Request &req,
239  rsm_msgs::GoalCompleted::Response &res) {
240  switch (_navigation_mode) {
241  case 0: { //Exploration
242  _exploration_goal_completed_msg.goal_status = req.status.goal_status;
243  res.message = "Exploration goal completed";
244  break;
245  }
246  case 1: { //Waypoint following
247  if (req.status.goal_status == rsm_msgs::GoalStatus::REACHED) {
248  if (_waypoint_position >= 0
249  && _waypoint_position < _waypoint_array.waypoints_size) {
250  _waypoint_array.waypoints[_waypoint_position].visited = true;
251  }
252  } else if (req.status.goal_status == rsm_msgs::GoalStatus::FAILED) {
253  if (_waypoint_position >= 0
254  && _waypoint_position < _waypoint_array.waypoints_size) {
255  _waypoint_array.waypoints[_waypoint_position].unreachable =
256  true;
257  }
258  }
259  res.message = "Waypoint completed";
260  break;
261  }
262  default: {
263  res.message = "Navigation goal completed";
264  break;
265  }
266  }
267  res.success = 1;
268  return true;
269 }
270 
271 bool ServiceProvider::getRobotPose(rsm_msgs::GetRobotPose::Request &req,
272  rsm_msgs::GetRobotPose::Response &res) {
274  _transform);
275  geometry_msgs::Pose pose;
276  pose.position.x = _transform.getOrigin().x();
277  pose.position.y = _transform.getOrigin().y();
278  pose.position.z = _transform.getOrigin().z();
279  pose.orientation.x = _transform.getRotation().x();
280  pose.orientation.y = _transform.getRotation().y();
281  pose.orientation.z = _transform.getRotation().z();
282  pose.orientation.w = _transform.getRotation().w();
283  res.pose = pose;
284  return true;
285 }
286 
287 bool ServiceProvider::getExplorationMode(std_srvs::Trigger::Request &req,
288  std_srvs::Trigger::Response &res) {
289  res.success = _exploration_mode;
290  res.message = "Exploration mode returned";
291  return true;
292 }
293 
294 bool ServiceProvider::setExplorationMode(std_srvs::SetBool::Request &req,
295  std_srvs::SetBool::Response &res) {
296  _exploration_mode = req.data;
297  res.success = 1;
298  res.message = "Exploration mode set";
299  return true;
300 }
301 
303  std_msgs::Bool msg;
304  msg.data = _exploration_mode;
306 }
307 
310 }
311 
312 bool ServiceProvider::setReverseMode(std_srvs::SetBool::Request &req,
313  std_srvs::SetBool::Response &res) {
314  if (req.data != _reverse_mode_active) {
315  _reverse_mode_active = req.data;
316  std_srvs::SetBool srv;
317  srv.request.data = _reverse_mode_active;
319  if (srv.response.success) {
320  res.success = 1;
321  res.message = "Mode set";
322  } else {
323  res.success = 0;
324  res.message = "Mode not set";
325  }
326  } else {
327  ROS_ERROR("Unable to call Set Navigation To Reverse service");
328  res.success = 0;
329  res.message = "Unable to set reverse mode";
330  }
331  } else {
332  res.success = 0;
333  res.message = "Already in requested mode";
334  }
335  return true;
336 }
337 
338 bool ServiceProvider::getReverseMode(std_srvs::Trigger::Request &req,
339  std_srvs::Trigger::Response &res) {
340  if (_reverse_mode_active) {
341  res.success = true;
342  res.message = "Reverse mode active";
343  } else {
344  res.success = false;
345  res.message = "Forward mode active";
346  }
347  return true;
348 }
349 
351  std_msgs::Bool msg;
352  msg.data = _reverse_mode_active;
354 }
355 
356 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
ros::ServiceServer _waypoint_unreachable_service
ros::ServiceServer _get_reverse_mode_service
bool setWaypointRoutine(rsm_msgs::SetWaypointRoutine::Request &req, rsm_msgs::SetWaypointRoutine::Response &res)
std::vector< std::string > _waypoint_routines
ros::ServiceServer _set_exploration_mode_service
bool resetWaypoints(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
tf::TransformListener _transform_listener
ros::ServiceServer _get_robot_pose_service
ros::ServiceServer _get_navigation_goal_service
void publish(const boost::shared_ptr< M > &message) const
bool NavigationGoalCompleted(rsm_msgs::GoalCompleted::Request &req, rsm_msgs::GoalCompleted::Response &res)
bool getExplorationMode(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
geometry_msgs::Pose _navigation_goal
tf::StampedTransform _transform
ROS transform from map to robot frame.
bool moveWaypoint(rsm_msgs::MoveWaypoint::Request &req, rsm_msgs::MoveWaypoint::Response &res)
bool call(MReq &req, MRes &res)
ros::ServiceClient _set_navigation_to_reverse_client
bool getWaypointRoutines(rsm_msgs::GetWaypointRoutines::Request &req, rsm_msgs::GetWaypointRoutines::Response &res)
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
bool getReverseMode(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
ros::ServiceServer _set_navigation_goal_service
ros::ServiceServer _navigation_goal_completed_service
bool addWaypoint(rsm_msgs::AddWaypoint::Request &req, rsm_msgs::AddWaypoint::Response &res)
ros::Publisher _exploration_goal_publisher
ros::ServiceServer _get_exploration_mode_service
ros::ServiceServer _get_waypoints_service
bool getNavigationGoal(rsm_msgs::GetNavigationGoal::Request &req, rsm_msgs::GetNavigationGoal::Response &res)
TFSIMD_FORCE_INLINE const tfScalar & x() const
bool getRobotPose(rsm_msgs::GetRobotPose::Request &req, rsm_msgs::GetRobotPose::Response &res)
ros::ServiceServer _set_waypoint_following_mode_service
ros::Publisher _reverse_mode_publisher
ros::Publisher _exploration_mode_publisher
TFSIMD_FORCE_INLINE const tfScalar & z() const
Definition: BaseState.h:8
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ros::ServiceServer _set_waypoint_routine_service
ros::ServiceServer _get_waypoint_routines_service
bool setNavigationGoal(rsm_msgs::SetNavigationGoal::Request &req, rsm_msgs::SetNavigationGoal::Response &res)
bool getWaypoints(rsm_msgs::GetWaypoints::Request &req, rsm_msgs::GetWaypoints::Response &res)
TFSIMD_FORCE_INLINE const tfScalar & y() const
ros::ServiceServer _reset_waypoints_service
std::string _robot_frame
Robot frame id.
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
rsm_msgs::WaypointArray _waypoint_array
TFSIMD_FORCE_INLINE Vector3 & getOrigin()
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
bool setWaypointFollowingMode(rsm_msgs::SetWaypointFollowingMode::Request &req, rsm_msgs::SetWaypointFollowingMode::Response &res)
bool setExplorationMode(std_srvs::SetBool::Request &req, std_srvs::SetBool::Response &res)
ros::ServiceServer _waypoint_visited_service
Quaternion getRotation() const
bool setReverseMode(std_srvs::SetBool::Request &req, std_srvs::SetBool::Response &res)
ros::ServiceServer _remove_waypoint_service
static Time now()
ros::ServiceServer _add_waypoint_service
bool removeWaypoint(rsm_msgs::RemoveWaypoint::Request &req, rsm_msgs::RemoveWaypoint::Response &res)
ros::ServiceServer _move_waypoint_service
ros::Publisher _waypoints_publisher
#define ROS_ERROR(...)
ros::ServiceServer _set_reverse_mode_service
rsm_msgs::GoalStatus _exploration_goal_completed_msg
bool waypointUnreachable(rsm_msgs::WaypointUnreachable::Request &req, rsm_msgs::WaypointUnreachable::Response &res)
bool waypointVisited(rsm_msgs::WaypointVisited::Request &req, rsm_msgs::WaypointVisited::Response &res)


rsm_core
Author(s): Marco Steinbrink
autogenerated on Tue Mar 16 2021 02:44:31