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

string to a float

asked 2016-07-25 07:49:58 -0500

chezzydc10 gravatar image

updated 2016-07-29 07:24:27 -0500

Hi, I will like to change the string message of one of my topics to a float

edit retag flag offensive close merge delete

Comments

Does your topic output's numbers in string format?

krishna43 gravatar image krishna43  ( 2016-07-25 10:50:19 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2016-07-27 15:56:13 -0500

Chrissi gravatar image

updated 2016-07-28 09:58:22 -0500

Not nice but the easiest is to just republish it I guess.

import rospy
from std_msgs.msg import String, Float64

rospy.init_node("spam")
p = rospy.Publisher("/topic_out", Float64)

def callback(msg):
    p.publish(float(msg.data))        

rospy.Subscriber("/topic_in", String, callback)
rospy.spin()

EDIT: You can also write a script that reads from the bag and writes to a new one. Have a look at the rosbag API for that.

EDIT 2:

I am not entirely sure how you subscribe to what but here is how it works for me:

Let's assume I have a node like this that produces the data:

import rospy
import numpy as np
from std_msgs.msg import String

rospy.init_node("data_node")

p = rospy.Publisher("/string_topic", String, queue_size=1)

Fs = 8000
f = 5
sample = 8000
x = np.arange(sample)
y = np.sin(2 * np.pi * f * x / Fs)

r = rospy.Rate(10)
for e in y:
    p.publish(str(e))
    r.sleep()

which just produces a sine wave and then I save that to a rosbag rosbag record /string_topic which generates the following bag

path:        2016-07-28-08-06-26.bag
version:     2.0
duration:    2:46s (166s)
start:       Jul 28 2016 08:06:29.43 (1469689589.43)
end:         Jul 28 2016 08:09:16.33 (1469689756.33)
size:        129.3 KB
messages:    1670
compression: none [1/1 chunks]
types:       std_msgs/String [992ce8a1687cec8c8bd883ec73ca41d1]
topics:      /string_topic   1670 msgs    : std_msgs/String

This I can then play rosbag play 2016-07-28-08-06-26.bag and use the script I posted above to subscribe to /string_topic:

import rospy
from std_msgs.msg import String, Float64

rospy.init_node("spam")
p = rospy.Publisher("/float_topic", Float64)

def callback(msg):
    p.publish(float(msg.data))        

rospy.Subscriber("/string_topic", String, callback)
rospy.spin()

I can the connect /float_topic to rqt_plot and see my data. My connection graph looks likes this:

rqt_graph

Where on the left you have the rosbag and on the right the rqt_plot.

Maybe your bag contains just the same number in all the messages? Can you upload it somewhere or post some of the example output of the topic you want to plot. Also, the output of rosbag info on your bag would be good.

EDIT 3:

To accommodate the data you got, we would need a different conversion node:

import rospy
from std_msgs.msg import String, Float64

rospy.init_node("spam")
publishers = []
for i in range(6):
    publishers.append(rospy.Publisher("/thermal_zone"+str(i), Float64))

def callback(msg):
    for s, p in zip(msg.data.split('\n'), publishers):
        p.publish(float(s.split(':')[1]))        

rospy.Subscriber("/string_topic", String, callback)
rospy.spin()

This is not tested as I do not have the data but it creates 6 publishers which are called thermal_zone0 to thermal_zone5 like in your data. In the callback it tries to split the incoming string based on the line break \n which I assume is in there but might be wrong. It then splits the string again based on the : and takes the right part of it which should be the number and then publishes it as a ... (more)

edit flag offensive delete link more

Comments

When i republished it as float, i was not getting the same data i had on my rosbag. I was getting the same number over and over again

chezzydc10 gravatar image chezzydc10  ( 2016-07-27 16:26:38 -0500 )edit

Python or cpp? Can't imagine how that could happen given above python script. In cpp it might because of a wrong string conversion.

Chrissi gravatar image Chrissi  ( 2016-07-27 16:46:02 -0500 )edit

In python. From the sample code u wrote, you are subscribing to the node but im subscribing to the rosbag in mine. I already have some recorded data in rosbag which plays as string but i will like them to play as float

chezzydc10 gravatar image chezzydc10  ( 2016-07-27 17:04:50 -0500 )edit

See edit in answer

Chrissi gravatar image Chrissi  ( 2016-07-28 03:16:08 -0500 )edit

I followed your first code but after playing the rosbag and running the python script, I got this error in the link below. The new node couldn't convert the string to float.

http://www.directupload.net/file/d/44...

chezzydc10 gravatar image chezzydc10  ( 2016-07-28 07:02:25 -0500 )edit

Your topic publishes a weird string like: "/sys/device/.../../:35000" and not just a number like "35000.0" so python of course doesn't know how to convert this to a float. You have to parse the string so you only have the number before doing float(). If you want help please edit your question

Chrissi gravatar image Chrissi  ( 2016-07-28 07:56:10 -0500 )edit

... and post some example output of your topic. Also please post the output of rosbag info of your bag just to make sure it is really just publishing a string message. Looks like it is publishing a number of strings in the same message.

Chrissi gravatar image Chrissi  ( 2016-07-28 07:56:33 -0500 )edit

This is the example of my topic when you echo it. The data has already been recorded so this is what i get when I play the rosbag. http://www.directupload.net/file/d/44... Also this is what i have in my rosbag info http://www.directupload.net/file/d/44...

chezzydc10 gravatar image chezzydc10  ( 2016-07-28 08:38:22 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-07-25 07:49:02 -0500

Seen: 1,997 times

Last updated: Jul 29 '16