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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. math.randomseed(os.time())
  30. local stim_matrix = {}
  31. local stim_code = 0
  32. local i = 1
  33. while i <= #matrix do
  34. stim_code = matrix[math.random(1,#matrix)]
  35. if is_element(stim_matrix, stim_code) == false then
  36. stim_matrix[i] = stim_code
  37. i = i + 1
  38. end
  39. end
  40. return(stim_matrix)
  41. end
  42. -- this function is called when the box is initialized
  43. function initialize(box)
  44. --load stimulation codes
  45. dofile(box:get_config("${Path_Data}") .. "/plugins/stimulation/lua-stimulator-stim-codes.lua")
  46. --load box settings
  47. row_base = _G[box:get_setting(2)]
  48. n_tactilos = box:get_setting(3)
  49. n_repetitions = box:get_setting(4)
  50. n_trials = box:get_setting(5)
  51. flash_duration = box:get_setting(6)
  52. noflash_duration = box:get_setting(7)
  53. inter_repetition_delay = box:get_setting(8)
  54. inter_trial_delay = box:get_setting(9)
  55. send_toggle = _G[box:get_setting(10)]
  56. free_spelling = box:get_setting(11)
  57. time_to_send = box:get_setting(12)
  58. --Set number of trials to 1 in free spelling mode
  59. if free_spelling == 'true' then
  60. n_trials = 1
  61. end
  62. --Lua variables
  63. send = false
  64. experiment_end = false
  65. tactilo_stimcodes = {}
  66. for x = 1, n_tactilos do
  67. tactilo_stimcodes[x] = row_base + x - 1
  68. end
  69. end
  70. -- this function is called when the box is uninitialized
  71. function uninitialize(box)
  72. end
  73. -- this function is called once by the box
  74. function process(box)
  75. while box:keep_processing() do
  76. if send and not experiment_end then
  77. -- iterate over the number of trials
  78. for trial = 1, n_trials do
  79. box:send_stimulation(1, OVTK_StimulationId_RestStart, box:get_current_time() , 0)
  80. wait_for(box, inter_trial_delay)
  81. box:send_stimulation(1, OVTK_StimulationId_RestStop, box:get_current_time() , 0)
  82. box:send_stimulation(1, OVTK_StimulationId_TrialStart ,box:get_current_time() , 0)
  83. -- iterate over the number of repetitions
  84. for segment = 1, n_repetitions do
  85. tactilo_stimcodes = create_sequence(tactilo_stimcodes)
  86. box:send_stimulation(1, OVTK_StimulationId_SegmentStart ,box:get_current_time() , 0)
  87. -- iterate over the number of tactilos
  88. for i = 1, #tactilo_stimcodes do
  89. box:send_stimulation(1, tactilo_stimcodes[i] ,box:get_current_time() , 0)
  90. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStart ,box:get_current_time() , 0)
  91. wait_for(box, flash_duration)
  92. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStop ,box:get_current_time() , 0)
  93. wait_for(box, noflash_duration)
  94. end
  95. box:send_stimulation(1, OVTK_StimulationId_SegmentStop ,box:get_current_time() , 0)
  96. wait_for(box, inter_repetition_delay)
  97. end
  98. box:send_stimulation(1, OVTK_StimulationId_TrialStop ,box:get_current_time() , 0)
  99. end
  100. if free_spelling == 'false' then
  101. -- end experiment if set to copy spelling
  102. box:send_stimulation(1, OVTK_StimulationId_ExperimentStop ,box:get_current_time() , 0)
  103. send = false
  104. experiment_end = true
  105. end
  106. else if not send and not experiment_end then
  107. -- delay the start of the experiment by t = time_to_send
  108. wait_for(box, time_to_send)
  109. send = true
  110. box:send_stimulation(1, OVTK_StimulationId_ExperimentStart ,box:get_current_time() , 0)
  111. end
  112. end
  113. -- releases cpu
  114. box:sleep()
  115. end
  116. end