Rosanswers logo

I seem unable to find what the proper procedure is to fill a std_msgs/Header field in a message using python.

I have found this for C++:

my_message message; // using a std_msgs/Header called header
message.header.stamp = ros::Time::now();

This example builds its own timestamp, increases the sequence number (which was not predefined), and ignores the frame field. Is this correct, and, more importantly, what is the equivalent Python code?

Originally posted by RafBerkvens on ROS Answers with karma: 386 on 2013-04-08

Post score: 17

Original comments

Comment by joq on 2013-04-08:
You should not set the sequence field, it is set when you publish a message containing the Header.

Comment by divi on 2022-07-07:
Hi @RafBerkvens , @jbohren , I'm facing the same problem, seq is incremented by publisher and I set
message.header.stamp = ros::Time::now(); in a callback function and i don't see anything in stamp while publishing. Please help


1 Answer 1


Rosanswers logo

Necessary imports:

import rospy
import std_msgs.msg

Create the structure:

h = std_msgs.msg.Header()
h.stamp = rospy.Time.now() # Note you need to call rospy.init_node() before this will work

Originally posted by jbohren with karma: 5809 on 2013-04-08

This answer was ACCEPTED on the original site

Post score: 26

Original comments

Comment by RafBerkvens on 2013-04-08:
How does this relate to the comment @joq posted on the original question, stating that the sequence field should not be set?

Comment by jbohren on 2013-04-09:
Updated the answer, seq is set for you by the publisher.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.