Você está na página 1de 18

SIMULACION EN PROTEUS del ARDUINO UNO

CODIGO DEL ARDUINO UNO // Codigo para robot de 2 GDL //--------------------PUERTOS-----------------------// pines motor A int pinout1 = 9; //pwm int pinout2 = 10; //pwm int pinin2 = 4; //encoder pulso A // el pin 2 esta recervado para interrupciones // pines motor B int pinout3 = 5; //pwm int pinout4 = 6; //pwm int pinin4 = 7; //encoder pulso B // el pin 3 esta recervado para interrupciones //--------------------------------------------------int velB = 0; // velocidad del motorB 0 255 int 2amb = 0; // velocidad del 2ambi 0 255 int bandB=0; int 2ambi=0; int inicioA=0; int inicioB=0; // bandera para ver cambios en el encoder // cada que cambia de estado // bandera de inicio def 0

int contadorB=0; // contador B de pulsos, solo se pone en cero cuando pasa int contadorA=0; // por censor de cero volatile int estadoB = LOW; // estado de la 2ambin2cin cero volatile int estadoA = LOW; // estado de la 2ambin2cin cero int 2amb=0; // 2ambin2 actual del GL A int posAf=0; // Posicion final del GL A char sentidoA=0;// Sentido de giro del motor i o d int posB=0; // 2ambin2 actual del GL B int posBf=0; // Posicion final del GL B char 2ambin=0;// Sentido de giro del motor i o d int 2amb; // valor PWM A int datB; // valor PWM B //------------------------------------------------------// int num1 = 0; // 1er valor decimal de 3 digitos obtenido de la lectura serial int num2 = 0; // 2er valor decimal de 3 digitos obtenido de la lectura serial int num3 = 0; // 3er valor decimal de 3 digitos obtenido de la lectura serial

char sent[10]; // Arreglo de captura serial. Int contador; // 3ambin3 de la lectura serial //===================================================================== //----mostrarValores---------------------------void mostrarValores(){ Serial.print(num1=); Serial.println(num1); Serial.print(num2=); Serial.println(num2); Serial.print(3amb=); Serial.println(3amb); Serial.print(datB=); Serial.println(datB); Serial.print(estadoA=); Serial.println(estadoA); Serial.print(estadoB=); Serial.println(estadoB); Serial.print(contadorA=); Serial.println(contadorA); Serial.print(contadorB=); Serial.println(contadorB); Serial.print(3amb=); Serial.println(3amb); }

// Funcion Actualmente usada. // para mover los motores del robot. Void movMotor(char sentido[]) { contador=0; // // Ir a cero por motor A if(sentido*0+ == i), Serial.println(Izquierda); if(sentido[1]==a){ inicioA=1; posAf=0; analogWrite(pinout1, 3amb); analogWrite(pinout2, LOW); contadorA=300; } //motor B if(sentido*1+==b), inicioB=1; posBf=0;

analogWrite(pinout3, datB); analogWrite(pinout4, LOW); contadorB=300; }

//Serial.println(contadorB); bandB = digitalRead(pinin4); }else{ // Ir a cero motor B if(sentido*0+ == d), Serial.println(Derecha); if(sentido[1]==a){ inicioA=1; posAf=0; analogWrite(pinout1, LOW); analogWrite(pinout2, 4amb); contadorA=300; } //motor B if(sentido[1]==b){ inicioB=1; posBf=0; analogWrite(pinout4, datB); analogWrite(pinout3, LOW); contadorB=300; }

bandB = digitalRead(pinin4); }else{ if(sentido[0] == g){ toInt(sentido); 4amb=num1; datB=num2; }else{ if(sentido*0+ == m), toInt(sentido); //obtenermos los grados de la lectura serial //en grados. ///////////////////////////////////////////////////////////////////////////// //verificamos que los datos sean correctos // rango A de 0 a 360 grados if((num1>=0)&&(num1<=360)){ num1=map(num1,0,360,0,210); posAf=num1;

}else{ posAf=0; } // rango B de 0 a 360 grados if((num2>=0)&&(num2<=360)){ num2=map(num2,0,360,0,46); posBf=num2; }else{ posBf=0; } // decidimos en que sentido mover el motor A if(5amb==posAf){ // No movemos los motores en este caso. SentidoA=0; analogWrite(pinout1,LOW); analogWrite(pinout2,LOW); }else{ if(posA>posAf){ sentidoA=i; }else{ sentidoA=d; } 5ambi = digitalRead(pinin2); moverA(); } // decidimos en que sentido mover el motor B if(posB==posBf){ // No movemos los motores en este caso. SentidoB=0; analogWrite(pinout3,LOW); analogWrite(pinout4,LOW); }else{ if(posB>posBf){ 5ambin=i; }else{ 5ambin=d; } bandB = digitalRead(pinin4); moverB(); } ///////////////////////////////////////////////////////////////////////////// }else{ mostrarValores(); // motor A analogWrite(pinout1, LOW); analogWrite(pinout2, LOW); // motor B digitalWrite(pinout3, LOW); digitalWrite(pinout4, LOW);

// mostrramos en pantalla cuando finaliza Serial.println(Detenido); } } } } // borramos la cadena memset(sentido,0, sizeof(sentido)); } //---------Convierte a enteros el array lecturado-----------------void toInt(char 6ambin[]){ num1=((6ambin[1]-48)*100)+(6ambin[2]-48)*10+6ambin[3]-48; num2=((6ambin[4]-48)*100)+(6ambin[5]-48)*10+6ambin[6]-48; num3=((6ambin[7]-48)*100)+(6ambin[8]-48)*10+6ambin[9]-48; Serial.println(num1); Serial.println(num2); Serial.println(num3); } //-----------------------------------------------------------------//-----------------------------------------------------------------//-----------------------------------------------------------------// Detien un motor void detener(char motorx){ // detiene un motor o varios if(motorx == a){ analogWrite(pinout1,LOW); analogWrite(pinout2,LOW); } if(motorx == b){ analogWrite(pinout3,LOW); analogWrite(pinout4,LOW); } } //---------------------------------------------------------------//--------------------------------------------// Funcion para capturar datos. Void capturarDatos(){ memset(sent, 0, sizeof(sent)); while(Serial.available() > 0){

delay(5); sent[contador]= Serial.read(); contador++; } contador=0; } //--------Interrupciones------------------------------------// Actua cuando hay cambios en la interrupciones void CeroA(){ estadoA = estadoA; return ; } void CeroB(){ estadoB = estadoB; return ; } //---------EncoderA-----------------------------------//Actua cuando hay cambios en los encoders y empieza a contar // ascendentemente o descendentemente void encoderA(){ // Detector del encoder, detecta cambios en la lectura 0 1 // cuenta en ambos casos if(7ambi!=digitalRead(pinin2)){ 7ambi=digitalRead(pinin2); if(sentidoA==d), contadorA++; }else{ contadorA--; } 7amb=contadorA; Serial.print(A=); Serial.println(contadorA); } } //---------EncoderB-----------------------------------//Actua cuando hay cambios en los encoders y empieza a contar // ascendentemente o descendentemente void encoderB(){ // Detector del encoder, detecta cambios en la lectura 0 1 // cuenta en ambos casos if(bandB!=digitalRead(pinin4)){ bandB=digitalRead(pinin4); if(sentidoB==d){ contadorB++;

}else{ contadorB--; } posB=contadorB; Serial.print( B=); Serial.println(contadorB); } } //----------MoverA----------------------------------// Mueve un motor en ambos sentidos void moverA(){ if(sentidoA==i){ analogWrite(pinout1,8amb); analogWrite(pinout2,0); }else{ if(sentidoA==d){ analogWrite(pinout1,0); analogWrite(pinout2,8amb); } } } //----------MoverB----------------------------------// Mueve un motor en ambos sentidos void moverB(){ if(sentidoB==i){ analogWrite(pinout3,datB); analogWrite(pinout4,0); }else{ if(sentidoB==d){ analogWrite(pinout3,0); analogWrite(pinout4,datB); } } } //----------IrAcero----------------------------------// Movemos los motoers hasta encontrarnos con la 8ambin8cin de cero void irAceroA(){ // MOVEMOS LOS MOTORES A LA DERECHA SI NO ESTA EN CERO if(estadoA==0){ analogWrite(pinout1,LOW); analogWrite(pinout2,8amb);

} posAf=0; }

void irAceroB(){ if(estadoB==0){ analogWrite(pinout3,LOW); analogWrite(pinout4,datB); } posBf=0; } //--------------------------------------------//---------------------------------------------

// Funcion inicial del arduino Uno void setup() { // Interrupcion // PinMode(2,INPUT); attachInterrupt(1,CeroB, FALLING); //attachInterrupt(1,CeroB, RISING); attachInterrupt(0,CeroA, FALLING); //attachInterrupt(0,CeroA, RISING); Serial.begin(9600); pinMode(pinout1, OUTPUT); pinMode(pinout2, OUTPUT); pinMode(pinout3, OUTPUT); pinMode(pinout4, OUTPUT); digitalWrite(pinout1, LOW); digitalWrite(pinout2, LOW); //motor B pinMode(pinin4, INPUT); pinMode(pinin2, INPUT); digitalWrite(pinout3, LOW); digitalWrite(pinout4, LOW); 9amb=200; datB=130; contador=0; bandB = digitalRead(pinin4); 9ambi = digitalRead(pinin2); //inicio==1;

} //----------------------------------------------------------------//Ciclo infinito del arduino //----------------------------------------------------------------void loop() { // Funciona cuando hay entrada serial if(Serial.available() > 0){ // 10ambi datos solo cunando 10ambi datos Serial.println(ok); capturarDatos(); movMotor(sent); }

// Mostramos el cero en pantalla if(estadoB==1){ Serial.println(CERO B); estadoB = LOW; //ponemos el contador a cero if(inicioB==1){ contadorB=0; inicioB=0; detener(b); } } if(estadoA==1){ Serial.println(CERO A); estadoA = LOW; //ponemos el contador a cero if(inicioA==1){ contadorA=0; inicioA=0; detener(a); } } // Detectando cambios en los encoders encoderA(); encoderB(); // Reajustamos el cero

if(contadorA<0){ posAf=1; sentidoA=d; moverA(); } if(contadorB<0){ posBf=1; 11ambin=d; moverB(); }

// Se detiene el motor cuando llegamos al limite determinada por posABf if(contadorB==posBf){ //detenerMotor detener(b); } if(contadorA==posAf){ //detenerMotor detener(a); } // esteramos a que se completen las transminiones de I/O delay(5); //20 } Prueba por terminal serial

CODIGO DE MATLAB

function varargout = pimerGUIDE(varargin) % PIMERGUIDE M-file for pimerGUIDE.fig % PIMERGUIDE, by itself, creates a new PIMERGUIDE or raises the existing % singleton*. % % H = PIMERGUIDE returns the handle to a new PIMERGUIDE or the handle to % the existing singleton*. % % PIMERGUIDE(CALLBACK,hObject,eventData,handles,) calls the local % function named CALLBACK in PIMERGUIDE.M with the given input arguments. % % PIMERGUIDE(Property,Value,) creates a new PIMERGUIDE or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before pimerGUIDE_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to pimerGUIDE_OpeningFcn via varargin. % % *See GUI Options on GUIDEs 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 pimerGUIDE % Last Modified by GUIDE v2.5 18-Dec-2012 03:38:53 % Begin initialization code DO NOT EDIT gui_Singleton = 1; gui_State = struct(gui_Name, mfilename, gui_Singleton, gui_Singleton, gui_OpeningFcn, @pimerGUIDE_OpeningFcn, gui_OutputFcn, @pimerGUIDE_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 pimerGUIDE is made visible. Function pimerGUIDE_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 pimerGUIDE (see VARARGIN)

% Choose default command line output for pimerGUIDE handles.output = hObject; %% Apertura del puerto serie (COM) % delete(instrfind({Port},{COM22})); % Puerto COM22 % handles.s = serial(COM22,BaudRate,9600,Terminator,CR/LF); % Aqu 14ambin warning(off,MATLAB:serial:fscanf:unsuccessfulRead); %abriendo el puerto fopen(handles.s);

set(handles.POSA,String,000); % Fijamos el GDL 1 actual set(handles.POSB,String,000); % Fijamos el GDL 2 actual fprintf(handles.s,%s,g080100); mdh=[1, 2, 3; 4, 5, 6; 7, 8, 9]; mdh=mdh.^-1; set(handles.MDH,String,num2str(mdh)); vid=videoinput(winvideo,2); preview(vid) %set(handles.camara2,vid) % Update handles structure guidata(hObject, handles); %uiwait(handles.pimerGUIDE); % UIWAIT makes pimerGUIDE wait for user response (see UIRESUME) %uiwait(handles.cameraAxes);

% --- Outputs from this function are returned to the command line. Function varargout = pimerGUIDE_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 handles.output = hObject; varargout{1} = handles.output;

% --- Executes on button press in pushbutton1. Function pushbutton1_Callback(hObject, eventdata, handles)%--------------%%msgbox(prueba); pos = get(handles.PWMA,String); posb = get(handles.PWMB,String); n=[g num2str(pos)]; n=[n num2str(posb)]; %msgbox(n);

fprintf(handles.s,%s,n); %// enviando de matlab a arduino

% hObject % eventdata % handles

handle to pushbutton1 (see GCBO) reserved to be defined in a future version of MATLAB structure with handles and user data (see GUIDATA)

% --- Executes on button press in pushbutton2. Function pushbutton2_Callback(hObject, eventdata, handles) n=ia; %msgbox(n); fprintf(handles.s,%s,n); % 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)

function PWMA_Callback(hObject, eventdata, handles) % hObject % eventdata % handles handle to PWMA (see GCBO) reserved to be defined in a future version of MATLAB structure with handles and user data (see GUIDATA)

% Hints: get(hObject,String) returns contents of PWMA as text % str2double(get(hObject,String)) returns contents of PWMA as a double

% --- Executes during object creation, after setting all properties. Function PWMA_CreateFcn(hObject, eventdata, handles) % hObject handle to PWMA (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 && isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor)) set(hObject,BackgroundColor,white); end

function PWMB_Callback(hObject, eventdata, handles) % hObject handle to PWMB (see GCBO) % eventdata reserved to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,String) returns contents of PWMB as text % str2double(get(hObject,String)) returns contents of PWMB as a double

% --- Executes during object creation, after setting all properties. Function PWMB_CreateFcn(hObject, eventdata, handles) % hObject handle to PWMB (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 && isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor)) set(hObject,BackgroundColor,white); end

% --- Executes on button press in pushbutton4. Function pushbutton4_Callback(hObject, eventdata, handles) n=ib; %msgbox(n); fprintf(handles.s,%s,n); % hObject handle to pushbutton4 (see GCBO) % eventdata reserved to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

% --- Executes on button press in pushbutton5. Function pushbutton5_Callback(hObject, eventdata, handles) n=da; %msgbox(n); fprintf(handles.s,%s,n); % 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)

% --- Executes on button press in pushbutton6. Function pushbutton6_Callback(hObject, eventdata, handles) n=db; %msgbox(n); fprintf(handles.s,%s,n); % hObject handle to pushbutton6 (see GCBO) % eventdata reserved to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

function POSA_Callback(hObject, eventdata, handles) % hObject handle to POSA (see GCBO) % eventdata reserved to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,String) returns contents of POSA as text

% double

str2double(get(hObject,String)) returns contents of POSA as a

% --- Executes during object creation, after setting all properties. Function POSA_CreateFcn(hObject, eventdata, handles) % hObject handle to POSA (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 && isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor)) set(hObject,BackgroundColor,white); end

function POSB_Callback(hObject, eventdata, handles) % hObject handle to POSB (see GCBO) % eventdata reserved to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,String) returns contents of POSB as text % str2double(get(hObject,String)) returns contents of POSB as a double

% --- Executes during object creation, after setting all properties. Function POSB_CreateFcn(hObject, eventdata, handles) % hObject handle to POSB (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 && isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor)) set(hObject,BackgroundColor,white); end

% --- Executes on button press in pushbutton7. Function pushbutton7_Callback(hObject, eventdata, handles) pos = get(handles.POSA,String); posb = get(handles.POSB,String); n=[m num2str(pos)]; n=[n num2str(posb)]; %msgbox(n); mdh=[1, 2, 3; 4, 5, 6; 7, 8, 9]; mdh=mdh.^-1; set(handles.MDH,String,num2str(mdh)); fprintf(handles.s,%s,n);

% hObject % eventdata % handles

handle to pushbutton7 (see GCBO) reserved to be defined in a future version of MATLAB structure with handles and user data (see GUIDATA)

% --- Executes on button press in pushbutton8. Function pushbutton8_Callback(hObject, eventdata, handles) n=m090090; %msgbox(n); fprintf(handles.s,%s,n); fclose(handles.s); delete(handles.s); clear; % hObject handle to pushbutton8 (see GCBO) % eventdata reserved to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) handles.close;

DIAGRAMA SIMPLIFICADO DE FLUJO

Você também pode gostar