Subscribing to a topic with Python

asked 2016-12-15 19:00:59 -0600

AkaTomo93 gravatar image

updated 2016-12-15 19:01:56 -0600

Hello people, I've had trouble subsciribing to a topic of type: /robot0/odom

Where odom is a type of nav_msgs/Odometry and it brings me x,y coordinates and orientation.

I've written a simple code for this, but when I run the script, it's not printing anything on the terminal.

#!/usr/bin/env python

import rospy
from nav_msgs.msg import Odometry

def callback(msg):
x=msg.pose.pose.position.x
y=msg.pose.pose.position.y
orientation_z=msg.pose.pose.orientation.z
orientation_w=msg.pose.pose.orientation.w

rospy.loginfo('x: {}, y:{}, orientation_z:{},' .format(x,y, orientation_z))

def main():
    rospy.init_node('ekf_slam')
    rospy.Subscriber("/robot0/odom",Odometry,callback)
    rospy.spin()

if __name__ == '__main__':
    main()

This code was working well when I had to subscribe on a topic with just rospy.Subscriber("/odom",Odometry,callback), but as soon as I switched the program and the topic became /robot0/odom type I began having problems.

Any help would be really appreciated.

edit retag flag offensive close merge delete

Comments

1

I guess you have the file properly indented and not as you posted it here, otherwise, the node would not work at all.

Check if you actually subscribed to the correct topic: rosnode info YOURNODE, rostopic info /robot0/odom

mgruhler gravatar imagemgruhler ( 2016-12-16 02:05:45 -0600 )edit

Hmm, so when I write rostopic info /robot0/odom I get back:

Type: nav_msgs/Odometry

Publishers: * /robot_manager ( http://tomo:41302/ )

Subscribers: None

What do you think could be the problem here?

AkaTomo93 gravatar imageAkaTomo93 ( 2016-12-16 03:29:45 -0600 )edit

WOW? It just started to work without me changing anything, I dont even understand?

I just went to sleep and tried to run the code again.

Thank you so much for helping me anyway, I definitely learned a lot from trying to fix this issue :)

AkaTomo93 gravatar imageAkaTomo93 ( 2016-12-16 03:32:38 -0600 )edit