-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRVCcontroller.c
More file actions
188 lines (172 loc) · 4.64 KB
/
RVCcontroller.c
File metadata and controls
188 lines (172 loc) · 4.64 KB
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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
//Sensor에서 특정 거리 이하면 장애물 존재한다고 판단하도록
#define OBSTACLE_STANDARD 5
int Front_Distance = 0; //Front Sensor Interface 값
int Left_Distance = 10; //Left Sensor Interface 값
int Right_Distance = 1; //Right Sensor Interface 값
bool Dust_Existance = false; //Dust Sensor Interface
// MoveLeft, MoveRight는 5 Tick 동안 진행되어야 하므로 Tick을 측정할 변수
int left_count = 0;
int right_count = 0;
//Determine Obstacle Location
int Det_OL(int Front_Distance, int Left_Distance, int Right_Distance) {
// 이진수 000 : Front - Left - Right
// 장애물 존재하면 해당 bit 1 / 존재하지 않으면 해당 bit 0
int obstacle = 0b000;
if (Front_Distance <= OBSTACLE_STANDARD) {
obstacle += 0b100;
printf("전방 장애물이 감지됨\n");
}
if (Left_Distance <= OBSTACLE_STANDARD) {
obstacle += 0b010;
printf("좌측 장애물이 감지됨\n");
}
if (Right_Distance <= OBSTACLE_STANDARD) {
obstacle += 0b001;
printf("우측 장애물이 감지됨\n");
}
return (obstacle);
}
bool Det_DE(bool Dust_Existance) {
return (Dust_Existance);
}
void MotorInterface(int Direction){
if(Direction == 1){
printf("전진 수행\n");
}
else if(Direction == 2){
printf("후진 수행\n");
}
else if(Direction == 3){
printf("좌회전 수행\n");
}
else if(Direction == 4){
printf("우회전 수행\n");
}
else if(Direction == -1){
printf("전진 중지\n");
}
else if(Direction == -2){
printf("후진 중지\n");
}
}
void CleanerInterface(bool Trigger){
if(Trigger){
printf("청소 가동\n");
}
else{
printf("청소 중지\n");
}
}
void CleanerPower(bool Trigger) {
CleanerInterface(Trigger);
}
void MoveForward(bool Enable_Disable) {
if (Enable_Disable)
MotorInterface(1);
else
MotorInterface(-1);
}
void MoveBackward(bool Enable_Disable) {
if (Enable_Disable)
MotorInterface(2);
else
MotorInterface(-2);
}
void TurnLeft(bool Trigger) {
if (Trigger) {
if (left_count > 0)
left_count -= 1;
else if (left_count == 0)
left_count = 4;
MotorInterface(3);
printf("좌회전 %d Tick 남음\n", left_count);
}
}
void TurnRight(bool Trigger) {
if (Trigger) {
if (right_count > 0)
right_count -= 1;
else if (right_count == 0)
right_count = 4;
MotorInterface(4);
printf("우회전 %d Tick 남음\n", right_count);
}
}
void main() {
int obstacle_Location;
bool dust_Existence;
int now_move_statement = 1; //이전 state가 Forward : 1 / Backword : -1 / 그 외 : 0
while (1) {
obstacle_Location = Det_OL(Front_Distance, Left_Distance, Right_Distance);
dust_Existence = Det_DE(Dust_Existance);
if (left_count == 0 && right_count == 0) {
if (obstacle_Location < 4 && !dust_Existence) {
if(now_move_statement == -1){
if (obstacle_Location == 1 || obstacle_Location == 0){
TurnLeft(true);
}
else if(obstacle_Location == 2){
TurnRight(true);
}
}
MoveForward(true);
now_move_statement = 1;
CleanerPower(false);
}
else if (obstacle_Location < 4 && dust_Existence) {
if(now_move_statement == -1){
if (obstacle_Location == 1 || obstacle_Location == 0){
TurnLeft(true);
}
else if(obstacle_Location == 2){
TurnRight(true);
}
}
MoveForward(true);
now_move_statement = 1;
CleanerPower(true);
}
else if (obstacle_Location == 4 || obstacle_Location == 5) { //Tick[F && !L] || Tick[!L]
if (now_move_statement == 1) {
MoveForward(false);
now_move_statement = 0;
}
else if(now_move_statement == -1) {
MoveBackward(false);
now_move_statement = 0;
}
TurnLeft(true);
}
else if (obstacle_Location == 6) { //Tick[F && L && !R]
if (now_move_statement == 1) {
MoveForward(false);
now_move_statement = 0;
}
else if(now_move_statement == -1) {
MoveBackward(false);
now_move_statement = 0;
}
TurnRight(true);
}
else if (obstacle_Location == 7) { //Tick[F && L && R]
if (now_move_statement == 1)
MoveForward(false);
MoveBackward(true);
now_move_statement = -1;
}
}
else {
if (left_count > 0) {
TurnLeft(true);
}
else if (right_count > 0) {
TurnRight(true);
}
}
usleep(200 * 1000); //wait(200ms)
}
}