-
Notifications
You must be signed in to change notification settings - Fork 0
/
Laberinto.c
266 lines (248 loc) · 5.39 KB
/
Laberinto.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
Blank Simple Project.c
http://learn.parallax.com/propeller-c-tutorials
*/
#include "simpletools.h" // Include simpletools header
#include "abdrive.h" // Include abdrive header
#include "ping.h"
#include<stdio.h>
#include<stdlib.h>
//Variables globales a utilizar
int izq = 0;
int der = 0;
int ade = 0;
int contador=0;
/* Stack has three properties. capacity stands for the maximum number of elements stack can hold.
Size stands for the current size of the stack and elements is the array of elements */
typedef struct Stack
{
int capacity;
int size;
int *elements;
}
Stack;
/* crateStack function takes argument the maximum number of elements the stack can hold, creates
a stack according to it and returns a pointer to the stack. */
Stack * createStack(int maxElements)
{
/* Create a Stack */
Stack *S;
S = (Stack *)malloc(sizeof(Stack));
/* Initialise its properties */
S->elements = (int *)malloc(sizeof(int)*maxElements);
S->size = 0;
S->capacity = maxElements;
/* Return the pointer */
return S;
}
void pop(Stack *S)
{
/* If stack size is zero then it is empty. So we cannot pop */
if(S->size==0)
{
printf("Stack is Empty\n");
return;
}
/* Removing an element is equivalent to reducing its size by one */
else
{
S->size--;
}
return;
}
int top(Stack *S)
{
if(S->size==0)
{
printf("Stack is Empty\n");
exit(0);
}
/* Return the topmost element */
return S->elements[S->size-1];
}
void push(Stack *S,int element)
{
/* If the stack is full, we cannot push an element into it as there is no space for it.*/
if(S->size == S->capacity)
{
printf("Stack is Full\n");
}
else
{
/* Push an element on the top of it and increase its size by one*/
S->elements[S->size++] = element;
}
return;
}
//Funciones con las que gira el robot
void izquierda(){
drive_goto(-25,26);
}
void derecha(){
drive_goto(26,-25);
}
//Función que revisa hacia el frente, derecha e izquierda, para saber si hay obstáculos en ese paso
void revisar(){
drive_goto(-25,26);
pause(10);
if(ping_cm(8)>15){
izq = 1;
}
else
{
izq=0;
}
drive_goto(52,-50);
pause(10);
if(ping_cm(8)>15){
der = 1;
}
else
{
der=0;
}
pause(10);
drive_goto(-25,26);
if(ping_cm(8)>10){
ade = 1;
}
else
{
ade=0;
}
}
//Función con la que el robot da una serie de minipasos, verificando en cada uno que no haya obstáculo. Al completar 8, termina el paso completo
void paso(){
int paso = 0;
while(paso<40){
paso = paso + 5;
drive_goto(5,5);
contador=contador+5;
if(ping_cm(8)<7){
break;
}
}
}
//Función en la que se revisa la última lectura del sensor.
//En base a ese dato, se revisa dónde hay un cruce, donde hay una bifurcación de varios caminos, y donde hay un tope
//Finalmente, para cada caso se pushea en los Stacks la información necesario para recordar el camino
//En especial, si se llega a un tope, se llama a la función backtracking
void magia(Stack *stder,Stack *stizq,Stack *stcamino)
{
int result=0;
result=der+izq+ade;
if(result>1)
{
push(stizq,izq);
if(ade==0)
{
push(stcamino,contador);
contador=0;
push(stcamino,contador);
push(stcamino,-2);
push(stder,0);
}
else
{
push(stcamino,contador);
contador=0;
push(stcamino,contador);
push(stder,der);
}
}
if(ade==0&&result==1)
{
if(izq==1)
{
push(stcamino,contador);
push(stcamino,-1);
contador=0;
}
if(der==1)
{
push(stcamino,contador);
push(stcamino,-2);
contador=0;
}
}
if(result==0)
{
push(stcamino,contador);
contador=0;
backtracking(stder,stizq,stcamino);
}
}
//Función que revisa el stack de camino para regresar al robot hasta a la última bifurcación que encontró
void backtracking(Stack *stder,Stack *stizq,Stack *stcamino)
{
int valor=0;
while(top(stcamino)!=0)
{
if(top(stcamino)>0)
{
valor=top(stcamino);
pop(stcamino);
valor=valor*(-1);
drive_goto(valor,valor);
}
if(top(stcamino)==-1)
{
pop(stcamino);
derecha();
}
if(top(stcamino)==-2)
{
pop(stcamino);
izquierda();
}
}
revisarStacks(stder,stizq,stcamino);
}
//Función que revisa los stacks izq y der para saber en qué dirección colocar al robot luego del backtracking
void revisarStacks(Stack *stder,Stack *stizq,Stack *stcamino){
if(top(stder)==1&&top(stizq)==0)
{
pop(stcamino);
derecha();
push(stcamino, -2);
paso();
}
if(top(stder)==0&&top(stizq)==1)
{
pop(stcamino);
izquierda();
push(stcamino, -1);
paso();
}
if(top(stder)==1&&top(stizq)==1)
{
pop(stder);
push(stder,0);
derecha();
push(stcamino, -2);
paso();
}
}
//Función main que tiene un while infinito para llamar a todas las funciones en cada ciclo, para que el robot resuelva el laberinto
int main()
{
//Se crean los stacks
Stack *stder = createStack(100);
Stack *stizq = createStack(100);
Stack *stcamino = createStack(100);
while(1){
revisar();
magia(stder,stizq,stcamino);
if(ade==1){
paso();
}
else if(der==1){
derecha();
paso();
}
else if(izq==1){
izquierda();
paso();
}
}
}