clc, clear all;
sensor1 = daq.createSession('ni'); %creating a session for the sensor
sensor1.addAnalogInputChannel('myDAQ3',0,'Voltage'); % add channels
sensor1.DurationInSeconds = 1.0; %setting the scan duration
sensor1.Rate = 500; %setting the scan rate
motor2 = daq.createSession('ni'); %creating a session for the motor
motor2.addAnalogOutputChannel('myDAQ3',0,'Voltage'); % add channels
% caliberating the hand
% calculating the mean voltage when arm is rested
L=zeros(4,1); % stacking the std values in a list
disp('Before wearing the arm, please relax your arm')
for i=1:4
[datarest,time] = sensor1.startForeground; %gathering data from the sensor
sensor1.release();
aver=std(datarest); %calculating std of the data collected
L(i)=aver; % stocking aver values
end
averest=mean(L) % calculating the mean of the list L
%calculating the mean voltage when arm is clenched
P=zeros(4,1); % stacking the std values in a list
disp('Now clench your arm')
for i=1:4
[datacont,time] = sensor1.startForeground; %gathering data from the sensor
sensor1.release();
avec=std(datacont);%calculating std of the data collected
P(i)=avec;% stocking aver values
end
avecont=mean(P)% calculating the mean of the list P
tresh=(averest+avecont)/2 % finding the average value between a clenching and resting arm
% Using muscle contraction to control a prosthetic arm
in=0;
disp(' Caliberation of the device is finished, you can now control the arm')
%giving time for the user towear the arm and get ready to control
while in==0
k=input('please enter any key when ready','s') %prompting the user to enter a key when ready
if isempty(k)==0 %checking if user entered a key
in=1;
end
end
stop=0;
disp('To exit the loop ,please click on ctrl+c')
figure;% a figure
h=animatedline; % creating an animated line which displays the contraction on screen
ax=gca;
ax.YGrid='on';
ax.YLim=[0 2];
startTime0 = datetime('now'); %getting the current time
stop=0;
% collecting the data to control the hand
while stop==0
startTime1 = datetime('now'); %update the current time in every loop
[data,time] = sensor1.startForeground; %collecting the data
aveQ=std(data) % calculating the standard deviation
% comparing the data collected with the calibrating data
if aveQ>tresh
fprintf('Contraction is detected')
motor2.outputSingleScan(2); %to close the claw
motor2.release();
else
motor2.outputSingleScan(10); %to open the claw
motor2.release();
end
s0=startTime1-startTime0; % represents the beggining of the x axis
t = datetime('now') - startTime0; % represtents in limit of the X axis
T=linspace(datenum(s0),datenum(t),500); % values in the x axis
addpoints(h,T,data); % assigning data to the animated line
ax.XLim = datenum([s0 t]) % converting the date into numeric form
datetick('x') % displaying date as hh:mm:ss
xlabel('time elapsed') % x axis label
ylabel('voltage') %y axis label
title('recording of the muscle contraction') %title of the graph
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]); %displaying graph in full screen
drawnow %plotting the data
end
( I want to explain this whole code step by step )