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

Accessing data from 4 seconds back

asked 2013-03-15 03:05:53 -0500

whiterose gravatar image

updated 2013-03-15 05:52:20 -0500

joq gravatar image

Hi all, I am trying to create my own acceleration node. The idea is to have the difference between 2 velocities, every 4 seconds for example. I know how to get the value of the velocity at the current time. but how do I get the velocity 4 seconds back? the value should be accessible as the program runs, or as bag files are being played.

Below is my code:

#!/usr/bin/env python
import roslib; roslib.load_manifest('kee_use_cases')
import rospy
import math
#from std_msgs.msg import String
from std_msgs.msg import *
from nav_msgs.msg import Odometry
from geometry_msgs.msg import *


    def callback(msg):
        rospy.loginfo("Received actual forward velocity <</odom>> message! The value is %f" %msg.twist.twist.linear.x)
        if rospy.Time.now() == 0:
            vel_init = msg.twist.twist.linear.x
            buffer_function(vel_init) #?
        else:
            #how do i get the the vel_init?
            vel_last = msg.twist.twist.linear.x
            acceleration = (vel_last - vel_init)/4
            talker(acceleration)

    def buffer_function(vel_init):
        return vel_init


    def talker(acc):
        rospy.loginfo("x_acceleration: %f"%acc)
        pub = rospy.Publisher('x_acceleration', Float64 )
        pub.publish(acc)

    def listener():
        rospy.Subscriber("odom", Odometry, callback)

    if __name__ == '__main__':
        rospy.init_node('x_acceleration', anonymous=True)

        duration = 4
        while not rospy.is_shutdown():
            try:
                listener()
                rospy.sleep(duration)
            except rospy.ROSInterruptException:
                pass
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-03-15 05:58:44 -0500

joq gravatar image

You need to save old messages in your callback in a persistent buffer.

If you need exactly four seconds, you'll probably need to interpolate between two successive messages.

If you just want to estimate acceleration, the simplest approach is storing only the previous message, and computing dt by subtracting the previous header.stamp from the current one.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-03-15 03:05:53 -0500

Seen: 160 times

Last updated: Mar 15 '13