function varargout = interface2(varargin) % INTERFACE2 MATLAB code for interface2.fig % INTERFACE2, by itself, creates a new INTERFACE2 or raises the existing % singleton*. % % H = INTERFACE2 returns the handle to a new INTERFACE2 or the handle to % the existing singleton*. % % INTERFACE2('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in INTERFACE2.M with the given input arguments. % % INTERFACE2('Property','Value',...) creates a new INTERFACE2 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before interface2_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to interface2_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 interface2 % Last Modified by GUIDE v2.5 15-Apr-2014 10:39:33 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @interface2_OpeningFcn, ... 'gui_OutputFcn', @interface2_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(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 interface2 is made visible. function interface2_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 interface2 (see VARARGIN) % Choose default command line output for interface2 handles.output = hObject; set(handles.pushbutton_ligamotor,'Enable','off'); set(handles.pushbutton_recebe,'Enable','off'); set(handles.pushbutton1,'Enable','on'); set(handles.pushbutton2,'Enable','off'); % Update handles structure guidata(hObject, handles); %find open communication devices, close them openDevices=instrfind('Status','open'); for i=1:length(openDevices) fclose(openDevices(i)); end serialInfo = instrhwinfo('serial'); % Finds available COM ports for k=1:length(serialInfo.AvailableSerialPorts) s_port(k)=serialInfo.AvailableSerialPorts(k); % Saves in a vector the available COM ports end set(handles.popupmenu1,'String',s_port); % Writes in a popupmenu the s_port elements % Update handles structure guidata(hObject, handles); % UIWAIT makes interface2 wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = interface2_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; % --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles) % hObject handle to pushbutton5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) t = 0:0.001:0.5-0.001; B= [ 0 0 0 0 0 0 0 0 0 1000 1000 3000 4000 5000 8000 8000 9000 11000 12000 13000 14000 14000 15000 15000 15000 16000 17000 17000 16000 17000 17000 18000 17000 17000 18000 18000 18000 19000 18000 18000 18000 18000 19000 19000 19000 19000 19000 20000 19000 19000 19000 19000 20000 20000 20000 20000 20000 20000 20000 20000 19000 19000 20000 20000 19000 19000 20000 20000 19000 20000 20000 19000 20000 20000 20000 19000 20000 20000 19000 19000 20000 19000 20000 20000 19000 20000 20000 19000 20000 20000 19000 20000 20000 19000 20000 20000 19000 20000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 19000 20000 20000 19000 20000 20000 19000 20000 19000 20000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 20000 19000 20000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 20000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 20000 20000 20000 19000 20000 19000 20000 19000 20000 20000 19000 20000 19000 20000 20000 19000 20000 19000 20000 20000 20000 20000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 20000 20000 20000 20000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 20000 20000 21000 20000 20000 19000 20000 19000 20000 19000 20000 20000 19000 20000 19000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 19000 20000 19000 20000 20000 20000 21000 20000 20000 20000 20000 20000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 19000 20000 20000 20000 19000 20000 21000 20000 20000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 21000 20000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 19000 21000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 20000 21000 20000 20000 20000 20000 20000 20000 19000 20000 19000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 20000 21000 20000 20000 20000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 20000 20000 19000 20000 19000 20000 19000 20000 19000 20000 19000 20000 20000 20000 20000 20000 20000 19000 20000 ]; t = 0:0.001:0.5-0.001; b = B/1024*2*pi %figure(1); stairs(t,b,'b-'); title('Curva característica do motor'); axis([0 0.5 0 200]); xlabel('Tempo(s)'); ylabel('Velocidade (RPM))'); legend('Resposta ao degrau'); grid hold off figure(2); plot(t,b,'r-'); % --- Executes on button press in pushbutton_recebe. function pushbutton_recebe_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_recebe (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) TxText = 'b'; TxText2 = ''; fprintf(handles.serialCom, TxText); fprintf(handles.serialCom, TxText2); flushinput(handles.serialCom); flushoutput(handles.serialCom); %fread(handles.serialCom,[500 1]) %length(RxText) %g = RxText*1000; RxText = fscanf(handles.serialCom,'%5d',[500 1]) fclose(handles.serialCom); flushinput(handles.serialCom); flushoutput(handles.serialCom); delete(handles.serialCom); %clear serialCom; y = RxText/1024*2*pi t = 0:0.001:0.499; t1 = t'; u = 12*ones(size(t1)); save velocidade y t u %stairs(t,b,'b-'); plot(t,y,'b-'); title('Resposta do Motor'); axis([0 0.5 0 200]) xlabel('Tempo(s)'); ylabel('Velocidae (rad/s)'); %RPM alterado em 06/05/2014 legend('Resposta ao degrau'); grid hold off serPortn = get(handles.popupmenu1, 'Value'); %%criando objeto do motor Ts = 1/1000; motor = iddata;motor.Tstart = 0; motor.Ts = Ts; motor.InputData = 12*ones(size(t1)); motor.OutputData = y; portValue=get(handles.popupmenu1,'Value'); portString=get(handles.popupmenu1,'String'); selectedPort=(portString{portValue}); serialCom = serial(selectedPort); set(handles.pushbutton_ligamotor,'Enable','on'); set(handles.pushbutton_recebe,'Enable','on'); % communication channel parameters set(serialCom,'BaudRate',9600); set(serialCom,'DataBits',8); set(serialCom,'Parity','none'); set(serialCom,'StopBits',1); set(serialCom,'FlowControl','none'); set(serialCom,'Timeout',2); set(serialCom,'InputBufferSize',65536); set(serialCom,'Terminator','CR'); %open the serial communication channel fopen(serialCom); handles.serialCom=serialCom; guidata(hObject, handles); %currList = get(handles.listbox2, 'String'); %if length(RxText) < 1 % RxText = 'Timeout @ '; % set(handles.listbox2, 'String', ... % [currList ; [RxText datestr(now)] ]); % else % set(handles.listbox2, 'String', ... % [currList ; ['Received @ ' datestr(now) ': ' RxText ] ]); %end %set(handles.listbox2, 'Value', length(currList) + 1 ); %catch e % disp(e) set(handles.pushbutton1,'Enable','off'); set(handles.pushbutton2,'Enable','on'); set(handles.popupmenu1,'Enable','off'); set(handles.pushbutton_ligamotor,'Enable','on'); % --- Executes on button press in pushbutton_ligamotor. function pushbutton_ligamotor_Callback(hObject, eventdata, handles) % hObject handle to pushbutton_ligamotor (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.pushbutton1,'Enable','off'); set(handles.pushbutton2,'Enable','on'); set(handles.popupmenu1,'Enable','off'); set(handles.pushbutton_ligamotor,'Enable','on'); TxText = 'a'; fprintf(handles.serialCom, TxText); set(handles.pushbutton1,'Enable','off'); set(handles.pushbutton2,'Enable','on'); set(handles.popupmenu1,'Enable','off'); set(handles.pushbutton_ligamotor,'Enable','on'); h = msgbox('Gerando vetor velocidade','Status'); % --- Executes on selection change in popupmenu1. function popupmenu1_Callback(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array % contents{get(hObject,'Value')} returns selected item from popupmenu1 % --- Executes during object creation, after setting all properties. function popupmenu1_CreateFcn(hObject, eventdata, handles) % hObject handle to popupmenu1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.pushbutton1,'Enable','on'); set(handles.pushbutton2,'Enable','off'); set(handles.popupmenu1,'Enable','on'); set(handles.pushbutton_ligamotor,'Enable','off'); set(handles.pushbutton_recebe,'Enable','off'); fclose(handles.serialCom); % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.pushbutton1,'Enable','off'); set(handles.pushbutton2,'Enable','on'); set(handles.popupmenu1,'Enable','off'); set(handles.pushbutton_ligamotor,'Enable','off'); ; %get selected port serPortn = get(handles.popupmenu1, 'Value'); portValue=get(handles.popupmenu1,'Value'); portString=get(handles.popupmenu1,'String'); selectedPort=(portString{portValue}); try serialCom = serial(selectedPort); set(handles.pushbutton_ligamotor,'Enable','on'); set(handles.pushbutton_recebe,'Enable','on'); % communication channel parameters set(serialCom,'BaudRate',9600); set(serialCom,'DataBits',8); set(serialCom,'Parity','none'); set(serialCom,'StopBits',1); set(serialCom,'FlowControl','none'); set(serialCom,'Timeout',15); set(serialCom,'InputBufferSize',65536); set(serialCom,'Terminator','CR'); %open the serial communication channel fopen(serialCom); handles.serialCom=serialCom; guidata(hObject, handles); catch e errordlg('A porta escolhida já esta sendo usada por outra aplicação, por favor selecione uma porta válida'); set(handles.pushbutton_ligamotor,'Enable','off'); set(handles.pushbutton_recebe,'Enable','off'); set(handles.pushbutton2,'Enable','off'); set(handles.pushbutton1,'Enable','on'); set(handles.pushbutton2,'Enable','off'); set(handles.popupmenu1,'Enable','on'); end