Skip to content

Commit ed9b05f

Browse files
Merge pull request #36 from MapIV/fix/ros1_ros2_rosbags_support
Fix/ros1 ros2 rosbags support
2 parents b0cc152 + b3069e1 commit ed9b05f

2 files changed

Lines changed: 41 additions & 18 deletions

File tree

src/pypcd4/pypcd4.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -616,32 +616,44 @@ def to_msg(self, header: Optional["Header"] = None) -> "PointCloud2":
616616
"""
617617
ROS_MSG_AVAILABLE = False
618618
try:
619-
from builtin_interfaces.msg import Time
620619
from sensor_msgs.msg import PointCloud2, PointField
621620
from std_msgs.msg import Header
622621

623622
ROS_MSG_AVAILABLE = True
623+
try:
624+
from builtin_interfaces.msg import Time # ROS2
625+
except ImportError:
626+
from std_msgs.msg import Time # ROS1
624627
except ImportError:
625628
pass
626-
try:
627-
from rosbags.typesys.stores.latest import builtin_interfaces__msg__Time as Time
628-
from rosbags.typesys.stores.latest import sensor_msgs__msg__PointCloud2 as PointCloud2
629-
from rosbags.typesys.stores.latest import sensor_msgs__msg__PointField as PointField
630-
from rosbags.typesys.stores.latest import std_msgs__msg__Header as Header
631629

632-
ROS_MSG_AVAILABLE = True
633-
except ImportError:
634-
pass
630+
if not ROS_MSG_AVAILABLE:
631+
try:
632+
# Fallback to rosbags
633+
from rosbags.typesys.stores.latest import (
634+
builtin_interfaces__msg__Time as Time,
635+
)
636+
from rosbags.typesys.stores.latest import (
637+
sensor_msgs__msg__PointCloud2 as PointCloud2,
638+
)
639+
from rosbags.typesys.stores.latest import (
640+
sensor_msgs__msg__PointField as PointField,
641+
)
642+
from rosbags.typesys.stores.latest import (
643+
std_msgs__msg__Header as Header,
644+
)
645+
646+
ROS_MSG_AVAILABLE = True
647+
except ImportError:
648+
pass
635649

636650
if not ROS_MSG_AVAILABLE:
637651
raise ImportError(
638-
"The PointCloud.to_msg() method requires ROS sensor_msgs or rosbags installed.\n"
639-
"Please install:\n"
640-
" - ROS/ROS2 as appropriate (check official documentation), or\n"
641-
" - rosbags via `pip install rosbags`. \n"
642-
" Rosbags can be used to write to bags but not .publish(msg)"
652+
"The PointCloud.to_msg() method requires ROS `sensor_msgs` or the `rosbags` pkg.\n"
653+
"Please install either:\n"
654+
" - ROS/ROS2 (check official documentation), or\n"
655+
" - rosbags via `pip install rosbags`."
643656
)
644-
645657
fields = []
646658
itemsize = 0
647659
row_step = 0
@@ -662,10 +674,16 @@ def to_msg(self, header: Optional["Header"] = None) -> "PointCloud2":
662674
)
663675
offset += type_.itemsize * count
664676

665-
data = self.pc_data.tobytes()
677+
data = self.pc_data.reshape(-1).view(np.uint8)
678+
679+
if header is None:
680+
try:
681+
header = Header(stamp=Time(sec=0, nanosec=0), frame_id="")
682+
except (TypeError, AttributeError):
683+
header = Header(stamp=Time(0), frame_id="", seq=0)
666684

667685
msg = PointCloud2(
668-
header=Header(stamp=Time(sec=0, nanosec=0), frame_id="") if header is None else header,
686+
header=header,
669687
height=1,
670688
width=self.points,
671689
is_dense=False,

tests/test/test_pypcd4.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,4 +961,9 @@ def test_list_pointcloud():
961961
concatenated_pc = PointCloud.from_list(pc_list)
962962
assert concatenated_pc.points == pc.points * num_pcs
963963
assert concatenated_pc.fields == pc.fields
964-
assert concatenated_pc.types == pc.types
964+
assert concatenated_pc.types == pc.types
965+
966+
if __name__ == "__main__":
967+
print("Testing PointCloud msg functionality...")
968+
test_pointcloud_tomsg()
969+
print("Msg test passed!")

0 commit comments

Comments
 (0)