howto use int16multiarray in python
hi, how do i use int16multiarray in python ? when i got this code:
#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String
from std_msgs.msg import Int16MultiArray
def talker():
pub = rospy.Publisher('mytopic', Int16MultiArray, queue_size=1)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(1) # 1hz
hello_float = Int16MultiArray()
hello_float.data = []
a=0
while not rospy.is_shutdown():
if a==0:
hello_float.data = []
hello_float.data.insert(0, [380,399,380,380,380,380,380,380] )
rospy.loginfo(hello_float)
pub.publish(hello_float)
a=1
elif a==1:
hello_float.data = []
hello_float.data.insert(0, [377,399,380,380,380,380,380,380] )
rospy.loginfo(hello_float)
pub.publish(hello_float)
a=0
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
with this, i get the following error:
rosrun pub_float_test talker.py
[INFO] [WallTime: 1465990335.722463] layout:
dim: []
data_offset: 0
data:
- [380, 399, 380, 380, 380, 380, 380, 380]
[INFO] [WallTime: 1465990336.723584] layout:
dim: []
data_offset: 0
data:
- [377, 399, 380, 380, 380, 380, 380, 380]
Traceback (most recent call last):
File "/home/ros/catkin_ws/src/pub_float_test/nodes/talker.py", line 37, in <module>
talker()
File "/home/ros/catkin_ws/src/pub_float_test/nodes/talker.py", line 29, in talker
pub.publish(hello_float)
File "/opt/ros/jade/lib/python2.7/dist-packages/rospy/topics.py", line 852, in publish
self.impl.publish(data)
File "/opt/ros/jade/lib/python2.7/dist-packages/rospy/topics.py", line 1036, in publish
serialize_message(b, self.seq, message)
File "/opt/ros/jade/lib/python2.7/dist-packages/rospy/msg.py", line 152, in serialize_message
msg.serialize(b)
File "/opt/ros/jade/lib/python2.7/dist-packages/std_msgs/msg/_Int16MultiArray.py", line 114, in serialize
except struct.error as se: self._check_types(struct.error("%s: '%s' when writing '%s'" % (type(se), str(se), str(_x))))
UnboundLocalError: local variable '_x' referenced before assignment
how do i clear a int16Multiarray ? there is no hello_float.data.clear() like in c ?