You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

p300-tactile-stimulator.lua 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. -- This Lua script sends Stimulations to the speller visualization box for a tactile p300-System with 6 stimulators
  2. --
  3. -- Author : Tobias Baumann
  4. -- Date : 2021-12-06
  5. -- Revised: 2021-19-11
  6. --This function lets the box sleep until a fixed moment
  7. function wait_until(box, time)
  8. while box:get_current_time() < time do
  9. box:sleep()
  10. end
  11. end
  12. --This function lets the box wait for a fixed duration
  13. function wait_for(box, duration)
  14. wait_until(box, box:get_current_time() + duration)
  15. end
  16. --this function checks, wether value already is an element of the given stim_matrix
  17. function is_element(matrix, value)
  18. for i = 1, #matrix do
  19. if #matrix == 0 then
  20. return(false)
  21. elseif value == matrix[i] then
  22. return(true)
  23. end
  24. end
  25. return(false)
  26. end
  27. --this function creates a sequence of stimulations by shuffeling the values of the given stim_matrix
  28. function create_sequence(matrix)
  29. local stim_matrix = {}
  30. local stim_code = 0
  31. local i = 1
  32. while i <= #matrix do
  33. stim_code = matrix[math.random(1,#matrix)]
  34. if is_element(stim_matrix, stim_code) == false then
  35. stim_matrix[i] = stim_code
  36. i = i + 1
  37. end
  38. end
  39. return(stim_matrix)
  40. end
  41. -- this function is called when the box is initialized
  42. function initialize(box)
  43. --randomseed
  44. math.randomseed(os.time())
  45. --load stimulation codes
  46. dofile(box:get_config("${Path_Data}") .. "/plugins/stimulation/lua-stimulator-stim-codes.lua")
  47. --load box settings
  48. row_base = _G[box:get_setting(2)]
  49. n_tactilos = box:get_setting(3)
  50. n_repetitions = box:get_setting(4)
  51. n_trials = box:get_setting(5)
  52. flash_duration = box:get_setting(6)
  53. noflash_duration = box:get_setting(7)
  54. inter_repetition_delay = box:get_setting(8)
  55. inter_trial_delay = box:get_setting(9)
  56. send_toggle = _G[box:get_setting(10)]
  57. free_spelling = box:get_setting(11)
  58. time_to_send = box:get_setting(12)
  59. --Set number of trials to 1 in free spelling mode
  60. if free_spelling == 'true' then
  61. n_trials = 1
  62. end
  63. --Lua variables
  64. send = false
  65. experiment_end = false
  66. tactilo_stimcodes = {}
  67. for x = 1, n_tactilos do
  68. tactilo_stimcodes[x] = row_base + x - 1
  69. end
  70. end
  71. -- this function is called when the box is uninitialized
  72. function uninitialize(box)
  73. end
  74. -- this function is called once by the box
  75. function process(box)
  76. while box:keep_processing() do
  77. if send and not experiment_end then
  78. -- iterate over the number of trials
  79. for trial = 1, n_trials do
  80. box:send_stimulation(1, OVTK_StimulationId_RestStart, box:get_current_time() , 0)
  81. wait_for(box, inter_trial_delay)
  82. box:send_stimulation(1, OVTK_StimulationId_RestStop, box:get_current_time() , 0)
  83. box:send_stimulation(1, OVTK_StimulationId_TrialStart ,box:get_current_time() , 0)
  84. -- iterate over the number of repetitions
  85. for segment = 1, n_repetitions do
  86. tactilo_stimcodes = create_sequence(tactilo_stimcodes)
  87. box:send_stimulation(1, OVTK_StimulationId_SegmentStart ,box:get_current_time() , 0)
  88. -- iterate over the number of tactilos
  89. for i = 1, #tactilo_stimcodes do
  90. box:send_stimulation(1, tactilo_stimcodes[i] ,box:get_current_time() , 0)
  91. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStart ,box:get_current_time() , 0)
  92. wait_for(box, flash_duration)
  93. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStop ,box:get_current_time() , 0)
  94. wait_for(box, noflash_duration)
  95. end
  96. box:send_stimulation(1, OVTK_StimulationId_SegmentStop ,box:get_current_time() , 0)
  97. wait_for(box, inter_repetition_delay)
  98. end
  99. box:send_stimulation(1, OVTK_StimulationId_TrialStop ,box:get_current_time() , 0)
  100. end
  101. if free_spelling == 'false' then
  102. -- end experiment if set to copy spelling
  103. box:send_stimulation(1, OVTK_StimulationId_ExperimentStop ,box:get_current_time() , 0)
  104. send = false
  105. experiment_end = true
  106. end
  107. else if not send and not experiment_end then
  108. -- delay the start of the experiment by t = time_to_send
  109. wait_for(box, time_to_send)
  110. send = true
  111. box:send_stimulation(1, OVTK_StimulationId_ExperimentStart ,box:get_current_time() , 0)
  112. end
  113. end
  114. -- releases cpu
  115. box:sleep()
  116. end
  117. end