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. 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. col_base = _G[box:get_setting(3)]
  49. n_rows = box:get_setting(4)
  50. n_repetitions = box:get_setting(5)
  51. flash_duration = box:get_setting(7)
  52. noflash_duration = box:get_setting(8)
  53. inter_repetition_delay = box:get_setting(9)
  54. inter_trial_delay = box:get_setting(10)
  55. send_toggle = _G[box:get_setting(11)]
  56. time_to_send = box:get_setting(13)
  57. if box:get_setting(12) == 'true' then
  58. free_spelling = true
  59. n_trials = 1
  60. else
  61. free_spelling = false
  62. n_trials = box:get_setting(6)
  63. end
  64. --Lua variables
  65. send = false
  66. experiment_end = false
  67. tactilo_stimcodes = {}
  68. for x = 1, n_rows do
  69. tactilo_stimcodes[x] = row_base + x - 1
  70. end
  71. end
  72. -- this function is called when the box is uninitialized
  73. function uninitialize(box)
  74. end
  75. -- this function is called once by the box
  76. function process(box)
  77. while box:keep_processing() do
  78. if send and not experiment_end then
  79. -- iterate over the number of trials
  80. for trial = 1, n_trials do
  81. box:send_stimulation(1, OVTK_StimulationId_RestStart, box:get_current_time() , 0)
  82. wait_for(box, inter_trial_delay)
  83. box:send_stimulation(1, OVTK_StimulationId_RestStop, box:get_current_time() , 0)
  84. box:send_stimulation(1, OVTK_StimulationId_TrialStart ,box:get_current_time() , 0)
  85. -- iterate over the number of repetitions
  86. for segment = 1, n_repetitions do
  87. tactilo_stimcodes = create_sequence(tactilo_stimcodes)
  88. box:send_stimulation(1, OVTK_StimulationId_SegmentStart ,box:get_current_time() , 0)
  89. -- iterate over the number of tactilos
  90. for i = 1, #tactilo_stimcodes do
  91. box:send_stimulation(1, tactilo_stimcodes[i] ,box:get_current_time() , 0)
  92. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStart ,box:get_current_time() , 0)
  93. wait_for(box, flash_duration)
  94. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStop ,box:get_current_time() , 0)
  95. wait_for(box, noflash_duration)
  96. end
  97. box:send_stimulation(1, OVTK_StimulationId_SegmentStop ,box:get_current_time() , 0)
  98. wait_for(box, inter_repetition_delay)
  99. end
  100. box:send_stimulation(1, OVTK_StimulationId_TrialStop ,box:get_current_time() , 0)
  101. end
  102. if not free_spelling then
  103. -- end experiment if set to copy spelling
  104. box:send_stimulation(1, OVTK_StimulationId_ExperimentStop ,box:get_current_time() , 0)
  105. send = false
  106. experiment_end = true
  107. end
  108. else if not send and not experiment_end then
  109. -- delay the start of the experiment by t = time_to_send
  110. wait_for(box, time_to_send)
  111. send = true
  112. box:send_stimulation(1, OVTK_StimulationId_ExperimentStart ,box:get_current_time() , 0)
  113. end
  114. end
  115. -- releases cpu
  116. box:sleep()
  117. end
  118. end