StateInterface.cpp
Go to the documentation of this file.
2 
3 namespace rsm {
4 
6  _plugin_loader("rsm_core", "rsm::BaseState") {
7  ros::NodeHandle private_nh("~");
8  private_nh.param<std::string>("calculate_goal_plugin",
9  _calculate_goal_plugin, "rsm::CalculateGoalState");
10  private_nh.param<std::string>("navigation_plugin", _navigation_plugin,
11  "rsm::NavigationState");
12  private_nh.param<std::string>("mapping_plugin", _mapping_plugin,
13  "rsm::MappingDummyState");
14 
15  ros::NodeHandle nh("rsm");
16  _operation_mode_sub = nh.subscribe<rsm_msgs::OperationMode>(
17  "operationMode", 1, &StateInterface::operationModeCallback, this);
18  _simple_goal_sub = nh.subscribe<geometry_msgs::PoseStamped>("simpleGoal", 1,
20  _start_stop_exploration_service = nh.advertiseService(
21  "startStopExploration",
23  _start_stop_waypoint_following_service = nh.advertiseService(
24  "startStopWaypointFollowing",
26  _stop_2d_nav_goal_service = nh.advertiseService("stop2DNavGoal",
28  _state_info_publisher = nh.advertise<std_msgs::String>("stateInfo", 10);
29  _state_info_service = nh.advertiseService("stateInfo",
31  _set_navigation_goal_client = nh.serviceClient<
32  rsm_msgs::SetNavigationGoal>("setNavigationGoal");
33 
34  _current_state = NULL;
35  _next_state = NULL;
36  _on_interrupt = false;
37 }
38 
40  if (_current_state) {
41  _current_state->onExit();
42  }
43 }
44 
46  int plugin_type, std::string routine) {
47  try {
48  switch (plugin_type) {
49  case 1: {
50  return _plugin_loader.createInstance(_calculate_goal_plugin);
51  break;
52  }
53  case 2: {
54  return _plugin_loader.createInstance(_navigation_plugin);
55  break;
56  }
57  case 3: {
58  return _plugin_loader.createInstance(_mapping_plugin);
59  break;
60  }
61  case 4: {
62  std::ostringstream s;
63  s << "rsm::" << routine << "RoutineState";
64  return _plugin_loader.createInstance(s.str());
65  break;
66  }
67  default: {
68  if (!routine.empty()) {
69  std::ostringstream s;
70  s << "rsm::" << routine;
71  return _plugin_loader.createInstance(s.str());
72  } else {
73  ROS_ERROR(
74  "No matching plugin type found, return to Idle State");
75  return boost::make_shared<IdleState>();
76  }
77  break;
78  }
79  }
80  } catch (const std::exception& e) {
81  ROS_ERROR("Plugin state could not be created, return to Idle State");
82  return boost::make_shared<IdleState>();
83  }
84 }
85 
87 // check 1st time awake
88  if (!_current_state && _next_state) {
90  _current_state->onEntry();
91  _next_state = NULL;
92  }
93 
94  if (_current_state) {
95  _current_state->onActive();
96 
97  if (_next_state) // do transition
98  {
99  // do de-initialization of current state
100  _current_state->onExit();
101  }
102  }
103 
104  if (_next_state) // do transition
105  {
106  // do transition to next state
108  _next_state = NULL;
109  _current_state->onEntry();
110  std_msgs::String state_info;
111  state_info.data = _current_state->getName();
112  _state_info_publisher.publish(state_info);
113  }
114 }
115 
118  if (_current_state != nextState) {
119  if (nextState != NULL && nextState->getStateInterface() == NULL) {
120  _next_state = nextState;
121  _next_state->setStateInterface(this);
122  _next_state->onSetup();
123  } else {
124  ROS_ERROR(
125  "Next state instance invalid. Either NULL or already assigned state passed.");
126  }
127  }
128 }
129 
131  const rsm_msgs::OperationMode::ConstPtr& operation_mode) {
132  if (operation_mode->emergencyStop) {
133  _on_interrupt = true;
134  if (_current_state) {
136  }
137  } else if (operation_mode->mode == 2) {
138  _on_interrupt = true;
139  if (_current_state) {
141  }
142  } else {
143  if (_on_interrupt) {
144  _on_interrupt = false;
145  if (_current_state) {
146  _current_state->onInterrupt(INTERRUPT_END);
147  }
148  }
149  }
150 }
151 
153  const geometry_msgs::PoseStamped::ConstPtr& goal) {
154  _on_interrupt = true;
155  if (_current_state) {
157  }
158  rsm_msgs::SetNavigationGoal srv;
159  srv.request.goal = goal->pose;
160  srv.request.navigationMode = SIMPLE_GOAL;
162  } else {
163  ROS_ERROR("Failed to call Set Navigation Goal service");
164  }
165 }
166 
168  std_srvs::SetBool::Request &req, std_srvs::SetBool::Response &res) {
169  if (_current_state) {
170  bool success;
171  std::string message;
172  if (req.data) {
173  _current_state->onExplorationStart(success, message);
174  } else {
175  _current_state->onExplorationStop(success, message);
176  }
177  res.success = success;
178  res.message = message;
179  } else {
180  res.success = false;
181  res.message = "No active state";
182  }
183  return true;
184 }
185 
187  std_srvs::SetBool::Request &req, std_srvs::SetBool::Response &res) {
188  if (_current_state) {
189  bool success;
190  std::string message;
191  if (req.data) {
192  _current_state->onWaypointFollowingStart(success, message);
193  } else {
194  _current_state->onWaypointFollowingStop(success, message);
195  }
196  res.success = success;
197  res.message = message;
198  } else {
199  res.success = false;
200  res.message = "No active state";
201  }
202  return true;
203 }
204 
205 bool StateInterface::stop2dNavGoal(std_srvs::Trigger::Request &req,
206  std_srvs::Trigger::Response &res) {
207  if(_current_state){
209  }
210  return true;
211 }
212 
213 bool StateInterface::stateInfoService(std_srvs::Trigger::Request &req,
214  std_srvs::Trigger::Response &res) {
215  res.success = true;
216  res.message = _current_state->getName();
217  return true;
218 }
219 
220 }
ros::ServiceServer _start_stop_waypoint_following_service
ros::Publisher _state_info_publisher
bool stop2dNavGoal(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
void publish(const boost::shared_ptr< M > &message) const
boost::shared_ptr< rsm::BaseState > getPluginState(int plugin_type, std::string routine="")
Returns a plugin-state corresponding to the given type.
#define SIMPLE_GOAL_STOP_INTERRUPT
Definition: BaseState.h:14
std::string _navigation_plugin
Navigation state plugin name for plugin loader.
XmlRpcServer s
bool call(MReq &req, MRes &res)
ros::ServiceServer _start_stop_exploration_service
void transitionToVolatileState(boost::shared_ptr< rsm::BaseState > next_state)
pluginlib::ClassLoader< rsm::BaseState > _plugin_loader
Plugin loader for state plugins.
bool startStopWaypointFollowingService(std_srvs::SetBool::Request &req, std_srvs::SetBool::Response &res)
bool stateInfoService(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
#define TELEOPERATION_INTERRUPT
Definition: BaseState.h:12
ros::ServiceServer _state_info_service
Definition: BaseState.h:8
ros::Subscriber _simple_goal_sub
bool param(const std::string &param_name, T &param_val, const T &default_val) const
bool startStopExplorationService(std_srvs::SetBool::Request &req, std_srvs::SetBool::Response &res)
ros::Subscriber _operation_mode_sub
ros::ServiceClient _set_navigation_goal_client
#define EMERGENCY_STOP_INTERRUPT
Definition: BaseState.h:11
std::string _mapping_plugin
Mapping state plugin name for plugin loader.
boost::shared_ptr< rsm::BaseState > _next_state
Upcoming state.
void simpleGoalCallback(const geometry_msgs::PoseStamped::ConstPtr &goal)
void operationModeCallback(const rsm_msgs::OperationMode::ConstPtr &operation_mode)
boost::shared_ptr< rsm::BaseState > _current_state
Currently active state.
#define INTERRUPT_END
Definition: BaseState.h:10
std::string _calculate_goal_plugin
Calculate goal state plugin name for plugin loader.
#define SIMPLE_GOAL_INTERRUPT
Definition: BaseState.h:13
#define SIMPLE_GOAL
Definition: BaseState.h:18
#define ROS_ERROR(...)
ros::ServiceServer _stop_2d_nav_goal_service


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