Coverage Control Library
Loading...
Searching...
No Matches
decentralized_cvt.h
Go to the documentation of this file.
1/*
2 * This file is part of the CoverageControl library
3 *
4 * Author: Saurav Agarwal
5 * Contact: sauravag@seas.upenn.edu, agr.saurav1@gmail.com
6 * Repository: https://github.com/KumarRobotics/CoverageControl
7 *
8 * Copyright (c) 2024, Saurav Agarwal
9 *
10 * The CoverageControl library is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at your
13 * option) any later version.
14 *
15 * The CoverageControl library is distributed in the hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18 * Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * CoverageControl library. If not, see <https://www.gnu.org/licenses/>.
22 */
23
29#ifndef CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_DECENTRALIZED_CVT_H_
30#define CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_DECENTRALIZED_CVT_H_
31
32#include <omp.h>
33
34#include <algorithm>
35#include <iostream>
36#include <vector>
37
43
44namespace CoverageControl {
45
58 private:
59 Parameters const params_;
60 size_t num_robots_ = 0;
61 CoverageSystem &env_;
62 PointVector robot_global_positions_;
63 PointVector goals_, actions_;
64 std::vector<double> voronoi_mass_;
65
66 bool is_converged_ = false;
67
68 public:
70 : DecentralizedCVT(params, params.pNumRobots, env) {}
71 DecentralizedCVT(Parameters const &params, size_t const &num_robots,
72 CoverageSystem &env)
73 : params_{params}, num_robots_{num_robots}, env_{env} {
74 robot_global_positions_ = env_.GetRobotPositions();
75 actions_.resize(num_robots_);
76 goals_ = robot_global_positions_;
77 voronoi_mass_.resize(num_robots_, 0);
79 }
80
81 PointVector GetActions() { return actions_; }
82
83 PointVector GetGoals() { return goals_; }
84
85 void ComputeGoals() {
86#pragma omp parallel for num_threads(num_robots_)
87 for (size_t iRobot = 0; iRobot < num_robots_; ++iRobot) {
88 Point2 const &pos = robot_global_positions_[iRobot];
89 MapUtils::MapBounds index, offset;
91 params_.pWorldMapSize, index, offset);
92 MapType robot_map = env_.GetRobotMap(iRobot);
93 MapType robot_local_map = robot_map.block(index.left + offset.left,
94 index.bottom + offset.bottom,
95 offset.width, offset.height);
96 Point2 map_translation(
97 (index.left + offset.left) * params_.pResolution,
98 (index.bottom + offset.bottom) * params_.pResolution);
99
100 PointVector robot_neighbors_pos;
101 for (size_t jRobot = 0; jRobot < num_robots_; ++jRobot) {
102 if (iRobot == jRobot) {
103 continue;
104 }
105 Point2 relative_pos =
106 robot_global_positions_[jRobot] - robot_global_positions_[iRobot];
107 if (relative_pos.norm() < params_.pCommunicationRange) {
108 robot_neighbors_pos.push_back(robot_global_positions_[jRobot]);
109 }
110 }
111 PointVector robot_positions(robot_neighbors_pos.size() + 1);
112
113 robot_positions[0] = robot_global_positions_[iRobot] - map_translation;
114 /* std::cout << "Voronoi: " << robot_positions[0][0] << " " <<
115 * robot_positions[0][1] << std::endl; */
116 int count = 1;
117 for (Point2 const &pos : robot_neighbors_pos) {
118 robot_positions[count] = pos - map_translation;
119 ++count;
120 }
121 Point2 map_size(offset.width, offset.height);
122 Voronoi voronoi(robot_positions, robot_local_map, map_size,
123 params_.pResolution, true, 0);
124 auto vcell = voronoi.GetVoronoiCell();
125 voronoi_mass_[iRobot] = vcell.mass();
126 goals_[iRobot] = vcell.centroid() + robot_positions[0] + map_translation;
127 if (goals_[iRobot][0] < 0 or goals_[iRobot][0] > params_.pWorldMapSize or
128 goals_[iRobot][1] < 0 or goals_[iRobot][1] > params_.pWorldMapSize) {
129 std::cout << "Goal out of bounds: " << goals_[iRobot][0] << " "
130 << goals_[iRobot][1] << std::endl;
131 std::cout << robot_global_positions_[iRobot][0] << " "
132 << robot_global_positions_[iRobot][1] << std::endl;
133 std::cout << vcell.centroid()[0] << " " << vcell.centroid()[1]
134 << std::endl;
135 std::cout << map_translation[0] << " " << map_translation[1]
136 << std::endl;
137 std::cout << robot_local_map.sum() << std::endl;
138 throw std::runtime_error(
139 "Goal out of bounds: this should not have happened for convex "
140 "environments");
141 }
142 }
143 }
144
146 is_converged_ = true;
147 robot_global_positions_ = env_.GetRobotPositions();
148 ComputeGoals();
149 for (size_t iRobot = 0; iRobot < num_robots_; ++iRobot) {
150 actions_[iRobot] = Point2(0, 0);
151 Point2 diff = goals_[iRobot] - robot_global_positions_[iRobot];
152 double dist = diff.norm();
153 /* std::cout << "Robot " << iRobot << " goal: " << goals_[iRobot][0] <<
154 * " " << goals_[iRobot][1] << " " << dist << std::endl; */
155 if (dist < kEps) {
156 continue;
157 }
158 if (env_.CheckOscillation(iRobot)) {
159 continue;
160 }
161 double speed = dist / params_.pTimeStep;
162 /* double speed = 2 * dist * voronoi_mass_[iRobot]; */
163 /* if (dist > 0 and voronoi_mass_[iRobot] == 0) { */
164 /* speed = dist / params_.pTimeStep; */
165 /* } */
166 speed = std::min(params_.pMaxRobotSpeed, speed);
167 Point2 direction(diff);
168 direction.normalize();
169 actions_[iRobot] = speed * direction;
170 is_converged_ = false;
171 }
172 return 0;
173 }
174
175 bool IsConverged() const { return is_converged_; }
176};
177
178} // namespace CoverageControl
179#endif // CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_DECENTRALIZED_CVT_H_
Contains the abstract class for coverage control algorithms.
The CoverageSystem class is the main class for the coverage control library.
PointVector GetRobotPositions(bool force_no_noise=false)
Get the global positions of all robots.
bool CheckOscillation(size_t const robot_id) const
const MapType & GetRobotMap(size_t const id) const
DecentralizedCVT(Parameters const &params, size_t const &num_robots, CoverageSystem &env)
DecentralizedCVT(Parameters const &params, CoverageSystem &env)
Class to store parameters.
Definition parameters.h:48
double pTimeStep
Each time step corresponds to pTimeStep seconds.
Definition parameters.h:140
double pCommunicationRange
Radius of communication (in meters)
Definition parameters.h:134
Class for computing Voronoi cells.
Definition voronoi.h:116
The file contains the CoverageSystem class, which is the main class for the coverage control library.
double const kEps
Definition constants.h:48
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MapType
Definition typedefs.h:48
std::vector< Point2 > PointVector
Definition typedefs.h:51
Eigen::Vector2d Point2
Definition typedefs.h:44
Utility functions for transforming maps.
void ComputeOffsets(double const resolution, Point2 const &pos, int const submap_size, int const map_size, MapBounds &index, MapBounds &offset)
Definition map_utils.h:59
Namespace for the CoverageControl library.
Contains parameters.
Contains typedefs for the library.