RoomNavigationTargetCreator.cpp
Go to the documentation of this file.
2
3#include <cstddef>
4
5#include <SimoxUtility/color/Color.h>
6
9
12
18
19#include <range/v3/range/conversion.hpp>
20#include <range/v3/view/reverse.hpp>
21
23{
27
30 const algorithms::Costmap& obstacleDistancesCostmap,
31 const algorithms::Room& room,
32 const core::Position& global_P_robot,
33 viz::Client* arviz) const
34 {
35 using ShortestPathFasterAlgorithm = algorithms::spfa::ShortestPathFasterAlgorithm;
36
37 const ShortestPathFasterAlgorithm spfa(obstacleDistancesCostmap,
38 ShortestPathFasterAlgorithm::Parameters());
39
40 ShortestPathFasterAlgorithm::Result result = spfa.spfa(conv::to2D(global_P_robot));
41
42 const armarx::navigation::algorithms::Costmap navigationPlanningCostmap
44 obstacleDistancesCostmap, result.distances, result.reachable);
45
46 armarx::navigation::algorithms::Costmap roomCostmap = navigationPlanningCostmap;
47
48 const std::size_t cX = roomCostmap.getGrid().rows();
49 const std::size_t cY = roomCostmap.getGrid().cols();
50
51 // invalidate everything that is in collision
52 // roomCostmap.getMutableMask().value() = roomCostmap.getGrid().array() >= 0.5;
53
54#pragma omp parallel for schedule(static)
55 for (unsigned int x = 0; x < cX; x++)
56 {
57 for (unsigned int y = 0; y < cY; y++)
58 {
60 const algorithms::Costmap::Position position = roomCostmap.toPositionGlobal(index);
61
62 auto pos3d = conv::to3D(position);
63 pos3d.z() = room.zFloor + room.height / 2; // just at the center (height)
64
65 // invalid outside of the room
66 const bool isInside = room.isInside(pos3d);
67
68 roomCostmap.getMutableMask().value()(x, y) &= isInside;
69 roomCostmap.getMutableGrid()(x, y) *= static_cast<float>(isInside);
70 }
71 }
72
73 ARMARX_INFO << "Closest position to room: " << roomCostmap.optimum().position;
74
75 const core::Position closestReachablePositionOnRoomBoundary =
76 conv::to3D(roomCostmap.optimum().position);
77
78
79 // We don't stop here. We want to place the robot in front of the room at a
80 const auto plan = spfa.plan(conv::to2D(global_P_robot),
81 conv::to2D(closestReachablePositionOnRoomBoundary));
82
83 if (arviz != nullptr)
84 {
85 auto layer = arviz->layer("RoomNavigationTargetCreator");
86 algorithms::visualize(roomCostmap, layer, "room_costmap", 3); // FIXME magic number
87
88 layer.add(viz::Sphere("closestReachablePositionOnRoomBoundary")
89 .position(closestReachablePositionOnRoomBoundary)
90 .radius(50)
91 .color(simox::Color::blue()));
92
93 arviz->commit(layer);
94 }
95
96
97 ARMARX_CHECK(plan.success);
98
99 const auto posInFrontOfRoom = [&]() -> core::Position
100 {
101 namespace r = ::ranges;
102 namespace rv = ::ranges::views;
103
104 const auto reversedPlan = rv::reverse(plan.path) | r::to_vector;
105
106 const auto goalPos = conv::to2D(closestReachablePositionOnRoomBoundary);
107
108 for (const auto& pos : reversedPlan)
109 {
110 if ((pos - goalPos).norm() > params.distanceToRoomEntry)
111 {
112 return conv::to3D(pos);
113 }
114 }
115
116 ARMARX_INFO << "There is something wrong with the path!";
117 return conv::to3D(reversedPlan.back()); // the current robot pos
118 }();
119
120 if (arviz != nullptr)
121 {
122 auto layer = arviz->layer("RoomNavigationTargetCreator_target");
123
124 layer.add(viz::Sphere("posInFrontOfRoom")
125 .position(posInFrontOfRoom)
126 .radius(50)
127 .color(simox::Color::green()));
128
129 arviz->commit(layer);
130 }
131
132 return {.global_P_roomEntry = closestReachablePositionOnRoomBoundary,
133 .global_P_robot = posInFrontOfRoom};
134 }
135} // namespace armarx::navigation::rooms
uint8_t index
static Costmap WithSameDimsAs(const Costmap &other, const Grid &grid, const std::optional< Mask > &mask=std::nullopt)
Create a costmap with the same dimensions and parameters as the given one.
Definition Costmap.cpp:854
std::optional< Costmap::Mask > & getMutableMask() noexcept
Definition Costmap.cpp:535
Position toPositionGlobal(const Index &index) const
Definition Costmap.cpp:75
Result getClosestPositionOutsideOfRoom(const algorithms::Costmap &obstacleDistancesCostmap, const algorithms::Room &room, const core::Position &global_P_robot, viz::Client *arviz=nullptr) const
virtual Layer layer(std::string const &name) const
Definition Client.cpp:80
CommitResult commit(StagedCommit const &commit)
Definition Client.cpp:89
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
void visualize(const algorithms::Costmap &costmap, viz::Layer &layer, const std::string &name, const float zOffset)
std::vector< Eigen::Vector2f > to2D(const std::vector< Eigen::Vector3f > &v)
Definition eigen.cpp:29
std::vector< Eigen::Vector3f > to3D(const std::vector< Eigen::Vector2f > &v)
Definition eigen.cpp:14
Eigen::Vector3f Position
Definition basic_types.h:36
This file offers overloads of toIce() and fromIce() functions for STL container types.
double norm(const Point &a)
Definition point.hpp:102
bool isInside(const Eigen::Vector3f &point, bool restrictTo2dCheck=false) const
Definition Room.cpp:17
void add(ElementT const &element)
Definition Layer.h:31