Ask Your Question
0

Cannot save data Callback function

asked 2019-04-09 07:17:00 -0600

Tayssir Boubaker gravatar image

updated 2019-04-09 11:25:23 -0600

What I am really looking for is a way to save all value's message that a node receives (in a list, CSV file..) in a way that I can later do something with this values.

So this is data received :

image description

I tried to put all these values in myData List:

def callback(data):
    id = data.id
    x = data.pose.x
    y = data.pose.y

    #print data
    #list_x.append(x)
    #print id ,x,y
    goals={}
    goals["x"]=x
    goals["y"]=y
    #print goals
    myData=[goals]
    print myData

but it's not what I want(Even with append) ! it put every value in a list like this :

image description

Also, I tried to save this value in a CSV File :

with open('goals_point.csv', 'with') as csvFile:
       writer = csv.writer(csvFile)
       writer.writerows([myData])
       csvFile.close()

it saves only the last value :

image description

So how can I save collected messages (data)? Any idea?

Thanks for answering and for your time in advance,

Tayssir

edit retag flag offensive close merge delete

Comments

Please don't use an image to display text. Images are not searchable and people cannot copy and paste the text from the image. Please see the support page

jayess gravatar imagejayess ( 2019-04-09 12:14:56 -0600 )edit

2 Answers

Sort by » oldest newest most voted
1

answered 2019-04-09 09:59:12 -0600

Teo Cardoso gravatar image

You could save the data into a text file, and import this to excel ( or similar ).

file = open("yourfile.txt", "w+")
data_to_save = str(data.pose.x) + "\t" + str(data.pose.y) + "\n"
file.write(data_to_save)

With this code above you'll be able to create a multi-lines into your file. However, if you call the same program again the data will be overwritten.

If you need: https://www.guru99.com/reading-and-wr...

edit flag offensive delete link more

Comments

thank you, I try it but it saves only the last value,

a = open('goals.txt', 'w+')
    data_to_save = str(data.pose.x) + "\t" + str(data.pose.y) + "\n"
    a.write(data_to_save)

output txtfile :

10.0    20.0

I think that i should not put it in my callback. So the problem now is who can I got data.pose out of my callback !??

Tayssir Boubaker gravatar imageTayssir Boubaker ( 2019-04-09 10:55:29 -0600 )edit

A parte de open('goals.txt', 'w+') você deve colocar em uma parte onde isso não se repita, ou sejá, no começo do programa não pode colocar essa linha dentro do callback, somente a parte de escrever.

Teo Cardoso gravatar imageTeo Cardoso ( 2019-04-09 13:31:40 -0600 )edit

Sorry, You shouldn't put the line:

a = open('goals.txt', 'w+')

inside the callback function, this line must be call once.

The lines which write inside the text file must be in the callback function.

something like this:

a = open('goals.txt', 'w+')

def   callbackFunction():
      data_to_save = str(data.pose.x) + "\t" + str(data.pose.y) + "\n"
      a.write(data_to_save)
Teo Cardoso gravatar imageTeo Cardoso ( 2019-04-09 13:32:35 -0600 )edit

Thanks i got it ;)

Tayssir Boubaker gravatar imageTayssir Boubaker ( 2019-04-09 15:53:02 -0600 )edit
2

answered 2019-04-09 08:27:50 -0600

Reamees gravatar image

As I understand your main goal is to save some topic as a csv. If that is the case you can do that without writing a single line of code. You can pipe the output of rostopic echo with the -p flag to some file and get a csv that way, my answer to a similar question.

rostopic echo -p /turtlebot/stamped_transform/transform/translation > xyz_output.csv

will produce a text file xyz_output.csv with the data formated like the example below, you can change the topic to suit your needs.

%time,field.x,field.y,field.z
1531725938918082952,-354.547943115,-41.9697914124,-33.9759483337
1531725939051723003,-354.530578613,-41.9656219482,-33.9720916748
1531725939181190967,-354.52633667,-41.9511375427,-33.9718017578

There is also the ROS utility called rosbag, in case you don't need the data in csv, but just something to record and play back the data.


As for why your code is not working, it's hard to say without seeing the whole code. Also don't post screenshots of your code and terminal outputs. Anything that is text should be in text format in the question, so it would be copy paste-able and searchable.

If you do want your own node to write the CSV then there are some things that seem out of place in the code.

it saves only the last value :

Your code is most probably not saving only the last value, it is creating the same file with different values and only the last iteration of the cycle remains. You are opening the file with the w option, which overwrites the file every time it is called. Opening the file with w is not the cause of your issues though. Instead of the with file opening method you should probably open the file when you are initializing the node and write to it in the callback. You will then need to close the file when you close the node. Handling writing csv files is more of a general programming question that is better suited for a general programming QA forum.

edit flag offensive delete link more

Comments

Thanks, I'm absolute beginner and I'm just getting started learning ROS and I have no idea about rosbag but I will try it. Also , I try to redirect my output from terminal to my existing file ..but dosent working :"bash:No such file or directory" Finally,about saving csv file ,how could I access to my data when im out my callback!

Tayssir Boubaker gravatar imageTayssir Boubaker ( 2019-04-09 11:15:16 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-04-09 07:17:00 -0600

Seen: 71 times

Last updated: Apr 09