Skip to content

Commit d6bf7d0

Browse files
committed
fixed save point cloud
1 parent 9ef1150 commit d6bf7d0

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

orbbec_camera/include/orbbec_camera/utils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ sensor_msgs::msg::CameraInfo convertToCameraInfo(OBCameraIntrinsic intrinsic,
3434

3535
void saveRGBPointsToPly(const std::shared_ptr<ob::Frame>& frame, const std::string& fileName);
3636

37-
void soavePointCloudMsgToPly(const sensor_msgs::msg::PointCloud2& msg, const std::string& fileName);
37+
void saveRGBPointCloudMsgToPly(const sensor_msgs::msg::PointCloud2& msg, const std::string& fileName);
38+
39+
void saveDepthPointsToPly(const sensor_msgs::msg::PointCloud2& msg, const std::string& fileName);
3840

3941
void savePointsToPly(const std::shared_ptr<ob::Frame>& frame, const std::string& fileName);
4042

orbbec_camera/launch/astra.launch.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def generate_launch_description():
2323
DeclareLaunchArgument('connection_delay', default_value='100'),
2424
DeclareLaunchArgument('color_width', default_value='640'),
2525
DeclareLaunchArgument('color_height', default_value='480'),
26-
DeclareLaunchArgument('color_fps', default_value='30'),
26+
DeclareLaunchArgument('color_fps', default_value='10'),
2727
DeclareLaunchArgument('color_format', default_value='RGB'),
2828
DeclareLaunchArgument('enable_color', default_value='true'),
2929
DeclareLaunchArgument('flip_color', default_value='false'),
@@ -32,15 +32,15 @@ def generate_launch_description():
3232
DeclareLaunchArgument('enable_color_auto_exposure', default_value='true'),
3333
DeclareLaunchArgument('depth_width', default_value='640'),
3434
DeclareLaunchArgument('depth_height', default_value='480'),
35-
DeclareLaunchArgument('depth_fps', default_value='30'),
35+
DeclareLaunchArgument('depth_fps', default_value='10'),
3636
DeclareLaunchArgument('depth_format', default_value='Y11'),
3737
DeclareLaunchArgument('enable_depth', default_value='true'),
3838
DeclareLaunchArgument('flip_depth', default_value='false'),
3939
DeclareLaunchArgument('depth_qos', default_value='default'),
4040
DeclareLaunchArgument('depth_camera_info_qos', default_value='default'),
4141
DeclareLaunchArgument('ir_width', default_value='640'),
4242
DeclareLaunchArgument('ir_height', default_value='480'),
43-
DeclareLaunchArgument('ir_fps', default_value='30'),
43+
DeclareLaunchArgument('ir_fps', default_value='10'),
4444
DeclareLaunchArgument('ir_format', default_value='Y10'),
4545
DeclareLaunchArgument('enable_ir', default_value='true'),
4646
DeclareLaunchArgument('flip_ir', default_value='false'),

orbbec_camera/launch/multi_camera.launch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ def generate_launch_description():
1212
launch_file_dir = os.path.join(package_dir, 'launch')
1313
launch1_include = IncludeLaunchDescription(
1414
PythonLaunchDescriptionSource(
15-
os.path.join(launch_file_dir, 'dabai_dcw.launch.py')
15+
os.path.join(launch_file_dir, 'astra.launch.py')
1616
),
1717
launch_arguments={
1818
'camera_name': 'camera_01',
19-
'usb_port': '5-3.4.4.3.1',
19+
'usb_port': '5-3.4.4.3',
2020
'device_num': '2'
2121
}.items()
2222
)
2323

2424
launch2_include = IncludeLaunchDescription(
2525
PythonLaunchDescriptionSource(
26-
os.path.join(launch_file_dir, 'dabai_dcw.launch.py')
26+
os.path.join(launch_file_dir, 'astra.launch.py')
2727
),
2828
launch_arguments={
2929
'camera_name': 'camera_02',
30-
'usb_port': '5-3.4.4.1.1',
30+
'usb_port': '5-3.4.4.1',
3131
'device_num': '2'
3232
}.items()
3333
)

orbbec_camera/src/ob_camera_node.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ void OBCameraNode::publishDepthPointCloud(const std::shared_ptr<ob::FrameSet> &f
623623
std::filesystem::create_directory(current_path + "/point_cloud");
624624
}
625625
RCLCPP_INFO_STREAM(logger_, "Saving point cloud to " << filename);
626-
soavePointCloudMsgToPly(point_cloud_msg_, filename);
626+
saveDepthPointsToPly(point_cloud_msg_, filename);
627627
}
628628
}
629629

@@ -733,7 +733,7 @@ void OBCameraNode::publishColoredPointCloud(const std::shared_ptr<ob::FrameSet>
733733
std::filesystem::create_directory(current_path + "/point_cloud");
734734
}
735735
RCLCPP_INFO_STREAM(logger_, "Saving point cloud to " << filename);
736-
soavePointCloudMsgToPly(point_cloud_msg_, filename);
736+
saveRGBPointCloudMsgToPly(point_cloud_msg_, filename);
737737
}
738738
}
739739

orbbec_camera/src/utils.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ void saveRGBPointsToPly(const std::shared_ptr<ob::Frame> &frame, const std::stri
8787
}
8888
}
8989

90-
void soavePointCloudMsgToPly(const sensor_msgs::msg::PointCloud2 &msg,
91-
const std::string &fileName) {
90+
void saveRGBPointCloudMsgToPly(const sensor_msgs::msg::PointCloud2 &msg,
91+
const std::string &fileName) {
9292
FILE *fp = fopen(fileName.c_str(), "wb+");
9393
CHECK_NOTNULL(fp);
9494

@@ -134,6 +134,45 @@ void soavePointCloudMsgToPly(const sensor_msgs::msg::PointCloud2 &msg,
134134
fclose(fp);
135135
}
136136

137+
void saveDepthPointsToPly(const sensor_msgs::msg::PointCloud2 &msg, const std::string &fileName) {
138+
FILE *fp = fopen(fileName.c_str(), "wb+");
139+
CHECK_NOTNULL(fp);
140+
141+
sensor_msgs::PointCloud2ConstIterator<float> iter_x(msg, "x");
142+
sensor_msgs::PointCloud2ConstIterator<float> iter_y(msg, "y");
143+
sensor_msgs::PointCloud2ConstIterator<float> iter_z(msg, "z");
144+
145+
// First, count the actual number of valid points
146+
size_t valid_points = 0;
147+
for (; iter_x != iter_x.end(); ++iter_x, ++iter_y, ++iter_z) {
148+
if (!std::isnan(*iter_x) && !std::isnan(*iter_y) && !std::isnan(*iter_z)) {
149+
++valid_points;
150+
}
151+
}
152+
153+
// Reset the iterators
154+
iter_x = sensor_msgs::PointCloud2ConstIterator<float>(msg, "x");
155+
iter_y = sensor_msgs::PointCloud2ConstIterator<float>(msg, "y");
156+
iter_z = sensor_msgs::PointCloud2ConstIterator<float>(msg, "z");
157+
158+
fprintf(fp, "ply\n");
159+
fprintf(fp, "format ascii 1.0\n");
160+
fprintf(fp, "element vertex %zu\n", valid_points);
161+
fprintf(fp, "property float x\n");
162+
fprintf(fp, "property float y\n");
163+
fprintf(fp, "property float z\n");
164+
fprintf(fp, "end_header\n");
165+
166+
for (; iter_x != iter_x.end(); ++iter_x, ++iter_y, ++iter_z) {
167+
if (!std::isnan(*iter_x) && !std::isnan(*iter_y) && !std::isnan(*iter_z)) {
168+
fprintf(fp, "%.3f %.3f %.3f\n", *iter_x, *iter_y, *iter_z);
169+
}
170+
}
171+
172+
fflush(fp);
173+
fclose(fp);
174+
}
175+
137176
void savePointsToPly(const std::shared_ptr<ob::Frame> &frame, const std::string &fileName) {
138177
size_t point_size = frame->dataSize() / sizeof(OBPoint);
139178
FILE *fp = fopen(fileName.c_str(), "wb+");

0 commit comments

Comments
 (0)