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

How to read msg from /odom at time series n and (n-1)

asked 2019-02-16 10:10:01 -0500

Pujie gravatar image

updated 2019-02-16 10:10:32 -0500

Hi there, I hope to compare the message from the consecutive time series n and (n-1) from /odom in python. I am confused about how to store the msg at time n-1 and time n at the same time, especially for the time sequence t=1.

def listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("/odom", Odometry, simple_callback)
    rospy.spin()
def simple_callback():

if __name__== '__main__':
    listener()

The problem which confused me is that: I was thinking to use some variables to store the msg at sequence n-1 and n like : current_odom=[ ] and last_odom=[ ]. However, I don't how to store the msg at time t=1, because listener() and simple_callback are in loop, I don't know when to pass the data in current_odom=[ ] and last_odom=[ ]. Could you please give me any hints?

Thank you.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-02-16 14:43:43 -0500

humanoid3 gravatar image

updated 2019-02-16 14:47:16 -0500

The quick and dirty way to do this would be to create a global variable for last and current odom. You could save this variable at the end of every loop. It would look something like this

last_odom = Odometry()
current_odom = Odometry()

def listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("/odom", Odometry, simple_callback)
    rospy.spin()

def simple_callback(data):
    global last_odom 
    global current_odom
    last_odom = current_odom
    current_odom = data

if __name__== '__main__':
    listener()
edit flag offensive delete link more

Comments

Does last_odom = Odometry() mean generate an "object of Odometry" ?

Pujie gravatar image Pujie  ( 2019-02-16 17:26:12 -0500 )edit

Yes, it will create an empty Odometry type object.

humanoid3 gravatar image humanoid3  ( 2019-02-16 18:45:43 -0500 )edit

Instantiate an Odometry object

jayess gravatar image jayess  ( 2019-02-16 18:46:58 -0500 )edit

Thank you very much!

Pujie gravatar image Pujie  ( 2019-02-16 19:26:50 -0500 )edit

Learn a new word "Instantiate", thank you!

Pujie gravatar image Pujie  ( 2019-02-16 19:28:22 -0500 )edit
1

It should be noted that the odometry messages are not time stamped so if you want to know the delta t in seconds you'll also have to record the times the messages are received.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-02-17 04:33:45 -0500 )edit

Thank you! They said that I can check something like time in msg.header.stamp to get the msg time

Pujie gravatar image Pujie  ( 2019-02-17 18:58:43 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2019-02-16 10:10:01 -0500

Seen: 363 times

Last updated: Feb 16 '19