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

How to use Float32MultiArray (Publisher)

asked 2019-06-03 09:06:47 -0500

Shanika gravatar image

updated 2019-06-03 10:10:33 -0500

Hello,
I am trying to write a python code using the Float32MultiArray to run my robot's motors.

The code is below:

motor = rospy.Publisher('encoder', Float32MultiArray, queue_size = 2)

while not rospy is shutdown():

    arg = Float32MultiArray()
    arg.layout.dim[0].label ="x"
    arg.layout.dim[0].size = 1
    arg.layout.dim[0].stride = 1

    arg.layout.dim[1].label ="y"
    arg.layout.dim[1].size = 1
    arg.layout.dim[1].stride = 1

    arg.layout.data_offset = 1
    arg.data = [100, 100]

    motor.publish(arg)

However, I have the error below:

TypeError: Invalid number of arguments, arg should be ['layout','data'] args are(0.0)

Could you please help me ?

Thank you in advance,

Shanika

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-06-03 11:21:43 -0500

updated 2019-06-03 11:22:09 -0500

You have to allocate the arrays, when you're trying to access dim[] and data[] you haven't yet allocated it to have any size or inserted something into it.

E.i.

thing = MultiArrayDimension()
thing.label = "x"
thing.size = 1
thing.stride = 1
arg.layout.dim.append(thing)
edit flag offensive delete link more

Comments

Thank you, I will try this right now

Shanika gravatar image Shanika  ( 2019-06-04 02:21:11 -0500 )edit

I looked a long time for this information

wydmynd gravatar image wydmynd  ( 2020-10-21 04:25:32 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2019-06-03 09:06:47 -0500

Seen: 3,932 times

Last updated: Jun 03 '19