ugv_random_walk.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <ros/console.h>
3 #include <actionlib/server/simple_action_server.h>
4 #include <cpswarm_msgs/CoverageAction.h>
5 #include <cpswarm_msgs/TargetPositionEvent.h>
6 #include "lib/ugv_random_walk.h"
7 
8 using namespace ros;
9 
14 
18 cpswarm_msgs::CoverageResult result;
19 
23 typedef actionlib::SimpleActionServer<cpswarm_msgs::CoverageAction> action_server_t;
24 
30 void ActionCallback(const cpswarm_msgs::CoverageGoalConstPtr& goal, action_server_t* as)
31 {
32  NodeHandle nh;
33 
34  // set loop rate
35  double loop_rate;
36  nh.param(this_node::getName() + "/loop_rate", loop_rate, 5.0);
37  Rate rate(loop_rate);
38 
39  ROS_INFO("Executing coverage");
40 
41  // coverage library
42  ugv_random_walk ugv_coverage;
43 
44  // execute coverage until state changes
46  while (ok() && !as->isPreemptRequested() && state == STATE_ACTIVE) {
47  ROS_DEBUG("Coverage step");
48  behavior_state_t result = ugv_coverage.step();
49  if (state == STATE_ACTIVE)
50  state = result;
51  rate.sleep();
52  spinOnce();
53  }
54 
55  // coverage succeeded
56  if (state == STATE_SUCCEEDED) {
57  ROS_INFO("Coverage succeeded, found target %d at [%f, %f]", result.target_id, result.target_pose.pose.position.x, result.target_pose.pose.position.y);
58 
59  as->setSucceeded(result);
60  }
61 
62  // coverage aborted
63  else if (state == STATE_ABORTED) {
64  ROS_INFO("Coverage aborted");
65  as->setAborted();
66  }
67 
68  // coverage was preempted
69  else{
70  ROS_INFO("Coverage preempted");
71  as->setPreempted();
72  }
73 }
74 
79 void found_callback (const cpswarm_msgs::TargetPositionEvent::ConstPtr& msg)
80 {
82  result.target_id = msg->id;
83  result.target_pose = msg->pose;
84 }
85 
92 int main (int argc, char** argv)
93 {
94  // init ros node
95  init(argc, argv, "ugv_coverage");
96  NodeHandle nh;
97 
98  // define which log messages are shown
99  if (console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, console::levels::Info)) {
100  console::notifyLoggerLevelsChanged();
101  }
102  else{
103  ROS_ERROR("Could not set logger level!");
104  }
105 
106  // stop covering once a target has been found
107  int queue_size;
108  nh.param(this_node::getName() + "/queue_size", queue_size, 1);
109  bool single_target;
110  nh.param(this_node::getName() + "/single_target", single_target, true);
111  Subscriber found_sub;
112  if (single_target)
113  found_sub = nh.subscribe("target_found", queue_size, found_callback);
114 
115  // start action server
116  action_server_t as(nh, "ugv_coverage", boost::bind(&ActionCallback, _1, &as), false);
117  as.start();
118 
119  // wait for action client
120  spin();
121 
122  return 0;
123 }
124 
behavior_state_t
An enumeration for the state of the behavior algorithm.
cpswarm_msgs::CoverageResult result
The target found during execution of the coverage algorithm.
An implementation of the coverage class that allows to cover a given area with the random walk algori...
void found_callback(const cpswarm_msgs::TargetPositionEvent::ConstPtr &msg)
Callback function to receive details of a target that has been detected.
void ActionCallback(const cpswarm_msgs::CoverageGoalConstPtr &goal, action_server_t *as)
Callback of the action server which executes the coverage task until it is preempted or finished...
behavior_state_t step()
Move the swarm member to a new position.
behavior_state_t state
The state of the behavior algorithm.
actionlib::SimpleActionServer< cpswarm_msgs::CoverageAction > action_server_t
An action server type that allows to start and stop the coverage task.
int main(int argc, char **argv)
Main function to be executed by ROS.


ugv_random_walk
Author(s): Micha Sende
autogenerated on Sun Dec 29 2019 10:26:23