ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to access a ros message in a launch file?

asked 2023-08-01 09:48:13 -0500

ros5853 gravatar image

updated 2023-08-03 02:10:41 -0500

Hello Is it possible to access a ros message in a launch file and based on the value of the message publish to another topic? Moreor like a conditional statement. I checked online and seems no method to access the ros messages in a launch file.

    #!/usr/bin/env python3

import ros_compatibility as roscomp from ros_compatibility.node import CompatibleNode from ros_compatibility.qos import QoSProfile, DurabilityPolicy

from carla_msgs.msg import CarlaEgoVehicleStatus from ra_comm.msg import DriverWish import rospy

import sys

class RAssistCondition(CompatibleNode):

def __init__(self):
    super(RAssistCondition, self).__init__("ra_assist_condition")

    self.role_name = self.get_param("role_name", "ego_vehicle")
    self.driver_wish_pub = rospy.Publisher('/ra/core/smach/driver_wish', DriverWish, queue_size=10)

        qos_profile=QoSProfile(depth=1, durability=DurabilityPolicy.TRANSIENT_LOCAL))

    self.published = False

def racondition(self, vehicle_status):

    if not vehicle_status.velocity:  # pylint: disable=no-member
        self.logerr("Cannot determine the vehicle velocity")

    self.vehicle_velocity = vehicle_status.velocity
    self.logerr("Vehicle velocity info received. velocity={}".format(self.vehicle_velocity))

    if (self.vehicle_velocity > 0) and not self.published:
            self.published = True
            self.logerr("publishing driver wish")

            self.logerr("Vehicle velocity info received. velocity={}".format(self.vehicle_velocity))
            self.logerr("Error while publishing driver wish")

# publish function
def pubDriverWish(self):
    curr_ts =
    self.logerr("publishing driver wish")
    rospy.loginfo('[Display] Publish driver wish')
    msg = DriverWish()
    msg.Header.stamp = curr_ts
    msg.driver_wish = True

def main(args=None): """ main function

roscomp.init("ra_assit_condition", args)

ra_assit_condition = None


    ra_assit_condition = RAssistCondition()
    rospy.logerr("Condition for reverse assist")
except KeyboardInterrupt:
    if ra_assit_condition is not None:
        ra_assit_condition.logerr("Done, deleting twist to control")
        del ra_assit_condition

if __name__ == '__main__':

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2023-08-02 02:12:57 -0500

Jeremy Fix gravatar image


I would say this is not the way to proceed. A launch file is just an xml description of nodes to start, parameters to load, ... You cannot wait for a message or decide depending on the value.

It seems to me that what you want to achieve needs to write a node which can then listen for a message and decide whether or not to publish to another topic. This node you would write could be included in your launch.

edit flag offensive delete link more


Thanks for the answer. I am a begineer in ROS and my scenario is that I have to subscribe to a topic which has a message. Based on the message value i have to publish to an already existing topic. So in this scenario, what may be the best method to proceed? Create a node like you said ?

ros5853 gravatar image ros5853  ( 2023-08-02 02:25:07 -0500 )edit

Definitely, you must write a subscriber/publisher node. Either in C++ or python.

Have a look to the tutorials , these are really great resources, especially when you begin.

Jeremy Fix gravatar image Jeremy Fix  ( 2023-08-02 02:34:31 -0500 )edit

I used the above links to create a node in python as a package. I then used the node in a launch file and i can see it in the list of nodes. But the commands in the node is not executing. The commands are to publish to a topic. I also put all the loginfo but none of them is displaying in CLI. How does happen? The connection of the node in rqt is also the same as i needed. I added the code in the question.

ros5853 gravatar image ros5853  ( 2023-08-02 06:15:24 -0500 )edit

Oh, wait; Can you be more accurate on the exact versions of ROS you are using ?

You tagged it ROS1 but then you are using the ros_bridge ; Do you need ROS1 or are you running ROS2 ?

By the way, one comment on the submitted code : you should not create a new instance of rospy publisher in the callback racondtion but you should create it once for all in the constructor

Also, once you ran your code, did you try to submit a message to the /carla/ego_vehicle/vehicle_status topic from the command line ? Like "ros2 topic pub /carla/ego_vehicle/vehicle_status carla_msgs/msg/CarlaEgoVehicleStatus {'velocity': 1}"

Jeremy Fix gravatar image Jeremy Fix  ( 2023-08-02 13:32:09 -0500 )edit

Yes, It is working with ROS1. You mean to include the 'driver_wish_pub' in the constructor, rather than in the racondition? And where can i give the conditon to check whether to execute. I also edited the code little bit and uploaded.

Now I have one problem which i was not understanding:

The self.velocity_status checking condition is not followed. The messages are published even it is less than zero

ros5853 gravatar image ros5853  ( 2023-08-03 01:57:07 -0500 )edit

Question Tools



Asked: 2023-08-01 09:48:13 -0500

Seen: 57 times

Last updated: Aug 03 '23