SocialCostmapBuilder.cpp
Go to the documentation of this file.
1/**
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Fabian Reister ( fabian dot reister at kit dot edu )
17 * @date 2022
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
23
24#include <cmath>
25#include <cstddef>
26
27#include <Eigen/Geometry>
28
29#include <SimoxUtility/math/convert/mat3f_to_rpy.h>
30#include <VirtualRobot/CollisionDetection/CollisionChecker.h>
31#include <VirtualRobot/Robot.h>
32#include <VirtualRobot/SceneObjectSet.h>
33
37
42#include <range/v3/numeric/accumulate.hpp>
43#include <range/v3/range/conversion.hpp>
44#include <range/v3/view/transform.hpp>
45
47{
48
49
52 sceneBounds(sceneBounds), parameters(parameters)
53 {
54 }
55
58 {
59 const auto humanPoses = getHumanPoses(humans);
60 return create(humanPoses, mode);
61 }
62
64 SocialCostmapBuilder::create(const std::vector<navigation::core::Pose2D>& humanPoses, Mode mode)
65 {
66 const auto grid = createUniformGrid(sceneBounds, parameters);
67
68 ARMARX_VERBOSE << "Creating grid";
69 navigation::algorithms::Costmap costmap(grid, parameters, sceneBounds);
70
71 ARMARX_VERBOSE << "Filling grid with size (" << costmap.getGrid().rows() << "/"
72 << costmap.getGrid().cols() << ")";
73
74 switch (mode)
75 {
76 case Mode::Mode1:
77 fillGridCostsMode1(costmap, humanPoses);
78 break;
79 case Mode::Empty:
80 ARMARX_VERBOSE << "Empty mode selected";
81 costmap.getMutableGrid().setOnes();
82 break;
83 }
84
85 ARMARX_VERBOSE << "Filled grid";
86
87 initializeMask(costmap);
88
89 return costmap;
90 }
91
92 void
93 SocialCostmapBuilder::initializeMask(navigation::algorithms::Costmap& costmap)
94 {
95 costmap.getMutableMask() = costmap.getGrid().array() > 0.F;
96
97 ARMARX_VERBOSE << "Initializing mask: Fraction of valid elements: "
98 << costmap.getMask()->cast<float>().sum() / costmap.getMask()->size();
99 }
100
101 Eigen::MatrixXf
102 SocialCostmapBuilder::createUniformGrid(const navigation::algorithms::SceneBounds& sceneBounds,
104 {
106
107 ARMARX_VERBOSE << "Scene bounds are " << sceneBounds.min << " and " << sceneBounds.max;
108
109 //+1 for explicit rounding up
110 size_t c_x = (sceneBounds.max.x() - sceneBounds.min.x()) / parameters.cellSize + 1;
111 size_t c_y = (sceneBounds.max.y() - sceneBounds.min.y()) / parameters.cellSize + 1;
112
113 ARMARX_VERBOSE << "Grid size: " << c_x << ", " << c_y;
114
115 ARMARX_VERBOSE << "Resetting grid";
116 Eigen::MatrixXf grid(c_x, c_y);
117 grid.setZero();
118
119 return grid;
120 }
121
122 auto
123 toM(Eigen::Isometry3f t) -> Eigen::Isometry3f
124 {
125 t.translation() *= 0.001;
126 return t;
127 }
128
129 auto
130 toM(Eigen::Vector3f t) -> Eigen::Vector3f
131 {
132 t *= 0.001;
133 return t;
134 }
135
136 float
137 SocialCostmapBuilder::computeCostMode1(const navigation::core::Position& position,
138 const std::vector<navigation::core::Pose2D>& humanPoses)
139 {
141 // for real experiments use something greater than 600'000
142 //float std_deviation = 600000.0f;
143 float std_deviation = 1'000'000.0f;
144 float peak_height = 1.0f;
145
146 const auto costs =
147 humanPoses | ranges::views::transform(
148 [&](const navigation::core::Pose2D& global_T_human_2d) -> float
149 {
150 float result = peak_height * exp(-0.5 * (global_T_human_2d.translation() - position.head<2>()).squaredNorm() / std_deviation);
151
152 return result;
153 });
154
155 //return ranges::accumulate(costs, 0.f);
156 if (ranges::empty(costs))
157 {
158 return 0.f;
159 }
160 return ranges::max(costs);
161 }
162
163 void
164 SocialCostmapBuilder::fillGridCostsMode1(navigation::algorithms::Costmap& costmap, const std::vector<navigation::core::Pose2D>& humanPoses)
165 {
166
167 const std::size_t c_x = costmap.getGrid().rows();
168 const std::size_t c_y = costmap.getGrid().cols();
169
170 //ARMARX_INFO << VAROUT(humans.front().estimateAt(Clock::Now()).translation());
171
172 for (unsigned int x = 0; x < c_x; x++)
173 {
174 for (unsigned int y = 0; y < c_y; y++)
175 {
177 const navigation::algorithms::Costmap::Position position = costmap.toPositionGlobal(index);
178
179 costmap.getMutableGrid()(x, y) =
180 computeCostMode1(armarx::navigation::conv::to3D(position), humanPoses);
181 }
182 }
183 }
184
185 std::vector<navigation::core::Pose2D>
186 SocialCostmapBuilder::getHumanPoses(const navigation::human::Humans& humans)
187 {
188 return humans | ranges::views::transform(
189 [](const navigation::human::Human& human) -> navigation::core::Pose2D
190 {
191 return human.estimateAt(Clock::Now());
192 }) | ranges::to_vector;
193 }
194
195
196}
uint8_t index
static DateTime Now()
Current time on the virtual clock.
Definition Clock.cpp:93
std::optional< Costmap::Mask > & getMutableMask() noexcept
Definition Costmap.cpp:535
const std::optional< Mask > & getMask() const noexcept
Definition Costmap.cpp:529
navigation::algorithms::Costmap create(const navigation::human::Humans &humans, Mode mode)
SocialCostmapBuilder(const navigation::algorithms::SceneBounds sceneBounds, const navigation::algorithms::Costmap::Parameters &parameters)
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
auto toM(Eigen::Isometry3f t) -> Eigen::Isometry3f
std::vector< Eigen::Vector3f > to3D(const std::vector< Eigen::Vector2f > &v)
Definition eigen.cpp:14
Eigen::Isometry2f Pose2D
Definition basic_types.h:34
Eigen::Vector3f Position
Definition basic_types.h:36
std::vector< Human > Humans
Definition types.h:48
This file offers overloads of toIce() and fromIce() functions for STL container types.
float cellSize
How big each cell is in the uniform grid.
Definition Costmap.h:31
#define ARMARX_TRACE
Definition trace.h:77