0001 % This script communicates with the program TSL2561_data_acquisition running on
0002 % an arduino supported atmega uc. It instructs the uc how the TSL2561's
0003 % sensitivity and integration time should be set and receives the counts output
0004 % from the uc.
0005 %
0006 % The atmega uc is controlled with simple commands over the serial line in the
0007 % format cnnn, where 'c' means command and 'nnn' is a 3 character argument.
0008 % s - stands for sensitivity, valid arguments are 1 and 16.
0009 % i - stands for integration time, valid arguments are 13, 101, and 402.
0010 % t - stands for transmit, valid arguments are 001-999. the transmit command
0011 %       instructs the uc to enable the TSL2561 and output nnn measurements taken
0012 %       by the frequency meter.
0013 % l - stands for light, valid arguments are 000 or 111. if the argument is
0014 %       000/111 then the pin specified in the uc code will be brought low/high.
0015 %       this pin can be used to drive an LED or switch a transitor or relay 
0016 %       off/on to control power to a lamp.
0017 %
0018 % The output counts is recorded when the lamp is on (counts_light) and off 
0019 % (counts_dark) so that the photodiode's thermal current, measured by
0020 % counts_dark, can be removed.
0021 %
0022 % counts_light and counts_dark are not automatically saved. You should check for
0023 % bad data before saving. If bad samples are present, remove them and run
0024 % the script save_data.
0025 %
0026 % Tested in:
0027 % -- WinXP: MATLAB 7.5.0 (R2700b), Octave 3.2.4
0028 % -- Debian Wheezy: Octave 3.2.4
0029 %
0030 % MATLAB NOTES:
0031 % fgetl doesn't return -1 when it reaches the end of the stream. Therefore, the
0032 % uc sends a special end of transmission 'EOT' signal.
0033 %
0034 
0035 %**** USER SUPPLIED DATA ****%
0036 
0037 if strcmpi(filesep, '/') % in Linux
0038     port = '/dev/ttyUSB0';
0039     % same tty magic phrase used by arduino
0040     stty_arg = ['10:0:8bd:0:3:1c:7f:15:4:0:0:0:11:13:1a:0:12:f:17:16:0:0:' ...
0041                 '0:0:0:0:0:0:0:0:0:0:0:0:0:0'];
0042     system(['/bin/stty --file=' port ' ' stty_arg]);
0043 elseif strcmpi(filesep, '\') % in Windows
0044     port = 'COM9';
0045 end
0046 
0047 sensitivity = 16;
0048 int_time = 101;
0049 num_samples = 20; % number of frequency measurements.
0050 
0051 light_pos = 8;
0052 slit_pos = 2.5;
0053 % distance of light source from detector, [m]
0054 distance = (light_pos-slit_pos)*0.0254;
0055 lamp_type = 'CFL';
0056 
0057 % this is used by the script save_data.
0058 fname = ['s' num2str(sensitivity) 'i' num2str(int_time) '_' lamp_type '_' ...
0059          num2str(light_pos) '.mat'];
0060 
0061 %**** END USER SUPPLIED DATA ****%
0062 
0063 counts_light = -1;
0064 counts_dark = -1;
0065 
0066 if (exist('OCTAVE_VERSION'))
0067     serial_write = @(spr, sstr) fputs(spr, sstr);
0068 else
0069     serial_write = @(spr, sstr) fprintf(spr, '%s', sstr);
0070 end
0071 
0072 % pause_time is the amount of time to allow the serial buffer to fill with
0073 % samples. one sample takes about 1.2 seconds to generate. 0.1 seconds per
0074 % sample is added to be safe.
0075 pause_time = (1.2 + 0.1)*(num_samples+2); % seconds
0076 %pause_time = 20; %seconds, *DEBUG*DEBUG*DEBUG*DEBUG*
0077 
0078 
0079 sp = serial_open(port);
0080 
0081 serial_write(sp, 'l111'); %turn on lamp (optional).
0082 
0083 % by not having a newline the cursor stays at the end of the line.
0084 fprintf('Please turn on lamp. Then press the Enter key to continue.')
0085 % the pause command without an argument did not halt the script, so using
0086 % input instead.
0087 ign = input('', 's');
0088 fprintf('\nThank you. Proceeding.')
0089 
0090 scmd = sprintf('s%03d', sensitivity);
0091 serial_write(sp, scmd); %set sensitivity
0092 icmd = sprintf('i%03d', int_time);
0093 serial_write(sp, icmd); %set integration time
0094 tcmd = sprintf('t%03d', num_samples+2); %+2 because first two are discarded.
0095 serial_write(sp, tcmd); %tell uc to start transmitting.
0096 
0097 pause(pause_time); %wait for data to be generated and transmitted to buffer
0098 
0099 k = 1;
0100 ign = fgetl(sp); % the first sample is usually bad, so discard it.
0101 ign = fgetl(sp); % the second sample is usually bad, so discard it.
0102 c = fgetl(sp);
0103 while (ischar(c) && ~strcmp(c, 'EOT') && k <= num_samples)
0104     counts_light(k,:) = str2double(c);
0105     k = k+1;
0106     c = fgetl(sp);
0107 end
0108 
0109 % once fgetl has returned -1 (reached EOF) the serial port must be
0110 % closed and re-opened to get any new data.
0111 fclose(sp);
0112 
0113 fprintf('\nFinished gathering counts with lamp on.\n\n');
0114 
0115 %------------------------------------------------------------------------------%
0116 %------------------------------------------------------------------------------%
0117 
0118 %re-open serial port
0119 sp = serial_open(port);
0120 
0121 serial_write(sp, 'l000'); %turn off lamp (optional).
0122 
0123 fprintf('Please turn off lamp. Then press the Enter key to continue.')
0124 %pause
0125 % the pause command without an argument did not halt the script, so using
0126 % input instead.
0127 ign = input('', 's');
0128 fprintf('\nThank you. Proceeding.')
0129 
0130 % sometimes re-opening serial port resets uc so the sensitivity and frequency
0131 % scaling commands must be resent.
0132 scmd = sprintf('s%03d', sensitivity);
0133 serial_write(sp, scmd); %set sensitivity
0134 icmd = sprintf('i%03d', int_time);
0135 serial_write(sp, icmd); %set integration time
0136 tcmd = sprintf('t%03d', num_samples+2); %+2 because first two are discarded.
0137 serial_write(sp, tcmd); %tell uc to start transmitting.
0138 
0139 pause(pause_time); %wait for data to be generated and transmitted to buffer
0140 
0141 k = 1;
0142 ign = fgetl(sp); % the first sample is usually bad, so discard it.
0143 ign = fgetl(sp); % the second sample is usually bad, so discard it.
0144 c = fgetl(sp);
0145 while (ischar(c) && ~strcmp(c, 'EOT') && k <= num_samples)
0146     counts_dark(k,:) = str2double(c);
0147     k = k+1;
0148     c = fgetl(sp);
0149 end
0150 
0151 serial_write(sp, 's000'); %tell TSL2561 to power down
0152 fclose(sp);
0153 
0154 fprintf('\nFinished gathering counts with lamp off.\n\n');
0155 
0156 %------------------------------------------------------------------------------%
0157 %------------------------------------------------------------------------------%
0158 
0159 counts_light
0160 counts_dark
0161 
0162 disp('Check for bad data before saving.');
0163 rsp = input('Do you want to save? [(y)es/(n)o]: ', 's');
0164 if (~isempty(rsp) && lower(rsp(1)) == 'y')
0165     save_data;
0166 end
0167