0
$\begingroup$

Rosanswers logo

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


Originally posted by Tayssir Boubaker on ROS Answers with karma: 17 on 2019-04-09

Post score: 0


Original comments

Comment by jayess on 2019-04-09:
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

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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-writing-files-in-python.html


Originally posted by Teo Cardoso with karma: 378 on 2019-04-09

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Tayssir Boubaker on 2019-04-09:
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 !??

Comment by Teo Cardoso on 2019-04-09:
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.

Comment by Teo Cardoso on 2019-04-09:
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)

Comment by Tayssir Boubaker on 2019-04-09:
Thanks i got it ;)

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by Reamees with karma: 591 on 2019-04-09

This answer was NOT ACCEPTED on the original site

Post score: 2


Original comments

Comment by Tayssir Boubaker on 2019-04-09:
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!

$\endgroup$

Your Answer

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