lib/ugv_random_walk.cpp
Go to the documentation of this file.
1 #include "lib/ugv_random_walk.h"
2 
4 {
5  NodeHandle nh;
6 
7  // init random number generator
8  int seed;
9  nh.param<int>("/rng_seed", seed, 0);
10  if (seed != 0) {
11  ROS_INFO("Using seed %d for random number generation.", seed);
12  rng = new random_numbers::RandomNumberGenerator(seed);
13  }
14  else {
15  rng = new random_numbers::RandomNumberGenerator();
16  }
17 
18  // init service clients
19  bound_client = nh.serviceClient<cpswarm_msgs::ClosestBound>("area/closest_bound");
20  bound_client.waitForExistence();
21  clear_sector_client = nh.serviceClient<cpswarm_msgs::GetSector>("obstacle_detection/get_clear_sector");
22  clear_sector_client.waitForExistence();
23 
24  // initial direction as drone is placed
25  direction = pos.get_yaw();
26  ROS_INFO("Initial direction %.2f", direction);
27 
28  // read parameters
29  // get step size from optimized candidate
30  nh.param(this_node::getName() + "/step_size", step_size, 3.0);
31  ROS_INFO("Step size %.2f", step_size);
32 }
33 
35 {
36  delete rng;
37 }
38 
40 {
41  // update position information
42  spinOnce();
43 
44  // compute new goal
45  geometry_msgs::Pose goal = select_goal();
46 
47  // reflect from bound
48  if (pos.out_of_bounds(goal)) {
49  if (reflect() == false)
50  return STATE_ABORTED;
51  goal = select_goal();
52  }
53 
54  // move to new position
55  if (pos.move(goal) == false)
56  return STATE_ABORTED;
57 
58  // change direction
59  if (new_direction() == false)
60  return STATE_ABORTED;
61 
62  // return state to action server
63  return STATE_ACTIVE;
64 }
65 
67 {
68  // get sector clear of obstacles and other uavs
69  cpswarm_msgs::GetSector clear;
70  if (clear_sector_client.call(clear) == false){
71  ROS_ERROR("Failed to get clear sector");
72  return false;
73  }
74 
75  ROS_DEBUG("Clear [%.2f, %.2f] size %.2f", clear.response.min, clear.response.max, clear.response.max - clear.response.min);
76 
77  // generate random direction until one is found inside of area not occupied by obstacles
78  geometry_msgs::Pose goal;
79  do {
80  direction = rng->uniformReal(clear.response.min, clear.response.max);
81  goal = select_goal();
82  ROS_DEBUG_THROTTLE(1, "Checking goal [%.2f, %.2f, %.2f] in direction %.2f...", goal.position.x, goal.position.y, goal.position.z, direction);
83  } while (pos.out_of_bounds(goal));
84 
85  ROS_INFO("Changing direction %.2f", direction);
86 
87  return true;
88 }
89 
91 {
92  // get boundary
93  geometry_msgs::Point b1;
94  geometry_msgs::Point b2;
95  cpswarm_msgs::ClosestBound cb;
96  cb.request.point = pos.get_pose().position;
97  if (bound_client.call(cb)){
98  b1 = cb.response.coords[0];
99  b2 = cb.response.coords[1];
100  }
101  else{
102  ROS_ERROR("Failed to get area boundary");
103  return false;
104  }
105 
106  // calculate reflection
107  direction = remainder(-atan2(b2.y - b1.y, b2.x - b1.x) - direction, 2*M_PI);
108 
109  ROS_INFO("Changing direction %.2f", direction);
110 
111  return true;
112 }
113 
114 geometry_msgs::Pose ugv_random_walk::select_goal ()
115 {
116  // compute goal position
117  return pos.compute_goal(step_size, direction);
118 }
behavior_state_t
An enumeration for the state of the behavior algorithm.
random_numbers::RandomNumberGenerator * rng
The random number generator used for selecting a random direction.
ServiceClient bound_client
Service client for determining closest mission area boundary to the current UGV position.
bool reflect()
Compute new direction as reflection from wall.
double step_size
The distance that the UGV travels in one step.
bool new_direction()
Compute new direction using rng.
behavior_state_t step()
Move the swarm member to a new position.
ugv_random_walk()
Constructor that initializes the private member variables.
position pos
A helper object for position related tasks.
~ugv_random_walk()
Destructor that deletes the private member objects.
ServiceClient clear_sector_client
Service client for determining the sector clear of obstacles.
geometry_msgs::Pose select_goal()
Compute goal position from direction.
double direction
The direction in which the UGV is traveling. It is measured in radian, clockwise starting from north...


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