Skip to content

Commit 17e0119

Browse files
multihypotheses working now
Signed-off-by: JesusSilvaUtrera <jesus.silva@ekumenlabs.com>
1 parent cea8212 commit 17e0119

7 files changed

Lines changed: 65 additions & 120 deletions

File tree

localization/beluga_demo_mh_amcl/include/beluga_demo_mh_amcl/mh_amcl.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ class MH_AMCL_Node : public rclcpp_lifecycle::LifecycleNode {
157157
std::shared_ptr<mh_amcl::MapMatcher> map_matcher_;
158158

159159
// Callbacks
160-
void map_callback(const nav_msgs::msg::OccupancyGrid::ConstSharedPtr &msg);
160+
void map_callback(nav_msgs::msg::OccupancyGrid::ConstSharedPtr msg);
161161
void laser_callback(sensor_msgs::msg::LaserScan::ConstSharedPtr msg);
162162
void initpose_callback(
163-
const geometry_msgs::msg::PoseWithCovarianceStamped::ConstSharedPtr
164-
&pose_msg);
163+
geometry_msgs::msg::PoseWithCovarianceStamped::ConstSharedPtr
164+
pose_msg);
165165
};
166166

167167
} // namespace mh_amcl

localization/beluga_demo_mh_amcl/include/beluga_demo_mh_amcl/utils.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ std_msgs::msg::ColorRGBA getColor(Color color_id, double alpha = 1.0);
6767
* frame to the global frame
6868
*/
6969
std::tuple<double, double>
70-
mapToWorld(std::shared_ptr<beluga_ros::OccupancyGrid> costmap, int localX,
71-
int localY);
70+
mapToWorld(std::shared_ptr<beluga_ros::OccupancyGrid> costmap, int local_x,
71+
int local_y);
7272

7373
/**
7474
* @brief Function to transform the coordinates of a point from global frame to
@@ -77,7 +77,7 @@ mapToWorld(std::shared_ptr<beluga_ros::OccupancyGrid> costmap, int localX,
7777
*/
7878
std::tuple<int, int>
7979
worldToMapNoBounds(std::shared_ptr<beluga_ros::OccupancyGrid> costmap,
80-
double worldX, double worldY);
80+
double world_x, double world_y);
8181

8282
/**
8383
* @brief Function to transform the coordinates of a point from global frame to
@@ -87,7 +87,7 @@ worldToMapNoBounds(std::shared_ptr<beluga_ros::OccupancyGrid> costmap,
8787
*/
8888
std::tuple<int, int>
8989
worldToMapEnforceBounds(std::shared_ptr<beluga_ros::OccupancyGrid> costmap,
90-
double worldX, double worldY);
90+
double world_x, double world_y);
9191

9292
/**
9393
* @brief extracts the X, Y coordinates and the yaw angle from a Sophus::SE2d

localization/beluga_demo_mh_amcl/rviz/custom_rviz.rviz

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ Panels:
1010
- /Sensors1
1111
- /Localization1
1212
- /Localization1/ParticleCloud1/Topic1
13-
- /Markers1
14-
- /Markers1/Mapped landmarks1/Topic1
1513
Splitter Ratio: 0.6411764621734619
16-
Tree Height: 472
14+
Tree Height: 486
1715
- Class: rviz_common/Selection
1816
Name: Selection
1917
- Class: rviz_common/Tool Properties
@@ -247,11 +245,6 @@ Visualization Manager:
247245
ParticleCloud: true
248246
Value: true
249247
World map: true
250-
Markers:
251-
Detected landmarks: true
252-
Mapped landmarks: true
253-
Spotlight detections: true
254-
Value: true
255248
RobotModel: false
256249
Sensors:
257250
Front camera: true
@@ -327,56 +320,14 @@ Visualization Manager:
327320
bodies: true
328321
heads: true
329322
Topic:
330-
Depth: 5
323+
Depth: 10
331324
Durability Policy: Volatile
332-
History Policy: Keep Last
325+
History Policy: Keep All
333326
Reliability Policy: Best Effort
334327
Value: /particle_markers
335328
Value: true
336329
Enabled: true
337330
Name: Localization
338-
- Class: rviz_common/Group
339-
Displays:
340-
- Class: rviz_default_plugins/Image
341-
Enabled: true
342-
Max Value: 1
343-
Median window: 5
344-
Min Value: 0
345-
Name: Spotlight detections
346-
Normalize Range: true
347-
Topic:
348-
Depth: 5
349-
Durability Policy: Volatile
350-
History Policy: Keep Last
351-
Reliability Policy: Reliable
352-
Value: /landmarks/spotlight_detections
353-
Value: true
354-
- Class: rviz_default_plugins/MarkerArray
355-
Enabled: true
356-
Name: Mapped landmarks
357-
Namespaces:
358-
{}
359-
Topic:
360-
Depth: 5
361-
Durability Policy: Transient Local
362-
History Policy: Keep Last
363-
Reliability Policy: Reliable
364-
Value: /landmarks/feature_map_markers
365-
Value: true
366-
- Class: rviz_default_plugins/MarkerArray
367-
Enabled: true
368-
Name: Detected landmarks
369-
Namespaces:
370-
{}
371-
Topic:
372-
Depth: 5
373-
Durability Policy: System Default
374-
History Policy: Keep Last
375-
Reliability Policy: System Default
376-
Value: /landmarks/landmark_detection_markers
377-
Value: true
378-
Enabled: true
379-
Name: Markers
380331
Enabled: true
381332
Global Options:
382333
Background Color: 48; 48; 48
@@ -453,11 +404,9 @@ Window Geometry:
453404
Height: 1043
454405
Hide Left Dock: false
455406
Hide Right Dock: false
456-
QMainWindow State: 000000ff00000000fd00000004000000000000019b00000379fc0200000009fb0000001200530065006c0065006300740069006f006e00000001e10000009b0000005c00fffffffb0000001e0054006f006f006c002000500072006f007000650072007400690065007302000001ed000001df00000185000000a3fb000000120056006900650077007300200054006f006f02000001df000002110000018500000122fb000000200054006f006f006c002000500072006f0070006500720074006900650073003203000002880000011d000002210000017afb000000100044006900730070006c006100790073010000003b00000259000000c700fffffffb0000002000730065006c0065006300740069006f006e00200062007500660066006500720200000138000000aa0000023a00000294fb00000014005700690064006500530074006500720065006f02000000e6000000d2000003ee0000030bfb0000000c004b0069006e0065006300740200000186000001060000030c00000261fb0000002800530070006f0074006c006900670068007400200064006500740065006300740069006f006e0073010000029a0000011a0000002800ffffff00000001000001de00000379fc0200000007fb0000001e0054006f006f006c002000500072006f00700065007200740069006500730100000041000000780000000000000000fb0000001800460072006f006e0074002000630061006d006500720061010000003b000001300000002800fffffffb0000000c00430061006d00650072006101000001710000012b0000002800fffffffb0000000a0049006d006100670065010000003b000001330000000000000000fb0000000c00430061006d0065007200610100000174000001330000000000000000fb0000000a0056006900650077007301000002a200000112000000a000fffffffb0000001200530065006c0065006300740069006f006e010000025a000000b200000000000000000000000200000490000000a9fc0100000001fb0000000a00560069006500770073030000004e00000080000002e10000019700000003000007800000003efc0100000002fb0000000800540069006d00650100000000000007800000025300fffffffb0000000800540069006d00650100000000000004500000000000000000000003fb0000037900000004000000040000000800000008fc0000000100000002000000010000000a0054006f006f006c00730100000000ffffffff0000000000000000
407+
QMainWindow State: 000000ff00000000fd00000004000000000000019b00000379fc020000000bfb0000001200530065006c0065006300740069006f006e00000001e10000009b0000005c00fffffffb0000001e0054006f006f006c002000500072006f007000650072007400690065007302000001ed000001df00000185000000a3fb000000120056006900650077007300200054006f006f02000001df000002110000018500000122fb000000200054006f006f006c002000500072006f0070006500720074006900650073003203000002880000011d000002210000017afb000000100044006900730070006c006100790073010000003b00000267000000c700fffffffb0000002000730065006c0065006300740069006f006e00200062007500660066006500720200000138000000aa0000023a00000294fb00000014005700690064006500530074006500720065006f02000000e6000000d2000003ee0000030bfb0000000c004b0069006e0065006300740200000186000001060000030c00000261fb0000002800530070006f0074006c006900670068007400200064006500740065006300740069006f006e007301000001b5000000af0000000000000000fb0000001800460072006f006e0074002000630061006d00650072006101000002a80000010c0000002800fffffffb0000002800530070006f0074006c006900670068007400200064006500740065006300740069006f006e007301000002e8000000cc000000000000000000000001000001de00000379fc0200000007fb0000001e0054006f006f006c002000500072006f00700065007200740069006500730100000041000000780000000000000000fb0000001800460072006f006e0074002000630061006d006500720061010000003b000000690000000000000000fb0000000c00430061006d00650072006101000000aa0000012b0000000000000000fb0000000a0049006d006100670065010000003b000001330000000000000000fb0000000c00430061006d006500720061010000003b000002440000002800fffffffb0000000a0056006900650077007301000002850000012f000000a000fffffffb0000001200530065006c0065006300740069006f006e010000025a000000b200000000000000000000000200000490000000a9fc0100000001fb0000000a00560069006500770073030000004e00000080000002e10000019700000003000007800000003efc0100000002fb0000000800540069006d00650100000000000007800000025300fffffffb0000000800540069006d00650100000000000004500000000000000000000003fb0000037900000004000000040000000800000008fc0000000100000002000000010000000a0054006f006f006c00730100000000ffffffff0000000000000000
457408
Selection:
458409
collapsed: false
459-
Spotlight detections:
460-
collapsed: false
461410
Time:
462411
collapsed: false
463412
Tool Properties:

localization/beluga_demo_mh_amcl/src/mh_amcl/map_matcher.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ float MapMatcher::match(
183183

184184
for (int i = 0; i < scan.size(); i = i + scale) {
185185
tf2::Vector3 test_point = transform * scan[i];
186-
auto [gi, gj] =
186+
auto [local_x, local_y] =
187187
utils::worldToMapNoBounds(costmap, test_point.x(), test_point.y());
188188

189-
if (gi > 0 && gj > 0 && gi < costmap->width() && gj < costmap->height() &&
190-
costmap->data_at(gi, gj) == utils::LETHAL_OBSTACLE) {
189+
if (local_x > 0 && local_y > 0 && local_x < costmap->width() && local_y < costmap->height() &&
190+
costmap->data_at(local_x, local_y) == utils::LETHAL_OBSTACLE) {
191191
hits++;
192192
}
193193
total++;

localization/beluga_demo_mh_amcl/src/mh_amcl/mh_amcl.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void MH_AMCL_Node::predict() {
288288
}
289289

290290
void MH_AMCL_Node::map_callback(
291-
const nav_msgs::msg::OccupancyGrid::ConstSharedPtr &msg) {
291+
nav_msgs::msg::OccupancyGrid::ConstSharedPtr msg) {
292292
// Every time a new map is sent, update the costmap and the map matcher
293293
costmap_ = std::make_shared<beluga_ros::OccupancyGrid>(msg);
294294
map_matcher_ = std::make_shared<mh_amcl::MapMatcher>(msg);
@@ -312,9 +312,6 @@ void MH_AMCL_Node::correct() {
312312
}
313313

314314
last_time_ = last_laser_->header.stamp;
315-
316-
RCLCPP_INFO_STREAM(get_logger(),
317-
"Correct [" << (now() - start).seconds() << " secs]");
318315
}
319316

320317
void MH_AMCL_Node::reseed() {
@@ -323,15 +320,11 @@ void MH_AMCL_Node::reseed() {
323320
for (auto &particles : particles_population_) {
324321
particles->reseed();
325322
}
326-
327-
RCLCPP_INFO_STREAM(get_logger(), "==================Reseed ["
328-
<< (now() - start).seconds()
329-
<< " secs]");
330323
}
331324

332325
void MH_AMCL_Node::initpose_callback(
333-
const geometry_msgs::msg::PoseWithCovarianceStamped::ConstSharedPtr
334-
&pose_msg) {
326+
geometry_msgs::msg::PoseWithCovarianceStamped::ConstSharedPtr
327+
pose_msg) {
335328
// Having a new initial pose retriggers the algorithm
336329
if (pose_msg->header.frame_id == "map") {
337330
RCLCPP_INFO(get_logger(), "Map initial pose received!");
@@ -517,10 +510,10 @@ void MH_AMCL_Node::manage_hypotheses() {
517510

518511
signed char MH_AMCL_Node::get_cost(const geometry_msgs::msg::Pose &pose) {
519512
// Get the corresponding cost from the costmap for a specific pose
520-
auto [i, j] =
513+
auto [local_x, local_y] =
521514
utils::worldToMapNoBounds(costmap_, pose.position.x, pose.position.y);
522515

523-
std::optional<signed char> opt = costmap_->data_at(i, j);
516+
std::optional<signed char> opt = costmap_->data_at(local_x, local_y);
524517
if (opt.has_value()) {
525518
return static_cast<unsigned char>(opt.value());
526519
} else {

localization/beluga_demo_mh_amcl/src/mh_amcl/particles_distribution.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ void ParticlesDistribution::correct_once(
371371
p.hits = p.hits / static_cast<float>(scan.ranges.size());
372372
quality_ = std::max(quality_, p.hits);
373373
}
374-
RCLCPP_WARN_STREAM(parent_node_->get_logger(), "Quality computed: " << quality_);
375374
}
376375

377376
tf2::Transform ParticlesDistribution::get_tranform_to_read(

localization/beluga_demo_mh_amcl/src/mh_amcl/utils.cpp

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -115,58 +115,62 @@ std_msgs::msg::ColorRGBA getColor(Color color_id, double alpha) {
115115
}
116116

117117
std::tuple<double, double>
118-
mapToWorld(std::shared_ptr<beluga_ros::OccupancyGrid> costmap, int localX,
119-
int localY) {
120-
double worldX = costmap->origin().translation().x() +
121-
(localX + 0.5) * costmap->resolution();
122-
double worldY = costmap->origin().translation().y() +
123-
(localY + 0.5) * costmap->resolution();
124-
return std::make_tuple(worldX, worldY);
118+
mapToWorld(std::shared_ptr<beluga_ros::OccupancyGrid> costmap,
119+
int local_x,
120+
int local_y)
121+
{
122+
// map origin in world frame
123+
double origin_x = costmap->origin().translation().x();
124+
double origin_y = costmap->origin().translation().y();
125+
double res = costmap->resolution();
126+
127+
// use the origin + the center of the cell (local + 0.5) times the resolution of each cell
128+
double world_x = origin_x + (local_x + 0.5) * res;
129+
double world_y = origin_y + (local_y + 0.5) * res;
130+
return std::make_tuple(world_x, world_y);
125131
}
126132

127133
std::tuple<int, int>
128134
worldToMapNoBounds(std::shared_ptr<beluga_ros::OccupancyGrid> costmap,
129-
double worldX, double worldY) {
130-
// No clamping or bounds checking is done here, result might be negative or
131-
// exceed the grid limits
132-
int localX = static_cast<int>((worldX + costmap->origin().translation().x()) /
133-
costmap->resolution());
134-
int localY = static_cast<int>((worldY + costmap->origin().translation().y()) /
135-
costmap->resolution());
136-
return std::make_tuple(localX, localY);
135+
double world_x,
136+
double world_y)
137+
{
138+
double origin_x = costmap->origin().translation().x();
139+
double origin_y = costmap->origin().translation().y();
140+
double res = costmap->resolution();
141+
142+
// subtract origin, divide by resolution, floor to get cell index
143+
int local_x = static_cast<int>(std::floor((world_x - origin_x) / res));
144+
int local_y = static_cast<int>(std::floor((world_y - origin_y) / res));
145+
return std::make_tuple(local_x, local_y);
137146
}
138147

139148
std::tuple<int, int>
140149
worldToMapEnforceBounds(std::shared_ptr<beluga_ros::OccupancyGrid> costmap,
141-
double worldX, double worldY) {
142-
int localX;
143-
int localY;
144-
145-
// If the world coordinates given are inferior to the costmap origin, start on
146-
// the origin of the grid
147-
if (worldX < costmap->origin().translation().x() ||
148-
worldY < costmap->origin().translation().y())
149-
localX = 0;
150-
localY = 0;
151-
152-
localX = static_cast<int>((worldX + costmap->origin().translation().x()) /
153-
costmap->resolution());
154-
localY = static_cast<int>((worldY + costmap->origin().translation().y()) /
155-
costmap->resolution());
156-
157-
// Clamp the indices within grid dimensions:
158-
if (localX < 0) {
159-
localX = 0;
160-
} else if (localX >= costmap->width()) {
161-
localX = costmap->width() - 1;
162-
}
163-
if (localY < 0) {
164-
localY = 0;
165-
} else if (localY >= costmap->height()) {
166-
localY = costmap->height() - 1;
150+
double world_x,
151+
double world_y)
152+
{
153+
double origin_x = costmap->origin().translation().x();
154+
double origin_y = costmap->origin().translation().y();
155+
double res = costmap->resolution();
156+
int width = costmap->width();
157+
int height = costmap->height();
158+
159+
// again subtract origin, divide by resolution, floor to get cell index
160+
int local_x = static_cast<int>(std::floor((world_x - origin_x) / res));
161+
int local_y = static_cast<int>(std::floor((world_y - origin_y) / res));
162+
163+
// if completely before the map origin, snap to (0,0)
164+
if (world_x < origin_x || world_y < origin_y) {
165+
local_x = 0;
166+
local_y = 0;
167167
}
168168

169-
return std::make_tuple(localX, localY);
169+
// clamp into [0_width-1], [0_height-1]
170+
local_x = std::clamp(local_x, 0, width - 1);
171+
local_y = std::clamp(local_y, 0, height - 1);
172+
173+
return std::make_tuple(local_x, local_y);
170174
}
171175

172176
std::tuple<double, double, double>

0 commit comments

Comments
 (0)