function varargout = fit(varargin) % FIT M-file for fit.fig % FIT, by itself, creates a new FIT or raises the existing % singleton*. % % H = FIT returns the handle to a new FIT or the handle to % the existing singleton*. % % FIT('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in FIT.M with the given input arguments. % % FIT('Property','Value',...) creates a new FIT or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before fit_OpeningFunction gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to fit_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help fit % Last Modified by GUIDE v2.5 24-May-2004 10:18:56 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @fit_OpeningFcn, ... 'gui_OutputFcn', @fit_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin & isstr(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before fit is made visible. function fit_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to fit (see VARARGIN) % Choose default command line output for fit handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes fit wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = fit_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; %======================================================= % Load the Measured Step Response %======================================================= % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) global tstep ystep data encoder gain zeta omega_d final_time scale % % clear the screen % axes(handles.axes1); cla reset % % we can only use the first cart % encoder = 1; file = uigetfile('.dat','Load'); if file ~= 0 data = load(file); end; if (isempty(encoder) == 0) tstep = data(:,2); ystep = data(:,encoder+3); end; % % convert from counts to cm % scale = 1/2196.0; % this is an approximation ystep = ystep*scale; % % Set the initial final time for the step response % final_time = tstep(end); set(handles.edit1,'String',num2str(final_time)); % % Set the ranges of the final time slider % set(handles.slider1,'Min',0); set(handles.slider1,'Max',final_time); set(handles.slider1,'SliderStep',[0.001 0.1]); set(handles.slider1,'Value',final_time); % % Determine the initial estimate of the gain % N = floor(length(ystep)/2); gain = ystep(N); set(handles.edit2,'String',num2str(gain)); set(handles.slider2,'Min',0); set(handles.slider2,'Max',5); set(handles.slider2,'SliderStep',[0.001 0.1]); set(handles.slider2,'Value',gain); % % Estimate the initial damping ratio % b = (max(ystep)-gain)/gain; zeta = (-log(b)/pi)/sqrt(1+(log(b)/pi)^2); set(handles.edit3,'String',num2str(zeta)); set(handles.slider3,'Min',0); set(handles.slider3,'Max',1); set(handles.slider3,'SliderStep',[0.001 0.1]); set(handles.slider3,'Value',zeta); % % Estimate the initial damped frequency % omega_d = 20; set(handles.edit4,'String',num2str(omega_d)); set(handles.slider4,'Min',0); set(handles.slider4,'Max',100); set(handles.slider4,'SliderStep',[0.001 0.1]); set(handles.slider4,'Value',omega_d); % return; % %======================================================= % Change the final time %======================================================= % function edit1_Callback(hObject, eventdata, handles) global final_time % final_time = str2num(get(handles.edit1,'String')); set(handles.slider1,'Value',final_time); return; % %======================================================= % Read the final time from the slider %======================================================= % % --- Executes on slider movement. function slider1_Callback(hObject, eventdata, handles) global final_time % final_time = get(handles.slider1,'Value'); set(handles.edit1,'String',num2str(final_time)); % return; % %=========================================================== % Change the gain %=========================================================== function edit2_Callback(hObject, eventdata, handles) global gain % gain = str2num(get(handles.edit2,'String')); set(handles.slider1,'Value',gain); return; % %============================================================ % Read the gain from the slider %=========================================================== % --- Executes on slider movement. function slider2_Callback(hObject, eventdata, handles) global gain % gain = get(handles.slider2,'Value'); set(handles.edit2,'String',num2str(gain)); % return; % %======================================================== % Get the Damping Ratio %======================================================== % function edit3_Callback(hObject, eventdata, handles) global zeta % zeta = str2num(get(handles.edit3,'String')); set(handles.slider3,'Value',zeta); return; % %======================================================= % Read the damping ratio from the slider %====================================================== % function slider3_Callback(hObject, eventdata, handles) global zeta % zeta = get(handles.slider3,'Value'); set(handles.edit3,'String',num2str(zeta)); % return; % %========================================================== % Get the damped frequency %========================================================= % function edit4_Callback(hObject, eventdata, handles) global omega_d % omega_d = str2num(get(handles.edit4,'String')); set(handles.slider4,'Value',omega_d); % return; % %========================================================== % Read the damped frequency from the slider %========================================================== % function slider4_Callback(hObject, eventdata, handles) global omega_d % omega_d = get(handles.slider4,'Value'); set(handles.edit4,'String',num2str(omega_d)); % return; % %=========================================================== % Plot the measured/estimated step response %=========================================================== % function pushbutton2_Callback(hObject, eventdata, handles) global omega_d omega_n zeta gain H ystep tstep encoder scale Ts final_time % omega_n = omega_d/sqrt(1-zeta^2); t = linspace(0,tstep(end),10000); u = ones(1,length(t)); H = tf(gain,[1/omega_n^2 2*zeta/omega_n 1]) y = lsim(H,u,t); axes(handles.axes1); cla reset plot(t,y,'b-',tstep,ystep,'r.-'); grid; xlabel('Time (sec)'); ylabel('Displacement (cm)'); if (isempty(final_time) == 0) axis([0 final_time min(min(y),min(ystep)) max(max(y),max(ystep))]); end; legend('Estimated','Measured'); % return; % %====================================================== % Make a Figure with our results %===================================================== % function pushbutton3_Callback(hObject, eventdata, handles) global omega_d omega_n zeta gain H ystep tstep encoder scale Ts final_time % omega_n = omega_d/sqrt(1-zeta^2); t = linspace(0,tstep(end),10000); u = ones(1,length(t)); H = tf(gain,[1/omega_n^2 2*zeta/omega_n 1]) y = lsim(H,u,t); figure; plot(t,y,'b-',tstep,ystep,'r.-'); grid; xlabel('Time (sec)'); ylabel('Displacement (cm)'); if (isempty(final_time) == 0) axis([0 final_time min(min(y),min(ystep)) max(max(y),max(ystep))]); end; legend('Estimated','Measured'); title(['Fitting to Step Response: gain = ' num2str(gain,3) ' V/cm, \omega_n = ' num2str(omega_n,3) ' rad/sec, \zeta = ' num2str(zeta,3) ]); % return; %========================================================================== %========================================================================== %========================================================================== % % --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles) % hObject handle to edit3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes during object creation, after setting all properties. function edit4_CreateFcn(hObject, eventdata, handles) % hObject handle to edit4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc set(hObject,'BackgroundColor','white'); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes during object creation, after setting all properties. function slider1_CreateFcn(hObject, eventdata, handles) % hObject handle to slider1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background, change % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER. usewhitebg = 1; if usewhitebg set(hObject,'BackgroundColor',[.9 .9 .9]); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes during object creation, after setting all properties. function slider2_CreateFcn(hObject, eventdata, handles) % hObject handle to slider2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background, change % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER. usewhitebg = 1; if usewhitebg set(hObject,'BackgroundColor',[.9 .9 .9]); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes during object creation, after setting all properties. function slider3_CreateFcn(hObject, eventdata, handles) % hObject handle to slider3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background, change % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER. usewhitebg = 1; if usewhitebg set(hObject,'BackgroundColor',[.9 .9 .9]); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end % --- Executes during object creation, after setting all properties. function slider4_CreateFcn(hObject, eventdata, handles) % hObject handle to slider4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background, change % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER. usewhitebg = 1; if usewhitebg set(hObject,'BackgroundColor',[.9 .9 .9]); else set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); end