Commits

Jinyang Liu committed 81babad

Update matlab code for Rev1.2 firmware

Comments (0)

Files changed (33)

Matlab Codes/IO_tools/choose_functions.fig

Binary file modified.

Matlab Codes/IO_tools/choose_functions.m

 
 function button_add_function_Callback(hObject, eventdata, handles)
 % --- Executes on button press in button_add_function.
-panel_control_paths;
+load('Pcontol_paths.mat');
 cd(function_path)
 [FileName,PathName] = uigetfile('*function*.mat','Select a FUNCTION file');
 % returns a 0 if cancel is pressed
 
 function button_add_folder_Callback(hObject, eventdata, handles)
 % --- Executes on button press in button_add_folder.
-dir_path = uigetdir;
+load('Pcontrol_paths.mat');
+dir_path = uigetdir(function_path);
 if ~isequal(dir_path, 0)
     dir_struct = dir(fullfile(dir_path, '*function*.mat'));
     [sorted_names,sorted_index] = sortrows({dir_struct.name}');
 % --- Executes on button press in button_burn.
 function button_burn_Callback(hObject, eventdata, handles)
 global SD
-panel_control_paths;
+load('Pcontrol_paths.mat');
 %cd(root_path); % go back to matlab root
 % step 1 - get the drive letter
 SD_drive = get_SD_drive;
         dos(['del ' SD_drive ':\*.fun']);
     end
     
-     dos(['copy /Y ' temp_path '\*.fun ' SD_drive ':\']);  
+     result1 = dos(['copy /Y "' temp_path '\*.fun" ' SD_drive ':\']);  % SS
      %['dd if=' temp_path '\SD.img of=\\.\' SD_drive ': bs=1k &']
     %dos(['dd if=' temp_path '\SD.img of=\\.\' SD_drive ': bs=1k &']);
+    
+        % In some card reader, sd_drive might be a removeable drive, but it is wrong one
+    % give user a chance to input the correct drive
+    if result1
+        SD_drive = userInputSDDrive;
+        
+        if length(dir([SD_drive, ':\*.fun']))
+            dos(['del ' SD_drive ':\*.fun']);
+        end
+        
+        result2 = dos(['copy /Y "' temp_path '\*.fun" ' SD_drive ':\']); % SS
+        
+        if result2
+            disp('The SD_drive is a invalid drive!');
+            return;
+        end
+    end
+    
     SD.function = handles.SD;
     save([controller_path '\SD'], 'SD');
     save([SD_drive ':\SD'], 'SD');

Matlab Codes/IO_tools/choose_pats.fig

Binary file modified.

Matlab Codes/IO_tools/choose_pats.m

 
 function button_add_file_Callback(hObject, eventdata, handles)
 % --- Executes on button press in button_add_file.
-panel_control_paths;
+load('Pcontrol_paths.mat');
 cd(pattern_path)
 [FileName,PathName] = uigetfile('Pattern*.mat','Select a PATTERN file');
 % returns a 0 if cancel is pressed
 
 function button_add_folder_Callback(hObject, eventdata, handles)
 % --- Executes on button press in button_add_folder.
-dir_path = uigetdir;
+load('Pcontrol_paths.mat');
+dir_path = uigetdir(pattern_path);
 if ~isequal(dir_path, 0)
     dir_struct = dir(fullfile(dir_path, 'Pattern*.mat'));
     [sorted_names,sorted_index] = sortrows({dir_struct.name}');
 % --- Executes on button press in button_burn.
 function button_burn_Callback(hObject, eventdata, handles)
 global SD;
-panel_control_paths;
+load('Pcontrol_paths.mat');
 % step 1 - get the drive letter
 SD_drive = get_SD_drive;
 if (SD_drive ~= -1)
         dos(['del ' SD_drive ':\*.pat']);
     end
 
-     results = dos(['copy /Y ' temp_path '\*.pat ' SD_drive ':\']);  
-     %['dd if=' temp_path '\SD.img of=\\.\' SD_drive ': bs=1k &']
-    %dos(['dd if=' temp_path '\SD.img of=\\.\' SD_drive ': bs=1k &']);
+     result1 = dos(['copy /Y "' temp_path '\*.pat" ' SD_drive ':\']);  %% SS
 
-    if results
-        errordlg('Failed to use the dos copy command.');
-        return;
+    % In some card reader, sd_drive might be a removeable drive, but it is wrong one
+    % give user a chance to input the correct drive
+    if result1
+        SD_drive = userInputSDDrive;
+        if length(dir([SD_drive, ':\*.pat']))
+            dos(['del ' SD_drive ':\*.pat']);
+        end
+        
+        result2 = dos(['copy /Y "' temp_path '\*.pat" ' SD_drive ':\']);  %% SS
+        
+        if result2
+            disp('The SD_drive is a invalid drive!');
+            return;
+        end
     end
     
     SD.pattern = handles.SD;

Matlab Codes/IO_tools/get_SD_drive.m

 % [SD_drive] = get_SD_drive
 % function takes no inputs and returns the letter of the SD drive
 % it also checks to see that the SD drive is indeed removable
-global myPCCfg;
 
-panel_control_paths;
+load('Pcontrol_paths.mat');
 
 if exist('myPCCfg.mat','file')
     load([controller_path '\myPCCfg'],'-mat');
     if isfield(myPCCfg, 'SDDrive')
         SD_drive = myPCCfg.SDDrive;
-        myPCCfg.SDDrive = SD_drive;
-        save([controller_path '\myPCCfg'], 'myPCCfg');
-    else
-        SD_drive = userInputSDDrive;
-        myPCCfg.SDDrive = SD_drive;
-        save([controller_path '\myPCCfg'], 'myPCCfg');
+        defaultDrive = {SD_drive};
     end
 else
-    SD_drive = userInputSDDrive;
+    defaultDrive = {'E'};
 end
+       
+SD_drive = userInputSDDrive(defaultDrive);
 
-% this first line will need to be modified by the user to name the drive
-% letter of the SD drive. This is very important!!! even though there is
-% some error checking, if this is wrong, bad things may happen!
 
-% check to make sure the drive is removable
-% step 1 - issue an operating system command to get the drive type
-[s, drive_type] = dos(['fsutil fsinfo drivetype ' SD_drive ':']);
-% step 2 - see if drive type contains string - 'removable drive'
-if (s == 0) && ~isempty(strfind(lower(drive_type), 'removable drive'));
-    display(['SD drive selected is drive ' SD_drive ' which is a removable drive']);
-else
-    SD_drive = userInputSDDRive();
-end
+myPCCfg.SDDrive = SD_drive;
+save([controller_path '\myPCCfg'], 'myPCCfg');

Matlab Codes/IO_tools/make_flash_image.m

 SD.num_patterns = num_patterns;
 
 %clean the temp folder
-panel_control_paths;
-dos(['del /Q ' temp_path '\*.pat']); 
+load('Pcontrol_paths.mat');
+%dos(['del /Q ' temp_path '\*.pat']); % SS
+dos(['del /Q "' temp_path '\*.pat"']); % SS
 
 for j = 1:num_patterns
     load([file_list(j).PathName '\' file_list(j).FileName]);

Matlab Codes/IO_tools/make_function_image.m

 SD.numfunc = num_functions;
 
 %clean the temp folder
-panel_control_paths;
-dos(['del /Q ' temp_path '\*.fun']); 
+load('Pcontrol_paths.mat');
+%dos(['del /Q ' temp_path '\*.fun']); %SS
+dos(['del /Q "' temp_path '\*.fun"']); %SS
 pos_func_counter = 0;
 vel_func_counter = 0;
 

Matlab Codes/IO_tools/open_SD_image.m

 function SD_image = open_SD_image()
 
-panel_control_paths;
+load('Pcontrol_paths.mat');
 fid = fopen([temp_path '\SD.img'] , 'r');
 SD_image = fread(fid, inf, 'uchar');
 fclose('all');

Matlab Codes/IO_tools/userInputSDDrive.m

-function SDdrive = userInputSDDrive()
+function SDdrive = userInputSDDrive(defaultanswer)
 
 prompt={'Enter the drive letter for the SD card:'};
 name='Input SD card drive letter';
 numlines=1;
-defaultanswer={'E'};
+
 answer=inputdlg(prompt,name,numlines,defaultanswer);
-SDdrive = answer{1};
 
-if ~ischar(answer{1})
+if ~isempty(answer) && ischar(answer{1})
+    SDdrive = answer{1};
+else
     SDdrive = -1;
 end

Matlab Codes/IO_tools/vec2dec_fast.m

 function out = vec2dec_fast(vec, gs)
-% BINVEC2DEC_FAST Convert binary vector to decimal number fast.
+% VEC2DEC_FAST Convert binary vector to decimal number fast.
 %
-%    BINVEC2DEC_FAST(B, gs) interprets the vector B and returns the
+%    VEC2DEC_FAST(B, gs) interprets the vector B and returns the
 %    equivalent decimal number.  The least significant bit is 
 %    represented by the first column.
 %
 
 % Error if vec is not defined.
 if isempty(vec)
-   error('daq:binvec2dec:argcheck', 'B must be defined.  Type ''daqhelp binvec2dec'' for more information.');
+   error('daq:vec2dec:argcheck', 'B must be defined.  Type ''daqhelp binvec2dec'' for more information.');
 end
 
 % Error if vec is not a double.

Matlab Codes/Patterns/make_4x4_block_48.m

 pattern.Panel_map = [12 8 4 11 7 3 10 6 2  9 5 1; 24 20 16 23 19 15 22 18 14 21 17 13; 36 32 28 35 31 27 34 30 26 33 29 25; 48 44 40 47 43 39 46 42 38 45 41 37];
 pattern.BitMapIndex = process_panel_map(pattern);
 pattern.data = make_pattern_vector(pattern);
-directory_name = 'C:\Documents and Settings\liuj\My Documents\MATLAB\Xmega_Panel_controller\Patterns';
+directory_name = 'C:\temp';
 %directory_name = 'c:\matlabroot\Panels\Patterns';
 str = [directory_name '\Pattern_4x4_blocks_48']
 save(str, 'pattern');

Matlab Codes/Patterns/make_6_wide_stripe_patterns_48.m

 % %     3     7    11    15    19    23    27    31    35    39    43    47
 % %     2     6    10    14    18    22    26    30    34    38    42    46
 % %     1     5     9    13    17    21    25    29    33    37    41    45
-directory_name = 'C:\Documents and Settings\liuj\My Documents\MATLAB\Xmega_Panel_controller\Patterns';
+directory_name = 'C:\temp';
 pattern.BitMapIndex = process_panel_map(pattern);
 pattern.data = make_pattern_vector(pattern);
 

Matlab Codes/Patterns/make_gs_stripes_pattern_12.m

 pattern.BitMapIndex = process_panel_map(pattern);
 pattern.data = make_pattern_vector(pattern);
 
-directory_name = 'C:\Documents and Settings\liuj\My Documents\MATLAB\Xmega_Panel_controller\Patterns';
+directory_name = 'C:\temp';
 str = [directory_name '\Pattern_gs_stripes_12Pan']
 save(str, 'pattern');

Matlab Codes/arena_config/choose_configs.fig

Binary file modified.

Matlab Codes/arena_config/choose_configs.m

 function button_add_file_Callback(hObject, eventdata, handles)
 % --- Executes on button press in button_add_file.
 curPath = pwd;
-panel_control_paths;
+load('Pcontrol_paths.mat');
 cd(cfg_path);
 
 [FileName,PathName] = uigetfile('cfg*.mat','Select a configuration file');
 
 function button_add_folder_Callback(hObject, eventdata, handles)
 % --- Executes on button press in button_add_folder.
-curPath = pwd;
-panel_control_paths;
-cd(cfg_path);
 
-dir_path = uigetdir;
+load('Pcontrol_paths.mat');
+dir_path = uigetdir(cfg_path);
 if ~isequal(dir_path, 0)
     dir_struct = dir(fullfile(dir_path, 'cfg*.mat'));
     [sorted_names,sorted_index] = sortrows({dir_struct.name}');
     set(handles.button_remove_file, 'enable', 'On');
     set(handles.button_loadcfg, 'enable', 'On');
 end
-cd(curPath);
+
 
 % --- Executes on button press in button_remove_file.
 function button_remove_file_Callback(hObject, eventdata, handles)
 global SD
 
 curPath = pwd;
-panel_control_paths;
+load('Pcontrol_paths.mat');
 cd(cfg_path);
 handles.SD = make_config_image(handles.file);
 % step 1 - get the drive letter
         dos(['del ' SD_drive ':\*.cfg']);
     end
     
-    dos(['copy /Y ' temp_path '\*.cfg ' SD_drive ':\']);  
+    result1 = dos(['copy /Y ' temp_path '\*.cfg ' SD_drive ':\']);
+    
+    % In some card reader, sd_drive might be a removeable drive, but it is wrong one
+    % give user a chance to input the correct drive
+    if result1
+        SD_drive = userInputSDDrive;
+        
+        if length(dir([SD_drive, ':\*.cfg']))
+            dos(['del ' SD_drive ':\*.cfg']);
+        end
+        
+        result2 = dos(['copy /Y ' temp_path '\*.cfg ' SD_drive ':\']);
+        
+        if result2
+            disp('The SD_drive is a invalid drive!');
+            return;
+        end
+    end
      
     SD.arenaConfig = handles.SD;
     save([controller_path '\SD'], 'SD');

Matlab Codes/arena_config/make_config_image.m

 SD.num_configs = num_configs;
 
 %clean the temp folder
-panel_control_paths;
+load('Pcontrol_paths.mat');
 dos(['del /Q ' temp_path '\*.cfg']); 
 
 for j = 1:num_configs

Matlab Codes/binvec2dec.m

+function out = binvec2dec(vec)
+% BINVEC2DEC Convert binary vector to decimal number.
+%
+%    BINVEC2DEC(B) interprets the binary vector B and returns the
+%    equivalent decimal number.  The least significant bit is 
+%    represented by the first column.
+%
+%    Non-zero values will be mapped to 1, e.g. [1 2 3 0] maps
+%    to [1 1 1 0].
+% 
+%    Note: The binary vector cannot exceed 52 values.
+%
+%    Example:
+%       binvec2dec([1 1 1 0 1]) returns 23
+%
+%    See also DEC2BINVEC, BIN2DEC.
+%
+
+%    MP 11-11-98
+%    Copyright 1998-2003 The MathWorks, Inc.
+%    $Revision: 1.7.2.4 $  $Date: 2003/08/29 04:40:41 $
+
+% Error if B is not defined.
+if isempty(vec)
+   error('daq:binvec2dec:argcheck', 'B must be defined.  Type ''daqhelp binvec2dec'' for more information.');
+end
+
+% Error if B is not a double.
+if (~isa(vec, 'double') && ~isa(vec, 'logical'))
+   error('daq:binvec2dec:argcheck', 'B must be a binvec.');
+end
+
+% Non-zero values map to 1.
+vec = vec~=0;
+
+% Convert the binvec [0 0 1 1] to a binary string '1100';
+h = deblank(num2str(fliplr(vec)'))';
+
+% Convert the binary string to a decimal number.
+out = bin2dec(h);
+
+
+

Matlab Codes/binvec2dec_fast.m

+function out = vec2dec_fast(vec, gs)
+% BINVEC2DEC_FAST Convert binary vector to decimal number fast.
+%
+%    BINVEC2DEC(B) interprets the binary vector B and returns the
+%    equivalent decimal number.  The least significant bit is 
+%    represented by the first column.
+%
+%    Non-zero values will be mapped to 1, e.g. [1 2 3 0] maps
+%    to [1 1 1 0].
+% 
+%    Note: The binary vector cannot exceed 52 values.
+%
+%    Example:
+%       binvec2dec([1 1 1 0 1]) returns 23
+%
+%    See also DEC2BINVEC, BIN2DEC.
+%
+
+%    MP 11-11-98
+%    Copyright 1998-2003 The MathWorks, Inc.
+%    $Revision: 1.7.2.4 $  $Date: 2003/08/29 04:40:41 $
+
+% Error if B is not defined.
+if isempty(vec)
+   error('daq:binvec2dec:argcheck', 'B must be defined.  Type ''daqhelp binvec2dec'' for more information.');
+end
+
+% Error if B is not a double.
+if (~isa(vec, 'double') | ( any(vec > (2^(gs)-1)) | any(vec < 0) ) )
+   error('B must be a gsvec, 0 - 2^(gs)-1');
+end
+
+for j = 1:length(vec)
+    binvec(j,:) = dec2bin(vec(j),gs);
+end
+
+% then just turn each binvec into a dec value
+for j = 1:gs
+    out(j) = bin2dec(flipud(binvec(:,j))');
+end
+
+% Non-zero values map to 1.
+vec = vec~=0;
+
+% Convert the binvec [0 0 1 1] to a binary string '1100';
+h = fliplr(vec)';
+
+% Convert the binary string to a decimal number.
+
+[m,n] = size(h);
+
+% Convert to numbers
+twos = pow2(n-1:-1:0);
+
+out = twos*h;
+

Matlab Codes/controller/PControl.fig

Binary file modified.

Matlab Codes/controller/PControl.m

 handles.Running = 0;
 hPcontrol = gcf;
 % run the init program -
+if ~exist('PControl_paths.mat', 'file')
+    initialize_Pcontrol_paths;
+end
+
 handles.PC = PControl_init;
 
 movegui(gcf, 'center');
 currentState.posFuncX = 1;
 currentState.posFuncY = 1;
 currentState.pattID = 1;
+currentState.closeSetPosFun = 0;
+currentState.closeSetFun = 0;
 
 currentState.cfgID = 1;
 
 function menu_set_Pat_ID_Callback(hObject, eventdata, handles)
 global currentState newString SD;
 
-if isfield(SD, 'pattern')
+if SD.pattern.num_patterns ~=0
     setPattern;
     
     %wait till user chooses a pattern or closes the setPattern GUI
     end
     
 else
-    warndlg('There is no pattern loaded on the SD card yet. You should load them before set a pattern!');
+    warndlg('You have no patterns to be set, please load patterns to SD card first.', 'Empty pattern list'); 
 end
 
 % --------------------------------------------------------------------
 function menu_test_adc_Callback(hObject, eventdata, handles)
 % open up a dialog box to get the channel number
 
-prompt = {'Please connect DAC1 to an ADC channel to be tested and connect DAC2 to a scope. You should see a 0 - 4 Volt triangle wave for about 20 seconds. Enter the ADC channel to be tested, from 1 to 8'};
+prompt = {'Please connect DAC0 to an ADC channel to be tested and connect DAC1 to a scope. You should see a 0 - 4 Volt triangle wave for about 20 seconds. Enter the ADC channel to be tested, from 1 to 8'};
 dlg_title = 'test ADC';
 num_lines= 1; def = {'1'};
 answer  = inputdlg(prompt,dlg_title,num_lines,def);
 function menu_test_DIO_Callback(hObject, eventdata, handles)
 % open up a dialog box to get the channel number
 
-helpdlg('Please connect any of INT1-4 to ADC1 channel and connect DAC2 to a scope. You should see a square wave for about 20 seconds.','DIO help');
+helpdlg('Please connect any of INT0-1 to ADC0 channel and connect DAC1 to a scope. You should see a square wave for about 20 seconds.','DIO help');
 
-Panel_com('dio_test', [0]);  %the argument 0 means using ADC1 for the input of the INT1-4
+Panel_com('dio_test', [0]);  %the argument 0 means using ADC0 for the input of the INT1-4
 
 
 % --------------------------------------------------------------------
 % handles    structure with handles and user data (see GUIDATA)
 Panel_com('pc_dumping_mode');
 
+
+
+% --------------------------------------------------------------------
+function set_Pcontrol_paths_Callback(hObject, eventdata, handles)
+% hObject    handle to set_Pcontrol_paths (see GCBO)
+% eventdata  reserved - to be defined in a future version of MATLAB
+% handles    structure with handles and user data (see GUIDATA)
+change_paths;
+
+
+% --- Executes on button press in Update.
+function Update_Callback(hObject, eventdata, handles)
+% hObject    handle to Update (see GCBO)
+% eventdata  reserved - to be defined in a future version of MATLAB
+% handles    structure with handles and user data (see GUIDATA)
+Panel_com('update_gui_info');

Matlab Codes/controller/PControl_init.m

 %PControl_init
 %PControl initializer script
 global serialPort SD myPCCfg;
-panel_control_paths;
+load('Pcontrol_paths.mat');
 
 PC.x_gain_max = 10;
 PC.x_gain_min = -10;
 %Panel_com('sync_sd_info');
 
 % load the SD file - 
-panel_control_paths;
+load('Pcontrol_paths.mat');
 
-try
-    load([controller_path '\SD'])
-    PC.num_patterns = SD.pattern.num_patterns;
-    %PC.numVelFunc = SD.function.numVelFunc;
-    %PC.numPosFunc = SD.function.numPosFunc;
-    for j = 1:SD.pattern.num_patterns
-        PC.pattern_x_size(j) = SD.pattern.x_num(j);
-        PC.pattern_y_size(j) = SD.pattern.y_num(j);
-    end
-catch
-    try % load SD.mat from SD card
-        Panel_com('sync_sd_info');
+if exist('SD.mat','file')
+    try
         load([controller_path '\SD'])
         PC.num_patterns = SD.pattern.num_patterns;
         %PC.numVelFunc = SD.function.numVelFunc;
             PC.pattern_x_size(j) = SD.pattern.x_num(j);
             PC.pattern_y_size(j) = SD.pattern.y_num(j);
         end
-    catch
-        error('cannot find a clean SD.mat file on either PC or SD card!')
+    catch ME
+        SD.pattern.num_patterns=0;
+        warndlg('The SD.mat file on the PC is corrupted by sync_ds_info command, you have to load pattern, fucntion, and/or config files to SD card again.','corrupted SD.mat file');
     end
+        
+else  % first time to ran PControl
+    SD.pattern.num_patterns=0;
+    warndlg('No SD.mat file is found on your PC, you can either use sync_sd_info command to load the SC.mat from SD card or load pattern, fucntion, and/or config files to a empty SD card.', 'No SD.mat found');
 end
 
 PC.num_patterns = SD.pattern.num_patterns;

Matlab Codes/controller/Panel_com.m

     case 'sync_sd_info'
         send_serial(char([1 hex2dec('14')]));
         
+        pause(0.5);
+        %add 'get_version' command because it can trigger the byteavailable
+        %event in the call back function we also remove the corresponding
+        %string before save the mat file
+        send_serial(char([1 hex2dec('15')]));  
+        
     case 'get_version'
         send_serial(char([1 hex2dec('15')]));
         
     case 'quiet_mode_off' %In this mode, feedback information from controller will be shown on the GUI
         send_serial(char([1, hex2dec('18')]));
         
+    case 'update_gui_info' % Update gain, offset, and position information
+        send_serial(char([1, hex2dec('19')]));
+        
     case 'controller_mode'
         send_serial(char([1 hex2dec('21')]));
         
     case 'pc_dumping_mode'
         send_serial(char([1 hex2dec('22')]));
         
-    case 'reset_funccnt_x'  %reset function x count
+    case 'enable_extern_trig'
         send_serial(char([1 hex2dec('23')]));
         
-    case 'reset_funccnt_y'  %reset function y  count
+    case 'disable_extern_trig'
         send_serial(char([1 hex2dec('24')]));
         
+    case 'read_and_set_max_voltage'
+        send_serial(char([1 hex2dec('26')]));
+        
+        
         % two byte commands:
     case 'reset'
         if (~isequal(length(argument),1)||(~isnumeric(argument)))
         
     case 'get_adc_value'
         
-        if ((~isequal(length(argument),1))||(~isnumeric(argument))||(argument(1) >4)||(argument(1) <= 0))
-            error('get_adc_value requires 1 numerical argument, channel number, that is between 1 and 4');
+        if ((~isequal(length(argument),1))||(~isnumeric(argument))||(argument(1) >6)||(argument(1) <= 0))
+            error('get_adc_value requires 1 numerical argument, channel number, that is between 1 and 6');
         end
         
         send_serial( char([2 hex2dec('10') argument(1)]));
+       
+%     case 'set_max_adc23'
+%         
+%         if ((~isequal(length(argument),1))||(~isnumeric(argument))||(argument(1) >10)||(argument(1) <= 0))
+%             error('set_max_adc23 requires 1 numerical argument, max ADC input, that is between 0 and 10');
+%         end
+%         
+%         send_serial( char([2 hex2dec('11') argument(1)]));
         
         % three byte commands:
         
     case 'set_mode'
-        if ((~isequal(length(argument),2))||(~isnumeric(argument))||any(argument > 5)||any(argument(1) < 0))
+        if ((~isequal(length(argument),2))||(~isnumeric(argument))||any(argument > 6)||any(argument(1) < 0))
             error('Loop mode command requires 2 numerical argument, 0,1,2,3,4, or 5 for both X, and Y');
         end
         send_serial( char([3  hex2dec('10') argument(1) argument(2)]));
         % update address: 0xFF; current address, new address
         send_serial( char([3 255 argument(1) argument(2)]));
         
-    case 'set_posfunc_id'
+    case 'set_posfunc_id' 
+        % argument 1 is the channel number 
+        % 1:X channel    2:Y channel
+        % argument 2 is the function id
+        % 0 is default function
         if (~isequal(length(argument),2)||(~isnumeric(argument)))
             error('set position function command requires 2 numerical arguments');
         end
         send_serial( char([3 hex2dec('15') argument(1) argument(2)]));
         
     case 'set_velfunc_id'
+        % argument 1 is the channel number 
+        % 1:X channel    2:Y channel
+        % argument 2 is the function id
+        % 0 is default function
         if (~isequal(length(argument),2)||(~isnumeric(argument)))
             error('set velocity function command requires 2 numerical arguments');
         end
         end
         
         send_serial( char([3 hex2dec('30') dec2char(argument(1),2)]));
-  
+        
+    case 'set_max_voltage'
+        %set the max voltage for channel x and y
+        if ((~isequal(length(argument),2))||(~isnumeric(argument))||(argument(1) >10)||(argument(1) <= 0)||(argument(2) >10)||(argument(2) <= 0))
+            error('set_max_voltage requires 2 numerical argument, max ADC input, that is between 0 and 10');
+        end
+        send_serial(char([3 hex2dec('35') argument(1) argument(2)]));
+        
 % four byte commands:     
     case 'set_ao'
          if ~isequal(length(argument),2)||(~isnumeric(argument))
              error('set_AO(chan, val): channel number ranges from 1 to 4');
          end
          
-         if (argument(2) > 2047)||(argument(2) < 0)
-             error('set_AO(chan, val): val ranges from 0 to 2047 (0-5V)');
+         if (argument(2) > 32767)||(argument(2) < -32767)
+             error('set_AO(chan, val): val ranges from -32767 to 32767 (-10V-+10V)');
          end
          
-         send_serial(char([4 hex2dec('10') argument(1) dec2char(argument(2),2)]));
+         if argument(2) > 0
+            send_serial(char([4 hex2dec('10') argument(1) dec2char(argument(2),2)]));
+         else
+            send_serial(char([4 hex2dec('11') argument(1) dec2char(abs(argument(2)),2)]));
+         end
              
 % five byte commands:
     case 'set_position'
         %Note: these are all signed arguments, so we need to convert to 2's complement if necessary
         send_serial( [5 hex2dec('71') signed_byte_to_char(argument)]);
         
-        % twelve byte command
-        %compress the 96 0/1 laser pattern into a 12 bytes data
+        %compress the 1000 0/1 laser pattern into a 125 bytes data
         %Panel_com('send_laser_pattern',pattern);
-        %argument pattern is a numerical array with 96 elements whose value is either 0 or 1
+        %argument pattern is a binary vector with length from 1 to 1000.
         %for example
-        %Panel_com('send_laser_pattern',[ones(1,24),zeros(1,24),ones(1,24),zeros(1,24)]);
+        %Panel_com('send_laser_pattern',[ones(1,250),zeros(1,250),ones(1,250),zeros(1,250)]);
     case 'send_laser_pattern'
-        if (~isequal(length(argument),96)||(~isnumeric(argument)))
-            error('send_laser_pattern command requires 96 numerical arguments (0 or 1)');
+        if (~isnumeric(argument) || length(argument)> 1000)
+            error('send_laser_pattern command requires binary arguments (0 or 1) amd max number of aruments is 1000');
+        end
+        % pad the remained arguments with 0 
+        if length(argument) < 1000
+            for i = length(argument)+1:1000
+                argument(i) = 0;
+            end
         end
         
-        for i=1:12
+        for i=1:125
             binString = ''; %set binString to empty string
             for j =1:8
                 temp = argument((i-1)* 8+ j);
                 end
                 binString = [binString num2str(temp)];
             end
-            arg12(i) = bin2dec(binString);
+            arg125(i) = bin2dec(binString);
         end
         
         %Note: these are all signed arguments, so we need to convert to 2's complement if necessary
-        send_serial([12 arg12]);
+        send_serial([62 arg125(1:62)]);  %send first 62 byte data
+        pause(0.5);
+        send_serial([63 arg125(63:125)]); %send second 63 byte data
         
         % variable lenth of bytes
         %frame dump into the frame buffer over USB
         send_serial(char([50, signed_16Bit_to_char(argument(1)), signed_16Bit_to_char(argument(2)),...
             signed_16Bit_to_char(argument(3)), argument(4), argument(5), argument(6), argument(7:end)]));
 
-        % case 'dump_frame'
-        % first argument is the 'dump_frame'
-        % argument 2 is x_AO,  0 <= x_AO < 2048
-        % argument 3 is y_AO, 0 < y_AO < 2048
-        % argument 4 is the number of the panels
-        % argument 5 is the gray scale level
-        % argument 6 is the flag for the row compression
-        % argument 7 to the end is the frame data
-        % for example:
-        % Panel_com('dump_frame', [1, 0, 48, 1, 0, round(rand(1,8*48)*255)]);
-%         if (length(argument)>1550)||(~isnumeric(argument))
-%             error('dumping frame requires numerical arguments, maximum data length is 1536 bytes');
-%         end
-%         
-%         if (argument(1)>2048) && (argument(1)<0)
-%             error('analog output value for x channel in the range of 0 - 2047');
-%         else
-%             argument = [signed_16Bit_to_char(argument(1)), argument(2:end)];
-%         end
-%         
-%         if (argument(2)>2048) && (argument(2)<0)
-%             error('analog output value for y channel in the range of 0 - 2047');
-%         else
-%             argument = [argument(1:2),  signed_16Bit_to_char(argument(3)), argument(4:end)];
-%         end
-% 
-%         totalIteration = ceil(length(argument)/50);
-%         remained = mod((length(argument)), 50);
-%         data = zeros(1, 50);
-%        
-%         %tranfer 50 data each time
-%         for counter = 1:totalIteration - 1
-%             for i = 1: 50
-%                 data(i) = argument((counter-1)* 50 + i);
-%             end
-%             send_serial(char([52,counter, totalIteration, data]));
-%         end
-%         
-%         %tranfer the remained data
-%         data= zeros(1, 50);
-%         for i = 1: remained
-%             data(i) = argument((totalIteration - 1)*50 + i);
-%         end
-%         send_serial(char([52,totalIteration, totalIteration, data]));
-%         if (length(argument)>1550)||(~isnumeric(argument))
-%             error('dumping frame requires numerical arguments, maximum data length is 1536 bytes');
-%         end
-%         
-%         if (argument(1)>2048) && (argument(1)<0)
-%             error('analog output value for x channel in the range of 0 - 2047');
-%         else
-%             argument = [signed_16Bit_to_char(argument(1)), argument(2:end)];
-%         end
-%         
-%         if (argument(2)>2048) && (argument(2)<0)
-%             error('analog output value for y channel in the range of 0 - 2047');
-%         else
-%             argument = [argument(1:2),  signed_16Bit_to_char(argument(3)), argument(4:end)];
-%         end
-% 
-%         totalIteration = ceil(length(argument)/400);
-%         remained = mod((length(argument)), 400);
-%         data = zeros(1, 400);
-%        
-%         %tranfer 400 data each time
-%         for counter = 1:totalIteration - 1
-%             for i = 1: 400
-%                 data(i) = argument((counter-1)* 400 + i);
-%             end
-%             send_serial(char([50,counter, totalIteration, data]));
-%         end
-%         
-%         %tranfer the remained data
-%         data= zeros(1, 400);
-%         for i = 1: remained
-%             data(i) = argument((totalIteration - 1)*400 + i);
-%         end
-%         send_serial(char([50,totalIteration, totalIteration, data]));
-        
     otherwise
         error('invalid command name, please check help')
 end

Matlab Codes/controller/Pattern_Player.m

 
 % --------------------------------------------------------------------
 function menu_load_pattern_Callback(hObject, eventdata, handles)
-panel_control_paths;
+load('Pcontrol_paths.mat');
 cd(pattern_path)
 [FileName,PathName] = uigetfile('P*.mat','Select a Pattern File');
 if (all(FileName ~= 0))

Matlab Codes/controller/SD.mat

Binary file modified.

Matlab Codes/controller/displayOnGui.m

 function displayOnGui(obj,event)
 
+global hPcontrol
 % Define error message.
 error1 = 'Type ''help instrument\instrcallback'' for an example using INSTRCALLBACK.';
 error1Id = 'MATLAB:instrument:instrcallback:invalidSyntax';
 % fprintf([EventType ' event occurred at ' datestr(EventDataTime,13),...
 % 	' for the object: ' name '.\n']);
 
+if ~isempty(hPcontrol)
+    handles = guihandles(hPcontrol);
+end
+
 % Display the error string.
 if strcmpi(EventType, 'error')
 	newString = fprintf([EventData.Message '\n']);
 end
 
+
+
 if strcmpi(EventType, 'BytesAvailable')
         byteLen = obj.BytesAvailable;
         if byteLen >= 250  %make sure 250 is a good threshhold
                         indexJ = indexJ+1;
                     end
                 end
-                panel_control_paths;
+                %We used two commands in order to synchronize sd card information in 2011b. 
+                %one command is 'sync_sd_info' and the other is 'get_version'
+                %The reply of 'get_version' can trigger the byteavailable
+                %event
+                matFile = matFile(1:end-31); %Remove the answer from the current version query 
+                load('Pcontrol_paths.mat');
                 SDfile = fullfile(controller_path, 'SD.mat');
                 sdFid = fopen(SDfile, 'w');
                 fwrite(sdFid, matFile);
                 fclose(sdFid);
-                newString = 'The SD.matis tranferred to PC. Please restart PControl.';
+                newString = 'The SD.mat is tranferred to PC. Please restart PControl.';
             else %the data are not SD.mat
                 newString = char(out)';
             end
             
-        elseif byteLen > 0  % 0 < byteLen < 1000
+        elseif byteLen > 0  % 0 < byteLen < 250
              messRevd = fscanf(obj);
              newString = strtrim(messRevd);  %found a hardware reset
              if strcmp(newString, 'Main Controller Works')
                  initialized_PControl_display();
              end
+             if strncmp(newString, 'update:', 7);
+                update_display_xy(newString);
+             end
         else  %byteLen = 0
             return;
         end

Matlab Codes/controller/init_serial.m

         %!mode com8:115200,n,8,1
         serialPort = serial(serialPortName);
         serialPort.BaudRate = 921600;
+        %serialPort.BaudRate = 115200;
         serialPort.DataBits=8;
         serialPort.Parity='none';
         serialPort.StopBits=1;

Matlab Codes/controller/play_current_pattern.m

 guidata(hObject, handles);
 
 global currentState;
-panel_control_paths;
+load('Pcontrol_paths.mat');
 pattFullName = fullfile(pattern_path, currentState.pattName);
 load(pattFullName);
     

Matlab Codes/controller/setPattern.m

 
 
 handles.output = hObject;
+
 patternList = SD.pattern.pattNames;
 set(handles.setAPattern, 'String', patternList);
 set(handles.setAPattern, 'Value', currentState.pattID);
 currentState.pattName = SD.pattern.pattNames{1};
-currentState.chosePat = 0; % a flag to judge whether user chooses a pattern 
+currentState.chosePat = 0; % a flag to judge whether user chooses a pattern
 currentState.closeSetPat = 0; % a flag to check whether user closes the GUI
+
 guidata(hObject, handles);
 
-
 % UIWAIT makes setAPattern wait for user response (see UIRESUME)
 % uiwait(handles.figure1);
 
 % Get default command line output from handles structure
 varargout{1} = handles.output;
 
+
 % --- Executes on selection change in setAPattern.
 function setAPattern_Callback(hObject, eventdata, handles)
 % hObject    handle to setAPattern (see GCBO)
 % hObject    handle to loadPattern (see GCBO)
 % eventdata  reserved - to be defined in a future version of MATLAB
 % handles    structure with handles and user data (see GUIDATA)
-global currentState;
-currentState.chosePat =1;
-currentState.closeSetPat = 1;
-pattID = currentState.pattID;
-Panel_com('set_pattern_id', [pattID]); 
-close(gcf);
+global currentState SD;
+if SD.pattern.num_patterns ~=0
+    currentState.chosePat =1;
+    currentState.closeSetPat = 1;
+    pattID = currentState.pattID;
+    Panel_com('set_pattern_id', [pattID]);
+    close(gcf);
+end
+    
 
 % --- Executes on button press in showPatt.
 function showPatt_Callback(hObject, eventdata, handles)
 % eventdata  reserved - to be defined in a future version of MATLAB
 % handles    structure with handles and user data (see GUIDATA)
 global currentState;
-panel_control_paths;
+load('Pcontrol_paths.mat');
 pattFullName = fullfile(pattern_path, currentState.pattName);
 if exist(pattFullName)
     play_current_pattern;

Matlab Codes/functions/setFunction.fig

Binary file modified.

Matlab Codes/functions/setFunction.m

 
 % Edit the above text to modify the response to help setFunction
 
-% Last Modified by GUIDE v2.5 17-Dec-2009 11:37:05
+% Last Modified by GUIDE v2.5 21-Apr-2011 08:30:51
 % Begin initialization code - DO NOT EDIT
 gui_Singleton = 1;
 gui_State = struct('gui_Name',       mfilename, ...
 close(gcf);
 
 function display_function(functionName)
-panel_control_paths; % run to get path for functions.
+load('Pcontrol_paths.mat');
 funcFile = fullfile(function_path,functionName);
 if ~exist(funcFile)
     [filename, pathname] = uigetfile('function*.mat', 'Pick an velocity function file');
 global currentState;
 currentState.closeSetFun = 1;
 delete(hObject);
-
-

Matlab Codes/initialize_Pcontrol_paths.m

+%This script initialized the default paths for the whole project. It is
+%only ran one time when user first install the PControl module. It will
+%generate a PControl_paths.mat file.
+
+
+% set PControl paths
+mfile_path = mfilename('fullpath');
+[root_path, ~, ~] = fileparts(mfile_path);
+% function_path = strcat(root_path, filesep, 'functions'); % commented SS
+% pattern_path =  strcat(root_path, filesep, 'patterns'); % commented SS
+function_path = fullfile(root_path, 'functions');  % SS
+pattern_path =  fullfile(root_path, 'patterns');   % SS
+controller_path = strcat(root_path, filesep, 'controller');
+cfg_path = strcat(root_path, filesep, 'arena_config');
+
+%%% The following lines were commented out by SS
+% % Create temporary directory if it doesn't exist
+% home_path = sprintf('%s%s',getenv('HOMEDRIVE'),getenv('HOMEPATH'));
+% pcontrol_home_path = strcat(home_path, filesep, 'pcontrol');
+% if ~exist(pcontrol_home_path, 'dir')
+%     [flag, msg, ~] = mkdir(home_path, 'pcontrol');
+%     if flag ~= true
+%         error('unable to create directory: %s, %s', home_path_pcontrol, msg);
+%     end
+% end
+% temp_path = strcat(pcontrol_home_path, filesep, 'temp');
+% if ~exist(temp_path, 'dir')
+%    [flag, msg, ~] = mkdir(pcontrol_home_path, 'temp');
+%    if flag ~= true
+%        error('unable to create directory: %s, %s', temp_path, msg);
+%    end
+% end
+
+%%% added by SS
+temp_path = fullfile(root_path, 'temp');
+if ~exist(temp_path, 'dir')
+   [flag, msg, ~] = mkdir(temp_path);
+   if flag ~= true
+       error('unable to create directory: %s, %s', temp_path, msg);
+   end
+end
+
+save(fullfile(root_path,'Pcontrol_paths.mat'), 'function_path', 'pattern_path', 'controller_path', 'cfg_path', 'root_path', 'temp_path');
+
+% function_path = 'D:\Michael_Reiser\XmegaController_Matlab\functions';
+% pattern_path =  'D:\Michael_Reiser\XmegaController_Matlab\patterns';
+% temp_path = 'C:\temp';
+% root_path = 'D:\Michael_Reiser\XmegaController_Matlab';
+% controller_path = 'D:\Michael_Reiser\XmegaController_Matlab\controller';
+% cfg_path = 'D:\Michael_Reiser\XmegaController_Matlab\arena_config';

Matlab Codes/test_scripts/jitterTesting.m

 %set patten id first
-Panel_com('set_pattern_id', 9);
+Panel_com('set_pattern_id', 1);
 
 %create analog input channel and logging mode, NI-USB6009
-ai = analoginput('nidaq', 'Dev2');
+ai = analoginput('nidaq', 'Dev6');
  
 %AI0 and AI4 are the postive and negative inputs of differential analog input channel 0
 chans = addchannel(ai, 0); 
 set(ai, 'SampleRate', 10000);
 set(ai,'LoggingMode','Disk&Memory');
-set(ai,'LogFileName','c:\temp\jitter1.daq');
+set(ai,'LogFileName','c:\temp\jitter10.daq');
 set(ai,'SamplesPerTrigger', inf);
 
 start(ai);

Matlab Codes/vec2dec_fast.m

+function out = vec2dec_fast(vec, gs)
+% VEC2DEC_FAST Convert binary vector to decimal number fast.
+%
+%    VEC2DEC(B) interprets the binary vector B and returns the
+%    equivalent decimal number.  The least significant bit is 
+%    represented by the first column.
+%
+%    Non-zero values will be mapped to 1, e.g. [1 2 3 0] maps
+%    to [1 1 1 0].
+% 
+%    Note: The binary vector cannot exceed 52 values.
+%
+%    Example:
+%       vec2dec_fast([1 1 1 0 1],1) returns 23
+%
+
+
+
+% Error if vec is not defined.
+if isempty(vec)
+   error('daq:vec2dec:argcheck', 'B must be defined.  Type ''daqhelp binvec2dec'' for more information.');
+end
+
+% Error if vec is not a double.
+if (~isa(vec, 'double') | ( any(vec > (2^(gs)-1)) | any(vec < 0) ) )
+   error('B must be a gsvec, 0 - 2^(gs)-1');
+end
+
+if gs~= 1
+    for j = 1:length(vec)
+        %binvec(j,:) = dec2bin(vec(j),gs);
+        d = vec(j);
+        n = round(double(gs));
+        [~,e]=log2(max(d));
+        binvec(j,:) = rem(floor(d*pow2(1-max(n,e):0)),2);
+    end
+    
+    % Convert the binary string to a decimal number.
+    
+    n=length(vec);
+    
+    % Convert to numbers
+    twos = pow2(n-1:-1:0);
+    
+    % then just turn each binvec into a dec value
+    for j = 1:gs
+        out(j) = twos*flipud(binvec(:,j));
+    end
+    
+else
+    % Convert the binvec [0 0 1 1] to a binary string '1100';
+    h = fliplr(vec);
+    
+    % Convert the binary string to a decimal number.
+    
+    [m,n] = size(h);
+    
+    % Convert to numbers
+    twos = pow2(n-1:-1:0);
+    
+    out = twos*h';
+
+end