-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.c
312 lines (248 loc) · 4.45 KB
/
maze.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "maze.h"
#include "sonic.h"
#include "motor.h"
#include "user.h"
//#define printf //
void message(char m)
{
signed char pl=motor.power_left;
signed char pr=motor.power_right;
motor_left_power(0);
motor_right_power(0);
beep_message(m);
motor_left_power(pl);
motor_right_power(pr);
}
MAZE maze={0};
int absval(int val)
{
return (val < 0 ? (-val) : val);
}
void maze_interupt()
{
maze.started=1;
asm("nop");
}
void maze_loop()
{
while (1)
{
if (maze.started)
{
PORTAbits.RA0=0;
asm("nop");
maze.started=start_done;
collect_sensor_data();
centering();
if (is_branch())
branching();
maze_debug();
}
}
}
void maze_debug()
{
static char i=0;
i++;
if (i==9)
{
i=0;
printf("%s", ENDL);
printf("LF:\t%d%s", maze.lf, ENDL);
printf("LB:\t%d%s", maze.lb, ENDL);
printf("FNT:\t%d%s", maze.front, ENDL);
printf("RGT:\t%d%s", maze.right, ENDL);
printf("DIF:\t%d%s", maze.difference, ENDL);
if (maze.angle==ang_vlow)
printf("ANG:\tVlow%s", ENDL);
if (maze.angle==ang_low)
printf("ANG:\tLow%s", ENDL);
if (maze.angle==ang_ok)
printf("ANG:\tOk%s", ENDL);
if (maze.angle==ang_high)
printf("ANG:\tHigh%s", ENDL);
if (maze.angle==ang_vhigh)
printf("ANG:\tVhigh%s", ENDL);
if (maze.dist_state==dist_ok)
printf("DST:\tOK%s", ENDL);
if (maze.dist_state==dist_close)
printf("DST:\tClose%s", ENDL);
if (maze.dist_state==dist_far)
printf("DST:\tFar%s", ENDL);
}
}
void collect_sensor_data()
{
maze.lf +=ping_lf();
maze.lf /=2;
maze.front +=ping_front();
maze.front /=2;
maze.lb +=ping_lb();
maze.lb /=2;
maze.right +=ping_right();
maze.right /=2;
maze.difference =maze.lf - maze.lb;
maze.dist =maze.lf;
}
void centering_angle()
{
//angle constant
#define AC_VLOW -15
#define AC_LOW -5
#define AC_HIGH 5
#define AC_VHIGH 15
//|| VLOW | LOW | OK | HIGH | VHIGH ||
if (maze.difference < AC_VLOW)
{
maze.angle=ang_vlow;
return;
}
if (maze.difference < AC_LOW)
{
maze.angle=ang_low;
return;
}
if (maze.difference < AC_HIGH )
{
maze.angle=ang_ok;
return;
}
if (maze.difference < AC_VHIGH)
{
maze.angle=ang_high;
return;
}
//if (maze.difference > AC_VHIGH)
//{
maze.angle=ang_vhigh;
return;
//}
}
char centering_state()
{
const char dist_close=55;
const char dist_far=55;
//Close ?
if (maze.dist < dist_close)
{
maze.dist_state=dist_close;
if (maze.angle < ang_ok)
return cs_close_close;
if (maze.angle > ang_ok)
return cs_close_open;
return cs_close;
}//Far ?
else if (maze.dist > dist_far)
{
maze.dist_state=dist_far;
if (maze.angle < ang_ok)
return cs_far_close;
if (maze.angle > ang_ok)
return cs_far_open;
return cs_far;
}//No, then OK
else
{
maze.dist_state=dist_ok;
if (maze.angle < ang_ok)
return cs_ok_close;
if (maze.angle > ang_ok)
return cs_ok_open;
return cs_ok;
}
}
void center_straight()
{
motor_left_power(50);
motor_right_power(50);
}
void center_close()
{
motor_left_power(50 - 8);
motor_right_power(50);
}
void center_open()
{
motor_left_power(50);
motor_right_power(50 - 8);
}
void centering()
{
//-----------------------------
//Determine State
//0 OK
//1 OK, closing
//2 OK, opening
//3 Close, closing
//4 Close, opening (No action)
//5 Far, closing (No action)
//6 Far, opening
centering_angle();
char state=centering_state();
//-----------------------------
//act upon state
switch (state) {
case cs_close_open:
case cs_far_close:
case cs_ok:
//No action required yet !
//message(2);
center_straight();
break;
case cs_close:
case cs_close_close:
case cs_ok_close:
//stop closing onto wall
//message(1);
center_open();
break;
case cs_far:
case cs_far_open:
case cs_ok_open:
//stop opening onto wall
//message(3);
center_close();
break;
default:
message(10);
break;
}
}
char is_branch()
{
//FOR BRANCHING
#define GRACE_TIME (20);
//grace period timer
static char grace=GRACE_TIME;
if (grace)
grace--;
if (maze.right > 140 && grace == 0)
{
grace=GRACE_TIME;
return 1;
}
if (maze.lf > 140 && grace == 0)
{
grace=GRACE_TIME;
return 1;
}
if (maze.front < 80 && grace == 0)
{
grace=GRACE_TIME;
return 1;
}
return 0;
}
void branching()
{
sys_delay_ms(2);
#define TURN_POWER (60)
//currently around 10 ticks per second
#define TURN_ENC (38)
if (maze.lf > maze.right)
//Left is open
motor_set_target(TURN_ENC,-TURN_ENC);
else
//Right is open
motor_set_target(-TURN_ENC,TURN_ENC);
}