-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutonomousModule.c
235 lines (220 loc) · 6.7 KB
/
AutonomousModule.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//////////////////////////////////////////////////////////////////////////////////////
// Autonomous Module //
//////////////////////////////////////////////////////////////////////////////////////
// The miscellaneous autonomous functions that allow simpler control over the //
// robot's individual modules. //
//////////////////////////////////////////////////////////////////////////////////////
const float AUT_target = TRJ_angularSpeedAtRange(13);
const float AUT_SHORT_SPEED = 1263;
int AUT_ballsFired = 0;
task AUT_countFiredBalls() {
AUT_ballsFired = 0;
writeDebugStreamLine("[Auton]: Counting fired balls...");
while (true) {
if (PID_ballFired == true) {
AUT_ballsFired++;
writeDebugStreamLine("[Auton]: Ball %i fired!", AUT_ballsFired);
/*PID_ballFired = false;
while (true) {
wait1Msec(1000 / PID_SPEED_INTERVALS_PER_SECOND);
if (PID_ballFired == true) {
PID_ballFired = false; // Cancel event.
} else {
break; // Ball is "fully fired".
}
}*/
wait1Msec(200);
PID_ballFired = false;
} else {
wait1Msec(1000 / PID_SPEED_INTERVALS_PER_SECOND * 2);
}
}
}
void AUT_surge(int power = 127, float time = 0) {
// To move forward or backward. (Nautical term)
// Positive is forward, negative is backward.
motor[PRT_wheelsLeft] = power;
motor[PRT_wheelsRight] = power;
/*motor[PRT_wheelFrontRight] = power;
motor[PRT_wheelBackRight] = power;
motor[PRT_wheelFrontLeft] = power;
motor[PRT_wheelBackLeft] = power;*/
if (time != 0) {
wait1Msec((int)(time * 1000));
motor[PRT_wheelsLeft] = power;
motor[PRT_wheelsRight] = power;
/*motor[PRT_wheelFrontRight] = 0;
motor[PRT_wheelBackRight] = 0;
motor[PRT_wheelFrontLeft] = 0;
motor[PRT_wheelBackLeft] = 0;*/
}
}
/*
void AUT_strafe(int power, float time = 0) {
// To move left or right without rotation.
// Positive is to the right, negative is to the left.
motor[PRT_wheelFrontRight] = -power;
motor[PRT_wheelBackRight] = power;
motor[PRT_wheelFrontLeft] = power;
motor[PRT_wheelBackLeft] = -power;
if (time != 0) {
wait1Msec((int)(time * 1000));
motor[PRT_wheelFrontRight] = 0;
motor[PRT_wheelBackRight] = 0;
motor[PRT_wheelFrontLeft] = 0;
motor[PRT_wheelBackLeft] = 0;
}
}*/
void AUT_rotate(int power, float time = 0) {
// To turn left or right without surge movement.
// Positive is clockwise, negative is counter clockwise.
motor[PRT_wheelsLeft] = power;
motor[PRT_wheelsRight] = -power;
/*motor[PRT_wheelFrontRight] = -power;
motor[PRT_wheelBackRight] = -power;
motor[PRT_wheelFrontLeft] = power;
motor[PRT_wheelBackLeft] = power;*/
if (time != 0) {
wait1Msec((int)(time * 1000));
motor[PRT_wheelsLeft] = 0;
motor[PRT_wheelsRight] = 0;
/*motor[PRT_wheelFrontRight] = 0;
motor[PRT_wheelBackRight] = 0;
motor[PRT_wheelFrontLeft] = 0;
motor[PRT_wheelBackLeft] = 0;*/
}
}
void AUT_halt() {
motor[PRT_wheelsLeft] = 0;
motor[PRT_wheelsRight] = 0;
/*motor[PRT_wheelFrontRight] = 0;
motor[PRT_wheelBackRight] = 0;
motor[PRT_wheelFrontLeft] = 0;
motor[PRT_wheelBackLeft] = 0;*/
}
void AUT_feedLower(int power = 127, float time = 0) {
// To pull inward or push outward from lower belt feed.
// Positive is inward, negative is outward.
motor[PRT_feedLower] = power;
if (time != 0) {
wait1Msec((int)(time * 1000));
motor[PRT_feedLower] = 0;
}
}
void AUT_feedUpper(int power = 127, float time = 0) {
// To pull inward or push outward from upper belt feed.
// Positive is inward, negative is outward.
motor[PRT_feedUpper] = power;
if (time != 0) {
wait1Msec((int)(time * 1000));
motor[PRT_feedUpper] = 0;
}
}
void AUT_warmGuns() {
if (GUN_enabled) {
GUN_spool = false;
GUN_warming = true;
} else if (PID_enabled) {
PID_target[Left] = AUT_target;
PID_target[Right] = AUT_target;
}
}
void AUT_coolGuns() {
if (GUN_enabled) {
GUN_warming = false;
GUN_spool = false;
} else if (PID_enabled) {
PID_target[Left] = 0;
PID_target[Right] = 0;
}
}
/*
void AUT_spoolGuns() {
GUN_warming = false;
GUN_spool = true;
}
*/
// Align the robot perpendicular to a surface using sonar. Both values must be within tolerance of each other.
// If the robot needs to be slightly off of perpendicular, offset the right side by a percentage to reach a
// desired angle.
bool AUT_alignWithSonar(float left, float right, float tolerance = 0.5, float rightPercentage = 1.0) {
if (left != SNR_INVALID && right != SNR_INVALID) {
if (abs((rightPercentage * left) - right) <= tolerance) {
AUT_halt();
return true; // Return true to indicate now aligned.
} else if ((rightPercentage * left) > right) {
AUT_rotate(-48); // Closer to right. Rotate counter-clockwise.
} else if (right > (left * rightPercentage)) {
AUT_rotate(48); // Closer to left. Rotate clockwise.
}
}
// Don't move anything. Wait for input.
return false;
}
bool AUT_distanceWithSonar(float left, float right, float target, float tolerance = 0.5) {
if (SNR_distanceInchesLeft != SNR_INVALID && SNR_distanceInchesRight != SNR_INVALID) {
float average = (SNR_distanceInchesLeft + SNR_distanceInchesRight) / 2;
if (average < target - tolerance) {
AUT_surge(28);
} else if (average > target + tolerance) {
AUT_surge(-28);
} else {
AUT_halt();
return true;
}
wait1Msec(50);
}
// Don't move anything. Wait for input.
return false;
}
void AUT_fireOnce() {
AUT_warmGuns();
if (GUN_enabled) {
while (GUN_power < 127) {
wait1Msec(50);
}
AUT_feedLower(127);
AUT_feedUpper(127);
wait1Msec(1500);
AUT_coolGuns();
motor[PRT_feedLower] = 0;
motor[PRT_feedUpper] = 0;
} else if (PID_enabled) {
while (!PID_ready) {
wait1Msec(50);
}
AUT_feedLower(127);
AUT_feedUpper(127);
wait1Msec(1500);
AUT_coolGuns();
motor[PRT_feedLower] = 0;
motor[PRT_feedUpper] = 0;
}
}
void AUT_shutDown() {
AUT_halt();
AUT_coolGuns();
motor[PRT_feedLower] = 0;
motor[PRT_feedUpper] = 0;
}
void AUT_demonstrate() {
writeDebugStreamLine("[Auton]: Enabling autonomous demonstration procedure.");
writeDebugStreamLine("[Auton]: Surging forward...");
AUT_surge(64, 0.5);
writeDebugStreamLine("[Auton]: Strafing right...");
//AUT_strafe(64, 0.5);
writeDebugStreamLine("[Auton]: Surging backward...");
AUT_surge(-64, 0.5);
writeDebugStreamLine("[Auton]: Strafing left...");
//AUT_strafe(-64, 0.5);
writeDebugStreamLine("[Auton]: Rotating right...");
AUT_rotate(64, 0.5);
writeDebugStreamLine("[Auton]: Strafing left...");
AUT_rotate(-64, 0.5);
writeDebugStreamLine("[Auton]: Warming gun...");
AUT_fireOnce();
writeDebugStreamLine("[Auton]: Gun fired!");
AUT_shutDown();
writeDebugStreamLine("[Auton]: Shutting down...");
writeDebugStreamLine("[Auton]: Autonomous demonstration procedure complete.");
}