lib/uav_random_direction.cpp
Go to the documentation of this file.
2 
4 {
5  NodeHandle nh;
6 
7  // read parameters
8  nh.param(this_node::getName() + "/step_size_min", step_size_min, 1.0);
9  nh.param(this_node::getName() + "/step_size_max", step_size_max, 3.0);
10 
11  // init random number generator
12  int seed;
13  nh.param<int>("/rng_seed", seed, 0);
14  if (seed != 0) {
15  rng = new random_numbers::RandomNumberGenerator(seed);
16  }
17  else {
18  rng = new random_numbers::RandomNumberGenerator();
19  }
20 
21  // service client for obstacle detection
22  clear_sector_client = nh.serviceClient<cpswarm_msgs::GetSector>("obstacle_detection/get_clear_sector");
23  clear_sector_client.waitForExistence();
24 
25  // inititial direction as drone is placed
26  direction = pos.get_yaw();
27 
28  ROS_INFO("Initial direction %.2f", direction);
29 }
30 
32 {
33  delete rng;
34 }
35 
37 {
38  // update position information
39  spinOnce();
40 
41  // compute new goal with maximum distance
43  geometry_msgs::Pose goal = select_goal();
44 
45  // new goal is out of bounds
46  if (pos.out_of_bounds(goal)) {
47  // delta distance
48  double step = (step_size_max - step_size_min) / 10;
49 
50  // look for largest possible distance
51  for (int i = 0; i <= 10; ++i) {
52  // reduce distance
53  distance = step_size_max - i * step;
54 
55  // select goal at new distance
56  goal = select_goal();
57 
58  // found valid goal
59  if (!pos.out_of_bounds(goal))
60  break;
61  }
62 
63  // change direction
64  if (pos.out_of_bounds(goal)) {
65  if (new_direction() == false)
66  return STATE_ABORTED;
67  goal = select_goal();
68  }
69  }
70 
71  // obstacle in direction of new goal
72  if (pos.occupied(goal)) {
73  ROS_ERROR("Obstacle ahead!");
74  // change direction
75  if (new_direction() == false)
76  return STATE_ABORTED;
77  goal = select_goal();
78  }
79 
80  // move to new position
81  if (pos.move(goal) == false)
82  return STATE_ABORTED;
83 
84  return STATE_ACTIVE;
85 }
86 
87 geometry_msgs::Pose uav_random_direction::select_goal ()
88 {
89  // compute goal position
90  return pos.compute_goal(distance, direction);
91 }
92 
94 {
95  // get sector clear of obstacles and other uavs
96  cpswarm_msgs::GetSector clear;
97  if (clear_sector_client.call(clear) == false){
98  ROS_ERROR("Failed to get clear sector");
99  return false;
100  }
101 
102  ROS_DEBUG("Clear [%.2f, %.2f] size %.2f", clear.response.min, clear.response.max, clear.response.max - clear.response.min);
103 
104  // generate random direction until one is found inside of area not occupied by obstacles
105  geometry_msgs::Pose goal;
106  do {
107  direction = rng->uniformReal(clear.response.min, clear.response.max);
108  goal = select_goal();
109  ROS_DEBUG_THROTTLE(1, "Checking goal [%.2f, %.2f, %.2f] in direction %.2f...", goal.position.x, goal.position.y, goal.position.z, direction);
110  } while (pos.out_of_bounds(goal));
111 
112  ROS_INFO("Changing direction %.2f", direction);
113 
114  return true;
115 }
~uav_random_direction()
Destructor that deletes the private member objects.
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.
position pos
A helper object for position related tasks.
double direction
The direction in which the drone is travling. It is measured in radian, clockwise starting from north...
geometry_msgs::Pose select_goal()
Compute goal position from direction.
bool new_direction()
Compute new direction using rng.
uav_random_direction()
Constructor that initializes the private member variables.
behavior_state_t step()
Move the swarm member to a new position.
double step_size_max
The maximum distance that a UAV travels in one step.
ServiceClient clear_sector_client
Service client for determining the sector clear of obstacles.
double distance
The distance which the drone is traveling.
double step_size_min
The minimum distance that a UAV travels in one step.


uav_random_direction
Author(s): Micha Sende
autogenerated on Sun Dec 29 2019 10:25:58