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

Topic not subscribed to properly [closed]

asked 2014-08-20 16:45:45 -0600

joe.s gravatar image

updated 2014-08-20 16:54:35 -0600

ahendrix gravatar image

I'm writing a new node and am following examples on how to do that in python.
This node subscribes to a topic being published from another node (this other node's published topic is being published correctly as verified by "rostopic echo /topicname").
However, the subscriber node doesn't seem to be subscribing correctly to this topic (although the Callback is displaying the information to the command line correctly that it should be taking as input).
I'm in ROS Groovy.

The code is here:

#!/usr/bin/python

#JOE 
#August 2014

#publishes topic ilimb_grip_pub
#subscribes topic ilimb_define_pos

import rospy
from std_msgs.msg import String
import socket
#UDP_IP = "129.107.3.95"
UDP_IP = "127.0.0.1" #debug
UDP_PORT = 9000
Program_Name = "./handcontrol"

def callback(data):
    rospy.loginfo(rospy.get_caller_id()+"I heard %s",data.data)

def ilimb_grip_pub():

    # this listens to a user input for a user-defined hand configuration
    rospy.init_node('ilimb_grip_pub', anonymous=True)
    rospy.Subscriber("ilimb_define_pos", String, callback)     

    #this creates the publisher topic
    #pub = rospy.Publisher('ilimb_grip_pub', String) #needs to publish to a IP addr/ port
    #rospy.init_node('ilimb_move', anonymous=True)
    r = rospy.Rate(10) # 10hz


#this loop needs to be modified to publish hand configurations
#based on user input

    while not rospy.is_shutdown():
        print ilimb_define_pos.msg
        #~ define_pos= ilimb_define_pos.split(" ")
        #~ print define_pos
        #~ Digit_ID = define_pos[0]
        #~ Command = define_pos[1]
        #~ 
        #~ Message = "%s %d %s\n"%(Program_Name, Digid_ID, Command)         
                    #~ 
        #~ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        #~ sock.sendto(Message, (UDP_IP, UDP_PORT))
        #~ 
        #~ rospy.loginfo(Message)
        #~ #pub.publish(str)
        #~ r.sleep()

   # spin() simply keeps python from exiting until this node is stopped
        rospy.spin()
#~ 



if __name__ == '__main__':
    try:
        ilimb_grip_pub()
    except rospy.ROSInterruptException:
        pass

I'm getting the error:

NameError: global name 'ilimb_define_pos' is not defined.

This is strange as this is the topic to which I am subscribing... any ideas? I'm drawing a blank.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by joe.s
close date 2014-08-21 17:33:24.841007

2 Answers

Sort by ยป oldest newest most voted
1

answered 2014-08-20 16:59:40 -0600

ahendrix gravatar image

rospy does not create a variable named ilimb_define_pos when you subscribe to that topic. If you want to save the data that is received by the subscriber callback, you should create a global variable and explicitly copy the message into it.

You may want to review the rospy publisher and subscriber tutorial.

edit flag offensive delete link more

Comments

I only need to manipulate the data... but I need to save a local copy? I ask because I don't remember seeing that in the tutorial. That print statement is for debugging.

joe.s gravatar image joe.s  ( 2014-08-20 18:27:49 -0600 )edit

The message data is passed into the callback as an argument; it isn't valid outside the callback unless you assign it into a global variable.

ahendrix gravatar image ahendrix  ( 2014-08-20 19:02:17 -0600 )edit

Alternately, you can do all of your processing inside the callback routine.

ahendrix gravatar image ahendrix  ( 2014-08-20 19:02:36 -0600 )edit

Ok. Thank you. I'll see what I can come up with.

joe.s gravatar image joe.s  ( 2014-08-20 21:51:02 -0600 )edit

Ok. I've done this... but am receiving the error:

NameError: global name 'ilimb_pos' is not defined

... except that's what I thought I was doing in the callback(data) function But, if I uncomment the variable definition in "ilimb_pos none"... then I receive "none" as the output of the print command

Program_Name = "./handcontrol"
#ilimb_pos = None

def callback(data):
    rospy.loginfo(rospy.get_caller_id()+"I heard %s",data.data)
    global ilimb_pos
    ilimb_pos = data

def ilimb_grip_pub():

    # this listens to a user input for a user-defined hand configuration
    rospy.init_node('ilimb_grip_pub', anonymous=True)
    rospy.Subscriber("ilimb_define_pos", String, callback)     

    #this creates the publisher topic
    #rospy.init_node('ilimb_move', anonymous=True)
    r = rospy.Rate(10) # 10hz


    while not rospy.is_shutdown():
        #print Callback_Data
        print ilimb_pos
joe.s gravatar image joe.s  ( 2014-08-21 12:18:27 -0600 )edit

I would expect it to work if you uncomment the variable definition in the top-level scope. That said, I would expect it to print None until it receives the first message.

ahendrix gravatar image ahendrix  ( 2014-08-21 13:23:09 -0600 )edit

that does "fix" the issue... but that brings up another problem. I'm wanting to manipulate the subscribed data and expect it, "data" to be of type "string".... (should I start a new thread?) Here's the code I'm currently looking at:

ilimb_pos = None
l1 = []

def callback(data):
    rospy.loginfo(rospy.get_caller_id()+"I heard %s",data.data)
    global ilimb_pos
    ilimb_pos = data
    print ilimb_pos

    for elem in ilimb_pos:
        l1.extend(elem.strip().split(" "))
    print l1

I get this error:

for elem in ilimb_pos:
TypeError: 'String' object is not iterable

I'm researching now but could use a point in the right direction.

joe.s gravatar image joe.s  ( 2014-08-21 14:47:35 -0600 )edit

Please review the rospy tutorial and the definition for a string message again. The object your callback is receiving is a std_msgs/String object, which contains a single member called data which is a string. You log statement gets this right.

ahendrix gravatar image ahendrix  ( 2014-08-21 15:41:44 -0600 )edit
0

answered 2014-08-20 17:05:35 -0600

Murilo F. M. gravatar image

I'm no expert in Python, but to me your node is subscribing properly and the callback is being called. The issue is with:

print ilimb_define_pos.msg

That error means the global variable ilimb_define_pos (which happens to be the same as the topic name) is not defined.

Exclude that line from your code and that should solve your problem.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-08-20 16:45:45 -0600

Seen: 2,722 times

Last updated: Aug 20 '14