Get and save position data of an object using Graph

Typically: "How do I... ", "How can I... " questions
Post Reply
DavideIng
Posts: 39
Joined: 21 Sep 2017, 17:27

Get and save position data of an object using Graph

Post by DavideIng » 26 Feb 2018, 18:39

Hello,
I need to get and save the X-Y data relative to the position on the scene of an ePuck robot. After that I have to export it in excel an plot it.
I already add to the scene the Graph object and I selected: [file -> Export -> selected graphs as CSV], what I have to do now?
Do I have to add to the ePuck child script: simHandleGraph?

Does anyone might help me?

Thanks in advance
Davide

coppelia
Site Admin
Posts: 7150
Joined: 14 Dec 2012, 00:25

Re: Get and save position data of an object using Graph

Post by coppelia » 27 Feb 2018, 10:32

Hello,

why do you want to use graphs for that?
You can handle that task in a much more flexible and elegant way via a simple script. For instance, you can read the position of an object in each simulation step, add that data to a table, and at the end of the simulation, simply write a file with the content of the table.

Cheers

DavideIng
Posts: 39
Joined: 21 Sep 2017, 17:27

Re: Get and save position data of an object using Graph

Post by DavideIng » 27 Feb 2018, 17:34

I am trying to add the position data to a table in the following way:

Code: Select all

ePuck = simGetObjectHandle('ePuck')
Table={}
Table=simGetObjectPosition(ePuck,-1)
I am not sure that is the correct way to do this but, assuming that is, I don't understand how to write the table data in an excel file.

Any suggestion?
Thanks
Davide

DavideIng
Posts: 39
Joined: 21 Sep 2017, 17:27

Re: Get and save position data of an object using Graph

Post by DavideIng » 27 Feb 2018, 17:52

Is it correct to do this when the simulation is still running and the table is still updating:

Code: Select all

data="misure.xlsx"
file=io.open(data,"w")
file=write(Table)
file:close()

DavideIng
Posts: 39
Joined: 21 Sep 2017, 17:27

Re: Get and save position data of an object using Graph

Post by DavideIng » 27 Feb 2018, 18:53

Because doing this the file "misure" in the Vrep folder is empty.

Cheers

coppelia
Site Admin
Posts: 7150
Joined: 14 Dec 2012, 00:25

Re: Get and save position data of an object using Graph

Post by coppelia » 28 Feb 2018, 09:35

It seems you are making a lot of mistakes. I highly recommend to first try simple Lua scripts, and have a look at this crash course and the official Lua resources.

Cheers

DavideIng
Posts: 39
Joined: 21 Sep 2017, 17:27

Re: Get and save position data of an object using Graph

Post by DavideIng » 01 Mar 2018, 11:01

I need to write position data (X and Y coordinates) in and excel file until the simulation time is 30 seconds.
The code that I am using is the following:

Code: Select all

k=1
t1 = simGetSimulationTime()
if (t1 < 30) then
tableX={}
tableY={}
q=simGetObjectPosition(ePuck1,-1)
tableX[k]=q[1]
tableY[k]=q[2]
else
misureX="misurex.xlsx"
misureY="misurey.xlsx"
file1=io.open(misureX,"w")
file2=io.open(misureY,"w")
file1:write(tableX)
file2:write(tableY)
file1:close()
file2:close()
end
k=k+1
I get this error:

Code: Select all

[C]: in function 'write'
[string "SCRIPT ePuck#0"]:86: in function <[string "SCRIPT ePuck#0"]:45>
[C]: in function 'xpcall'
For that reason it doesn't execute the "else" statement so it doesn't write the table in the file.
Does anyone might help me?
Cheers

Post Reply