0
$\begingroup$

Rosanswers logo

I'm using arduino to send values from 0.00 to 5.00 and I'm trying to read them but I get this error

raise ROSSerializationException(str(e)) rospy.exceptions.ROSSerializationException: <class 'struct.error'>: 'required argument is not a float' when writing 'x: "0.92"

this is the code, how can I convert the value to float?

def __init__(self): 
rospy.init_node('my_tf_broadcaster')
self.ser = serial.Serial('/dev/ttyACM0', 115200)
    self.get_odometry()

def get_odometry(self):
self.pub_tf = rospy.Publisher("/tf", tf.msg.tfMessage)
    change = 0.0
    while not rospy.is_shutdown():
            # Run this loop at about 10Hz
            rospy.sleep(0.1)
    
    message = self.ser.readline()       
    t = geometry_msgs.msg.TransformStamped()
    t.header.stamp = rospy.Time.now()
        t.header.frame_id = "odom"
        t.child_frame_id = "base_link"
        t.transform.translation.x = message.rstrip()    #trying to read this value
        t.transform.translation.y = 1.0
        t.transform.translation.z = 0.0
        t.transform.rotation.x = 0.0
        t.transform.rotation.y = 0.0
        t.transform.rotation.z = 0.0
        t.transform.rotation.w = 1.0
    tfm = tf.msg.tfMessage([t])
        self.pub_tf.publish(tfm)

Originally posted by basicrobots on ROS Answers with karma: 1 on 2018-08-11

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

This is not a ROS question, but a Python one and you should have asked this on a more suitable forum (ie: one dedicated to Python development). Could you please review the support guidelines next time before you post?

In any case: the Python float(..) function accepts strings and will try to parse them for you, so passing whatever message.rstrip() returns to float(..) should work.


Edit:

when writing 'x: "0.92"

if you really write x: "0.92" on the serial prompt (or whatever program you're using for that), then that will not work, as float(..) will not be able to parse that.

And whatever self.ser.readline() returns is not a simple float, but the actual string x: "0.92".

As that is not a float, the ROS msg serialiser cannot deal with that and returns you the error.

Again: not a problem with ROS, but with the way you're setting the values for the message fields.


Originally posted by gvdhoorn with karma: 86574 on 2018-08-12

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by gvdhoorn on 2018-08-12:
Note: indenting for your code is incorrect here, leading to only rospy.sleep(0.1) being executed as part of the while loop.

Comment by basicrobots on 2018-08-12:
correct in python I can just do float(..) and it would work, but ROS when it grabs the values puts "" quotation marks and that's what's throwing.

Comment by gvdhoorn on 2018-08-12:
float(..) will parse strings, so I'm not sure what you are saying.

For me, something like: float('1.0') returns a valid float value.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.