Menu

Hand Controller Clc Clear Sensor1 Daqcreatesession Ni Creating Session Sensor Sensor1addan Q43899704

Hand controller

clc, clear all;

sensor1 = daq.createSession('ni'); %creating asession 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 asession 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 yourarm')

for i=1:4

[datarest,time] = sensor1.startForeground; %gathering data fromthe 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 fromthe 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 aclenching and

resting arm

% Using muscle contraction to control a prosthetic arm

in=0;

disp(' Caliberation of the device is finished, you cannow control the arm')

%giving time for the user towear the arm and get ready tocontrol

while in==0

k=input('please enter any key whenready','s') %prompting the user to entera

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 onctrl+c')

figure;% a figure

h=animatedline; % creating an animated line which displays thecontraction on

screen

ax=gca;

ax.YGrid='on';

ax.YLim=[0 2];

startTime0 = datetime('now'); %getting thecurrent time

stop=0;

% collecting the data to control the hand

while stop==0

startTime1 = datetime('now'); %update thecurrent 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 xaxis

t = datetime('now') – startTime0; % represtentsin limit of the X axis

T=linspace(datenum(s0),datenum(t),500); % values in the xaxis

addpoints(h,T,data); % assigning data to the animated line

ax.XLim = datenum([s0 t]) % converting the date into numericform

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 inDatalist

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

detecting Heartbeats

%proform operations

OverThresh = find(data>Thresh); %create bool – true ifover Threshhold

fran = OverThresh((find(data(OverThresh+1)<Thresh)));%1stentry in true

ranges

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

ranges

n=1;

while n<length(fran)+1

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

n= n+1;

end

BPM =(length(HB)-1)*60*s.Rate/(HB(find(HB,1,'last'))-HB(1))%findBeats 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)

Expert Answer


Answer to Hand controller clc, clear all; sensor1 = daq.createSession(‘ni’); %creating a session for the sensor sensor1.addAnalogI…

OR