-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal.c
244 lines (224 loc) · 5.66 KB
/
Final.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
236
237
238
239
240
241
242
243
244
#pragma config(Sensor, in1, armPot, sensorPotentiometer)
#pragma config(Sensor, dgtl1, liftPot, sensorQuadEncoder)
#pragma config(Motor, port1, leftBack, tmotorVex393_HBridge, openLoop, driveLeft)
#pragma config(Motor, port2, leftFront, tmotorVex393_MC29, openLoop, driveLeft)
#pragma config(Motor, port3, lift, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port4, arm, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port5, roller, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port6, mogo, tmotorVex393_MC29, openLoop)
#pragma config(Motor, port9, rightFront, tmotorVex393_MC29, openLoop, driveRight)
#pragma config(Motor, port10, rightBack, tmotorVex393_HBridge, openLoop, driveRight)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
//Declare all the necessary variables
const int MAX_NUM_CONES = 10;
const int numStackStates = 10;
const int armDefault = 0;
const int liftDefault = 0;
int liftConeValues[MAX_NUM_CONES];
int armStackValues[numStackStates];
string liftStackValues[numStackStates];
int rollerStackValues[numStackStates];
int numCones = 0;
int powerDriveLeft = 0;
int powerDriveRight = 0;
int deadZone = 20;
int stackState = 0;
int minAccuracy = 10;
int hit = 0;
bool cancelStackComplete = true;
bool clearStackComplete = true;
bool stackComplete = true;
//Returns a value in the range [min,max] if num doesn't belong in that range
int capVal(int num, int max, int min)
{
if(num > max)
{
return max;
}
else if(num < min)
{
return min;
}
return num;
}
//Drive control for the robot based on controller input
void drive(int chnn3,int chnn4)
{
if(abs(chnn3) < deadZone && abs(chnn4) < deadZone)
{
powerDriveLeft = 0;
powerDriveRight = 0;
}
int power = (int)sqrt(chnn3 * chnn3 + chnn4 * chnn4) * abs(chnn3)/chnn3;
powerDriveLeft = capVal (power + 2 * chnn4, power, -power);
powerDriveRight = capVal (power - 2 * chnn4, power, -power);
motor[leftFront] = powerDriveLeft;
motor[leftBack] = powerDriveLeft;
motor[rightFront] = powerDriveRight;
motor[rightBack] = powerDriveRight;
}
//Moves the lift and thus the arm to the specified position
bool moveTo(int armPos, int liftPos, int rollerDir)
{
motor[lift] = capVal((SensorValue[liftPot] - liftPos),100, -100);
motor[arm] = capVal((SensorValue[armPot] - armPos),100, -100);
motor[roller] = 100 * rollerDir;
return abs(SensorValue[liftPot] - liftPos) < minAccuracy && abs(SensorValue[armPot] - armPos) < minAccuracy;
}
bool moveToDefault()
{
return moveTo(armDefault, liftDefault, 1);
}
//Autostacking a cone onto the cap and returns whether it was sucessful or not
bool stack()
{
if(stackState < numStackStates)
{
string liftStackValStr = liftStackValues[stackState];
int liftStackVal = 0;
int strStartIndex = stringFind(liftStackValStr, "+STACK_HEIGHT");
if(strStartIndex != -1)
{
stringDelete(liftStackValStr, strStartIndex, 13);
liftStackVal = liftConeValues[numCones];
}
liftStackVal += atoi(liftStackValStr);
if(moveTo(armStackValues[stackState], liftStackVal, rollerStackValues[stackState]))
{
stackState++;
}
return false;
}
stackState = 0;
return true;
}
//Cancels a stack if necessary
bool cancelStack()
{
stackState = 0;
stackComplete = true;
return moveToDefault();
}
//Clear the stack
bool clearStack()
{
numCones = 0;
stackState = 0;
cancelStackComplete = true;
stackComplete = true;
return moveToDefault();
}
//Returns 0 for no flick, 1 for a upward flick, and - 1 for a downward flick
int flick(int input)
{
if(hit != 0)
{
if(abs(input) < 10)
{
int temp = hit;
hit = 0;
return temp;
}
if(time1[T1] > 1000)
{
hit = 0;
}
}
if(127 - abs(input) < 10)
{
if(input < 0)
{
hit = -1;
}
else
{
hit = 1;
}
clearTimer(T1);
}
return 0;
}
//Score on the Mobile Goal
void mobileGoal(bool upButton, bool downButton)
{
if(upButton && !downButton)
{
motor[mogo] = 127;
}
else if (downButton && !upButton)
{
motor[mogo] = -127;
}
else
{
motor[mogo] = 0;
}
}
//Lets the user control the lift if he wants to override the auto function
bool manualLiftOverride(bool upButton, bool downButton)
{
if(upButton && !downButton)
{
motor[lift] = 127;
}
else if (downButton && !upButton)
{
motor[lift] = -127;
}
else if (downButton && upButton)
{
motor[lift] = 0;
}
return !upButton && !downButton;
}
task main()
{
assign();
while(true)
{
drive(vexRT[Ch3], vexRT[Ch4]);
mobileGoal(vexRT[Btn6U],vexRT[Btn6D]);
if(manualLiftOverride(vexRT[Btn5U], vexRT[Btn5D]))
{
cancelStackComplete = true;
clearStackComplete = true;
stackComplete = true;
return;
}
int flickDirX = flick(vexRT[Ch1]);
int flickDirY = flick(vexRT[Ch2]);
if(flickDirY == 1)
{
numCones++;
}
if(flickDirY == -1)
{
numCones--;
cancelStackComplete = false;
}
if(flickDirX == 1)
{
stackComplete = false;
}
if(flickDirX == -1)
{
clearStackComplete = false;
}
if(!clearStackComplete)
{
clearStackComplete = clearStack();
continue;
}
if(!cancelStackComplete)
{
cancelStackComplete = cancelStack();
continue;
}
if(!stackComplete)
{
stackComplete = stack();
continue;
}
moveToDefault();
}
}