0
$\begingroup$

Rosanswers logo

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?


Originally posted by uzair on ROS Answers with karma: 87 on 2014-06-01

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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/172509/created-a-custom-msg-to-support-an-array-of-strings-but-i-get-thhis-warning-msg-when-i-use-it/


Originally posted by uzair with karma: 87 on 2014-06-01

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.