-
Notifications
You must be signed in to change notification settings - Fork 0
/
marvin.c
430 lines (379 loc) · 11.7 KB
/
marvin.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//Standard-Include-Files
#include <stdio.h>
#include <regc515c.h>
#include <stub.h>
#include "marvin.h"
#include "motor.h"
#include "config.h"
#include "timer.h"
// IR-Sensor Vars
unsigned char ir_goal_detected = FALSE;
// internal Sensor Vars
unsigned char sensor_ball_detected_no = FALSE;
unsigned char sensor_ball_detected_nw = FALSE;
unsigned char sensor_ball_detected_n = FALSE;
unsigned char sensor_i_have_the_ball = FALSE;
unsigned char sensor_i_have_the_goal = FALSE;
unsigned char sensor_left_wall_is_near = FALSE;
unsigned char sensor_right_wall_is_near = FALSE;
unsigned char sensor_left_wall_detector = FALSE;
unsigned char sensor_right_wall_detector = FALSE;
unsigned char sensor_left_sharp = 0;
unsigned char sensor_right_sharp = 0;
unsigned char sensor_west_sharp = 0;
unsigned char sensor_east_sharp = 0;
unsigned char internal_sharp_difference = 0;
unsigned char internal_obstacle_timeout = 0;
unsigned char internal_initial_found_ball = FALSE;
//main states
unsigned char state_start = FALSE;
unsigned char state_searching_ball = TRUE;
unsigned char state_walking_right = FALSE;
unsigned char state_walking_left = FALSE;
unsigned char state_running_to_the_wall = FALSE;
unsigned char state_calibrating_the_wall = FALSE;
unsigned char state_obstacle_left_wait = FALSE;
unsigned char state_obstacle_right_wait = FALSE;
unsigned char state_obstacle_left_turn = FALSE;
unsigned char state_obstacle_right_turn = FALSE;
unsigned char pid_process_1, pid_process_2;
unsigned char ir_goal_frequency = 4;
void AksenMain(void) {
pid_process_1 = process_start(ir_detector, 10);
// Einstellung der Torfrequenz
if (dip_pin(0) == 1) {
ir_goal_frequency = 4;
lcd_puts("Eigen: 125 Hz");
lcd_setxy(1,0);
lcd_puts("Gegner: 100 Hz");
} else {
ir_goal_frequency = 5;
lcd_puts("Eigen: 100 Hz");
lcd_setxy(1,0);
lcd_puts("Gegner: 125 Hz");
}
while (1) {
if (dip_pin(1)) {
////////////////////////////////
//
// SENSORIK
//
////////////////////////////////
// Timer
timer_check();
if (analog(PORT_BALL_DETECTOR_TOP) < MAX_ANALOG_VALUE_DETECTOR_TOP) {
timer_reset(0, BALL_TIMEOUT, &sensor_i_have_the_ball);
}
// Anfrage das Mittleren Sensorts (Torsuche)
if (ir_goal_detected == TRUE) {
timer_reset(1, GOAL_TIMEOUT, &sensor_i_have_the_goal);
}
//Ball NO Erkennung
if ((analog(PORT_BALL_DETECTOR_NO) < MAX_ANALOG_VALUE_DETECTOR_NO)
|| (analog(PORT_BALL_DETECTOR_NO_EXT)
< MAX_ANALOG_VALUE_DETECTOR_NO_EXT)) {
timer_reset(2, BALL_NO_TIMEOUT, &sensor_ball_detected_no);
}
//Ball NW Erkennung
if ((analog(PORT_BALL_DETECTOR_NW) < MAX_ANALOG_VALUE_DETECTOR_NW)
|| (analog(PORT_BALL_DETECTOR_NW_EXT)
< MAX_ANALOG_VALUE_DETECTOR_NW_EXT)) {
timer_reset(3, BALL_NW_TIMEOUT, &sensor_ball_detected_nw);
}
//Ball N Erkennung
if (analog(PORT_BALL_DETECTOR_N) < MAX_ANALOG_VALUE_DETECTOR_N) {
timer_reset(4, BALL_N_TIMEOUT, &sensor_ball_detected_n);
}
sensor_left_sharp = analog(PORT_SHARP_L);
sensor_right_sharp = analog(PORT_SHARP_R);
sensor_east_sharp = analog(PORT_SHARP_O);
sensor_west_sharp = analog(PORT_SHARP_W);
// Sensors
sensor_left_wall_is_near = (sensor_left_sharp
> CALIBRATE_DISTANCE_FAR) ? TRUE : FALSE;
sensor_right_wall_is_near = (sensor_right_sharp
> CALIBRATE_DISTANCE_FAR) ? TRUE : FALSE;
sensor_right_wall_detector = (sensor_east_sharp
>= SHARP_O_WALL_DETECTED) ? TRUE : FALSE;
sensor_left_wall_detector = (sensor_west_sharp
>= SHARP_W_WALL_DETECTED) ? TRUE : FALSE;
internal_sharp_difference
= (sensor_right_sharp > sensor_left_sharp) ? sensor_right_sharp
- sensor_left_sharp
: sensor_left_sharp - sensor_right_sharp;
////////////////////////////////
//
// ZUSTAENDE
//
////////////////////////////////
////////////////////////////////////
// Fallback if we lost the ball
if (!sensor_i_have_the_ball && !state_start) {
change_state(state_searching_ball);
/////////////////////////////
// 0. Startzustand
} else if (state_start) {
if (internal_initial_found_ball && !sensor_i_have_the_ball) {
change_state(state_searching_ball);
}
/////////////////////////////
// 1. Suche den Ball!
} else if (state_searching_ball) {
if (sensor_i_have_the_ball) {
change_state(state_running_to_the_wall);
}
////////////////////////////
// 2. Laufe auf die Wand zu
} else if (state_running_to_the_wall) {
// found wall to the left, walk right
if (sensor_left_wall_is_near || sensor_right_wall_is_near) {
change_state(state_calibrating_the_wall);
}
//////////////////////////////////
// 3. An der Wand ausrichten
} else if (state_calibrating_the_wall) {
if (internal_sharp_difference < MAX_WALKING_DIFFERENCE) {
if (sensor_east_sharp > sensor_west_sharp) {
change_state(state_walking_right);
} else {
change_state(state_walking_left);
}
}
//////////////////////////////////////
// 4.A Laufe nach links an der Wand entlang
} else if (state_walking_left) {
// TODO: Wait 1.5 - 2 sec and check distance to wall again
// change direction if other bot or own goal detected
if (sensor_left_wall_detector) {
change_state(state_obstacle_left_wait);
timer_reset(5, OBSTACLE_WAIT_TIMEOUT,
&internal_obstacle_timeout);
} else if (ir_goal_detected) {
change_state(state_walking_right);
}
///////////////////////////////////////
// 4.B Laufe nach rechts an der Wand entlang
} else if (state_walking_right) {
// change direction if other bot or own goal detected
if (sensor_right_wall_detector) {
change_state(state_obstacle_right_wait);
timer_reset(5, OBSTACLE_WAIT_TIMEOUT,
&internal_obstacle_timeout);
} else if (ir_goal_detected) {
change_state(state_walking_left);
}
//////////////////////////////////////////////////
// 5.A Warte darauf, dass Hindernis links verschwindet
} else if (state_obstacle_left_wait) {
if (!internal_obstacle_timeout) {
if (sensor_left_wall_detector) {
change_state(state_obstacle_left_turn);
timer_reset(5, OBSTACLE_TURN_TIMEOUT,
&internal_obstacle_timeout);
} else {
change_state(state_walking_left);
}
}
///////////////////////////////////////////////////
// 5.B Warte darauf, dass Hindernis rechts verschwindet
} else if (state_obstacle_right_wait) {
if (!internal_obstacle_timeout) {
if (sensor_right_wall_detector) {
change_state(state_obstacle_right_turn);
timer_reset(5, OBSTACLE_TURN_TIMEOUT,
&internal_obstacle_timeout);
} else {
change_state(state_walking_right);
}
}
/////////////////////////////////////
// 6.A Drehe dich um das Hindernis links
} else if (state_obstacle_left_turn) {
if((sensor_right_wall_detector && !sensor_left_wall_detector) || !internal_obstacle_timeout)
change_state(state_walking_left);
/////////////////////////////////////
// 6.B Drehe dich um das Hindernis rechts
} else if (state_obstacle_right_turn) {
if((sensor_left_wall_detector && !sensor_right_wall_detector) || !internal_obstacle_timeout)
change_state(state_walking_right);
}
////////////////////////////////
//
// AKTORIK
//
////////////////////////////////
////////////////////////
// State 0
if (state_start) {
if (sensor_i_have_the_ball) {
internal_initial_found_ball = TRUE;
if (sensor_left_sharp > sensor_right_sharp) {
trn_c(8);
} else {
trn_cc(8);
}
} else {
dir_n(10);
}
////////////////////////
// State 1
} else if (state_searching_ball) {
if (sensor_ball_detected_n) {
sensor_ball_detected_no = 0;
sensor_ball_detected_nw = 0;
dir_n(5);
} else if (sensor_left_wall_is_near) {
trn_c(5);
} else if (sensor_right_wall_is_near) {
trn_cc(5);
} else if (sensor_ball_detected_no) {
trn_c_nw(5);
} else if (sensor_ball_detected_nw) {
trn_cc_no(5);
} else {
dir_n(10);
}
////////////////////////
// State 2
} else if (state_running_to_the_wall) {
dir_n(5);
////////////////////////
// State 3
} else if (state_calibrating_the_wall) {
if(sensor_left_sharp < sensor_right_sharp) {
trn_cc_n(4);
} else {
trn_c_n(4);
}
////////////////////////
// State 4.A
} else if (state_walking_left) {
if (sensor_left_sharp < MAX_WALKING_DISTANCE) {
dir_nw(4);
} else if (sensor_left_sharp > MIN_WALKING_DISTANCE) {
dir_sw(4); //!!!
} else {
dir_w(6);
}
////////////////////////
// State 4.B
} else if (state_walking_right) {
if (sensor_left_sharp < MAX_WALKING_DISTANCE) {
dir_no(4); //!!!
} else if (sensor_left_sharp > MIN_WALKING_DISTANCE) {
dir_so(4);
} else {
dir_o(6);
}
////////////////////////
// State 5.A & State 5.B
} else if (state_obstacle_left_wait || state_obstacle_right_wait) {
dir_stop();
////////////////////////
// State 6.A
} else if (state_obstacle_left_turn) {
trn_cc(3);
////////////////////////
// State 6.B
} else if (state_obstacle_right_turn) {
trn_c(3);
}
// TODO: why is this here?
sleep(8);
}
////////////////////////////
//
// DEBUG
//
////////////////////////////
if (!dip_pin(2) && !dip_pin(3)) {
lcd_cls();
lcd_puts("W:");
lcd_ubyte(analog(PORT_BALL_DETECTOR_NW));
lcd_puts("C:");
lcd_ubyte(analog(PORT_BALL_DETECTOR_N));
lcd_puts("O:");
lcd_ubyte(analog(PORT_BALL_DETECTOR_NO));
lcd_setxy(1, 0);
lcd_puts("L:");
lcd_ubyte(analog(PORT_SHARP_L));
lcd_puts("R:");
lcd_ubyte(analog(PORT_SHARP_R));
lcd_puts("T:");
lcd_ubyte(analog(PORT_BALL_DETECTOR_TOP));
sleep(100);
} else if (!dip_pin(2) && dip_pin(3)) {
lcd_cls();
lcd_puts("G:");
lcd_ubyte(ir_goal_detected);
lcd_puts(" ");
lcd_ubyte(analog(PORT_SHARP_O));
lcd_puts(" ");
lcd_ubyte(analog(PORT_SHARP_W));
lcd_setxy(1, 0);
lcd_puts("B:");
lcd_ubyte(sensor_i_have_the_ball);
lcd_puts("G:");
lcd_ubyte(sensor_i_have_the_goal);
lcd_puts(" ");
lcd_ubyte(analog(PORT_BALL_DETECTOR_NW_EXT));
lcd_puts(" ");
lcd_ubyte(analog(PORT_BALL_DETECTOR_NO_EXT));
sleep(100);
}
}
}
void ir_detector() {
#define IR_PROC_WAIT (10 * 2 * ir_goal_frequency)
#define IR_PROC_PERIODS_FOR_OK 3
#define IR_PROC_MAX_ERROR_PER_PERIOD 3
unsigned long finishtime;
unsigned char led_received_l;
unsigned char led_received_c;
unsigned char led_received_r;
mod_ir0_maxfehler(IR_PROC_MAX_ERROR_PER_PERIOD);
mod_ir1_maxfehler(IR_PROC_MAX_ERROR_PER_PERIOD);
mod_ir2_maxfehler(IR_PROC_MAX_ERROR_PER_PERIOD);
do {
mod_ir0_takt(ir_goal_frequency);
mod_ir1_takt(ir_goal_frequency);
mod_ir2_takt(ir_goal_frequency);
ir_goal_detected = FALSE;
finishtime = akt_time() + IR_PROC_WAIT;
process_hog();
while (akt_time() < finishtime) {
led_received_c = mod_ir0_status();
led_received_l = mod_ir1_status();
led_received_r = mod_ir2_status();
if (led_received_c >= IR_PROC_PERIODS_FOR_OK) {
ir_goal_detected = TRUE;
break;
} else if (led_received_l >= IR_PROC_PERIODS_FOR_OK) {
ir_goal_detected = TRUE;
break;
} else if (led_received_r >= IR_PROC_PERIODS_FOR_OK) {
ir_goal_detected = TRUE;
break;
}
}
mod_ir0_takt(0);
mod_ir1_takt(0);
mod_ir2_takt(0);
(mod_ir0_status()) = 0;
(mod_ir1_status()) = 0;
(mod_ir2_status()) = 0;
sleep(20);
} while (1);
}
void reset_states() {
state_start = FALSE;
state_searching_ball = FALSE;
state_running_to_the_wall = FALSE;
state_walking_right = FALSE;
state_walking_left = FALSE;
state_obstacle_left_turn = FALSE;
state_obstacle_right_turn = FALSE;
state_obstacle_left_wait = FALSE;
state_obstacle_right_wait = FALSE;
state_calibrating_the_wall = FALSE;
}