CalculateGoalState.cpp
Go to the documentation of this file.
2 
3 namespace rsm {
4 
6 
7 }
8 
10 }
11 
13  //initialize services, publisher and subscriber
14  ros::NodeHandle nh("rsm");
15  _frontiers_sub = nh.subscribe<geometry_msgs::PoseArray>("explorationGoals",
17  _failed_goals_sub = nh.subscribe<geometry_msgs::PoseArray>("failedGoals", 10,
20  nh.serviceClient<rsm_msgs::SetNavigationGoal>("setNavigationGoal");
21  _get_robot_pose_service = nh.serviceClient<rsm_msgs::GetRobotPose>(
22  "getRobotPose");
25  //initialize variables
26  _name = "E: Calculate Goal";
27  _frontiers_received = false;
28  _failed_goals_received = false;
29 }
30 
32  //Request list of failed goals from Service Provider
33 }
34 
37  //Calculate frontier center closest to the robot
38  rsm_msgs::GetRobotPose srv;
39  if (_get_robot_pose_service.call(srv)) {
40  geometry_msgs::Pose current_pose = srv.response.pose;
41  double min_distance = std::numeric_limits<double>::infinity();
42  for (auto& pt : _frontier_points.poses) {
43  if (differentFromFailedGoals(pt.position)) {
44  double distance = pow(
45  (current_pose.position.x - pt.position.x), 2)
46  + pow((current_pose.position.y - pt.position.y), 2);
47  if (distance < min_distance) {
48  min_distance = distance;
49  _goal.position.x = pt.position.x;
50  _goal.position.y = pt.position.y;
51  }
52  }
53  }
54  if (min_distance == std::numeric_limits<double>::infinity()) {
55  ROS_ERROR(
56  "Exploration stopped because there are no more reachable goals");
58  } else {
59  double yaw = atan2(_goal.position.y - current_pose.position.y,
60  _goal.position.x - current_pose.position.x);
61  _goal.orientation = tf::createQuaternionMsgFromYaw(yaw);
62  _frontiers_received = false;
63  if (!_interrupt_occured) {
66  }
67  }
68 
69  } else {
70  ROS_ERROR("Failed to call Get Robot Pose service");
72  }
73  }
74 }
75 
77  rsm_msgs::SetNavigationGoal srv;
78  srv.request.goal = _goal;
79  srv.request.navigationMode = EXPLORATION;
81  } else {
82  ROS_ERROR("Failed to call Set Navigation Goal service");
83  }
84 }
85 
87  std::string &message) {
88  success = true;
89  message = "Exploration running";
90 }
91 
93  std::string &message) {
94  success = true;
95  message = "Exploration stopped";
97 }
98 
100  std::string &message) {
101  success = false;
102  message = "Exploration running";
103 }
104 
106  std::string &message) {
107  success = false;
108  message = "Exploration running";
109 }
110 
111 void CalculateGoalState::onInterrupt(int interrupt) {
112  switch (interrupt) {
115  boost::make_shared<EmergencyStopState>());
116  _interrupt_occured = true;
117  break;
120  boost::make_shared<TeleoperationState>());
121  _interrupt_occured = true;
122  break;
126  _interrupt_occured = true;
127  break;
128  }
129 }
130 
132  const geometry_msgs::PoseArray::ConstPtr& frontiers) {
133  _frontier_points = *frontiers;
134  _frontiers_received = true;
135 }
136 
138  const geometry_msgs::PoseArray::ConstPtr& failed_goals) {
139  _failed_goals = *failed_goals;
140  _failed_goals_received = true;
141 }
142 
143 bool CalculateGoalState::differentFromFailedGoals(geometry_msgs::Point point) {
144  double tolerance = 0.05;
145  for (auto iterator : _failed_goals.poses) {
146  double x_dif = abs(point.x - iterator.position.x);
147  double y_dif = abs(point.y - iterator.position.y);
148  if (x_dif <= tolerance && y_dif <= tolerance) {
149  return false;
150  }
151  }
152  return true;
153 }
154 
156  ROS_ERROR("Exploration stopped because no goal was selected in time");
158 }
159 
161  if (!_interrupt_occured) {
163  boost::make_shared<IdleState>());
164  }
165 }
166 
167 }
168 
void onExplorationStart(bool &success, std::string &message)
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
boost::shared_ptr< rsm::BaseState > getPluginState(int plugin_type, std::string routine="")
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
#define NAVIGATION_STATE
ros::Subscriber _failed_goals_sub
void onInterrupt(int interrupt)
Called when an operation mode interrupt was received.
bool call(MReq &req, MRes &res)
TFSIMD_FORCE_INLINE tfScalar distance(const Vector3 &v) const
void transitionToVolatileState(boost::shared_ptr< rsm::BaseState > next_state)
bool _interrupt_occured
#define TELEOPERATION_INTERRUPT
void onExplorationStop(bool &success, std::string &message)
Called when exploration was stopped manually.
geometry_msgs::PoseArray _failed_goals
bool differentFromFailedGoals(geometry_msgs::Point point)
Checks if a point is different from a previously failed goals including a small tolerance.
void onExit()
Called once when left.
void onEntry()
Called once when activated.
ros::ServiceClient _get_robot_pose_service
std::string _name
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
StateInterface * _stateinterface
ros::Subscriber _frontiers_sub
#define EMERGENCY_STOP_INTERRUPT
#define EXPLORATION
CalculateGoalState()
Constructor.
geometry_msgs::Pose _goal
void onSetup()
Called once when registered at StateInterface.
ros::ServiceClient _set_navigation_goal_service
void timerCallback(const ros::TimerEvent &event)
Callback for when no goal was chosen in time and calculation likely failed.
static geometry_msgs::Quaternion createQuaternionMsgFromYaw(double yaw)
geometry_msgs::PoseArray _frontier_points
void onActive()
Process method (step-wise, never block this method)
void onWaypointFollowingStart(bool &success, std::string &message)
void onWaypointFollowingStop(bool &success, std::string &message)
State for choosing a goal from all provided frontiers and calling Navigation when successful...
#define SIMPLE_GOAL_INTERRUPT
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
#define ROS_ERROR(...)
void frontiersCallback(const geometry_msgs::PoseArray::ConstPtr &frontiers)
Called when new frontiers are received.
void failedGoalsCallback(const geometry_msgs::PoseArray::ConstPtr &failed_goals)
Called when new failed goals are received.


rsm_additions
Author(s): Marco Steinbrink
autogenerated on Tue Mar 16 2021 02:44:35