function [aout, anames] = kinarm_loadanalog(file, destdir, write) %% function [out] = kinarm_loadanalog(file, destdir, write) %% load the analog channels - the kinematic variables plus EMGs - from %% plexon file 'file' & store them in a matrix [out] indexed by colum with %% anames. if 'write' is nonzero, write the saved results to %% file_analog.mat %% the NIDAQ AD gain has to be set to gain = 1 for unencoding of angles to %% work. olddir = pwd; filenodirstart = regexp(file, '[^\\]*$'); writefile = regexprep(file(filenodirstart:end), '\.plx', '_analog.mat'); readsource = 0; try cd(destdir); load(writefile); catch disp('parsed file not found, reading from plexon source') readsource = 1; end cd(destdir); mkdir('analog'); %%save the NIDAQ channels into here. cd('analog'); [nidaqnames, numnames] = dir2(2e8); if readsource > 0 maxlen = 3600*1000; %%an hour. anames =['shoulder '; ... %%1 'elbow '; ... %%2 'shoulderv'; ... %%3 'elbowv '; ... %%4 'shouldert'; ... %%5 'elbowt '; ... %%6 'juice '; ... %%7 'game '; ... %%8 'bicep '; ... %%9 'tricep '; ... %%10 'deltoid '; ... %%11 'pectoral '; ... %%12 'cartx '; ... %%13 'carty '; ... %%14 'cartvx '; ... %%15 'cartvy '; ... %%16 'time ']; %%17 anames = anames'; for k = 1:64 %%change this!!! look = ['nidaq_', num2str(k), '.mat']; cachefile = [destdir, '\', 'analog', '\', look]; dowrite = 1; if regexp2(nidaqnames, look) disp(['kinarm_loadanalog: found cached NIDAQ channel: ' cachefile]); load(cachefile); n = length(adout); dowrite = 0; found = 1; else found = 1; try [adfreq, n, ts, fn, adout] = loadPlexAD(file, k-1, maxlen); catch found = 0; end end if found > 0 adout = single(adout./2047); if k == 1 adout = 1.25*adout + 0.75; elseif k ==2 adout = 1.25*adout + 1.15; elseif k==3 || k==4 adout = 18.0*adout; end if k <= 12 disp(['reading channel: ', num2str(k),',' anames(:, k)']); aout(:, k) = single(adout); end if dowrite > 0 disp(['caching nidaq to: ', cachefile]); save(cachefile, 'adout', 'ts'); end end end %%cartx and carty. l1 = 11.5; l2 = 19.5; aout(:, 13) = l1*cos(aout(:, 1)) + l2*cos(aout(:, 1)+aout(:, 2)); aout(:, 14) = l1*sin(aout(:, 1)) + l2*sin(aout(:, 1)+aout(:, 2)); %%cartesian velocities aout(:, 15) = (-l1*sin(aout(:, 1)) -l2*sin(aout(:, 1) + aout(:, 2))).*aout(:, 3) + ... (-l2*sin(aout(:, 1) + aout(:, 2))).*aout(:, 4); aout(:, 16) = (l1*cos(aout(:, 1)) + l2*cos(aout(:, 1) + aout(:, 2))).*aout(:, 3) + ... (l2*cos(aout(:, 1) + aout(:, 2))).*aout(:, 4); %%time aout(:, 17) = single( (0:n-1)./ 1000 + ts(1)); if(write > 0) cd(destdir); disp(['kinarm_loadanalog: saving', destdir, '\', writefile]); save(writefile, 'aout', 'anames'); end end cd(olddir); % names = StringCopyCol(names, 'shoulder', 1); % disp('loading shoulder analog'); % [adfreq, n, ts, fn, out(:, 1)] = loadPlexAD(file, 0, maxlen); % aout(:, 1) = single((1.25.*out(:, 1)./2047) + 0.75); %%undo the encoding. % % disp(class(out)); % %save the start time % adstart = ts; % disp('loading elbow analog'); % [adfreq, n, ts, fn, out(:, 2)] = loadPlexAD(file, 1, maxlen); % out(:, 2) = single((1.25.*out(:, 2)./2047)) + 1.15; % names = StringCopyCol(names, 'elbow', 2); % disp(class(out)); % disp('loading shoulder vel analog'); % [adfreq, n, ts, fn, out(:, 3)] = loadPlexAD(file, 2, maxlen); % out(:, 3) = 18.0*out(:, 3)./2047; % names = StringCopyCol(names, 'shoulderv', 3); % disp('loading elbow vel analog'); % [adfreq, n, ts, fn, out(:, 4)] = loadPlexAD(file, 3, maxlen); % out(:, 4) = 18.0*out(:, 4)./2047; % names = StringCopyCol(names, 'elbowv', 4); % disp('loading shoulder torque analog'); % [adfreq, n, ts, fn, out(:, 5)] = loadPlexAD(file, 4, maxlen); % out(:, 5) = out(:, 5)./2047; % names = StringCopyCol(names, 'shouldert', 5); % disp('loading elbow torque analog'); % [adfreq, n, ts, fn, out(:, 6)] = loadPlexAD(file, 5, maxlen); % out(:, 6) = out(:, 6)./2047; % names = StringCopyCol(names, 'elbowt', 6); % %%eventually need to write some code to decript the gamedata signal % %%really should have made a better record than this serial bit % %%stream... % disp('loading juice analog'); % [adfreq, n, ts, fn, out(:, 7)] = loadPlexAD(file, 6, maxlen); % names = StringCopyCol(names, 'juice', 7); % disp('loading game data'); % [adfreq, n, ts, fn, out(:, 8)] = loadPlexAD(file, 7, maxlen); % names = StringCopyCol(names, 'game', 8); % %%emgs. % disp('loading bicep emg'); % [adfreq, n, ts, fn, out(:, 9)] = loadPlexAD(file, 8, maxlen); % names = StringCopyCol(names, 'bicep', 9); % disp('loading tricep emg'); % [adfreq, n, ts, fn, out(:, 10)] = loadPlexAD(file, 9, maxlen); % names = StringCopyCol(names, 'tricep', 10); % disp('loading deltiod emg'); % [adfreq, n, ts, fn, out(:, 11)] = loadPlexAD(file, 10, maxlen); % names = StringCopyCol(names, 'deltoid', 11); % disp('loading pectoral emg'); % [adfreq, n, ts, fn, out(:, 12)] = loadPlexAD(file, 11, maxlen); % names = StringCopyCol(names, 'pectoral', 12); % out(:, 7:12) = out(:, 7:12)./ 2047; % % disp(class(out)); % l1 = 11.5; % l2 = 19.5; % v_array = 'out'; % v_names = 'names'; % out(:, 13) = l1*cos(out(:, 1)) + l2*cos(out(:, 1)+out(:, 2)); % names = StringCopyCol(names, 'cartx', 13); % out(:, 14) = l1*sin(out(:, 1)) + l2*sin(out(:, 1)+out(:, 2)); % names = StringCopyCol(names, 'carty', 14); % pack % disp(class(out)); % %%take the p.derivatrives of the above expressions. % disp('computing cartesian velocities'); % % len = length(kinarm_x); % % jacob = zeros(len, 2, 2); % % jacob(:,1,1) = -l1*sin(shoulder) -l2*sin(shoulder + elbow); % % jacob(:,1,2) = -l2*sin(shoulder + elbow); % % jacob(:,2,1) = l1*cos(shoulder) + l2*cos(shoulder + elbow); % % jacob(:,2,2) = l2*cos(shoulder + elbow); % % temp = zeros(2, len); % % for r = 1:len % % temp(:, r) = squeeze(jacob(r, :, :))*[shoulderv(r); elbowv(r)]; % % end % %%this is much faster: % % kinarm_vx = (-l1*sin(shoulder) -l2*sin(shoulder + elbow)).*shoulderv + ... % % (-l2*sin(shoulder + elbow)).*elbowv; % % kinarm_vy = (l1*cos(shoulder) + l2*cos(shoulder + elbow)).*shoulderv + ... % % (l2*cos(shoulder + elbow)).*elbowv; % out(:, 15) = (-l1*sin(out(:, 1)) -l2*sin(out(:, 1) + out(:, 2))).*out(:, 3) + ... % (-l2*sin(out(:, 1) + out(:, 2))).*out(:, 4); % names = StringCopyCol(names, 'cartvx', 15); % out(:, 16) = (l1*cos(out(:, 1)) + l2*cos(out(:, 1) + out(:, 2))).*out(:, 3) + ... % (l2*cos(out(:, 1) + out(:, 2))).*out(:, 4); % names = StringCopyCol(names, 'cartvy', 16); % % % out(:, 15) = single((-l1*sin(v_('shoulder')) -l2*sin(v_('shoulder')+v_('elbow')))... % % .*v_('shoulderv') + ... % % (-l2*sin(v_('shoulder') + v_('elbow'))).*v_('elbowv')); % % out(:, 16) = signle((l1*cos(v_('shoulder')) + l2*cos(v_('shoulder') + v_('elbow')))... % % .*v_('shoulderv') + ... % % (l2*cos(v_('shoulder') + v_('elbow'))).*v_('elbowv')); % % pack % out(:, 17) = single( (0:n-1)./ 1000 + ts(1)); % names = StringCopyCol(names, 'time', 17); % function [var] = StringCopyCol(var, str, col) %% function StringCopyCol(var, str, col) %% copies string str into colum col of var %% extract as much redundancy as possible. var(1:length(str), col) = str; function [m] = v_(str) %% allows you to index an array by name - a cheapo struct. %% the name of the array is stored in the caller's namespace as varaible %% v_array = 'out'; (for example) and v_names = 'names'; where out and %% names in the callers namespace contain the data you want to index to and %% the names of these columns of data. %% the names are stored in columns. the argument to this function is %% matched to the columns of names using regexp(..); the first match is %% taken. array = evalin('caller', 'v_array'); %%retrieve the name names = evalin('caller', 'v_names'); %%this varaible stores the name of the names variable names = evalin('caller', names); %%redirection craziness!! str siz = size(names); for k = 1:siz(2) if regexp( names(:, k)', str) indx = k; break; end end expression = [array, '(:,', num2str(indx), ')']; m = evalin('caller', expression);