How to publish a message when the condition is met for the first time in python?
I want a node to subscribe /scan. And when the minimum of the range data is smaller than 0.5, the node publishes "hello" to /comm. Only the condition is met for the first time, the message is published. Then no message will be published.
import rospy from sensor_msgs.msg import LaserScan from std_msgs.msg import String
def callback(data): msg= min(data.ranges) if msg<0.2: rospy.loginfo(str) pub.publish(str) condition= True else: pass
rospy.init_node('detect') sub = rospy.Subscriber('/tb3_1/scan', LaserScan, callback) #We subscribe to the laser's topic pub = rospy.Publisher('/comm', String,queue_size=1)
str="hello"
msg=float
ctrl_c=True
condition = False
rospy.spin()