Skip to content

Commit 9f80c21

Browse files
committed
Fix the bug of not publishing the map
1 parent b7f71bd commit 9f80c21

File tree

5 files changed

+229
-48
lines changed

5 files changed

+229
-48
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vscode
22
lidarslam/launch/test.launch.py
3-
lidarslam/param/test.yaml
3+
lidarslam/param/test.yaml
4+
lidarslam/rviz/test.rviz

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ ros2 service call /map_save std_srvs/Empty
123123
demo data(ROS1) is `hdl_400.bag` in [hdl_graph_slam](https://github.com/koide3/hdl_graph_slam)
124124

125125
```
126-
rviz2 -d src/lidarslam_ros2/scanmatcher/config/mapping.rviz
126+
rviz2 -d src/lidarslam_ros2/lidarslam/rviz/mapping.rviz
127127
```
128128

129129
```
@@ -151,7 +151,7 @@ tar zxfv sample_moriyama_150324.tar.gz
151151
```
152152

153153
```
154-
rviz2 -d src/lidarslam_ros2/scanmatcher/config/mapping.rviz
154+
rviz2 -d src/lidarslam_ros2/scanmatcher/rviz/mapping.rviz
155155
```
156156

157157
```

scanmatcher/config/mapping.rviz renamed to lidarslam/rviz/mapping.rviz

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Visualization Manager:
5454
Color: 255; 255; 255
5555
Color Transformer: Intensity
5656
Decay Time: 0
57-
Enabled: false
57+
Enabled: true
5858
Invert Rainbow: false
5959
Max Color: 255; 255; 255
6060
Max Intensity: 255
@@ -71,7 +71,7 @@ Visualization Manager:
7171
Unreliable: false
7272
Use Fixed Frame: true
7373
Use rainbow: true
74-
Value: false
74+
Value: true
7575
- Alpha: 1
7676
Axes Length: 1
7777
Axes Radius: 0.10000000149011612

scanmatcher/rviz/mapping.rviz

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
Panels:
2+
- Class: rviz_common/Displays
3+
Help Height: 78
4+
Name: Displays
5+
Property Tree Widget:
6+
Expanded:
7+
- /Global Options1
8+
- /Status1
9+
Splitter Ratio: 0.5
10+
Tree Height: 613
11+
- Class: rviz_common/Selection
12+
Name: Selection
13+
- Class: rviz_common/Tool Properties
14+
Expanded:
15+
- /2D Nav Goal1
16+
- /Publish Point1
17+
Name: Tool Properties
18+
Splitter Ratio: 0.5886790156364441
19+
- Class: rviz_common/Views
20+
Expanded:
21+
- /Current View1
22+
Name: Views
23+
Splitter Ratio: 0.5
24+
Visualization Manager:
25+
Class: ""
26+
Displays:
27+
- Alpha: 0.5
28+
Cell Size: 10
29+
Class: rviz_default_plugins/Grid
30+
Color: 160; 160; 164
31+
Enabled: true
32+
Line Style:
33+
Line Width: 0.029999999329447746
34+
Value: Lines
35+
Name: Grid
36+
Normal Cell Count: 0
37+
Offset:
38+
X: 0
39+
Y: 0
40+
Z: 0
41+
Plane: XY
42+
Plane Cell Count: 25
43+
Reference Frame: <Fixed Frame>
44+
Value: true
45+
- Alpha: 1
46+
Autocompute Intensity Bounds: true
47+
Autocompute Value Bounds:
48+
Max Value: 10
49+
Min Value: -10
50+
Value: true
51+
Axis: Z
52+
Channel Name: intensity
53+
Class: rviz_default_plugins/PointCloud2
54+
Color: 255; 255; 255
55+
Color Transformer: Intensity
56+
Decay Time: 0
57+
Enabled: true
58+
Invert Rainbow: false
59+
Max Color: 255; 255; 255
60+
Max Intensity: 255
61+
Min Color: 0; 0; 0
62+
Min Intensity: 4
63+
Name: PointCloud2
64+
Position Transformer: XYZ
65+
Queue Size: 10
66+
Selectable: false
67+
Size (Pixels): 3
68+
Size (m): 0.009999999776482582
69+
Style: Flat Squares
70+
Topic: /map
71+
Unreliable: false
72+
Use Fixed Frame: true
73+
Use rainbow: true
74+
Value: true
75+
- Alpha: 1
76+
Axes Length: 1
77+
Axes Radius: 0.10000000149011612
78+
Class: rviz_default_plugins/Pose
79+
Color: 0; 255; 0
80+
Enabled: true
81+
Head Length: 0.30000001192092896
82+
Head Radius: 0.10000000149011612
83+
Name: Pose
84+
Shaft Length: 1
85+
Shaft Radius: 0.05000000074505806
86+
Shape: Arrow
87+
Topic: /current_pose
88+
Unreliable: false
89+
Value: true
90+
- Alpha: 1
91+
Buffer Length: 1
92+
Class: rviz_default_plugins/Path
93+
Color: 255; 255; 0
94+
Enabled: true
95+
Head Diameter: 0.30000001192092896
96+
Head Length: 0.20000000298023224
97+
Length: 0.30000001192092896
98+
Line Style: Lines
99+
Line Width: 0.029999999329447746
100+
Name: Path
101+
Offset:
102+
X: 0
103+
Y: 0
104+
Z: 0
105+
Pose Color: 255; 85; 255
106+
Pose Style: None
107+
Radius: 0.029999999329447746
108+
Shaft Diameter: 0.10000000149011612
109+
Shaft Length: 0.10000000149011612
110+
Topic: /path
111+
Unreliable: false
112+
Value: true
113+
Enabled: true
114+
Global Options:
115+
Background Color: 48; 48; 48
116+
Fixed Frame: map
117+
Frame Rate: 30
118+
Name: root
119+
Tools:
120+
- Class: rviz_default_plugins/MoveCamera
121+
- Class: rviz_default_plugins/Select
122+
- Class: rviz_default_plugins/FocusCamera
123+
- Class: rviz_default_plugins/Measure
124+
Line color: 128; 128; 0
125+
- Class: rviz_default_plugins/SetInitialPose
126+
Topic: /initialpose
127+
- Class: rviz_default_plugins/SetGoal
128+
Topic: /move_base_simple/goal
129+
- Class: rviz_default_plugins/PublishPoint
130+
Single click: true
131+
Topic: /clicked_point
132+
Transformation:
133+
Current:
134+
Class: rviz_default_plugins/TF
135+
Value: true
136+
Views:
137+
Current:
138+
Class: rviz_default_plugins/Orbit
139+
Distance: 76.20225524902344
140+
Enable Stereo Rendering:
141+
Stereo Eye Separation: 0.05999999865889549
142+
Stereo Focal Distance: 1
143+
Swap Stereo Eyes: false
144+
Value: false
145+
Focal Point:
146+
X: 0.2535023093223572
147+
Y: -7.471435070037842
148+
Z: 2.8620872497558594
149+
Focal Shape Fixed Size: true
150+
Focal Shape Size: 0.05000000074505806
151+
Invert Z Axis: false
152+
Name: Current View
153+
Near Clip Distance: 0.009999999776482582
154+
Pitch: 0.2347976267337799
155+
Target Frame: <Fixed Frame>
156+
Value: Orbit (rviz)
157+
Yaw: 1.3105117082595825
158+
Saved: ~
159+
Window Geometry:
160+
Displays:
161+
collapsed: false
162+
Height: 842
163+
Hide Left Dock: false
164+
Hide Right Dock: false
165+
QMainWindow State: 000000ff00000000fd000000040000000000000156000002f0fc0200000008fb0000001200530065006c0065006300740069006f006e00000001e10000009b0000005c00fffffffb0000001e0054006f006f006c002000500072006f007000650072007400690065007302000001ed000001df00000185000000a3fb000000120056006900650077007300200054006f006f02000001df000002110000018500000122fb000000200054006f006f006c002000500072006f0070006500720074006900650073003203000002880000011d000002210000017afb000000100044006900730070006c006100790073010000003d000002f0000000c900fffffffb0000002000730065006c0065006300740069006f006e00200062007500660066006500720200000138000000aa0000023a00000294fb00000014005700690064006500530074006500720065006f02000000e6000000d2000003ee0000030bfb0000000c004b0069006e0065006300740200000186000001060000030c000002610000000100000100000002f0fc0200000003fb0000001e0054006f006f006c002000500072006f00700065007200740069006500730100000041000000780000000000000000fb0000000a00560069006500770073010000003d000002f0000000a400fffffffb0000001200530065006c0065006300740069006f006e010000025a000000b200000000000000000000000200000490000000a9fc0100000001fb0000000a00560069006500770073030000004e00000080000002e10000019700000003000004420000003efc0100000002fb0000000800540069006d00650100000000000004420000000000000000fb0000000800540069006d006501000000000000045000000000000000000000024f000002f000000004000000040000000800000008fc0000000100000002000000010000000a0054006f006f006c00730100000000ffffffff0000000000000000
166+
Selection:
167+
collapsed: false
168+
Tool Properties:
169+
collapsed: false
170+
Views:
171+
collapsed: false
172+
Width: 1201
173+
X: 632
174+
Y: 79

scanmatcher/src/scanmatcher_component.cpp

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,10 @@ void ScanMatcherComponent::receiveCloud(
278278
{
279279
if (mapping_flag_ && mapping_future_.valid()) {
280280
auto status = mapping_future_.wait_for(0s);
281-
if (status == std::future_status::ready) {
281+
if (status == std::future_status::ready) {
282282
if (is_map_updated_ == true) {
283-
pcl::PointCloud<pcl::PointXYZI>::Ptr targeted_cloud_ptr(new pcl::PointCloud<pcl::PointXYZI>(targeted_cloud_));
283+
pcl::PointCloud<pcl::PointXYZI>::Ptr targeted_cloud_ptr(new pcl::PointCloud<pcl::PointXYZI>(
284+
targeted_cloud_));
284285
registration_->setInputTarget(targeted_cloud_ptr);
285286
is_map_updated_ = false;
286287
}
@@ -402,7 +403,9 @@ void ScanMatcherComponent::publishMapAndPose(
402403
geometry_msgs::msg::PoseStamped corrent_pose_stamped;
403404
corrent_pose_stamped = corrent_pose_stamped_;
404405
previous_position_ = position;
405-
mapping_task_ = std::packaged_task<void()>(std::bind(&ScanMatcherComponent::updateMap, this, cloud_ptr, final_transformation, corrent_pose_stamped));
406+
mapping_task_ =
407+
std::packaged_task<void()>(std::bind(&ScanMatcherComponent::updateMap, this, cloud_ptr,
408+
final_transformation, corrent_pose_stamped));
406409
mapping_future_ = mapping_task_.get_future();
407410
mapping_thread_ = std::thread(std::move(std::ref(mapping_task_)));
408411
mapping_flag_ = true;
@@ -413,49 +416,51 @@ void ScanMatcherComponent::updateMap(
413416
const pcl::PointCloud<pcl::PointXYZI>::ConstPtr cloud_ptr,
414417
Eigen::Matrix4f final_transformation, geometry_msgs::msg::PoseStamped corrent_pose_stamped)
415418
{
419+
pcl::PointCloud<pcl::PointXYZI>::Ptr tmp_ptr(new pcl::PointCloud<pcl::PointXYZI>());
420+
pcl::VoxelGrid<pcl::PointXYZI> voxel_grid;
421+
voxel_grid.setLeafSize(vg_size_for_map_, vg_size_for_map_, vg_size_for_map_);
422+
voxel_grid.setInputCloud(cloud_ptr);
423+
voxel_grid.filter(*tmp_ptr);
424+
425+
pcl::PointCloud<pcl::PointXYZI>::Ptr transformed_cloud_ptr(new pcl::PointCloud<pcl::PointXYZI>());
426+
pcl::transformPointCloud(*tmp_ptr, *transformed_cloud_ptr, final_transformation);
427+
428+
map_ += *transformed_cloud_ptr;
429+
430+
targeted_cloud_.clear();
431+
targeted_cloud_ += *transformed_cloud_ptr;
432+
int num_submaps = map_array_msg_.submaps.size();
433+
for (int i = 0; i < num_targeted_cloud_ - 1; i++) {
434+
if (num_submaps - 1 - i < 0) {continue;}
416435
pcl::PointCloud<pcl::PointXYZI>::Ptr tmp_ptr(new pcl::PointCloud<pcl::PointXYZI>());
417-
pcl::VoxelGrid<pcl::PointXYZI> voxel_grid;
418-
voxel_grid.setLeafSize(vg_size_for_map_, vg_size_for_map_, vg_size_for_map_);
419-
voxel_grid.setInputCloud(cloud_ptr);
420-
voxel_grid.filter(*tmp_ptr);
421-
422-
pcl::PointCloud<pcl::PointXYZI>::Ptr transformed_cloud_ptr(new pcl::PointCloud<pcl::PointXYZI>());
423-
pcl::transformPointCloud(*tmp_ptr, *transformed_cloud_ptr, final_transformation);
424-
425-
map_ += *transformed_cloud_ptr;
426-
427-
targeted_cloud_.clear();
428-
targeted_cloud_ += *transformed_cloud_ptr;
429-
int num_submaps = map_array_msg_.submaps.size();
430-
for (int i = 0; i < num_targeted_cloud_ - 1; i++) {
431-
if (num_submaps - 1 - i < 0) {continue;}
432-
pcl::PointCloud<pcl::PointXYZI>::Ptr tmp_ptr(new pcl::PointCloud<pcl::PointXYZI>());
433-
pcl::fromROSMsg(map_array_msg_.submaps[num_submaps - 1 - i].cloud, *tmp_ptr);
434-
targeted_cloud_ += *tmp_ptr;
435-
}
436+
pcl::fromROSMsg(map_array_msg_.submaps[num_submaps - 1 - i].cloud, *tmp_ptr);
437+
targeted_cloud_ += *tmp_ptr;
438+
}
436439

437-
/* map array */
438-
sensor_msgs::msg::PointCloud2::Ptr transformed_cloud_msg_ptr(new sensor_msgs::msg::PointCloud2);
439-
pcl::toROSMsg(*transformed_cloud_ptr, *transformed_cloud_msg_ptr);
440-
441-
lidarslam_msgs::msg::SubMap submap;
442-
submap.header.frame_id = global_frame_id_;
443-
submap.header.stamp = corrent_pose_stamped.header.stamp;
444-
latest_distance_ += trans_;
445-
submap.distance = latest_distance_;
446-
submap.pose = corrent_pose_stamped.pose;
447-
submap.cloud = *transformed_cloud_msg_ptr;
448-
submap.cloud.header.frame_id = global_frame_id_;
449-
map_array_msg_.header.stamp = corrent_pose_stamped.header.stamp;
450-
map_array_msg_.submaps.push_back(submap);
451-
map_array_pub_->publish(map_array_msg_);
452-
453-
is_map_updated_ = true;
454-
455-
rclcpp::Time map_time = clock_.now();
456-
double dt = map_time.seconds() - last_map_time_.seconds();
457-
if (dt > map_publish_period_) publishMap();
440+
/* map array */
441+
sensor_msgs::msg::PointCloud2::Ptr transformed_cloud_msg_ptr(new sensor_msgs::msg::PointCloud2);
442+
pcl::toROSMsg(*transformed_cloud_ptr, *transformed_cloud_msg_ptr);
443+
444+
lidarslam_msgs::msg::SubMap submap;
445+
submap.header.frame_id = global_frame_id_;
446+
submap.header.stamp = corrent_pose_stamped.header.stamp;
447+
latest_distance_ += trans_;
448+
submap.distance = latest_distance_;
449+
submap.pose = corrent_pose_stamped.pose;
450+
submap.cloud = *transformed_cloud_msg_ptr;
451+
submap.cloud.header.frame_id = global_frame_id_;
452+
map_array_msg_.header.stamp = corrent_pose_stamped.header.stamp;
453+
map_array_msg_.submaps.push_back(submap);
454+
map_array_pub_->publish(map_array_msg_);
455+
456+
is_map_updated_ = true;
457+
458+
rclcpp::Time map_time = clock_.now();
459+
double dt = map_time.seconds() - last_map_time_.seconds();
460+
if (dt > map_publish_period_) {
461+
publishMap();
458462
last_map_time_ = map_time;
463+
}
459464
}
460465

461466
Eigen::Matrix4f ScanMatcherComponent::getTransformation(geometry_msgs::msg::Pose pose)
@@ -501,6 +506,7 @@ void ScanMatcherComponent::receiveOdom(const nav_msgs::msg::Odometry odom_msg)
501506

502507
void ScanMatcherComponent::publishMap()
503508
{
509+
RCLCPP_INFO(get_logger(), "publish a map");
504510
pcl::PointCloud<pcl::PointXYZI>::Ptr map_ptr(new pcl::PointCloud<pcl::PointXYZI>(map_));
505511
sensor_msgs::msg::PointCloud2::Ptr map_msg_ptr(new sensor_msgs::msg::PointCloud2);
506512
pcl::toROSMsg(*map_ptr, *map_msg_ptr);

0 commit comments

Comments
 (0)