Question: Hand controller 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

Hand controller

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

Heart rate sensor

%house keeping (not really necessary)

clc

clear all

%setup MyDAQ

s = daq.createSession('ni');

s.addAnalogInputChannel('myDAQ3',0,'Voltage');

%Define constants

s.Rate = 1000;

s.DurationInSeconds = 3.0;

TH = 0.75; %Threshhold tolerance

%Define data

Datalist = 0;%all recorded voltage data

allBPM = 0;%all recorded heartrates

lnum = 1;%nth loop

%Define usefule little numbers

stop=0;

numdata = s.Rate*s.DurationInSeconds;%number of data points

while stop==0

[data,time] = s.startForeground; %call data

Datalist(numdata*(lnum-1)+1:numdata*lnum)=data;%record data in Datalist

Thresh = mean(data) + TH * (max(data)-mean(data));%calc Threshhold for

detecting Heartbeats

%proform operations

OverThresh = find(data>Thresh); %create bool - true if over Threshhold

fran = OverThresh((find(data(OverThresh+1)

ranges

lran = OverThresh((find(data(OverThresh-1)>Thresh)));%last entry in true

ranges

n=1;

while n

HB(n) = max(fran(n),lran(n));%list the peak of each heart beat

n= n+1;

end

BPM = (length(HB)-1)*60*s.Rate/(HB(find(HB,1,'last'))-HB(1))%find Beats Per

Minute

allBPM(lnum) = BPM;

%graph

set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);

plot(Datalist,'r')

xticks(0:s.Rate:length(Datalist))

xticklabels(0:s.DurationInSeconds*lnum)

xlim([numdata*(lnum-3),numdata*lnum])

xlabel('time (seconds)')

ylabel('Sensor Data (Volts)')

legend('Sensor Output')

voltaxis = ylim();

timeaxis = xlim();

text(timeaxis(1)+(timeaxis(2)-timeaxis(1))*0.03, voltaxis(2)-(voltaxis(2)-

voltaxis(1))*0.05,...

['BPM = ' num2str(BPM)],'FontSize',14,'Color', [0.8 0 0])

lnum= lnum+1;

end

s.release();

(can you explain this code please)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!