This repository has been archived by the owner on Jun 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautonFunctions.cpp
387 lines (339 loc) · 12.7 KB
/
autonFunctions.cpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
vex::brain Brain;
vex::controller Controller1 = vex::controller();
// Drive
vex::motor leftFront = vex::motor(vex::PORT1,vex::gearSetting::ratio18_1,false);
vex::motor leftBack = vex::motor(vex::PORT2,vex::gearSetting::ratio18_1,false);
vex::motor rightFront = vex::motor(vex::PORT9,vex::gearSetting::ratio18_1,true); // True = reversed
vex::motor rightBack = vex::motor(vex::PORT10,vex::gearSetting::ratio18_1,true);
// Intake
vex::motor intakeLeft = vex::motor(vex::PORT3,vex::gearSetting::ratio18_1,false);
vex::motor intakeRight = vex::motor(vex::PORT4,vex::gearSetting::ratio18_1,true);
// Puncher
vex::motor puncherLeft = vex::motor(vex::PORT5,vex::gearSetting::ratio36_1,true);
vex::motor puncherRight = vex::motor(vex::PORT6,vex::gearSetting::ratio36_1,false);
#include <cmath>
// Motivational quote
void littleCeasars(){
Controller1.Screen.print("lEtS gEt tHiS bReAd");
}
/*---------------------------------------------------------------------------*/
/* */
/* User Control Functions */
/* */
/*---------------------------------------------------------------------------*/
// Drive
void drive(){
leftFront.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct);
leftBack.spin(vex::directionType::fwd, Controller1.Axis3.value(), vex::velocityUnits::pct);
rightFront.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct);
rightBack.spin(vex::directionType::fwd, Controller1.Axis2.value(), vex::velocityUnits::pct);
}
void driveLock(){
if(Controller1.ButtonX.pressing()){
leftFront.stop(vex::brakeType::hold);
leftBack.stop(vex::brakeType::hold);
rightFront.stop(vex::brakeType::hold);
rightBack.stop(vex::brakeType::hold);
}
else if(Controller1.ButtonY.pressing()){
leftFront.stop(vex::brakeType::coast);
leftBack.stop(vex::brakeType::coast);
rightFront.stop(vex::brakeType::coast);
rightBack.stop(vex::brakeType::coast);
}
}
// Intake
void intake(){
if(Controller1.ButtonR2.pressing()){ // Intake ball
intakeLeft.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
intakeRight.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}
else if (Controller1.ButtonL1.pressing()){ // Reverse intake
intakeLeft.spin(vex::directionType::fwd, -100, vex::velocityUnits::pct);
intakeRight.spin(vex::directionType::fwd, -100, vex::velocityUnits::pct);
}
else if (Controller1.ButtonL2.pressing()){ // Run reversed bottom roller only
intakeRight.spin(vex::directionType::fwd, -100, vex::velocityUnits::pct);
}
else {
intakeLeft.stop(vex::brakeType::coast);
intakeRight.stop(vex::brakeType::coast);
}
}
// Puncher
void punch(){
if(Controller1.ButtonR1.pressing()){
puncherLeft.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
puncherRight.spin(vex::directionType::fwd, 100, vex::velocityUnits::pct);
}
else {
puncherLeft.stop(vex::brakeType::hold);
puncherRight.stop(vex::brakeType::hold);
}
}
/*---------------------------------------------------------------------------*/
/* */
/* Autonomous Functions */
/* */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Autonomous Drive Functions */
/*---------------------------------------------------------------------------*/
double sgn(double number){
if (number > 0) number = 1;
else number = -1;
return number;
}
void setLeftFrontPower (int power){
if(power==0) leftFront.stop(vex::brakeType::brake);
else{
leftFront.spin(vex::directionType::fwd,power,vex::velocityUnits::pct);
}
}
void setLeftBackPower (int power){
if(power==0) leftBack.stop(vex::brakeType::brake);
else{
leftBack.spin(vex::directionType::fwd,power,vex::velocityUnits::pct);
}
}
void setRightFrontPower (int power){
if(power==0) rightFront.stop(vex::brakeType::brake);
else{
rightFront.spin(vex::directionType::fwd,power,vex::velocityUnits::pct);
}
}
void setRightBackPower (int power){
if(power==0) rightBack.stop(vex::brakeType::brake);
else{
rightBack.spin(vex::directionType::fwd,power,vex::velocityUnits::pct);
}
}
void setDrivePower (int left, int right){
setLeftFrontPower(left);
setLeftBackPower(left);
setRightFrontPower(right);
setRightBackPower(right);
}
void autoDrive(int distance, int power = 100){
int direction = sgn(distance);
leftFront.resetRotation();
leftBack.resetRotation();
rightFront.resetRotation();
rightBack.resetRotation();
while(std::abs(leftFront.rotation(vex::rotationUnits::deg))<std::abs(distance)){
setDrivePower(power*direction, power*direction);
}
setDrivePower(0,0);
}
void autoTurn(int distance, int power = 60){
int direction = sgn(distance);
leftFront.resetRotation();
leftBack.resetRotation();
rightFront.resetRotation();
rightBack.resetRotation();
while(std::abs(leftFront.rotation(vex::rotationUnits::deg))<std::abs(distance)){
setDrivePower(-power*direction, power*direction);
}
setDrivePower(0,0);
}
// Drive with ramping
bool DriveRampingEnabled;
class Ramping{
public:
int ChangePct=1; // The amout of Pct change per loop
int ChangeMsec=1; // The amount of time in between loops
int RequestedPct=0; // Used to request Pct value change
int Pct=0; // Pct output
int MinUpPct=0; // Used as moveing up StillSpeed
int MinDownPct=0; // Used as moving down StillSpeed
int MaxPct=100; // the max pct value
Ramping(); // Object specifer
Ramping(int CP,int CM,int MaxP=100,int MinDP=0,int MinUP=0){
// ChangePct,ChangeMsec,MaxPct
ChangePct=CP;
ChangeMsec=CM;
MaxPct=MaxP;
MinUpPct=MinUP;
MinDownPct=MinDP;
}
void TaskRun(){
if(RequestedPct>Pct){ // Ramp up
Pct=Pct+ChangePct;
}
else if(RequestedPct<Pct){ // Ramp down
Pct=Pct-ChangePct;
}
// Limit Pct
if(Pct>MaxPct) Pct=MaxPct;
if(Pct<-MaxPct) Pct=-MaxPct;
if(Pct>0 && Pct<MinUpPct) Pct=MinUpPct;
if(Pct<0 && Pct>MinDownPct) Pct=MinDownPct;
}
}; // End of task
Ramping leftRamp(1,8); // First value = pct of pwr change after each interval passed
Ramping rightRamp(1,8); // Second value = number of Msec between each pct change
int Drive_Ramping(){
DriveRampingEnabled=true;
while(DriveRampingEnabled){
leftRamp.TaskRun();
rightRamp.TaskRun();
setDrivePower(leftRamp.Pct,rightRamp.Pct);
vex::task::sleep(leftRamp.ChangeMsec);
}
return 1;
}
void SetDRpower(int Lpower,int Rpower){ // DMR
leftRamp.RequestedPct = Lpower;
rightRamp.RequestedPct = Rpower;
}
void DI(int Lpower,int Rpower){
leftRamp.RequestedPct=Lpower; // Tells ramping to run at inputed power
rightRamp.RequestedPct=Rpower;
leftRamp.Pct=Lpower; // Instantly changes the ramping pct to desired power instead of ramping up
rightRamp.Pct=Rpower;
setDrivePower(leftRamp.Pct,rightRamp.Pct);
}
// Drive ramping with auto straightening
void driveRamp(double Distance,int Pct=75,int EndWait=200, int Correction=1){
// Update ramping speed
leftRamp.ChangeMsec = 8;
rightRamp.ChangeMsec = 8;
double Direction=sgn(Distance);
int Lpower=0;
int Rpower=0;
// Clear encoder
leftFront.resetRotation();
leftBack.resetRotation();
rightFront.resetRotation();
rightBack.resetRotation();
// Is it there yet?
while(std::abs(leftFront.rotation(vex::rotationUnits::deg))<std::abs(Distance)){
double LEncValue=leftFront.rotation(vex::rotationUnits::deg);
double REncValue=rightBack.rotation(vex::rotationUnits::deg);
// Straighten
if(std::abs(LEncValue)>std::abs(REncValue)){
Lpower=Pct-Correction;
Rpower=Pct;
}
else if(std::abs(LEncValue)<std::abs(REncValue)){
Lpower=Pct;
Rpower=Pct-Correction;
}
else if(std::abs(LEncValue)==std::abs(REncValue)){
Lpower=Pct;
Rpower=Pct;
}
// Correct direction
Lpower=Lpower*Direction;
Rpower=Rpower*Direction;
// Send to SetDRpower
SetDRpower(Lpower,Rpower);
vex::task::sleep(1);
}
SetDRpower(0,0);
vex::task::sleep(EndWait);
}
void rampTurn(double deg,int LPowerSend=40,int RPowerSend=40,int waitTime=200){ //-left,+right
int Dir=sgn(deg);
deg=std::abs(deg)/12.56;
LPowerSend=LPowerSend*Dir;
RPowerSend=RPowerSend*Dir;
leftFront.resetRotation();
leftBack.resetRotation();
rightFront.resetRotation();
rightBack.resetRotation();
while((std::abs(leftFront.rotation(vex::rotationUnits::rev))) <std::abs(deg)){
DI(LPowerSend,-RPowerSend);
vex::task::sleep(1);
}
DI(0,0);
vex::task::sleep(waitTime);
}
/*---------------------------------------------------------------------------*/
/* Autonomous Intake Functions */
/*---------------------------------------------------------------------------*/
void setIntakeLeftPower (int power){
if(power==0) intakeLeft.stop(vex::brakeType::coast);
else{
intakeLeft.spin(vex::directionType::fwd,power,vex::velocityUnits::pct);
}
}
void setIntakeRightPower (int power){
if(power==0) intakeRight.stop(vex::brakeType::coast);
else{
intakeRight.spin(vex::directionType::fwd,power,vex::velocityUnits::pct);
}
}
void setIntakePower (int left, int right){
setIntakeLeftPower(left);
setIntakeRightPower(right);
}
void autoIn(int rotations, int power = 100){
int direction = sgn(rotations);
intakeLeft.resetRotation();
intakeRight.resetRotation();
while(std::abs(intakeLeft.rotation(vex::rotationUnits::deg))<std::abs(rotations)){
setIntakePower(power*direction, power*direction);
}
setIntakePower(0,0);
}
void autoIntakeHalf(int rotations, int power = 100){
int direction = sgn(rotations);
intakeRight.resetRotation();
while(std::abs(intakeLeft.rotation(vex::rotationUnits::deg))<std::abs(rotations)){
setIntakePower(power*direction, power*direction);
}
setIntakeRightPower(0);
}
// Auto intake
void autoIntake(bool ON, bool In){
if(ON){
if(In){
setIntakePower(100,100);
}
if(!In){
setIntakePower(-100,-100);
}
}
if(!ON){
intakeLeft.stop();
intakeRight.stop();
}
}
/*---------------------------------------------------------------------------*/
/* Autonomous Puncher Functions */
/*---------------------------------------------------------------------------*/
void setPuncherLeftPower (int power){
if(power==0) puncherLeft.stop(vex::brakeType::hold);
else{
puncherLeft.spin(vex::directionType::fwd,power,vex::velocityUnits::pct);
}
}
void setPuncherRightPower (int power){
if(power==0) puncherRight.stop(vex::brakeType::hold);
else{
puncherRight.spin(vex::directionType::fwd,power,vex::velocityUnits::pct);
}
}
void setPuncherPower (int left, int right){
setPuncherLeftPower(left);
setPuncherRightPower(right);
}
void autoShoot(int rotations = 360, int power = 100){
int direction = sgn(rotations);
puncherLeft.resetRotation();
puncherRight.resetRotation();
while(std::abs(puncherLeft.rotation(vex::rotationUnits::deg))<std::abs(rotations)){
setPuncherPower(power*direction, power*direction);
}
setPuncherPower(0,0);
}
void pullBack(int rotations = 180, int power = 100){
int direction = sgn(rotations);
puncherLeft.resetRotation();
puncherRight.resetRotation();
while(std::abs(puncherLeft.rotation(vex::rotationUnits::deg))<std::abs(rotations)){
setPuncherPower(power*direction, power*direction);
}
setPuncherPower(0,0);
}