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

Revision history [back]

One thing I would definitely recommend changing is to not create the publisher inside the subscriber callback. Creating a new publisher each time is going to result in lost messages (as it takes a certain amount of time for subscribers to connect to that new publisher).

One thing I would definitely recommend changing is to not create the publisher inside the subscriber callback. Creating a new publisher each time is going to result in lost messages (as it takes a certain amount of time for subscribers to connect to that new publisher).

Second bug (that I found as I modified your code) is that you re-assign "move_turtlebot" to an odometry message - so you only end up processing one message before you probably are breaking your function by reassigning it to a message instance.

I'd recommend anytime you need to access variables from outside the callback, you should use a class (as opposed to a bunch of "global" usage).

This code runs, but is untested against actual messages:

#!/usr/bin/env python
import rospy
from tf.transformations import euler_from_quaternion
from nav_msgs.msg import Odometry

class OdometryModifier:

    def __init__(self):
        self.sub = rospy.Subscriber("odom", Odometry, self.callback)
        self.pub = rospy.Publisher('odom2', Odometry, queue_size=10)               

    def callback(self, data):
        data.header.stamp= rospy.Time.now()
        data.header.frame_id = "odom"
        data.pose.covariance= [0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
        self.pub.publish(data)

if __name__ == '__main__':
    try:
        rospy.init_node('move_turtlebot', anonymous=True)
        odom = OdometryModifier()
        rospy.spin()
    except rospy.ROSInterruptException:
        pass