This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Robot.cpp
531 lines (434 loc) · 11.7 KB
/
Robot.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "WPILib.h"
#include<thread>
#include <Math.h>
using namespace std;
#define DEBUG //Debug mode, shows stats as well as other functions for testing
//#define THROTTLE //Sets rollers to be controlled by the a_stick throttle
#define TRACKMULT .9 //Multiplier for vision tracking speed
#define ARM_BOTTOM_POSITION -6.0 //Arm encoder positions
#define ARM_UP_POSITION -590
#define ARM_CLIMB_POSITION -656
#define ARM_TRAVERSE_POSITION -190
#define WINCH_PORT_POSITION 4721
#define WINCH_ZERO_POSITION 0
#define SHIFT_UP_SPEED 3000 //Encoder speed(rate) limits for shifting gears
#define SHIFT_DOWN_SPEED 2500
#define R_TRAIN(x)r_motor1.x;r_motor2.x; //sets all the motors in each drive train or roller
#define L_TRAIN(x)l_motor1.x;l_motor2.x;
//#define ROLLERS(x)roller7.x;roller8.x;
class Robot: public SampleRobot
{
Joystick l_stick;
Joystick r_stick;
Joystick a_stick;
CANTalon l_motor1;
CANTalon l_motor2;
CANTalon r_motor1;
CANTalon r_motor2;
CANTalon winch5;
CANTalon a_motor6;
CANTalon roller7;
CANTalon roller8;
CANTalon a_motor9;
Compressor cmprssr;
std::shared_ptr<NetworkTable> table; //holds GRIP vision tracking values
std::vector<double> centerX; //values from the NetworkTable
std::vector<double> centerY;
std::vector<double> area;
Encoder armEncoderLeft;
Encoder r_drive,l_drive; //Left and right motor encoders
Solenoid shooter,shift;
float armPosition; //Keeps track of the arm position
float armSetPoint;
float winchPosition;
float winchSetPoint;
bool b_shift=1; //Whether or not in high gear (Solenoid on/off)
double avg_speed;
typedef enum {
Floor,
Shoot,
Climb,
Traverse
}ArmPosition;
typedef enum {
Port,
Zero
}WinchPosition;
public:
Robot() :
l_stick(0),
r_stick(1),
a_stick(2),
l_motor1(1),
l_motor2(2),
r_motor1(3),
r_motor2(4),
winch5(5),
a_motor6(6),
roller7(7),
roller8(8),
a_motor9(9),
armEncoderLeft(0,1,Encoder::k4X), //Encoder A channel on port 0, Encoder B channel on port 1
r_drive(2,3,Encoder::k4X),
l_drive(4,5,Encoder::k4X),
shooter(1),
shift(0)
{
armPosition = 0.0;
armSetPoint = 0.0; //Start the setpoint at 0
winchPosition = 0.0;
winchSetPoint = 0.0;
a_motor6.ConfigNeutralMode(CANTalon::kNeutralMode_Brake); //Sets the arm motors to break mode
a_motor9.ConfigNeutralMode(CANTalon::kNeutralMode_Brake);
winch5.SetEncPosition(0);
roller7.SetVoltageRampRate(24);
roller8.SetVoltageRampRate(24);
winch5.ConfigNeutralMode(CANTalon::kNeutralMode_Brake);
/*l_motor2.ConfigNeutralMode(CANTalon::kNeutralMode_Coast);
r_motor2.ConfigNeutralMode(CANTalon::kNeutralMode_Coast); */
}
void Autonomous()
{
StopMotors();
/*std::string autoSelected = SmartDashboard::GetString("Auto Selector", autoNameDefault);
if(autoSelected == autoNameCustom){
//Custom Auto goes here
} else {
//Default Auto goes here
}*/
}
void OperatorControl()
{
#ifdef DEBUG
PowerDistributionPanel pdp; //Initializes the pdp if DEBUG is not commented out at the top
#endif // DEBUG
StopMotors(); //Stops all motors before beginning to drive
SmartDashboard::PutString("DB/String 3", std::to_string(armEncoderLeft.GetDistance()));
while (IsOperatorControl() && IsEnabled())
{
/*
#ifdef DEBUG
#ifdef THROTTLE
ROLLERS(Set(a_stick.GetThrottle())); //If both DEBUG and THROTTLE are defined,
//then set the roller speed to the throttle
#else
if(a_stick.GetRawButton(7)) //If DEBUG, but not THROTTLE,
ROLLERS(Set(-1)); //then set rollers when button are pressed
if(a_stick.GetRawButton(8))
ROLLERS(Set(1));
if(!(a_stick.GetRawButton(8)||a_stick.GetRawButton(7)))
ROLLERS(Set(0));
#endif // THROTTLE
*/
//If DEBUG, then display PDP and Talon values in SmartDahsboard Strings
SmartDashboard::PutString("DB/String 5",
(string)"PDP 1: " + std::to_string(pdp.GetCurrent(1)) + (string)"A");
SmartDashboard::PutString("DB/String 6",
(string)"PDP 14: " + std::to_string(pdp.GetCurrent(14)) + (string)"A");
SmartDashboard::PutString("DB/String 8",
(string)"Talon 7: " + std::to_string(roller7.GetOutputCurrent()) + (string)"A");
SmartDashboard::PutString("DB/String 9",
(string)"Talon 8: " + std::to_string(roller8.GetOutputCurrent()) + (string)"A");
SmartDashboard::PutString("DB/String 1", std::to_string(l_drive.GetRate()).c_str());
SmartDashboard::PutString("DB/String 2", std::to_string(-r_drive.GetRate()).c_str());
SmartDashboard::PutString("DB/String 3", (string)"ArmEncoder: " + std::to_string(armEncoderLeft.GetDistance()));
SmartDashboard::PutString("DB/String 7", (string)"WincEncoder: " + std::to_string(winch5.GetEncPosition()));
//shooter.Set(a_stick.GetRawButton(1)); //If DEBUG, then shoot on a_stick trigger press
if(a_stick.GetRawButton(1))
{
shooter.Set(true);
}
else
{
shooter.Set(false);
}
//#endif // DEBUG
table = NetworkTable::GetTable("GRIP/myContoursReport");
centerX = table->GetNumberArray("centerX", llvm::ArrayRef<double>());
centerY = table->GetNumberArray("centerY", llvm::ArrayRef<double>());
area = table->GetNumberArray("area", llvm::ArrayRef<double>());
if(a_stick.GetRawButton(2))
{
DriveWinchToPosition(Port);
}
else if(a_stick.GetRawButton(3))
{
DriveWinchToPosition(Zero);
}
else
{
winch5.Set(a_stick.GetY());
}
if(l_stick.GetRawButton(1))
TrackTarget();
else if (r_stick.GetRawButton(1))
TrackTargetPivot();
/* else if (l_stick.GetRawButton(2))
{
FollowMe();
} */
else
TankDrive();
/*if(l_stick.GetRawButton(2))
{
DriveArmToPosition(Floor); //Set to the down position
}
else if(l_stick.GetRawButton(3))
{
DriveArmToPosition(Shoot); //Set to the up position
}*/
/*
#ifdef DEBUG
a_motor6.Set(a_stick.GetY()); //If DEBUG, then control arm with a_stick
a_motor9.Set(-a_stick.GetY());
#endif // DEBUG
*/
//Roller control
if(a_stick.GetRawButton(7))
{
roller7.Set(1);
roller8.Set(1);
}
else if(a_stick.GetRawButton(8))
{
roller7.Set(-1);
roller8.Set(-1);
}
else
{
roller7.Set(0);
roller8.Set(0);
}
//Arm position control
/*a_motor6.Set(a_stick.GetY());
a_motor9.Set(-a_stick.GetY());
*/
if(a_stick.GetRawButton(11))
{
//armSetPoint = ARM_BOTTOM_POSITION;
DriveArmToPosition(Floor);
}
else if(a_stick.GetRawButton(12))
{
DriveArmToPosition(Shoot);
}
else if(a_stick.GetRawButton(9))
{
DriveArmToPosition(Climb);
}
else
{
DriveArmToPosition(Traverse);
}
if(l_stick.GetRawButton(6))
shift.Set(0);
else
AutoShift();
Wait(0.005); // wait for a motor update time
}
}
void AutoShift() //Automatically shifts gears based on average speed of motors
{
avg_speed = (fabs(l_drive.GetRate())+fabs(r_drive.GetRate()))/2;
if(avg_speed>SHIFT_UP_SPEED)
shift.Set(1);
else if(avg_speed<SHIFT_DOWN_SPEED)
shift.Set(0);
}
void StopMotors() //Sets all motors to 0
{
l_motor1.Set(0);
l_motor2.Set(0);
r_motor1.Set(0);
r_motor2.Set(0);
winch5.Set(0);
a_motor6.Set(0);
roller7.Set(0);
roller8.Set(0);
a_motor9.Set(0);
}
void DriveArmToPosition(ArmPosition setpoint) //Sets arm in position 'setpoint'
{
//First give the encoder a setpoint to drive to
switch(setpoint){
case Floor:
armSetPoint = ARM_BOTTOM_POSITION;
break;
case Shoot:
armSetPoint = ARM_UP_POSITION;
break;
case Climb:
armSetPoint = ARM_CLIMB_POSITION;
break;
case Traverse:
armSetPoint = ARM_TRAVERSE_POSITION;
break;
default:
std::cerr<<"Non-existant arm position setting"<<endl;
}
armPosition = armEncoderLeft.GetDistance();
float error = armPosition - armSetPoint;
float motorOutputBeforeSqrt = (error/650);
float motorOutput;
if(motorOutputBeforeSqrt < 0) //Make sure we don't take the sqrt of a negative number
{
motorOutputBeforeSqrt = -motorOutputBeforeSqrt;
motorOutput = cbrt(motorOutputBeforeSqrt);
motorOutput = -motorOutput;
}
else
{
motorOutput = cbrt(motorOutputBeforeSqrt);
}
if(motorOutput < 0)
{
motorOutput = motorOutput/2;
}
if( (armSetPoint == ARM_BOTTOM_POSITION ) && (error < 20 && error > -20) )
{
a_motor9.Set(0.0);
a_motor6.Set(0.0);
}
else
{
a_motor9.Set(motorOutput);
a_motor6.Set(-motorOutput);
}
SmartDashboard::PutString("DB/String 4", (string)"MotorOutput " + std::to_string(motorOutput));
/*
if( (armPosition - armSetPoint) / 30 < .3) //Test these numbers
{
a_motor9.Set(-.3); //and these
a_motor6.Set(.3);
}
else if( (armPosition - armSetPoint) / 30 > -.65) //and these
{
a_motor9.Set(-.65); //and these
a_motor6.Set(.65);
}
else
{
a_motor9.Set(- (armPosition - armSetPoint) / 30); //and these
a_motor6.Set((armPosition - armSetPoint) / 30);
}
*/
}
void DriveWinchToPosition(WinchPosition setpoint) //Sets arm in position 'setpoint'
{
//First give the encoder a setpoint to drive to
switch(setpoint){
case Port:
winchSetPoint = WINCH_PORT_POSITION;
break;
case Zero:
winchSetPoint = WINCH_ZERO_POSITION;
break;
default:
std::cerr<<"Non-existant arm position setting"<<endl;
}
winchPosition = winch5.GetEncPosition();
float error = winchPosition - winchSetPoint;
float motorOutputBeforeSqrt = error / 1500;
//float motorOutput;
winch5.Set(motorOutputBeforeSqrt);
//SmartDashboard::PutString("DB/String 4", (string)"MotorOutput " + std::to_string(motorOutput));
}
void GearShift() //Shifts gears based on a single button press
{
if(l_stick.GetRawButton(6))
{
shift.Set(0);
b_shift=0;
}
else if(!b_shift)
{
shift.Set(1);
b_shift=1;
}
}
void TankDrive() //Drives each train based on each joystick
{
R_TRAIN(Set(-r_stick.GetY()));
L_TRAIN(Set(l_stick.GetY()));
}
void TrackTarget() //Turns towards any target in sight with only the right train
{
double distoff = 0.0;
if(!centerX.empty())
{
distoff = 160 - centerX[0];
if(centerX[0] < 140.00)
{
//l_motor1.Set(distoff/160 * TRACKMULT);
r_motor1.Set(distoff/160 * TRACKMULT);
}
else if(centerX[0] > 180.00)
{
//l_motor1.Set(distoff/160 * TRACKMULT);
r_motor1.Set(distoff/160 * TRACKMULT);
}
else if(centerX[0] > 140 && centerX[0] < 180)
{
l_motor1.Set(0);
r_motor1.Set(0);
}
}
}
void TrackTargetPivot() //Turns towards any target in sight with both trains
{
double distoff = 0.0;
if(!centerX.empty())
{
distoff = 160 - centerX[0];
if(centerX[0] < 140.00)
{
l_motor1.Set(distoff/160 * TRACKMULT);
r_motor1.Set(distoff/160 * TRACKMULT);
}
else if(centerX[0] > 180.00)
{
l_motor1.Set(distoff/160 * TRACKMULT);
r_motor1.Set(distoff/160 * TRACKMULT);
}
else if(centerX[0] > 140 && centerX[0] < 180)
{
l_motor1.Set(0);
r_motor1.Set(0);
}
}
}
void FollowMe() //Follows target in a straight line by measuring area of target
{
if (area[0] > 1000)
{
L_TRAIN(Set(0));
R_TRAIN(Set(0));
}
else if (area[0] < 500)
{
L_TRAIN(Set(0.3));
R_TRAIN(Set(-0.3));
}
else if (area[0] < 1000 && area[0] > 500)
{
L_TRAIN(Set(0.2));
R_TRAIN(Set(-0.2));
}
else
{
L_TRAIN(Set(0));
R_TRAIN(Set(0));
}
}
void Test()
{
StopMotors();
while(IsTest() && IsEnabled())
{
armPosition = armEncoderLeft.GetDistance();
SmartDashboard::PutString("DB/String 3", (string)"ArmEncoder: " + std::to_string(armEncoderLeft.GetDistance()));
SmartDashboard::PutString("DB/String 7", (string)"WincEncoder: " + std::to_string(winch5.GetEncPosition()));
}
}
};
START_ROBOT_CLASS(Robot)