<class 'struct.error'>: 'required argument is not a float' when writing 'data: [x, y]'

asked 2019-11-20 05:31:00 -0600

Redhwan gravatar image

updated 2019-11-20 18:59:40 -0600

I think that the issue is silly.

I'd like to run the code on two computers and I need to use a list. I followed this Tutorials here

I used my PC as a talker and computer of the robot as a listener.

when running the code on my PC, the output is good as I needed.

[INFO] [1574230834.705510]: [3.0, 2.1]
[INFO] [1574230834.805443]: [3.0, 2.1]

but once running the code on the computer of the robot, the output is:

Traceback (most recent call last):
  File "/home/redhwan/learn.py", line 28, in <module>
    talker()
  File "/home/redhwan/learn.py", line 23, in talker
    pub.publish(position.data)
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 886, in publish
    raise ROSSerializationException(str(e))
rospy.exceptions.ROSSerializationException: <class 'struct.error'>: 'required argument is not a float' when writing 'data: [3.0, 2.1]'

full code on PC:

#!/usr/bin/env python
import rospy
from std_msgs.msg import Float32
x = 3.0
y = 2.1

def talker():
    # if a == None:
    pub = rospy.Publisher('position', Float32, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    # rospy.init_node('talker')
    rate = rospy.Rate(10) # 10hz

    while not rospy.is_shutdown():
        position = Float32()
        a = [x,y]
        # a = x
        position.data = list(a)
        # position.data = a
        # hello_str = [5.0 , 6.1]
        rospy.loginfo(position.data)

        pub.publish(position.data)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

full code on the computer of the robot:

#!/usr/bin/env python
import rospy
from std_msgs.msg import Float32


def callback(data):
    # a = list(data)
    a = data.data
    print a

def listener():

    rospy.init_node('listener', anonymous=True)

    rospy.Subscriber("position", Float32, callback)

    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()


if __name__ == '__main__':
    listener()

Updated:

picture to the output with full code

when using one number as float everything is OK.

I understand how to publish and subscribe to them separately as the float but I'd like to do it as list

Any ideas or suggestion, it would be appreciated.

edit retag flag offensive close merge delete

Comments

Float32 is a message that contains only a single field data, which is a single float32. You cannot assign it a list.

when running the code on my PC, the output is good as I needed.

I won't accuse you of lying, but that would seem to be impossible.

You'll have to create a custom message which contains a field of type float32[] (or use std_msgs/Float32MultiArray, but that may be a bit involved).

gvdhoorn gravatar image gvdhoorn  ( 2019-11-20 06:17:17 -0600 )edit

I don't know why you wrote this sentence !!!!!! "I won't accuse you of lying, but that would seem to be impossible".

did you run the code??? did you get another output?

I put the full code here.

I took it to copy and paste after reading your comment to ensure from its output. for that, I updated my question by adding the picture.

Redhwan gravatar image Redhwan  ( 2019-11-20 19:06:54 -0600 )edit

@Redhwan please calm down. @gvdhoorn was simply suggesting that your statement

when running the code on my PC, the output is good as I needed.

is, at least, not accurately describing what you've been doing.

did you run the code??

As a matter of fact, I just did. And yes, the output from loginfo is actually appearing in the terminal. I guess that is what you tried to prove with your screenshot. But the talker node crashes the instant you open any subscriber to the /postion topic (try with rostopic echo on your PC). It has nothing to do with the robot and the

output is good as I needed.

can be doubted because it seems you haven't tried the listener (or rostopic echo, for that matter) on your PC.

That being said: I'm not familiar enough with the inner plumbing of rospy to explain why this ...(more)

mgruhler gravatar image mgruhler  ( 2019-11-21 03:02:50 -0600 )edit

@mgruhler no problem brother, it fixed by using Float32MultiArray

Redhwan gravatar image Redhwan  ( 2019-11-21 04:56:50 -0600 )edit