1
1
#! /usr/bin/env python3
2
2
3
+ import array
3
4
import copy
4
5
from functools import cache
6
+ import cv2
5
7
import numpy as np
6
8
import threading
7
9
@@ -691,13 +693,14 @@ def publish_camera_and_lidar(self, current_time: TimeMsg | None = None):
691
693
)
692
694
self .camera_info_publishers [camera .name ].publish (camera_info )
693
695
694
- if not camera .is_depth :
696
+ if camera .is_depth :
697
+ ros_image_compressed = compress_depth_image (frame )
698
+ else :
695
699
ros_image_compressed : CompressedImage = (
696
- self .bridge .cv2_to_compressed_imgmsg (frame )
697
- )
698
- self .camera_compressed_publishers [camera .name ].publish (
699
- ros_image_compressed
700
+ self .bridge .cv2_to_compressed_imgmsg (frame , "png" )
700
701
)
702
+ ros_image_compressed .header .frame_id = get_camera_frame (camera )
703
+ self .camera_compressed_publishers [camera .name ].publish (ros_image_compressed )
701
704
702
705
if camera .is_depth :
703
706
if camera == StretchCameras .cam_d405_depth :
@@ -903,7 +906,7 @@ def get_joint_states_callback(self, request, response):
903
906
min_limit , max_limit = min_max
904
907
if actuator == Actuators .arm :
905
908
joint_name = "joint_arm" # Instead of the telescoping names
906
- max_limit *= 4 # 4x the telescoping limit
909
+ max_limit *= 4 # 4x the telescoping limit
907
910
if actuator == Actuators .gripper :
908
911
joint_name = "gripper_aperture" # A different mapping from stretch_core command_groups
909
912
if actuator in [
@@ -987,6 +990,7 @@ def stow_the_robot(self):
987
990
988
991
def is_runstopped (self ):
989
992
return self .robot_mode == "runstopped"
993
+
990
994
def runstop_the_robot (self , runstopped , just_change_mode = False ):
991
995
if runstopped :
992
996
self .robot_mode_rwlock .acquire_read ()
@@ -1054,7 +1058,6 @@ def ros_setup(self):
1054
1058
"controller_calibration_file" ,
1055
1059
str (stretch_core_path / "config" / "controller_calibration_head.yaml" ),
1056
1060
)
1057
-
1058
1061
# large_ang = np.radians(45.0)
1059
1062
# filename = self.get_parameter('controller_calibration_file').value
1060
1063
# self.get_logger().debug('Loading controller calibration parameters for the head from YAML file named {0}'.format(filename))
@@ -1113,14 +1116,16 @@ def ros_setup(self):
1113
1116
1114
1117
self .odom_pub = self .create_publisher (Odometry , "odom" , 1 )
1115
1118
self .laser_scan_pub = self .create_publisher (
1116
- LaserScan , "/scan_filtered" , qos_profile = QoSProfile (
1117
- depth = 1 , reliability = ReliabilityPolicy . BEST_EFFORT
1118
- )
1119
+ LaserScan ,
1120
+ "/scan_filtered" ,
1121
+ qos_profile = QoSProfile ( depth = 1 , reliability = ReliabilityPolicy . BEST_EFFORT ),
1119
1122
)
1120
1123
1121
1124
self .camera_publishers = {
1122
1125
camera .name : self .create_publisher (
1123
- Image , get_camera_topic_name (camera ), qos_profile = QoSProfile (
1126
+ Image ,
1127
+ get_camera_topic_name (camera ),
1128
+ qos_profile = QoSProfile (
1124
1129
depth = 1 , reliability = ReliabilityPolicy .BEST_EFFORT
1125
1130
),
1126
1131
)
@@ -1135,11 +1140,11 @@ def ros_setup(self):
1135
1140
),
1136
1141
)
1137
1142
for camera in self .sim ._cameras_to_use
1138
- if not camera .is_depth
1139
1143
}
1140
1144
self .pointcloud_publishers = {
1141
1145
camera .name : self .create_publisher (
1142
- PointCloud2 , get_camera_pointcloud_topic_name (camera ),
1146
+ PointCloud2 ,
1147
+ get_camera_pointcloud_topic_name (camera ),
1143
1148
qos_profile = QoSProfile (
1144
1149
depth = 1 , reliability = ReliabilityPolicy .BEST_EFFORT
1145
1150
),
@@ -1149,14 +1154,15 @@ def ros_setup(self):
1149
1154
}
1150
1155
self .camera_info_publishers = {
1151
1156
camera .name : self .create_publisher (
1152
- CameraInfo , get_camera_info_topic_name (camera ),
1157
+ CameraInfo ,
1158
+ get_camera_info_topic_name (camera ),
1153
1159
qos_profile = QoSProfile (
1154
1160
depth = 1 , reliability = ReliabilityPolicy .BEST_EFFORT
1155
1161
),
1156
1162
)
1157
1163
for camera in self .sim ._cameras_to_use
1158
1164
}
1159
-
1165
+
1160
1166
self .clock_pub = self .create_publisher (
1161
1167
msg_type = Clock , topic = "/clock" , qos_profile = 5
1162
1168
)
@@ -1463,8 +1469,28 @@ def create_pointcloud_rgb_msg(
1463
1469
return cloud_msg
1464
1470
1465
1471
1472
+ __COMPRESSED_DEPTH_16UC1_HEADER = array .array ("B" , [0 ] * 12 )
1473
+
1474
+
1475
+ def compress_depth_image (frame : np .ndarray ):
1476
+ """
1477
+ Converts a F32 depth map in meters to a U16 map in millimeters
1478
+ """
1479
+ normalized_array = (frame * 1000 ).astype (np .uint16 )
1480
+
1481
+ _ , encoded_image = cv2 .imencode (".png" , normalized_array )
1482
+
1483
+ ros_image_compressed = CompressedImage ()
1484
+ ros_image_compressed .format = "16uc1; compressedDepth"
1485
+ ros_image_compressed .data = __COMPRESSED_DEPTH_16UC1_HEADER + array .array (
1486
+ "B" , encoded_image .tobytes ()
1487
+ )
1488
+
1489
+ return ros_image_compressed
1490
+
1491
+
1466
1492
def create_camera_info (
1467
- camera_settings :CameraSettings , frame_id : str , timestamp : TimeMsg
1493
+ camera_settings : CameraSettings , frame_id : str , timestamp : TimeMsg
1468
1494
):
1469
1495
camera_info_msg = CameraInfo ()
1470
1496
camera_info_msg .header = Header ()
@@ -1474,7 +1500,6 @@ def create_camera_info(
1474
1500
camera_info_msg .height = camera_settings .height
1475
1501
camera_info_msg .distortion_model = "plumb_bob"
1476
1502
1477
-
1478
1503
camera_info_msg .d = camera_settings .get_distortion_params_d ()
1479
1504
camera_info_msg .k = camera_settings .get_intrinsic_params_k ()
1480
1505
camera_info_msg .p = camera_settings .get_projection_matrix_p ()
@@ -1506,6 +1531,7 @@ def get_camera_topic_name(camera: StretchCameras):
1506
1531
1507
1532
raise NotImplementedError (f"Camera { camera } topic mapping is not implemented" )
1508
1533
1534
+
1509
1535
@cache
1510
1536
def get_camera_info_topic_name (camera : StretchCameras ):
1511
1537
"""
@@ -1524,6 +1550,7 @@ def get_camera_info_topic_name(camera: StretchCameras):
1524
1550
1525
1551
raise NotImplementedError (f"Camera { camera } topic mapping is not implemented" )
1526
1552
1553
+
1527
1554
@cache
1528
1555
def get_camera_pointcloud_topic_name (camera : StretchCameras ):
1529
1556
"""
0 commit comments