-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_scan.nxc
212 lines (141 loc) · 3.28 KB
/
field_scan.nxc
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
//motors and which ports they are connected
#define LEFT_MOTOR OUT_C
#define RIGHT_MOTOR OUT_B
#define US_SENSOR SensorUS(IN_3)
#define TORCH_SENSOR Sensor(IN_1)
#define BALL_SENSOR Sensor(IN_2)
//variable declarations
int CURRENT_READING = 0;
int PREVIOUS_READING = 0;
bool BALL_FOUND = false;
bool CIRCLE_SCAN = true;
sub initial_scan(){
}
sub search_light(){
int counter = 0;
int dia = 0;
dia = 20;
while (true) {
TextOut(1,LCD_LINE1,"TORCH SENSOR");
NumOut(2,LCD_LINE2,TORCH_SENSOR);
if (TORCH_SENSOR > 40) {
break;
}
//rotate robot to initite the search
OnFwd(LEFT_MOTOR, 35);
OnFwd(RIGHT_MOTOR, 5);
Wait(100);
dia = dia + 1;
counter = counter + 1;
}
}
sub approach_light(){
OnFwd(LEFT_MOTOR, 25);
OnFwd(RIGHT_MOTOR, 25);
Wait(1000);
while(true){
OnFwd(LEFT_MOTOR, 25);
OnFwd(RIGHT_MOTOR, 25);
//Wait(1500);
if (TORCH_SENSOR > 40){
//upclose release ball
//kick
RotateMotor(OUT_A, 95, +180);
//Wait(300);
break;
}
}
}
task avoid_walls(){
//OnRev(LEFT_MOTOR, 35);
//OnRev(RIGHT_MOTOR, 5);
int difference = 0;
Wait(1500);
while(true){
TextOut(1,LCD_LINE1,"US sensor");
NumOut(2,LCD_LINE2,US_SENSOR);
//TextOut(1,LCD_LINE3,"PREVIOUS_READING");
//NumOut(2,LCD_LINE4,PREVIOUS_READING);
//TextOut(1,LCD_LINE5,"CURRENT_READING");
//NumOut(2,LCD_LINE6,CURRENT_READING);
//Wait(100);
if (BALL_FOUND == false && CIRCLE_SCAN==true) {
//rotate robot to initite the search
OnFwd(LEFT_MOTOR, 50);
OnFwd(RIGHT_MOTOR, 10);
CURRENT_READING = US_SENSOR;
difference = PREVIOUS_READING - CURRENT_READING;
//stop scanning if ball is found
if (difference > 10) {
CIRCLE_SCAN=false;
//break;
}
PREVIOUS_READING = CURRENT_READING;
}
if(BALL_FOUND == false) {
//initial_scan();
ClearScreen();
TextOut(1,LCD_LINE1,"US SENSOR");
NumOut(2,LCD_LINE2,US_SENSOR);
//Wait(100);
if (US_SENSOR < 22) {
if (US_SENSOR < 7) {
OnRev(LEFT_MOTOR, 35);
OnRev(RIGHT_MOTOR, 35);
Wait(3000);
OnFwd(LEFT_MOTOR, 25);
OnFwd(RIGHT_MOTOR, 15);
Wait(1000);
}
else
{
OnFwd(LEFT_MOTOR, 15);
OnFwd(RIGHT_MOTOR, 15);
Wait(100);
}
}
else{
OnFwd(LEFT_MOTOR, 25);
OnFwd(RIGHT_MOTOR, 25);
}
}
}
}
task search_ball(){
while(true){
if (BALL_SENSOR < 25){
//override the avoid wall function
BALL_FOUND = true;
Wait(200);
//back up a little
OnRev(LEFT_MOTOR, 26);
OnRev(RIGHT_MOTOR, 26);
Wait(500);
//OnFwd(LEFT_MOTOR, 0);
//OnFwd(RIGHT_MOTOR, 0);
//Wait(200);
//put the kicker down
RotateMotor(OUT_A, 75, -180);
//Wait(300);
search_light();
approach_light();
OnRev(LEFT_MOTOR, 35);
OnRev(RIGHT_MOTOR, 15);
Wait(3500);
BALL_FOUND = false;
CIRCLE_SCAN = true;
}
}
}
task main(){
//set sensor
SetSensorUltrasonic(IN_3);
SetSensorLight(IN_1);
SetSensorType(IN_1,IN_TYPE_LIGHT_INACTIVE);
SetSensorMode(IN_1,IN_MODE_PCTFULLSCALE);
ResetSensor(IN_1);
SetSensorLight(IN_2);
//scan the field
//should be inside while loop
Precedes(avoid_walls,search_ball);
}