Coverage Control Library
Loading...
Searching...
No Matches
centralized_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_CENTRALIZED_CVT_H_
30#define CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_CENTRALIZED_CVT_H_
31
32#include <omp.h>
33
34#include <algorithm>
35#include <fstream>
36#include <iostream>
37#include <queue>
38#include <random>
39#include <set>
40#include <vector>
41
46
47namespace CoverageControl {
48
62 private:
63 Parameters const params_;
64 size_t num_robots_ = 0;
65 CoverageSystem &env_;
66 Voronoi voronoi_;
67 PointVector robot_global_positions_;
68 PointVector goals_, actions_;
69 std::vector<double> voronoi_mass_;
70
71 bool is_converged_ = false;
72
73 public:
74 CentralizedCVT(Parameters const &params, CoverageSystem &env)
75 : CentralizedCVT(params, params.pNumRobots, env) {}
76 CentralizedCVT(Parameters const &params, size_t const &num_robots,
77 CoverageSystem &env)
78 : params_{params}, num_robots_{num_robots}, env_{env} {
79 robot_global_positions_ = env_.GetRobotPositions();
80 actions_.resize(num_robots_);
81 goals_ = robot_global_positions_;
82 voronoi_mass_.resize(num_robots_, 0);
83 voronoi_ = Voronoi(robot_global_positions_, env_.GetSystemExploredIDFMap(),
84 Point2(params_.pWorldMapSize, params_.pWorldMapSize),
85 params_.pResolution);
86 ComputeGoals();
87 }
88
89 PointVector GetActions() { return actions_; }
90
91 auto GetGoals() { return goals_; }
92
93 auto &GetVoronoi() { return voronoi_; }
94
95 void ComputeGoals() {
96 voronoi_ = Voronoi(robot_global_positions_, env_.GetSystemExploredIDFMap(),
97 Point2(params_.pWorldMapSize, params_.pWorldMapSize),
98 params_.pResolution);
99 auto voronoi_cells = voronoi_.GetVoronoiCells();
100 for (size_t iRobot = 0; iRobot < num_robots_; ++iRobot) {
101 goals_[iRobot] = voronoi_cells[iRobot].centroid();
102 voronoi_mass_[iRobot] = voronoi_cells[iRobot].mass();
103 }
104 }
105
106 bool IsConverged() const { return is_converged_; }
107
109 is_converged_ = true;
110 robot_global_positions_ = env_.GetRobotPositions();
111 ComputeGoals();
112 auto voronoi_cells = voronoi_.GetVoronoiCells();
113 for (size_t iRobot = 0; iRobot < num_robots_; ++iRobot) {
114 actions_[iRobot] = Point2(0, 0);
115 Point2 diff = goals_[iRobot] - robot_global_positions_[iRobot];
116 double dist = diff.norm();
117 /* if (dist < 0.1 * params_.pResolution) { */
118 /* continue; */
119 /* } */
120 if (dist < kEps) {
121 continue;
122 }
123 if (env_.CheckOscillation(iRobot)) {
124 continue;
125 }
126 double speed = dist / params_.pTimeStep;
127 /* double speed = 2 * dist * voronoi_mass_[iRobot]; */
128 speed = std::min(params_.pMaxRobotSpeed, speed);
129 Point2 direction(diff);
130 direction.normalize();
131 actions_[iRobot] = speed * direction;
132 is_converged_ = false;
133 }
134 return 0;
135 }
136};
137
138} /* namespace CoverageControl */
139#endif // CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_CENTRALIZED_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
Class to store parameters.
Definition parameters.h:50
double pTimeStep
Each time step corresponds to pTimeStep seconds.
Definition parameters.h:141
Class for computing Voronoi cells.
Definition voronoi.h:119
The file contains the CoverageSystem class, which is the main class for the coverage control library.
double const kEps
Definition constants.h:49
std::vector< Point2 > PointVector
Definition typedefs.h:51
Eigen::Vector2d Point2
Definition typedefs.h:44
Namespace for the CoverageControl library.
Contains parameters.
Contains typedefs for the library.