-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdumpbot2.nqc
More file actions
123 lines (102 loc) · 1.8 KB
/
Copy pathdumpbot2.nqc
File metadata and controls
123 lines (102 loc) · 1.8 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
// dumpbot2.nqc
// follow a line, dump the cargo, then return
// linebot sensor, motors, and constants
#define EYE SENSOR_2
#define LEFT OUT_A
#define RIGHT OUT_C
#define LINE_THRESHOLD 51
#define STOPPER_THRESHOLD 42
#define TURN_SPEED 3
#define INITIAL_TIME 4
// dumper motor and constants
#define DUMPER OUT_B
#define DUMP_TIME 70
#define UNDUMP_TIME 100
void dump()
{
// dump the cargo bin
OnFwd(DUMPER);
Wait(DUMP_TIME);
Rev(DUMPER);
Wait(UNDUMP_TIME);
Off(DUMPER);
}
int direction, time, eye, ok_to_stop;
sub follow_line()
{
// initialize the variables
direction = 1;
time = INITIAL_TIME;
ok_to_stop = 0;
// start driving
OnFwd(LEFT+RIGHT);
while(true)
{
// read the sensor value
eye = EYE;
if (eye < LINE_THRESHOLD)
{
// we are either over the line or a stopper
if (eye > STOPPER_THRESHOLD)
ok_to_stop = 1;
else if (ok_to_stop == 1)
{
// found a stopper
Off(RIGHT+LEFT);
return;
}
}
else
{
// need to find the line again
ClearTimer(0);
if (direction == 1)
{
SetPower(RIGHT+LEFT, TURN_SPEED);
Rev(LEFT);
}
else
{
SetPower(RIGHT+LEFT, TURN_SPEED);
Rev(RIGHT);
}
while(true)
{
// have we found the line?
if (EYE < LINE_THRESHOLD)
{
time = INITIAL_TIME;
break;
}
if (Timer(0) > time)
{
// try the other direction
direction *= -1;
time *= 2;
break;
}
}
SetPower(LEFT+RIGHT, OUT_FULL);
Fwd(RIGHT+LEFT);
}
}
}
void turn_around()
{
// start turning
OnRev(LEFT);
OnFwd(RIGHT);
// wait until not over line
until(EYE >= LINE_THRESHOLD);
// wait until over line again
until(EYE < LINE_THRESHOLD);
Off(LEFT+RIGHT);
}
task main()
{
SetSensor(EYE, SENSOR_LIGHT);
follow_line();
dump();
turn_around();
follow_line();
}