JA_Main_Experiment_Stage2.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. function JA_Main_Experiment_Stage2(r_block, data_subject, PTP_name)
  2. %% initialise workspace
  3. addpath('C:\Users\neuro\Desktop\JA_Stage1');
  4. cd('C:\Users\neuro\Desktop\JA_Stage2');
  5. sca;
  6. close all;
  7. delete(instrfindall)
  8. %
  9. tinker = 0;
  10. show_RT = 0;
  11. relationship = r_block{1};
  12. TMS_delay = {'-0.05'} % from -100 to-0 from CF response
  13. tic;
  14. cfg = []
  15. cfg.relationship = relationship;%'competitive' %'parallel', 'joint', 'imagery'
  16. cfg.PTP_name = PTP_name;
  17. cfg.TMS_delay = TMS_delay;
  18. % [saving_settings, relationship, PTP_name] = JA_saving_settings(cfg);
  19. %
  20. [window, screen_dimensions, screenNum, CenterX, CenterY, ifi] = JA_Screen_Settings(tinker);
  21. screen_dimensions_ratio = screen_dimensions(4)/screen_dimensions(3);
  22. GD = JA_CFs_quickness_games_distribution(relationship);
  23. % final design is a table containing all the info of each trial
  24. [final_design, game, real_trials, catch_trials] = JA_CFs_trial_type_within_games_Stage2(GD);
  25. % add balanced out_slow and out_fast
  26. [final_design] = JA_Add_balanced_OUT_trials_CFs_within_games_Stage2(final_design)
  27. % add delays between ball landing and color appearence
  28. [final_design] = JA_ball_colors_delays(final_design, ifi)
  29. % CF's response delays
  30. [final_design] = JA_CF_response_delays_and_timings(final_design, ifi)
  31. % trigger values
  32. [final_design] = JA_trigger_values_setting(final_design)
  33. % PTP (SBJ) performance type
  34. final_design(:,14) = cell(size(final_design,1),1);
  35. final_design.Properties.VariableNames{14} = 'SBJ_performance_Type';
  36. % TMS delays
  37. [final_design] = JA_TMS_random_delays(final_design, TMS_delay, ifi)
  38. %% clear other opened ports
  39. delete(instrfindall);
  40. clear s
  41. %Set up the serial port
  42. global s;
  43. s = serial('COM5'); % serial COM for Arduino connection. different for different systems
  44. set(s, 'BaudRate', 115200); % set BaudRate to 115200 (as in Arduino!!!!!)
  45. % open serial
  46. fopen(s);
  47. WaitSecs(2);
  48. for d = 1:2
  49. scrittura(s); % scrittura reads instantaneous arduino values from the three pressure sensors
  50. end
  51. % define Parallel Port (mex file utility)
  52. % downloadable here
  53. % http://apps.usd.edu/coglab/psyc770/IO64.html
  54. ioObj = io64;
  55. status = io64(ioObj);
  56. % specify address where you want to send triggers (assumning triggering on the rise front of the TTL)
  57. address = hex2dec('DFB8'); %select LTP1 output port address (different for different systems)
  58. io64(ioObj, address, 0); %initialise the parallel port "out" pins to zero
  59. %%
  60. % Par is going to be the structure that contains all the infos
  61. Par = [];
  62. Par.PTP_name = PTP_name;
  63. Par.window = window;
  64. Par.screen_dimensions = screen_dimensions;
  65. Par.ifi = ifi; % 60Hz in our system
  66. Par.VBL = [];
  67. Par.tr = 1; % trial number
  68. Par.real_trials_x_game = real_trials;
  69. Par.catch_trials_x_game = catch_trials;
  70. Par.trials_x_game = real_trials + catch_trials;
  71. Par.charge = 0.3; % piston delay after press/lift threshold has been reached (secs)
  72. Par_baseline{Par.tr} = [];
  73. Par.relationship = relationship;
  74. Par.final_design = final_design;
  75. Par.initial_color = [0.5 0.5 0.5];
  76. Par.lift_color = [1 1 0];%yellow
  77. Par.press_color = [1 0.0 0.0];% red
  78. Par.catch_color = [1 0.55 0.0]; % orange
  79. Par.color_change_duration = 4*Par.ifi; %0.067 secs
  80. Par.address = address;
  81. Par.PP = ioObj;
  82. Par.threshold_catch_response = 0.05;
  83. Par.SBJ_RT_tmp = {[]};
  84. Par.SBJ_RT = {[]};
  85. Par.SBJ_classification = {[]};
  86. Par.SBJ_RT_median = {[]};
  87. Par.CF_RT = {[]};
  88. Par.CF_RT_median = {[]};
  89. Par.VBL_first_frame{1} = [];
  90. Par.VBL_ball_landed_on_table_CF{1} = [];
  91. Par.VBL_ball_delay_CF{1} = [];
  92. Par.VBL_ball_changes_color_CF{1} = [];
  93. Par.VBL_ball_back_to_gray_CF{1} = [];
  94. Par.VBL_piston_CF{1} = [];
  95. Par.VBL_TMS{1} = [];
  96. Par.TMS_delay = TMS_delay;
  97. Par.VBL_ball_landed_on_table_SBJ{1} = [];
  98. Par.VBL_ball_changes_color_SBJ{1} = [];
  99. Par.VBL_ball_back_to_gray_SBJ{1} = [];
  100. Par.VBL_piston_SBJ{1} = [];
  101. Par.outcome_feedback_trials = find(diff(cell2mat(Par.final_design.Games)));
  102. % task instructions from pictures
  103. JA_instruction_selection(Par);
  104. %%
  105. HideCursor
  106. switch Par.relationship
  107. case 'warming_up'
  108. % parallel port triggering (only EEG trigger, no TMS)
  109. % according to the relationship condition of the block
  110. EEG_Trigger(Par.PP,Par.address,122);
  111. case 'training'
  112. EEG_Trigger(Par.PP,Par.address,123);
  113. case 'joint'
  114. EEG_Trigger(Par.PP,Par.address,124);
  115. case 'parallel'
  116. EEG_Trigger(Par.PP,Par.address,125);
  117. case 'competitive'
  118. EEG_Trigger(Par.PP,Par.address,126);
  119. case 'imagery'
  120. EEG_Trigger(Par.PP,Par.address,127);
  121. end
  122. %%
  123. for x = 1:size(final_design,1)
  124. Par.tr = x;
  125. % calculate the CF_RT for the following trial
  126. % better here in order not to waste run-time
  127. % initialise CF and SBJ RTs
  128. Par.CF_RT{Par.tr} = [];
  129. Par.SBJ_RT{Par.tr} = [];
  130. [Par] = JA_Average_previous_trials(Par);
  131. % initialize objects on the screen
  132. [Ob, Par] = JA_Initialise_Shapes(Par);
  133. %at the onset check if sensors are pressed and saves the baseline
  134. %(initial pressure conditions which will be used to detect lifts and presses)
  135. [bs, Par] = JA_check_hands_Main_Experiment(Par,s);
  136. Par.baseline{Par.tr} = bs;
  137. % load the objects on the 'hidden background screen' before flipping it on the
  138. % forescreen
  139. [Ob, Par]= JA_Load_Obj_On_Screen(Par, Ob);
  140. [Par.VBL] = Screen(Par.window, 'Flip');
  141. Par.VBL_first_frame{Par.tr} = Par.VBL;
  142. value = Par.final_design.Trial_Type_Trigger(Par.tr);
  143. EEG_Trigger(Par.PP,Par.address,value);
  144. % trial starts --> ball falls down --> when toiches the table_CF it
  145. Par.VBL_ball_landed_on_table_CF{Par.tr} = [];
  146. arg = [];
  147. arg.player = 'CF';
  148. arg.distanceX = 0; % proportion of the horizontal screen side
  149. arg.distanceY = 0.4; % proportion of the vertical screen side
  150. [Ob, Par] = JA_Object_Movement(Par,Ob, arg);
  151. % after a delay --> change ball color
  152. Par.VBL_ball_changes_color_CF{Par.tr} = [];
  153. Par.VBL_ball_back_to_gray_CF{Par.tr} = [];
  154. arg = [];
  155. arg.player = 'CF';
  156. [Ob, Par] = JA_Ball_color_change(Par,Ob, arg);
  157. % after CF response waiting time CF piston hits the ball
  158. Par.VBL_TMS{Par.tr} = [];
  159. Par.VBL_piston_CF{Par.tr} = [];
  160. [Ob,Par] = JA_CF_performance(Par, Ob);
  161. % SBJ turn starts --> ball moves leftwards --> the ball touches the table_SBJ
  162. Par.VBL_ball_landed_on_table_SBJ{Par.tr} = [];
  163. arg = [];
  164. arg.player = 'SBJ';
  165. arg.distanceX = -0.4; % proportion of the vertical screen side
  166. arg.distanceY = 0.0; % proportion of the vertical screen side
  167. [Ob, Par] = JA_Object_Movement(Par,Ob, arg);
  168. % after a delay change color
  169. Par.VBL_ball_changes_color_SBJ{Par.tr} = [];
  170. Par.VBL_ball_back_to_gray_SBJ{Par.tr} = [];
  171. arg = [];
  172. arg.player = 'SBJ';
  173. [Ob, Par] = JA_Ball_color_change(Par,Ob, arg);
  174. % after SBJ response SBJ's piston hits the ball
  175. Par.VBL_piston_SBJ{Par.tr} = [];
  176. arg = [];
  177. arg.player = 'SBJ';
  178. arg.sensor = 'SBJ_RISP';
  179. [Ob,Par] = JA_SBJ_performance(Par, Ob, arg, s);
  180. % the ball has been hit by SBJ's piston and travels towards the top
  181. % of the screen
  182. arg = [];
  183. arg.player = 'no_player';
  184. arg.distanceX = 0.0; % proportion of the vertical screen side
  185. arg.distanceY = -0.4; % proportion of the vertical screen side
  186. [Ob, Par] = JA_Object_Movement(Par,Ob, arg);
  187. % feedback screen if something went wrong or catch trial
  188. % in order to avoid messages on the CF side in motor imagery blocks
  189. if strcmp(Par.relationship, 'imagery')
  190. [Par] = JA_interim_feedback_IMAGERY_Stage_2(Par);
  191. else
  192. [Par] = JA_interim_feedback(Par)
  193. end
  194. % outcome feedback: it presents outcome about the game performance.
  195. % motor imagery outcome feedbacks are already taken care of
  196. % inside the function
  197. [Par] = JA_outcome_feedback(Par, GD)
  198. scrittura(s)
  199. end
  200. sca
  201. % save(saving_settings);
  202. save(data_subject);
  203. end_block_time = toc;
  204. end