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

trouble subscribing to custom messages

asked 2012-06-12 17:26:40 -0600

punching gravatar image

updated 2012-06-14 18:41:30 -0600

First ROS script, jumping into the deep end. I have an Arduino publishing data (it's working fine) to a topic called Adc. I'm trying to write a Subscriber to read that data and eventually write it to a CSV file.

It compiles ok with no errors but shows nothing. The data types that are published are 5 different uint16s but when I tried "from std_msgs.msg import uint16) and then use uint16 below I get an error so I went back to String in the sample code.

Am I on the right track here?

#!/usr/bin/env python
import roslib; roslib.load_manifest('sensorcsv')
import rospy
from std_msgs.msg import String
def callback(data):
    rospy.loginfo(rospy.get_name()+"I heard %s",data.data)
    print rospy.get_name()+"I heard %s",data.data

def listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("Adc", String, callback)
    rospy.spin()

if __name__ == '__main__':<br>
    listener()
edit retag flag offensive close merge delete

5 Answers

Sort by ยป oldest newest most voted
0

answered 2012-06-14 21:05:25 -0600

punching gravatar image

Figured it out. For those that might run into this to.

This video helped explain it to me. http://vimeo.com/24616666

import roslib; roslib.load_manifest('sensorcsv')
import rospy
 # from rosserial_arduino import adc
from std_msgs.msg import UInt16
from rosserial_arduino.msg import Adc

def callback(data):
    print "Callback"
    print data.adc0, data.adc1, data.adc2, data.adc3, data.adc4, data.adc5
#    print rospy.get_name()+': ' + str(data.data)

def listener():
    print "listener"
    rospy.init_node('listener')
    rospy.Subscriber("adc", Adc, callback)
    print "rospy.subscriber"
    rospy.spin()
    print "rospy.spin()"

if __name__ == '__main__':
    print "Main"
    listener()
edit flag offensive delete link more

Comments

Can you please update your original post instead of those four different answers. It helps people understand what is going on.

dornhege gravatar image dornhege  ( 2012-06-15 01:15:17 -0600 )edit
2

answered 2012-06-12 22:08:05 -0600

Lorenz gravatar image

If you publish messages of type UInt16, subscribing with a string will just not work. ROS is strict on message data types. According to your question, you tried to import the data type uint16 which does not exist in std_msgs. The correct type is UInt16. Try:

from std_msgs.msg import UInt16

and change the line that creates the subscriber to:

rospy.Subscriber('Adc', UInt16, callback)
edit flag offensive delete link more
0

answered 2012-06-14 13:06:16 -0600

punching gravatar image

I'm getting closer.

Now when I try to connect the publisher script shows this. The publisher is publishing 6 UInt16 variables, is the problem that I'm only subscribing to one of them? Or is the topic type not really UInt16 but a new type made up of 6 UInt16s?

[WARN] [WallTime: 1339714526.664093] Could not process inbound connection: topic types do not match: [std_msgs/UInt16] vs. [rosserial_arduino/Adc]{'message_definition': 'uint16 data\n\n', 'callerid': '/listener', 'tcp_nodelay': '0', 'md5sum': '1df79edf208b629fe6b81923a544552d', 'topic': '/adc', 'type': 'std_msgs/UInt16'}

edit flag offensive delete link more
0

answered 2012-06-14 13:36:04 -0600

punching gravatar image

http://www.ros.org/wiki/rosserial_arduino/Tutorials/Arduino%20Oscilloscope

It is a custom message. I've tried to import into my script every combination of

from rosserial_arduino import Adc from rosserial_arduino.msg import Adc from Adc.msg import adc

etc. Everytime it says the module isn't found.

edit flag offensive delete link more
0

answered 2012-06-13 08:24:10 -0600

punching gravatar image

updated 2012-06-13 08:24:53 -0600

I think I put in Uint16 not UInt16 yesterday when I tried this. I fixed it and it compiled but same result.

I put some print commands in so I can see what commands are being executed.

The results of running this is

Main
listener
rospy.subscriber()
when I Ctrl-C it shows
rospy.spin()

Looks like the callback function isn't being called.

#!/usr/bin/env python
import roslib; roslib.load_manifest('sensorcsv')
import rospy

from std_msgs.msg import UInt16
def callback(data):
    print "Callback"
    rospy.loginfo(rospy.get_name()+"I heard %s",data.data)
    print rospy.get_name()+"I heard %s"+data.data

def listener():
    print "listener"
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("Adc", UInt16, callback)
    rospy.spin()
    print "rospy.spin()"

if __name__ == '__main__':
    print "Main"
    listener()
edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-06-12 17:26:40 -0600

Seen: 3,647 times

Last updated: Jun 14 '12