Ask Your Question
0

trying to publish an array of words in rospy using numpy

asked 2014-06-01 16:12:07 -0600

uzair gravatar image

I am trying to create a subscriber which accepts a word (meaning a string of characters) and then collects those words into an array and then publish the array of these words.

I am trying to use numpy for this. The length of my array always has to be 3 with the latest word at the end. This way I wil have two previous words with the latest word at the end of the array.

This is the code :

#!/usr/bin/env python
import rospy
from std_msgs.msg import String
from rospy.numpy_msg import numpy_msg
import numpy
#added

def callback(data):
    global c
    pub = rospy.Publisher('tag_history',numpy_msg(String))

    c.append(str(data.data))
    if len(c)>3:
       c=c[1:4]    
    d=numpy.array(c,dtype=numpy.str)
    print c
    pub.publish(d)
    rospy.sleep(0.5)



 def listener():

     rospy.init_node('tag_history', anonymous=True)

     rospy.Subscriber("DA_tags", String, callback)
     rospy.spin()


if __name__ == '__main__':

     global c
     c=[]
     listener()

When I run this code, this is the error i get :

[ERROR] [WallTime: 1401656539.688481] bad callback: <function callback at 0x2456758>
Traceback (most recent call last):
File "/opt/ros/hydro/lib/python2.7/dist-packages/rospy/topics.py", line 682, in _invoke_callback
cb(msg)
File "./tag_history.py", line 17, in callback
pub.publish(d)
File "/opt/ros/hydro/lib/python2.7/dist-packages/rospy/topics.py", line 802, in publish
raise ROSSerializationException(str(e))
ROSSerializationException: field data must be of type str

What does this error "field data must be of type str" mean? How do I remove it?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2014-06-01 23:35:04 -0600

uzair gravatar image

I fixed my problem by creating a custom message which is an array or strings. However whn I use it, I get a warning. I posted a new question here http://answers.ros.org/question/17250...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-06-01 16:12:07 -0600

Seen: 1,328 times

Last updated: Jun 01 '14