Skip to content

Commit 4f3e753

Browse files
authored
Publish top camera on /image_raw ros2 topic
1 parent d57aa90 commit 4f3e753

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

Binary file not shown.
31.4 KB
Binary file not shown.

controllers/nao_lola_python/nao_lola_python.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@
66
import umsgpack
77
from imageserver import ImageServer, CamIndex
88

9+
import rclpy
10+
from rclpy.node import Node
11+
from sensor_msgs.msg import Image
12+
from cv_bridge import CvBridge
13+
import numpy as np
14+
15+
class CameraPublisher(Node):
16+
def __init__(self):
17+
super().__init__('camera_publisher')
18+
self.publisher_ = self.create_publisher(Image, '/image_raw', 10)
19+
self.bridge = CvBridge()
20+
21+
def publish_image(self, image_data, width, height):
22+
image_np = np.frombuffer(image_data, dtype=np.uint8).reshape((height, width, 4))
23+
image_np = image_np[:, :, :3]
24+
image_msg = self.bridge.cv2_to_imgmsg(image_np, encoding="bgr8")
25+
image_msg.header.frame_id = "camera_top_simulated_nao"
26+
image_msg.header.stamp = self.get_clock().now().to_msg()
27+
28+
self.publisher_.publish(image_msg)
29+
930
class Nao (Robot):
1031
SOCK_PATH = "/tmp/robocup"
1132
TCP_BASE_PORT = 10000
@@ -405,6 +426,9 @@ def print_status(self):
405426
def __init__(self):
406427
Robot.__init__(self)
407428

429+
rclpy.init()
430+
self.camera_publisher = CameraPublisher()
431+
408432
self.tick = 0
409433

410434
self.parse_args();
@@ -448,7 +472,7 @@ def run(self):
448472
conn = None
449473
imgCounter = self.frametime
450474

451-
while True:
475+
while rclpy.ok():
452476
if self.stepBegin(self.timeStep) == -1:
453477
break
454478

@@ -475,9 +499,15 @@ def run(self):
475499
# send images on average every self.frametime milliseconds in simulation time
476500
imgCounter -= self.timeStep
477501
if self.args.camera and imgCounter <= 0:
478-
self.topImageServer.send(self.tick, self.cameraTop.getImage())
502+
image_data_top = self.cameraTop.getImage()
503+
width_top = self.cameraTop.getWidth()
504+
height_top = self.cameraTop.getHeight()
505+
self.topImageServer.send(self.tick, image_data_top)
479506
self.bottomImageServer.send(self.tick, self.cameraBottom.getImage())
480507
imgCounter += self.frametime
508+
509+
# ROS publisher
510+
self.camera_publisher.publish_image(image_data_top, width_top, height_top)
481511
else:
482512
try:
483513
(conn, addr) = sock.accept()
@@ -496,6 +526,7 @@ def run(self):
496526

497527
# remove socket on exit
498528
os.unlink(self.SOCK_PATH)
529+
rclpy.shutdown()
499530

500531
# stop image threads
501532
if self.args.camera:

0 commit comments

Comments
 (0)