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

howto use int16multiarray in python

asked 2016-06-15 06:45:33 -0600

inflo gravatar image

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 ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2016-06-15 09:08:42 -0600

In both cases of the if-elif statement, when you type the line hello_float.data = [] you are setting the data field to be an empty Python list. Then when you call insert you are inserting a new list into the zeroth entry of the original list. In other words, you are making the hello_float.data field a list of lists. This is most likely what is causing your error.

Your loop could instead read

a=0
while not rospy.is_shutdown():
    if a==0:
            hello_float.data = [380,399,380,380,380,380,380,380]
            rospy.loginfo(hello_float)
            pub.publish(hello_float)
            a=1
    elif a==1:
            hello_float.data = [377,399,380,380,380,380,380,380]
            rospy.loginfo(hello_float)
            pub.publish(hello_float)
            a=0
    rate.sleep()

and it would work fine. Pre Python 3.3, there is no list clear() method. Check out this SO post.

Note that the Int16MultiArray field can only be a one dimensional list. If you are trying to publish a matrix (or some other arbitrary multi-dimensional array), you will need to flatten that array into a single list and publish that. The fields in the dim array tell a subscriber how to unpack the flattened list correctly. For more details check out the documentation on the MultiArrayLayout message or this demo I made a while back that shows how to publish a random 3x3 matrix in Python and subscribe/unpack into an Eigen matrix in C++.

edit flag offensive delete link more

Comments

thanks alot

inflo gravatar image inflo  ( 2016-06-15 10:01:01 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-15 06:45:33 -0600

Seen: 8,655 times

Last updated: Jun 15 '16