Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 //Serial 00004 Serial pc(SERIAL_TX, SERIAL_RX); 00005 00006 //*Definicion entradas y salidas 00007 // Sensores 00008 DigitalIn p1(PA_3); 00009 DigitalIn p2(PD_7); 00010 DigitalIn p3(PC_0); 00011 DigitalIn p4(PD_6); 00012 DigitalIn p5(PC_3); 00013 DigitalIn p6(PD_5); 00014 DigitalIn p7(PF_3); 00015 DigitalIn p8(PD_4); 00016 DigitalIn p9(PF_5); 00017 00018 // motores 00019 DigitalOut dir_x(PC_6); 00020 DigitalOut step_x(PB_8); 00021 DigitalOut dir_y(PB_15); 00022 DigitalOut step_y(PB_9); 00023 00024 // finales de carrera 00025 DigitalIn finx(PC_9); 00026 DigitalIn finy(PC_8); 00027 00028 // variables 00029 float tiempo=2.5; 00030 int datos[10]; 00031 int datos2[10]; 00032 int fin_x; 00033 int fin_y; 00034 int a;// recibe el serial 00035 int contador; 00036 int v,suma; 00037 00038 // Posiciones 00039 int p[10][4]={{0,0,0,0},{0,112,-73,0},{0,112,73,0},{0,172,237,-60},{0,259,-73,0},{0,259,73,0},{0,172,237,60},{0,408,-73,0},{0,259,73,0},{0,320,237,60}}; 00040 int posicion; 00041 00042 int i,j,k; 00043 00044 void sensores(void); 00045 void adelante (void); 00046 void atras(void); 00047 void derecha(void); 00048 void izquierda (void); 00049 void origen(void); 00050 void movimientos(void); 00051 void inicio (void); 00052 00053 //////////////////////////////////////////////////////fin 00054 void fin (void) 00055 { 00056 for(i=0;i<=60;i++) 00057 atras(); 00058 00059 origen(); 00060 inicio(); 00061 } 00062 00063 //////////////////////////////////////////////////////inicio 00064 void inicio (void) 00065 { 00066 for(i=0;i<=264;i++) 00067 izquierda(); 00068 00069 for(i=0;i<=92;i++) 00070 adelante(); 00071 00072 } 00073 ///////////////////////////////////////////////////// movimientos 00074 void movimientos (void) 00075 { 00076 for(i=0;i<=p[posicion][1];i++) 00077 adelante(); 00078 00079 if(p[posicion][2]<0) 00080 { 00081 for(i=0;i<=(p[posicion][2]*-1);i++) 00082 izquierda(); 00083 } 00084 else 00085 { 00086 for(i=0;i<=p[posicion][2];i++) 00087 derecha(); 00088 } 00089 00090 00091 if(p[posicion][3]<0) 00092 { 00093 for(i=0;i<=(p[posicion][3]*-1);i++) 00094 atras(); 00095 } 00096 else 00097 { 00098 for(i=0;i<=p[posicion][3];i++) 00099 adelante(); 00100 } 00101 } 00102 /////////////////////////////////////////////////////origen 00103 void origen (void) 00104 { 00105 sensores(); 00106 while(fin_x==0) 00107 { 00108 sensores(); 00109 derecha(); 00110 } 00111 00112 while(fin_y==0) 00113 { 00114 sensores(); 00115 atras(); 00116 } 00117 00118 00119 } 00120 /////////////////////////////////////////////////// Adelante 00121 void adelante (void) 00122 { 00123 dir_y=0; 00124 step_y=1; 00125 wait_ms(tiempo); 00126 step_y=0; 00127 wait_ms(tiempo); 00128 00129 } 00130 /////////////////////////////////////////////////// Atras 00131 void atras (void) 00132 { 00133 dir_y=1; 00134 step_y=1; 00135 wait_ms(tiempo); 00136 step_y=0; 00137 wait_ms(tiempo); 00138 00139 00140 } 00141 /////////////////////////////////////////////////// Derecha 00142 void derecha (void) 00143 { 00144 dir_x=0; 00145 step_x=1; 00146 wait_ms(tiempo); 00147 step_x=0; 00148 wait_ms(tiempo); 00149 00150 } 00151 /////////////////////////////////////////////////// Derecha 00152 void izquierda (void) 00153 { 00154 dir_x=1; 00155 step_x=1; 00156 wait_ms(tiempo); 00157 step_x=0; 00158 wait_ms(tiempo); 00159 00160 } 00161 /////////////////////////////////////////////////////////////////////////////// Lectura sensores 00162 void sensores(void) 00163 { 00164 fin_x= finx.read(); 00165 fin_y= finy.read(); 00166 00167 00168 datos[1] = p1.read(); 00169 datos[2] = p2.read(); 00170 datos[3] = p3.read(); 00171 datos[4] = p4.read(); 00172 datos[5] = p5.read(); 00173 datos[6] = p6.read(); 00174 datos[7] = p7.read(); 00175 datos[8] = p8.read(); 00176 datos[9] = p9.read(); 00177 00178 /* 00179 pc.printf("%d ",fin_x); 00180 pc.printf("%d \n\n",fin_y); 00181 00182 pc.printf("%d ",datos[1]); 00183 pc.printf("%d ",datos[2]); 00184 pc.printf("%d \n",datos[3]); 00185 pc.printf("%d ",datos[4]); 00186 pc.printf("%d ",datos[5]); 00187 pc.printf("%d \n",datos[6]); 00188 pc.printf("%d ",datos[7]); 00189 pc.printf("%d ",datos[8]); 00190 pc.printf("%d \n\n\n",datos[9]); 00191 */ 00192 } 00193 00194 ////////////////////////////////////////////////////////////////////////////////main 00195 int main() { 00196 00197 00198 while(a==0) 00199 a = pc.getc(); 00200 00201 pc.printf("Inicio \n"); 00202 origen(); 00203 inicio(); 00204 contador=2; 00205 00206 00207 while(1) 00208 { 00209 pc.printf("Turno virtual \n"); 00210 pc.printf("Ingrese la posicion ha jugar \n"); 00211 a = pc.getc(); 00212 posicion=a-48; 00213 movimientos(); 00214 fin(); 00215 sensores(); 00216 00217 for(i=1;i<=9;i++) 00218 datos2[i]=datos[i]; 00219 00220 pc.printf("Turno Fisico \n"); 00221 while(v==0) 00222 { 00223 sensores(); 00224 for(i=0;i<=9;i++) 00225 suma=suma+datos[i]; 00226 00227 if(suma==contador) 00228 v=1; 00229 suma=0; 00230 } 00231 v=0; 00232 contador=contador+2; 00233 00234 for(i=0;i<=9;i++) 00235 { 00236 if(datos[i] != datos2[i]) 00237 pc.printf("%i \n",i); 00238 } 00239 00240 } 00241 }
Generated on Wed Aug 17 2022 08:41:31 by
1.7.2