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

how I convert string to float?

asked 2018-08-11 22:45:51 -0500

basicrobots gravatar image

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): 
self.ser = serial.Serial('/dev/ttyACM0', 115200)

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

    message = self.ser.readline()       
    t = geometry_msgs.msg.TransformStamped()
    t.header.stamp =
        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])
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-08-12 01:19:04 -0500

gvdhoorn gravatar image

updated 2018-08-12 14:00:47 -0500

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.


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.

edit flag offensive delete link more



Note: indenting for your code is incorrect here, leading to only rospy.sleep(0.1) being executed as part of the while loop.

gvdhoorn gravatar image gvdhoorn  ( 2018-08-12 01:19:16 -0500 )edit

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.

basicrobots gravatar image basicrobots  ( 2018-08-12 10:05:47 -0500 )edit

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.

gvdhoorn gravatar image gvdhoorn  ( 2018-08-12 13:58:37 -0500 )edit

Question Tools

1 follower


Asked: 2018-08-11 22:40:42 -0500

Seen: 1,185 times

Last updated: Aug 12 '18