How can I publish exactly once when the node is run?
I want to publish a single /initialpose message when a node is run. How can I make sure it's only published once?
This is what I have:
#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Pose, PoseWithCovarianceStamped
from std_msgs.msg import Header
from numpy import matrix
class PoseChange:
def __init__(self):
self.msg = PoseWithCovarianceStamped()
rospy.init_node("pose_change", anonymous=True)
rospy.Subscriber('/pose', Pose, self.callback)
self.pub = rospy.Publisher('/initialpose', PoseWithCovarianceStamped, queue_size=3)
def callback(self, data):
self.msg.header = Header()
self.msg.header.stamp = rospy.Time.now()
self.msg.header.frame_id = "map"
self.msg.pose.pose = data
self.msg.pose.covariance = matrix([[0.09, 0, 0, 0, 0, 0],
[0, 0.09, 0, 0, 0, 0],
[0, 0, 0.09, 0, 0, 0],
[0, 0, 0, 0.25, 0, 0],
[0, 0, 0, 0, 0.25, 0],
[0, 0, 0, 0, 0, 0.25]])
def run(self):
self.pub.publish(self.msg)
if __name__ == "__main__":
try:
m = PoseChange()
m.run()
except rospy.ROSInterruptException:
raise e
However, this never publishes when run. The /pose topic it's listening to is continuously publishing at 50 Hz.