<class 'struct.error'>: 'required argument is not a float' when writing 'data: [x, y]'
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:
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.
Float32
is a message that contains only a single fielddata
, which is a singlefloat32
. You cannot assign it a list.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).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 please calm down. @gvdhoorn was simply suggesting that your statement
is, at least, not accurately describing what you've been doing.
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 withrostopic echo
on your PC). It has nothing to do with the robot and thecan 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 no problem brother, it fixed by using
Float32MultiArray