WaypointFollowingState.cpp
Go to the documentation of this file.
2 
3 namespace rsm {
4 
6 }
7 
9 }
10 
12  //initialize services, publisher and subscriber
13  ros::NodeHandle nh("rsm");
14  _get_waypoints_service = nh.serviceClient<rsm_msgs::GetWaypoints>(
15  "getWaypoints");
17  rsm_msgs::SetWaypointFollowingMode>(
18  "setWaypointFollowingMode");
19  _reset_waypoints_service = nh.serviceClient<std_srvs::Trigger>(
20  "resetWaypoints");
22  rsm_msgs::WaypointVisited>("waypointVisited");
24  rsm_msgs::SetNavigationGoal>("setNavigationGoal");
25  //initialize variables
26  _name = "W: Waypoint Following";
28 }
29 
31  getWaypoints();
32 }
33 
35  int next_reachable_unvisited_waypoint_position = -1;
36  if (_waypoint_array.reverse) {
37  for (int i = _waypoint_array.waypoints_size - 1; i >= 0; i--) {
38  if (!_waypoint_array.waypoints[i].visited
39  && !_waypoint_array.waypoints[i].unreachable) {
40  next_reachable_unvisited_waypoint_position = i;
41  break;
42  }
43  }
44  } else {
45  for (int i = 0; i < _waypoint_array.waypoints_size; i++) {
46  if (!_waypoint_array.waypoints[i].visited
47  && !_waypoint_array.waypoints[i].unreachable) {
48  next_reachable_unvisited_waypoint_position = i;
49  break;
50  }
51  }
52  }
53  if (next_reachable_unvisited_waypoint_position != -1) {
54  _next_waypoint_position = next_reachable_unvisited_waypoint_position;
55  if (!_interrupt_occured) {
59  }
60  } else {
61  switch (_waypoint_array.mode) {
62  case 0: //SINGLE
63  {
65  break;
66  }
67  case 1: //ROUNDTRIP
68  {
70  getWaypoints();
71  break;
72  }
73  case 2: //PATROL
74  {
75  rsm_msgs::SetWaypointFollowingMode srv2;
76  srv2.request.mode = _waypoint_array.mode;
77  srv2.request.reverse = !_waypoint_array.reverse;
79  ROS_ERROR("Failed to call Set Waypoint Following Mode service");
81  }
84  getWaypoints();
85  break;
86  }
87  default:
88  break;
89  }
90  }
91 }
92 
94  //Set Navigation goal
95  if (_next_waypoint_position >= 0) {
96  rsm_msgs::SetNavigationGoal srv;
97  srv.request.goal =
99  srv.request.navigationMode = WAYPOINT_FOLLOWING;
100  srv.request.waypointPosition = _next_waypoint_position;
101  srv.request.routine =
102  _waypoint_array.waypoints[_next_waypoint_position].routine;
104  } else {
105  ROS_ERROR("Failed to call Set Navigation Goal service");
106  }
107  }
108 }
109 
111  std::string &message) {
112  success = false;
113  message = "Waypoint following running";
114 }
115 
117  std::string &message) {
118  success = false;
119  message = "Waypoint following running";
120 }
121 
123  std::string &message) {
124  success = false;
125  message = "Waypoint following running";
126 }
127 
129  std::string &message) {
130  success = true;
131  message = "Waypoint following stopped";
133 }
134 
136  switch (interrupt) {
139  boost::make_shared<EmergencyStopState>());
140  _interrupt_occured = true;
141  break;
144  boost::make_shared<TeleoperationState>());
145  _interrupt_occured = true;
146  break;
150  _interrupt_occured = true;
151  break;
152  }
153 }
154 
156  if (!_interrupt_occured) {
158  boost::make_shared<IdleState>());
159  }
160 }
161 
163  rsm_msgs::GetWaypoints srv;
164  if (_get_waypoints_service.call(srv)) {
165  _waypoint_array = srv.response.waypointArray;
166  if (!_waypoint_array.waypoints_size) {
167  ROS_ERROR(
168  "Waypoint Following stopped because no waypoints are available");
170  }
171  } else {
172  ROS_ERROR("Failed to call Get Waypoints service");
174  }
175 }
176 
178  //Set first/last waypoint to visited, so the routine won't be called twice
179  rsm_msgs::WaypointVisited srv;
180  if (_waypoint_array.reverse) {
181  srv.request.position = 0;
182  } else {
183  srv.request.position = _waypoint_array.waypoints_size - 1;
184  }
185  if (!_waypoint_visited_service.call(srv)) {
186  ROS_ERROR("Failed to call Waypoint Visited service");
187  }
188 }
189 
191  std_srvs::Trigger srv;
192  if (!_reset_waypoints_service.call(srv)) {
193  ROS_ERROR("Failed to call Reset Waypoints service");
195  }
196 }
197 
198 }
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
ros::ServiceClient _set_waypoint_following_mode_service
boost::shared_ptr< rsm::BaseState > getPluginState(int plugin_type, std::string routine="")
Returns a plugin-state corresponding to the given type.
void onInterrupt(int interrupt)
Called when an operation mode interrupt was received.
void onWaypointFollowingStart(bool &success, std::string &message)
#define NAVIGATION_STATE
void onExplorationStop(bool &success, std::string &message)
bool call(MReq &req, MRes &res)
ros::ServiceClient _waypoint_visited_service
rsm_msgs::WaypointArray _waypoint_array
void transitionToVolatileState(boost::shared_ptr< rsm::BaseState > next_state)
bool _interrupt_occured
Shows if an interupt occured.
Definition: BaseState.h:129
#define WAYPOINT_FOLLOWING
Definition: BaseState.h:17
#define TELEOPERATION_INTERRUPT
Definition: BaseState.h:12
void onExplorationStart(bool &success, std::string &message)
Definition: BaseState.h:8
std::string _name
Name of the state.
Definition: BaseState.h:133
StateInterface * _stateinterface
Pointer to State Interface handling all state transitions.
Definition: BaseState.h:125
#define EMERGENCY_STOP_INTERRUPT
Definition: BaseState.h:11
ros::ServiceClient _reset_waypoints_service
ros::ServiceClient _get_waypoints_service
void onWaypointFollowingStop(bool &success, std::string &message)
ros::ServiceClient _set_navigation_goal_service
#define SIMPLE_GOAL_INTERRUPT
Definition: BaseState.h:13
#define ROS_ERROR(...)


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