diff --git a/OOequipment/Equipment.m b/OOequipment/Equipment.m index 0702bbd..d5002df 100644 --- a/OOequipment/Equipment.m +++ b/OOequipment/Equipment.m @@ -103,28 +103,35 @@ classdef Equipment < handle end function data = bin_read(ecq,datalength,type,typesize) - if nargin < 4 - typesize = 8; - end - data = zeros(1,datalength); - totalread = 0; - maxlength = floor(ecq.tcp.InputBufferSize/typesize); - while datalength > 0 - readlength = min(datalength,maxlength); - [lastdata,lastcount] = fread(ecq.tcp,[1,readlength],type); - if lastcount == 0 - error('Data stream from device shorter than expected'); + try + if nargin < 4 + typesize = 8; + end + data = zeros(1,datalength); + totalread = 0; + maxlength = floor(ecq.tcp.InputBufferSize/typesize)-16; + while datalength > 0 + readlength = min(datalength,maxlength); + lastcount = 0; + [lastdata,lastcount] = fread(ecq.tcp,[1,readlength],type); + if lastcount == 0 + error('Data stream from device shorter than expected'); + end + data(totalread+1:totalread+lastcount) = lastdata; + totalread = totalread+lastcount; + datalength = datalength - lastcount; end - data(totalread+1:totalread+lastcount) = lastdata; - totalread = totalread+lastcount; - datalength = datalength - lastcount; + flushinput(ecq.tcp) + ecq.clear + catch ME + flushinput(ecq.tcp); + rethrow(ME); end - flushinput(ecq.tcp) - ecq.clear end function data = bin_read_float(ecq,datalength) - data = bin_read(ecq,datalength,'float',4); + data = bin_read(ecq,datalength,'float',4); + end function output = read(ecq) @@ -243,13 +250,14 @@ classdef Equipment < handle %The connection will be removed if the port is negative number. persistent tcpconnection; %make variable persistent to share tcp-handles across multiple function calls. - [ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); %Get a structname and a cleaned ipaddress. - if port > 0 %if port number is positive a connection is made. - if isempty(tcpconnection) + if isempty(tcpconnection) %if the tcpconnection is empty (first time function %call) make a struct in the variable. tcpconnection = struct; end + [ipname,ipAddress] = Equipment.ip2structname(ipAddress,abs(port)); %Get a structname and a cleaned ipaddress. + if port > 0 %if port number is positive a connection is made. + if ~isfield(tcpconnection, ipname) %check if the handle is already made before. tcpconnection.(ipname).tcp = tcpip(ipAddress,port); %Make TCP-connection @@ -262,7 +270,9 @@ classdef Equipment < handle end tcpobject = tcpconnection.(ipname).tcp; %return the TCP-connection handle. elseif port < 0 %If the portnumber is negative the connection is removed. - if tcpconnection.(ipname).nopen > 1 + if ~isfield(tcpconnection, ipname) + return; + elseif tcpconnection.(ipname).nopen > 1 %If more than one object uses this tcp-handle. Decrease %the number of connections in use by 1. tcpconnection.(ipname).nopen = tcpconnection.(ipname).nopen - 1; %Decrease the number by 1. @@ -270,7 +280,7 @@ classdef Equipment < handle %If only one handle uses this connection. The %connection is closed and the field is removed from the %tcpconnection struct. - flushinput(sc.tcp); + flushinput(tcpconnection.(ipname).tcp); fclose(tcpconnection.(ipname).tcp); tcpconnection = rmfield(tcpconnection,ipname); end diff --git a/OOequipment/RC_TransferFunction.mlx b/OOequipment/RC_TransferFunction.mlx index d85f1f2..7cbe783 100644 Binary files a/OOequipment/RC_TransferFunction.mlx and b/OOequipment/RC_TransferFunction.mlx differ diff --git a/OOequipment/RC_TransferFunction_script.m b/OOequipment/RC_TransferFunction_script.m new file mode 100644 index 0000000..c7b81a4 --- /dev/null +++ b/OOequipment/RC_TransferFunction_script.m @@ -0,0 +1,20 @@ +%% Start with loading the equipment if not done already +run('equipment_init'); + +%% Run the measurements +data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,n_steps,amplitude); +S = asymptote(S_smaller,S_larger,f,Fc); +figure; +subplot(2,1,1) +semilogx(data.frequency,data.magnitude,f,A,f,S) +ylabel('Magnitude [dB]','Fontsize',10); +xlabel('Frequency [Hz]','Fontsize',10); +legend('Measurement','Theory','Asymptote'); +grid on +title('RC-circuit Bodeplot') +subplot(2,1,2); +semilogx(data.frequency,data.phase,f,P) +ylabel('Phase [rad]','Fontsize',10); +xlabel('Frequency [Hz]','Fontsize',10); +legend('Measurement','Theory'); +grid on \ No newline at end of file diff --git a/OOequipment/asymptote.m b/OOequipment/asymptote.m new file mode 100644 index 0000000..d2902e3 --- /dev/null +++ b/OOequipment/asymptote.m @@ -0,0 +1,8 @@ +function S = asymptote(S_smaller,S_larger,f,Fc) +%ASYMPTOTE Summary of this function goes here +% Detailed explanation goes here +[~,I] = min(abs(f-Fc)); +S = [S_smaller(1:I) S_larger(I+1:end)]; + +end + diff --git a/OOequipment/bodePlot.m b/OOequipment/bodePlot.m index 71a553d..8cd7254 100644 --- a/OOequipment/bodePlot.m +++ b/OOequipment/bodePlot.m @@ -1,17 +1,16 @@ function bodePlot(magnitude,phase,frequency) %UNTITLED Summary of this function goes here % Detailed explanation goes here - - -figure; subplot(2,1,1) semilogx(frequency,magnitude) +hold on ylabel('Magnitude [dB]','Fontsize',10); xlabel('Frequency [Hz]','Fontsize',10); grid on title('Bodeplot') -ax = subplot(2,1,2); +subplot(2,1,2); semilogx(frequency,phase) +hold on ylabel('Phase [rad]','Fontsize',10); xlabel('Frequency [Hz]','Fontsize',10); grid on diff --git a/OOequipment/equiment_init.m b/OOequipment/equiment_init.m deleted file mode 100644 index 448c493..0000000 --- a/OOequipment/equiment_init.m +++ /dev/null @@ -1,2 +0,0 @@ -fg = FunctionGenerator('10.0.0.3',1234,10); -sc = Oscilloscope('10.0.0.2',5025,2); \ No newline at end of file diff --git a/OOequipment/equipment_init.m b/OOequipment/equipment_init.m new file mode 100644 index 0000000..fd238c9 --- /dev/null +++ b/OOequipment/equipment_init.m @@ -0,0 +1,7 @@ +if ~exist('functiongenerator','var') + functiongenerator = FunctionGenerator('10.0.0.3',1234,10); +end + +if ~exist('oscilloscope','var') + oscilloscope = Oscilloscope('10.0.0.2',5025,2); +end \ No newline at end of file diff --git a/OOequipment/phamag.m b/OOequipment/phamag.m index 2dcef0c..edb33e8 100644 --- a/OOequipment/phamag.m +++ b/OOequipment/phamag.m @@ -6,8 +6,8 @@ wave = [wave1;wave2].*W'; Fs = 1/T; n = 2^nextpow2(length); X = fft(wave,n,2)/n; -idx = round(frequency*n/Fs); -phase = mod(angle(mean(X(1,idx)))-angle(mean(X(2,idx)))+pi,2*pi)-pi; +idx = ceil(frequency*n/Fs); +phase = mod(angle(mean(X(2,idx)))-angle(mean(X(1,idx)))+pi,2*pi)-pi; magnitude = 20*log10(abs(X(2,idx))/abs(X(1,idx))); end diff --git a/OOequipment/transferFunction.m b/OOequipment/transferFunction.m index 58db1a0..f44f0e8 100644 --- a/OOequipment/transferFunction.m +++ b/OOequipment/transferFunction.m @@ -1,4 +1,4 @@ -function [data,raw] = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,n_steps,amplitude) +function data = transferFunction(oscilloscope,functiongenerator,f_start,f_stop,n_steps,amplitude) %TRANSFERFUNCTION Summary of this function goes here % Detailed explanation goes here @@ -10,12 +10,12 @@ function [data,raw] = transferFunction(oscilloscope,functiongenerator,f_start,f_ emptydata = zeros(n_steps,1); data = struct('magnitude',emptydata,'phase',emptydata,'frequency',f_array); for i = 1:n_steps + fprintf('Measurement %f of %f - Frequency: %.2f Hertz\n',i,n_steps,f_array(i)); functiongenerator.frequency = f_array(i); oscilloscope.auto; - wavedata = oscilloscope.waveform; - raw(i) = wavedata; + wavedata = oscilloscope.waveform(1:2); [data.phase(i),data.magnitude(i)] = phamag(wavedata.ch1,wavedata.ch2,wavedata.length,f_array(i),wavedata.sampletime); end - bodePlot(data.magnitude,data.phase,data.frequency) + %bodePlot(data.magnitude,data.phase,data.frequency) end