Ask Your Question
0

ROS2 Image subscriber

asked 2020-09-06 04:08:48 -0500

Kreace gravatar image

Hello i'm pretty new to ROS2, i would like to make a simple Image subscriber of a ROS2 topic which come from a camera (which is on a drone) in a Gazebo simulation.

I'm using ROS2 foxy and Gazebo11.

First i launch the Gazebo simulator with : gzserver --verbose -s libgazebo_ros_factory.so in a first terminal. Then i spawn a model of a drone with : ros2 run gazebo_ros spawn_entity.py -entity TITI -x 0 -y 0 -z 0.5 -file ~/src_sitl_gazebo/sitl_gazebo/models/typhoon_h480/typhoon_h480.sdf I have modify the .sdf file in order to make it communicate with ROS2 here is the modification

<sensor name="camera" type="camera">
    <pose>0.0 0 -0.162 0 0 0</pose>
    <camera>
      <horizontal_fov>2.0</horizontal_fov>
      <image>
        <format>R8G8B8</format>
        <width>640</width>
        <height>360</height>
      </image>
      <clip>
        <near>0.05</near>
        <far>15000</far>
      </clip>
    </camera>
    <always_on>1</always_on>
    <update_rate>30</update_rate>
    <visualize>true</visualize>
    <plugin name="camera_controller" filename="libgazebo_ros_camera.so">
        <alwaysOn>true</alwaysOn>
        <updateRate>0.0</updateRate>
        <cameraName>camera</cameraName>
        <imageTopicName>image_raw</imageTopicName>
        <cameraInfoTopicName>camera_info</cameraInfoTopicName>
        <frameName>camera_link</frameName>
        <hackBaseline>0.07</hackBaseline>
        <distortionK1>0.0</distortionK1>
        <distortionK2>0.0</distortionK2>
        <distortionK3>0.0</distortionK3>
        <distortionT1>0.0</distortionT1>
        <distortionT2>0.0</distortionT2>
    </plugin>
  </sensor>

I tried to use the ROS2 migration syntax that I found here https://github.com/ros-simulation/gaz... but it gave me some errors.

After that I use :

ros2 topic list

and it gave me this output :

/camera/camera_info
/camera/image_raw
/clock
/parameter_events
/rosout

So I guess the Image Publisher is created but maybe I'm wrong there. Then I wrote a script which is a simple subscriber :

import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Image

class MinimalSubscriber(Node):

    def __init__(self):
        super().__init__('minimal_subscriber')
        self.subscription = self.create_subscription(
            Image,
            '/camera/image_raw',
            self.listener_callback,
            10)
        self.subscription  # prevent unused variable warning

    def listener_callback(self):
        print("In callback")

    def main(args=None):
        rclpy.init(args=args)
        minimal_subscriber = MinimalSubscriber()
        rclpy.spin(minimal_subscriber)
       # Destroy the node explicitly
       # (optional - otherwise it will be done automatically
       # when the garbage collector destroys the node object)
       minimal_subscriber.destroy_node()
       rclpy.shutdown()

if __name__ == '__main__':
    main()

So in the callback I just print a message to see if the callback is working but i don't receive any message in the terminal.

Thank you in advance for helping me feel free to tell me if you need additional information my post is already long i don't want to overload it.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2020-09-08 17:40:06 -0500

dhood gravatar image

I think you might be running into Quality of Service (QoS) incompatibilities.

As far as I know the gazebo plugin publishes with the "sensor data" QoS profile ("best effort" reliability), and your subscriber is using "reliable" reliability. If you try to echo the message with ros2 topic echo /camera/image_raw --no-arr and you manage to see the image data, then that confirms the issue is with your MinimalSubscriber.

You can switch to the sensor data QoS profile by changing your subscription creation to:

self.subscription = self.create_subscription(
    Image,
    '/camera/image_raw',
    self.listener_callback,
    qos_profile_sensor_data)

(Add from rclpy.qos import qos_profile_sensor_data to the top of your node to get that.)

edit flag offensive delete link more

Comments

Nice! That was the answer, thanks for your help dhood!

Kreace gravatar image Kreace  ( 2020-09-09 02:44:08 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2020-09-06 04:08:48 -0500

Seen: 999 times

Last updated: Sep 08 '20