This repository has been archived by the owner on Nov 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·109 lines (92 loc) · 1.9 KB
/
main.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
#include "global.h"
#include "motor.h"
#include "usart.h"
#include "button.h"
#include "sensors.h"
#define SONAR_RANGE 120
void setup(void){
motor_setup();
usart_setup();
button_setup();
sensors_setup();
}
robot_state state = STOP;
int main(void){
setup();
line_sensor line_sensors;
line_sensor last_sensor = FRONT_LEFT;
while (1){
if ((line_sensors = read_line_sensors())){
state = (state == STOP) ? STOP : EVADE;
}
switch(state){
case SEEK:
printf("STATE: seek\n");
while(read_sonar() > SONAR_RANGE){
if (last_sensor == FRONT_RIGHT){
spin_left(255);
} else {
spin_right(255);
}
}
state = TARGET;
break;
case TARGET:
printf("STATE: target\n");
switch (read_front_sensors()){
case FRONT_TARGET_LEFT:
steer_left(255);
break;
case FRONT_TARGET_RIGHT:
steer_right(255);
break;
case FRONT_TARGET_LEFT | FRONT_TARGET_RIGHT:
go_forward(255);
break;
default:
go_forward(255);
if (read_sonar() > SONAR_RANGE)
state = SEEK;
}
break;
case ATTACK:
printf("STATE: attack\n");
go_forward(255);
break;
case EVADE:
printf("STATE: evade\n");
do{
switch(line_sensors){
case FRONT_LEFT:
printf("EVADE: FRONT_LEFT\n");
spin_right(255);
last_sensor = FRONT_LEFT;
_delay_ms(500);
break;
case FRONT_RIGHT:
printf("EVADE: FRONT_RIGHT\n");
last_sensor = FRONT_RIGHT;
spin_left(255);
_delay_ms(500);
break;
case FRONT_LEFT | FRONT_RIGHT:
printf("EVADE: FRONT\n");
go_reverse(255);
_delay_ms(1000);
brake();
spin_left(255);
_delay_ms(1000);
brake();
go_forward(255);
break;
}
} while((line_sensors = read_line_sensors()));
state = SEEK;
break;
case STOP:
printf("STATE: stopped\n");
break;
}
}
return 0;
}