Ask Your Question
0

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

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

Pujie gravatar image

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

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 -0600

humanoid3 gravatar image

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

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 imagePujie ( 2019-02-16 17:26:12 -0600 )edit

Yes, it will create an empty Odometry type object.

humanoid3 gravatar imagehumanoid3 ( 2019-02-16 18:45:43 -0600 )edit

Instantiate an Odometry object

jayess gravatar imagejayess ( 2019-02-16 18:46:58 -0600 )edit

Thank you very much!

Pujie gravatar imagePujie ( 2019-02-16 19:26:50 -0600 )edit

Learn a new word "Instantiate", thank you!

Pujie gravatar imagePujie ( 2019-02-16 19:28:22 -0600 )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 imagePeteBlackerThe3rd ( 2019-02-17 04:33:45 -0600 )edit

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

Pujie gravatar imagePujie ( 2019-02-17 18:58:43 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 65 times

Last updated: Feb 16 '19