lua stimulation some useful example

Concerning processing components: filters, file load/save, visualizations, communication ...
Post Reply
matthieuG
Posts: 54
Joined: Thu Nov 12, 2009 10:22 am
Location: grenoble

lua stimulation some useful example

Post by matthieuG »

Hi,

With the new box lua Stimulator, It is now possible to build many stimulator without developpement (C++ programming). Your example is great but one of the element is missing : how to extract value of the tuple get_stimulation() return?

I tried :
io.write(string.format("At time %f on input 1 got stimulation id:%s date:%s duration:%s\n", t, get_stimulation(1, 1)))
Stim = get_stimulation(1, 1)
id = Stim[0]
date = Stim[1]
duration = Stim[2]
io.write(string.format("Stimulation is : id:%d date:%f duration:%f\n", Stim[0],Stim[1],Stim[2]))

Which return in the console :
At time 4.710938 on input 1 got stimulation id:32769 date:4.6953125 duration:0
[...ERROR...] "attempt to index global 'Stim" (a number value)"

So Stim is maybe not a tuple. How to extract these information?

yrenard
Site Admin
Posts: 645
Joined: Fri Sep 01, 2006 3:39 pm
Contact:

Re: lua stimulation some useful example

Post by yrenard »

Dear MatthieuG,

this question is specific to Lua language. You'd better read their <a href=http://www.lua.org/manual/5.1>online manual</a> in order to understand this language. However, the following sample code should answer your question :

Code: Select all

#!/usr/bin/lua

function test()
	return 1, 2, 3
end


io.write(string.format("values : %i %i %i\n", test()))

a, b, c = test()

io.write(string.format("values : %i %i %i\n", a, b, c))
Hope this helps,
Yann

matthieuG
Posts: 54
Joined: Thu Nov 12, 2009 10:22 am
Location: grenoble

Re: lua stimulation some useful example

Post by matthieuG »

Hi,

As expected, I join an example. You can find in this one all openViBE function for lua and a way to use array. In deep, this file allow to generate targets for P300speller with a specific input string (random target is boring)
Attachments
TargetStimulationP300Gen.txt
lua example
(5.78 KiB) Downloaded 505 times

yrenard
Site Admin
Posts: 645
Joined: Fri Sep 01, 2006 3:39 pm
Contact:

Re: lua stimulation some useful example

Post by yrenard »

Dear matthieuG,

thank you for this nice contribution. Based on what I read in your Lua script, I assume the Lua Stimulator box should be used as a "transformation" box after the P300 Speller Stimulator box ?

I suggest that you also attach a sample scenario demonstrating this script (e.g. P300 Speller Stimulator + Lua Stimulator + Stimulation Listener)

Yann

tonio112003
Posts: 13
Joined: Wed Feb 20, 2013 4:13 pm

Re: lua stimulation some useful example

Post by tonio112003 »

Hi!

Do you know why do I get I problem with the lua script you uploaded?

It says "TargetStimulationP300Gen.txt:84: attempt to call global 'get_current_time' (a nil value)"
"] At time 0.000 sec <Box algorithm::Target Letter Generation> Lua script terminated"

Thanks!

jlegeny
Posts: 239
Joined: Tue Nov 02, 2010 8:51 am
Location: Mensia Technologies Paris FR
Contact:

Re: lua stimulation some useful example

Post by jlegeny »

Hello tonio112003,

the algorithm in the previous post was created with an older API for lua scripts. In the newer API, the functions initialize(), uninitialize() and process() take the box as a parameter and all of the openvibe functions are members of this object. As an example here is the previous script rewritten for the new API:

Code: Select all

-- this function is called when the box is initialized
function initialize(box)
        io.write("initialize has been called\n")

	  --Row1 = { '1', '-1'}
	  Row1 = { 1, -1}
	  Row2 = { 2, -1}
	  Row3 = { 3, -1}
	  Row4 = { 4, -1}
	  Row5 = { 5, -1}
	  Row6 = { 6, -1}
	  Column1 = { -1, 1}
	  Column2 = { -1, 2}
	  Column3 = { -1, 3}
	  Column4 = { -1, 4}
	  Column5 = { -1, 5}
	  Column6 = { -1, 6}
		cmdA = { Row1, Column1 }
	  cmdB = { Row1, Column2 }
	  cmdC = { Row1, Column3 }
	  cmdD = { Row1, Column4 }
	  cmdE = { Row1, Column5 }
	  cmdF = { Row1, Column6 }
		cmdG = { Row2, Column1 }
	  cmdH = { Row2, Column2 }
	  cmdI = { Row2, Column3 }
	  cmdJ = { Row2, Column4 }
	  cmdK = { Row2, Column5 }
	  cmdL = { Row2, Column6 }
	  	  cmdM = { Row3, Column1 }
	  cmdN = { Row3, Column2 }
	  cmdO = { Row3, Column3 }
	  cmdP = { Row3, Column4 }
	  cmdQ = { Row3, Column5 }
	  cmdR = { Row3, Column6 }
	  	  cmdS = { Row4, Column1 }
	  cmdT = { Row4, Column2 }
	  cmdU = { Row4, Column3 }
	  cmdV = { Row4, Column4 }
	  cmdW = { Row4, Column5 }
	  cmdX = { Row4, Column6 }
	  	  cmdY = { Row5, Column1 }
	  cmdZ = { Row5, Column2 }
	  cmd1 = { Row5, Column3 }
	  cmd2 = { Row5, Column4 }
	  cmd3 = { Row5, Column5 }
	  cmd4 = { Row5, Column6 }
	  	  cmd5 = { Row6, Column1 }
	  cmd6 = { Row6, Column2 }
	  cmd7 = { Row6, Column3 }
	  cmd8 = { Row6, Column4 }
	  cmd9 = { Row6, Column5 }
	  cmd_ = { Row6, Column6 }
	  --CommandInput = { cmdA, cmdB, cmdC, cmdD, cmdE }
	  CommandInput = { cmdC, cmdO, cmdR, cmdB, cmdE, cmdA, cmdU, cmdR, cmdE, cmdN, cmdA, cmdR, cmdD, 
					cmdL, cmdI, cmdE, cmdV, cmdR, cmdE, cmdE, cmdT, cmdO, cmdR, cmdT, cmdU, cmdE,
					cmdP, cmdA, cmdV, cmdO, cmdI, cmdS, cmdE, cmdN, cmdT, cmdD, cmdE, cmdD, cmdE, cmdL, cmdA,
					cmdF, cmdO, cmdN, cmdT, cmdA, cmdI, cmdN, cmdE}
	  
	  --CommandContext = {Row1=33025, Row2=33026, Row3=33027, Row4=33028, Row5=33029, Row6=33030, 
		--				Column1=33031, Column2=33032, Column3=33033, Column4=33034, Column5=33035, Column6=33036}
		CommandContextRow = {33025, 33026, 33027, 33028, 33029, 33030}
		CommandContextCol = {33031, 33032, 33033, 33034, 33035, 33036}
 
end

-- this function is called when the box is uninitialized
function uninitialize(box)
        io.write("uninitialize had been called\n")
 
end

-- this function is called once by the box
function process(box)
        io.write("process has been called\n")
		count = 0

        -- enters infinite loop
        -- cpu will be released with a call to sleep
        -- at the end of the loop
        while true do

                -- gets current simulated time
                t = box:get_current_time()

                -- loops on all inputs of the box
					-- loops on every received stimulation for a given input
					for stimulation = 1, box:get_stimulation_count(1) do

                        -- logs the received stimulation
                        --io.write(string.format("At time %f on input 1 got stimulation id:%s date:%s duration:%s\n", t, get_stimulation(1, 1)))

						--check witch stimulation it is
						emit = 0
						id,date,duration = box:get_stimulation(1, 1)
						--io.write(string.format("Stimulation is : id:%d date:%f duration:%f\n", id,date,duration))
						
						if (id==32769) then
							--io.write(string.format("Stimulation is OVTK_StimulationId_ExperimentStart\n"))
						end
						if (id==32770) then
							--io.write(string.format("Stimulation is OVTK_StimulationId_ExperimentStop\n"))
						end
						if (id==32771) then
							--io.write(string.format("Stimulation is OVTK_StimulationId_SegmentStartOVTK_StimulationId_TrialStop\n"))
						end
						if (id==32772) then
							--io.write(string.format("Stimulation is a OVTK_StimulationId_SegmentStop\n"))
						end
						if (id==32773) then
							--io.write(string.format("Stimulation is a OVTK_StimulationId_TrialStart\n"))
						end
						if (id==32774) then
							--io.write(string.format("Stimulation is a OVTK_StimulationId_TrialStop\n"))
						end
						if (id==32774) then
							--io.write(string.format("Stimulation is a OVTK_StimulationId_TrialStop\n"))
						end
						if (id==32775) then
							--io.write(string.format("Stimulation is a OVTK_StimulationId_BaselineStart\n"))
						end
						if (id==32776) then
							--io.write(string.format("Stimulation is a OVTK_StimulationId_BaselineStop\n"))
						end
						if (id==32777) then
							io.write(string.format("Stimulation is a OVTK_StimulationId_RestStart\n"))
							emit = 1
						end
						if (id==32778) then
							--io.write(string.format("Stimulation is a OVTK_StimulationId_RestStop\n"))
							count=count+1
							io.write(string.format("next Letter, idx = %d\n",count))
						end
						if (id==32779) then
							--io.write(string.format("Stimulation is a Visual Start\n"))
							--emit = 1
						end
						if (id==32772) then
							--io.write(string.format("Stimulation is a Segment Stop\n"))
							--count=count+1
							--io.write(string.format("next Letter, idx = %d\n",count))
						end

                        -- discards it
                        box:remove_stimulation(1, 1)

                        -- add triggers : new OVTK_StimulationId_Label_XX stimulation 
						if (emit==1) then
							if (count < #CommandInput) then 
								io.write(string.format("case r = %s, c= %s\n",CommandInput[count+1][1][1],CommandInput[count+1][2][2]))
								box:send_stimulation(1, CommandContextRow[CommandInput[count+1][1][1]], t, duration) 
								box:send_stimulation(1, CommandContextCol[CommandInput[count+1][2][2]], t, duration) 
							else
								io.write("out of CommandInput\n")
							end
						end
						
						--resend the base stimulation
						--box:send_stimulation(1, id, t, duration)
					end

                -- releases cpu
                box:sleep()
        end
end


tonio112003
Posts: 13
Joined: Wed Feb 20, 2013 4:13 pm

Re: lua stimulation some useful example

Post by tonio112003 »

Dear jlegeny,

thank you very much for your quick and full answer!
Now it´s clear for me!

Post Reply