ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Finally, I built a custom message as follows:
sensor_msgs/Image[ 2 ] data
where batch size is 2.
Then, add it to my publisher as follows:
#!/usr/bin/env python3
# Revision $Id$
import rclpy
from rclpy.node import Node
from custom_batch.msg import Batch #### custom message
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
import cv2
import numpy as np
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(Batch, 'Image', 10) ###### here
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0
self.im_list = []
self.cv_image1 = cv2.imread('3.jpg')
self.cv_image2 = cv2.imread('2.jpg')
self.bridge = CvBridge()
def timer_callback(self):
#### custom message
my_msg = Batch()
my_msg.data[0] = self.bridge.cv2_to_imgmsg(np.array(self.cv_image1), "bgr8")
my_msg.data[1] = self.bridge.cv2_to_imgmsg(np.array(self.cv_image2), "bgr8")
#####
self.publisher_.publish(my_msg) ## custom message
self.get_logger().info('Publishing a perfect pistachio ')
def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
minimal_publisher.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()