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