forked from YeoJiSu/Avoiding_Obstacles_Robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobstacle.c
203 lines (178 loc) ยท 6.33 KB
/
obstacle.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
#include <stdio.h> // merge ์ถฉ๋ ์คํ ๊ฐ์ดํด๋ณด๊ธฐ
#include <stdbool.h>
#define arrSize 1000
#define GO 1
#define STOP 0
#define Arrival 17
#define TESTSIZE 36 // ๋๋ฒ๊น
์ฉ์ผ๋ก ์์ผ๋ก ์๋ฃ์ถ๊ฐ๋ ์ฌ๊ธฐ ์ซ์๋ง ๊ณ ์น๋ฉด ๋ฉ๋๋ค!!
typedef enum {
BACK, LEFT, FORWARD, RIGHT
}Direction;
typedef struct Trace {
Direction dir;
int time;
}Trace;
typedef struct robot {
Direction direction; // ์ถ๋ฐ์ ์ ์์ผ
bool isGo; // ์ ์ง ๋๋ ์ถ๋ฐ
Trace* trace; // ์ง๋์จ ๊ธธ (MAP)
int forwardBackward; // ์๋ค ๋ณ์ (๊ฒฐ์น์ ๊น์ง ๋จ์ ๊ฑฐ๋ฆฌ)
int leftRight; // ์ข์ฐ ๋ณ์ (์ขํ์ , ์ฐํ์ ๊ฒฐ์ ์์)
}robot;
void turnLeft(robot* rbt)
{
rbt->direction -= 1;
if (rbt->direction < BACK)
rbt->direction = RIGHT;
}
void turnRight(robot* rbt) {
rbt->direction += 1;
if (rbt->direction > RIGHT)
rbt->direction = BACK;
}
Trace trace2[arrSize] = { {FORWARD, 0}, };
int rbt_index = 0; // ๋ก๋ด ๋ฐฉํฅ์ด ๋ฐ๋ ๋ ์ฆ๊ฐํ๋ index. ๊ฒฐ๊ตญ Trace ์ ์ฅ์ฉ!
int exampleF[TESTSIZE] = { 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
int exampleL[TESTSIZE] = { 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,};
int exampleR[TESTSIZE] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
// if(o, Dirction == LEFT)
void setNewDirectionToTrace(robot* rbt) {
rbt_index++;
rbt->trace[rbt_index].dir = rbt->direction;
rbt->trace[rbt_index].time = 0;
}
bool noObstacle(int idx) {
return !exampleF[idx] && !exampleL[idx] && !exampleR[idx];
}
bool frontLeftObstacle(int idx) {
return exampleF[idx] && exampleL[idx] && !exampleR[idx];
}
bool frontRightObstacle(int idx) {
return exampleF[idx] && !exampleL[idx] && exampleR[idx];
}
bool leftObstacle(int idx) {
return !exampleF[idx] && exampleL[idx] && !exampleR[idx];
}
bool rightObstacle(int idx) {
return !exampleF[idx] && !exampleL[idx] && exampleR[idx];
}
bool frontObstacle(int idx) {
return exampleF[idx] && !exampleL[idx] && !exampleR[idx];
}
bool isRobotForward(robot* rbt) {
return rbt->direction == FORWARD;
}
bool isRobotLeft(robot* rbt) {
return rbt->direction == LEFT;
}
bool isRobotRight(robot* rbt) {
return rbt->direction == RIGHT;
}
bool isRobotBack(robot* rbt) {
return rbt->direction == BACK;
}
bool isRobotArrived(robot* rbt) {
return rbt->forwardBackward >= Arrival;
}
void timePassed(robot* rbt) {
rbt->trace[rbt_index].time++;
}
int main(void) {
struct robot rbt = { FORWARD, GO, trace2, 0, 0 }; // ๋ก๋ด ์ํ ์ด๊ธฐํ;
// robot* rbt_ptr = &rbt;
// turnLeft(&rbt);
// setNewDirectionToTrace(rbt_ptr);
for (int currPos = 0; currPos < TESTSIZE; currPos++)
{
if (isRobotArrived(&rbt)) { // ๋์ฐฉ์ง์ ๋์ฐฉํ์ผ๋ฉด ๋ฉ์ถฐ๋ผ
break;
}
if (isRobotForward(&rbt) && noObstacle(currPos)) { // ํ์ฌ ์์น์ ์ฅ์ ๋ฌผ์ด ์๋ ๊ฒฝ์ฐ
// time ++;
// setNewDir ์ฌ์ฉ ์ ํจ;
// increase Trace[index].time;
timePassed(&rbt);
}
if (isRobotForward(&rbt) && leftObstacle(currPos)) {
timePassed(&rbt);
}
if (isRobotForward(&rbt) && rightObstacle(currPos)) {
timePassed(&rbt);
}
else if (isRobotForward(&rbt) && frontLeftObstacle(currPos)) {
// frontLeft๊ฐ ์์ด์ง ๋ ๊น์ง ํ์ ํ ํ, S/W๋ก turnRight()๋ฅผ ํ๋ฒ๋ง ํด์ผ Back์ํ๋ก ๊ฐ์ง ์์.
while (frontLeftObstacle(currPos)) {
// Rotate Robot() : H/W
currPos++;
}
turnRight(&rbt); // setStatus
setNewDirectionToTrace(&rbt); // setTrace
}
else if (isRobotForward(&rbt) && frontRightObstacle(currPos)) {
while (frontRightObstacle(currPos)) {
// Rotate Robot() : H/W
currPos++;
}
turnLeft(&rbt); // setStatus
setNewDirectionToTrace(&rbt); // setTrace
}
else if (isRobotRight(&rbt) && leftObstacle(currPos)) {
// Go until noLeftObstacle;
timePassed(&rbt);
rbt.leftRight++;
}
else if (isRobotLeft(&rbt) && rightObstacle(currPos)) {
// Go until noRightObstacle;
timePassed(&rbt);
rbt.leftRight--;
}
else if (isRobotLeft(&rbt)&& noObstacle(currPos)) {
turnRight(&rbt); // setStatus
setNewDirectionToTrace(&rbt); // setTrace
}
else if (isRobotLeft(&rbt)&& frontRightObstacle(currPos)) {
turnRight(&rbt); // setStatus
setNewDirectionToTrace(&rbt); // setTrace
}
else if (isRobotRight(&rbt) && noObstacle(currPos)) {
turnLeft(&rbt); // setStatus
setNewDirectionToTrace(&rbt); // setTrace
}
else if (isRobotRight(&rbt) && frontLeftObstacle(currPos)) {
turnLeft(&rbt); // setStatus
setNewDirectionToTrace(&rbt); // setTrace
}
else if (isRobotForward(&rbt) && frontObstacle(currPos)) {
if (rbt.leftRight > 0) {
while (frontObstacle(currPos)) {
// Rotate Robot() : H/W -> ๋ก๋ด์ ์ผ์ชฝ์ผ๋ก ๋๋ฆฌ๊ธฐ
currPos++;
}
turnLeft(&rbt); // setStatus
}
else{
while (frontObstacle(currPos)) {
// Rotate Robot() : H/W -> ๋ก๋ด์ ์ค๋ฅธ์ชฝ์ผ๋ก ๋๋ฆฌ๊ธฐ
currPos++;
}
turnRight(&rbt); // setStatus
}
setNewDirectionToTrace(&rbt); // setTrace
}
// ์ง์ง ๋ณ์ ๊ณ์ฐํ๊ธฐ
if (isRobotForward(&rbt) && (noObstacle(currPos) || rightObstacle(currPos) || leftObstacle(currPos))) {
rbt.forwardBackward++;
}
}
for (int i = 0; i < TESTSIZE; i++) {
if (rbt.trace[i].dir == LEFT) {
printf("์ผ์ชฝ์ผ๋ก %d\n", rbt.trace[i].time);
}
if (rbt.trace[i].dir == FORWARD) {
printf("์์ผ๋ก %d\n", rbt.trace[i].time);
}
if (rbt.trace[i].dir == RIGHT) {
printf("์ค๋ฅธ์ชฝ์ผ๋ก %d\n", rbt.trace[i].time);
}
}
}