child script, write forceVector to file

Typically: "How do I... ", "How can I... " questions
Post Reply
xelda1988
Posts: 13
Joined: 02 Jul 2013, 17:13

child script, write forceVector to file

Post by xelda1988 » 02 Jul 2013, 17:34

I have code from the following child script, which accesses a force sensor:

Code: Select all

-- Put some initialization code here

-- opens file to write forces into
local file = io.open("/home/test_force_output.txt", "a")

--handle for force sensor
forceSensorHandle=simGetObjectHandle("Force_sensor")
counter = 1

while (simGetSimulationState()~=sim_simulation_advancing_abouttostop) do

result, forceVector,torqueVector=simReadForceSensor(forceSensorHandle)

--print(counter)
file:write(counter*0.05)
file:write(",")

file:write(forceVector[1])

file:write("\n")

counter = counter + 1


end

file:close()
I get the following compilation errror:

Code: Select all

Lua runtime error: [string "SCRIPT Force_sensor"]:53: bad argument #1 to 'write' (string expected, got nil)
Simulation paused.  
What's the problem, seems like there is nothing written to the variable forceVector?!

Please Help

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

Re: child script, write forceVector to file

Post by coppelia » 02 Jul 2013, 17:50

Hello,

As a quick guess: simReadForceSensor will return a status value (i.e. result) that is bit-coded. If bit0 is not set, this means the call didn't succeed. This usually happens if simReadForceSensor is called before the first call to simHandleDynamics (i.e. most of the time in the first simulation pass). If your force sensor is set-up with a filter, then you might have to wait a few passes until the sensor measured enough values for the filter to produce an output (here again, check for bit0 of the first returned value).

Just check your status value. Only if bit0 is set will there be a second value returned (i.e. forceVector).

Cheers

xelda1988
Posts: 13
Joined: 02 Jul 2013, 17:13

Re: child script, write forceVector to file

Post by xelda1988 » 03 Jul 2013, 09:15

That seems to be the problem, because I already wrote the bit coded variable to the output file and the value jumped to 1 after about 500ms.
Thanks for the super fast feedback :)!

alexander_the_great
Posts: 6
Joined: 25 Mar 2014, 02:30

Re: child script, write forceVector to file

Post by alexander_the_great » 29 Jun 2014, 21:11

Hello I have a question on this matter. I am trying to write to a file in Vrep and it is giving me this lua error:

"Lua runtime error: [string "SCRIPT scriptRunner"]:213: attempt to index global 'telemFile' (a nil value)"

my filename is telemFile and I store the file and the scene inside the vrep folder that comes with downloading vrep.
But I cannot seem to get it to work. I have also tried storing the telemFile.txt and the scene in Desktop but that didn't work either. How did you do it?

Thanks.

Eric
Posts: 186
Joined: 11 Feb 2013, 16:39

Re: child script, write forceVector to file

Post by Eric » 30 Jun 2014, 21:01

well to simply write a file in LUA

Code: Select all

name = "toto.txt"
file = io.open(name, "w")
file:write("Hello World")
file:close()
to read a file that is inside v-rep folder

Code: Select all

name = "toto.txt"
file = io.open(name, "r")
print(file:read())
file:close()
The scene does not need to be saved inside v-rep s folder.

Cheers

Eric

Post Reply