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

How to publish the ros message only once to a topic?

asked 2023-08-02 07:27:44 -0500

ros5853 gravatar image

Hello I am a ros beginner and is creating a nod epackage to subscribe to a message and publish to another topic. But I was in a trouble and In my case I only need to publish the message only once. In the current code, it is publishing many times and causing problem. What change i have to make in the code so as to publish the message only once?

#!/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



class RAssistCondition(CompatibleNode):

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

        self.role_name = self.get_param("role_name", "ego_vehicle")

        self.new_subscription(
            CarlaEgoVehicleStatus,
            "/carla/{}/vehicle_status".format(self.role_name),
            self.racondition,
            qos_profile=QoSProfile(depth=1, durability=DurabilityPolicy.TRANSIENT_LOCAL))


    def racondition(self, vehicle_status):

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

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



        if self.vehicle_velocity > 0:
            try:
                self.driver_wish_pub = rospy.Publisher('/raaaaa/XXX/smach/driver_wish', DriverWish, queue_size=10)
                self.pubDriverWish()
                self.logerr("publishing driver wish")
            except:
                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 = rospy.Time.now()
        rospy.loginfo('[Display] Publish driver wish')
        msg = DriverWish()
        msg.Header.stamp = curr_ts
        msg.driver_wish = True
        self.driver_wish_pub.publish(msg)
        self.driver_wish_last_ts = curr_ts

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

    :return:
    """
    roscomp.init("ra_assit_condition", args)



    ra_assit_condition = None

    rospy.sleep(15)

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


if __name__ == '__main__':

    main()
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-08-02 13:24:13 -0500

dcconner gravatar image

Your racondition is a callback that is called every time it receives a vehicle status message. This then calls publish. If you only want to pub once (e.g. on first message), you could add a some flag that is initialized true in constructor, and then false after first publishing.

You should also define the publisher driver_wish_pub once in the constructor, not every time your racondition is called. Then just call publish driver_wish_pub.publish when you want to publish.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2023-08-02 07:27:44 -0500

Seen: 91 times

Last updated: Aug 02 '23