-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.java
More file actions
445 lines (337 loc) · 13.2 KB
/
Robot.java
File metadata and controls
445 lines (337 loc) · 13.2 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
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
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
package frc.robot;
//import com.ctre.phoenix.motorcontrol.ControlMode;
//import com.ctre.phoenix.motorcontrol.NeutralMode;
//import com.ctre.phoenix.motorcontrol.can.SSPX; // Ron i swear i will delete this line if its the last thing i do
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMax.IdleMode;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;
/*basically have to create a PID loop */
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj2.command.PIDSubsystem;
import edu.wpi.first.wpilibj2.command.*;
import edu.wpi.first.wpilibj.*;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.math.filter.SlewRateLimiter;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
public class Robot extends TimedRobot {
/*
* Autonomous selection options.
*/
private static final String kNothingAuto = "do nothing";
private static final String kConeAuto = "cone";
private static final String kCubeAuto = "cube";
private String m_autoSelected;
/*
* HowdyBot PID code
*/
private final SendableChooser<String> m_chooser = new SendableChooser<>();
public static final double kMaxSpeed = 4466; //RPM
public static PIDController leftController;
public static PIDController rightController;
public static double driveKp = .001;
/*
* Drive motor controller instances.
*
* Change the id's to match your robot.
* Change kBrushed to kBrushless if you are using NEO's.
* Use the appropriate other class if you are using different controllers.
*/
CANSparkMax driveLeftLeader = new CANSparkMax(5, MotorType.kBrushless);
CANSparkMax driveRightLeader = new CANSparkMax(3, MotorType.kBrushless); // id had problems
CANSparkMax driveLeftFollower = new CANSparkMax(2, MotorType.kBrushless);
CANSparkMax driveRightFollower = new CANSparkMax(6, MotorType.kBrushless);
SlewRateLimiter filter = new SlewRateLimiter(0.5);
/*
* Mechanism motor controller instances.
*
* Like the drive motors, set the CAN id's to match your robot or use different
* motor controller classses (TalonFX, TalonSRX, Spark, Spark2SP) to match your
* robot.
*
* The arm is a NEO on Everybud.
* The intake is a NEO 550 on Everybud.
*/
CANSparkMax armLeader = new CANSparkMax(7, MotorType.kBrushless); // <- id here has problems
CANSparkMax armFollower = new CANSparkMax(4, MotorType.kBrushless); // might have problems
CANSparkMax intake = new CANSparkMax(11, MotorType.kBrushless);// <- id here has problems
/**
* The starter code uses the most generic joystick class.
*
* The reveal video was filmed using a logitech gamepad set to
* directinput mode (switch set to D on the bottom). You may want
* to use the XBoxController class with the gamepad set to XInput
* mode (switch set to X on the bottom) or a different controller
* that you feel is more comfortable.
*/
Joystick j = new Joystick(1); // Was 0 but changed due to testing
/*
* Magic numbers. Use these to adjust settings.
*/
/**
* How many amps the arm motor can use.
*/
static final int ARM_CURRENT_LIMIT_A = 20;
/**
* Percent output to run the arm up/down at
*/
static final double ARM_OUTPUT_POWER = 0.35;
/**
* How many amps the intake can use while picking up
*/
static final int INTAKE_CURRENT_LIMIT_A = 25;
/**
* How many amps the intake can use while holding
*/
static final int INTAKE_HOLD_CURRENT_LIMIT_A = 5;
/**
* Percent output for intaking
*/
static final double INTAKE_OUTPUT_POWER = 1.0;
/**
* Percent output for holding
*/
static final double INTAKE_HOLD_POWER = 0.07;
/**
* Time to extend or retract arm in auto
*/
static final double ARM_EXTEND_TIME_S = 2.0;
/**
* Time to throw game piece in auto
*/
static final double AUTO_THROW_TIME_S = 0.375;
/**
* Time to drive back in auto
*/
static final double AUTO_DRIVE_TIME = 6.0;
/**
* Speed to drive backwards in auto
*/
static final double AUTO_DRIVE_SPEED = -0.25;
/**
* This method is run once when the robot is first started up.
*/
@Override
public void robotInit() {
m_chooser.setDefaultOption("do nothing", kNothingAuto);
m_chooser.addOption("cone and mobility", kConeAuto);
m_chooser.addOption("cube and mobility", kCubeAuto);
SmartDashboard.putData("Auto choices", m_chooser);
leftController = new PIDController(driveKp, 0, 0);
rightController = new PIDController(driveKp, 0, 0);
/*
* You will need to change some of these from false to true.
*
* In the setDriveMotors method, comment out all but 1 of the 4 calls
* to the set() methods. Push the joystick forward. Reverse the motor
* if it is going the wrong way. Repeat for the other 3 motors.
*/
driveLeftLeader.setInverted(false);
driveLeftFollower.setInverted(false);
driveRightLeader.setInverted(false);
driveRightFollower.setInverted(false);
/*
* Set the arm and intake to brake mode to help hold position.
* If either one is reversed, change that here too. Arm out is defined
* as positive, arm in is negative.
*/
armLeader.setInverted(true);
armLeader.setIdleMode(IdleMode.kBrake);
armLeader.setSmartCurrentLimit(ARM_CURRENT_LIMIT_A);
armFollower.setIdleMode(IdleMode.kBrake);
armFollower.setSmartCurrentLimit(ARM_CURRENT_LIMIT_A);
armFollower.follow(armLeader,true);
intake.setInverted(false);
intake.setIdleMode(IdleMode.kBrake);
}
/**
* Calculate and set the power to apply to the left and right
* drive motors.
*
* @param forward Desired forward speed. Positive is forward.
* @param turn Desired turning speed. Positive is counter clockwise from
* above.
*/
public void setDriveMotors(double left, double right) {
SmartDashboard.putNumber("drive left power (%)", left);
SmartDashboard.putNumber("drive right power (%)", right);
// see note above in robotInit about commenting these out one by one to set
// directions.
driveLeftLeader.set(filter.calculate(left));
driveLeftFollower.set(filter.calculate(left));
driveRightLeader.set(filter.calculate(right));
driveRightFollower.set(filter.calculate(right));
}
/**
* Set the arm output power. Positive is out, negative is in.
*
* @param percent -1 to 1
*/
public void setArmMotor(double percent) {
armLeader.set(percent);
// armFollower.set(percent); // used to test a troubleshoot
System.out.println(percent);
SmartDashboard.putNumber("arm power (%)", percent*100);
SmartDashboard.putNumber("arm motor current (amps)", armLeader.getOutputCurrent());
SmartDashboard.putNumber("arm motor temperature (C)", armLeader.getMotorTemperature());
}
/**
* Set the arm output power.
*
* @param percent desired speed
* @param amps current limit
*/
public void setIntakeMotor(double percent, int amps) {
intake.set(percent);
intake.setSmartCurrentLimit(amps);
SmartDashboard.putNumber("intake power (%)", percent);
SmartDashboard.putNumber("intake motor current (amps)", intake.getOutputCurrent());
SmartDashboard.putNumber("intake motor temperature (C)", intake.getMotorTemperature());
}
/**
* This method is called every 20 ms, no matter the mode. It runs after
* the autonomous and teleop specific period methods.
*/
@Override
public void robotPeriodic() {
SmartDashboard.putNumber("Time (seconds)", Timer.getFPGATimestamp());
}
double autonomousStartTime;
double autonomousIntakePower;
@Override
public void autonomousInit() {
//might want to change kBrake to kCoast
// stem gals: changed from kCoast to kBrake
driveLeftLeader.setIdleMode(IdleMode.kBrake);
driveLeftFollower.setIdleMode(IdleMode.kBrake);
driveRightLeader.setIdleMode(IdleMode.kBrake);
driveRightFollower.setIdleMode(IdleMode.kBrake);
m_autoSelected = m_chooser.getSelected();
System.out.println("Auto selected: " + m_autoSelected);
if (m_autoSelected == kConeAuto) {
autonomousIntakePower = INTAKE_OUTPUT_POWER;
} else if (m_autoSelected == kCubeAuto) {
autonomousIntakePower = -INTAKE_OUTPUT_POWER;
}
autonomousStartTime = Timer.getFPGATimestamp();
}
@Override
public void autonomousPeriodic() {
if (m_autoSelected == kNothingAuto) {
setArmMotor(0.0);
setIntakeMotor(0.0, INTAKE_CURRENT_LIMIT_A);
//setDriveMotors(0.0, 0.0);
setDriveMotors(-0.8,0.8); // added during start of ntx day 2
return;
}
double timeElapsed = Timer.getFPGATimestamp() - autonomousStartTime;
if (timeElapsed < ARM_EXTEND_TIME_S) {
setArmMotor(ARM_OUTPUT_POWER);
setIntakeMotor(0.0, INTAKE_CURRENT_LIMIT_A);
setDriveMotors(0.0, 0.0);
} else if (timeElapsed < ARM_EXTEND_TIME_S + AUTO_THROW_TIME_S) {
setArmMotor(0.0);
setIntakeMotor(autonomousIntakePower, INTAKE_CURRENT_LIMIT_A);
setDriveMotors(0.0, 0.0);
} else if (timeElapsed < ARM_EXTEND_TIME_S + AUTO_THROW_TIME_S + ARM_EXTEND_TIME_S) {
setArmMotor(-ARM_OUTPUT_POWER);
setIntakeMotor(0.0, INTAKE_CURRENT_LIMIT_A);
setDriveMotors(0.0, 0.0);
} else if (timeElapsed < ARM_EXTEND_TIME_S + AUTO_THROW_TIME_S + ARM_EXTEND_TIME_S + AUTO_DRIVE_TIME) {
setArmMotor(0.0);
setIntakeMotor(0.0, INTAKE_CURRENT_LIMIT_A);
setDriveMotors(AUTO_DRIVE_SPEED, 0.0);
} else {
setArmMotor(0.0);
setIntakeMotor(0.0, INTAKE_CURRENT_LIMIT_A);
setDriveMotors(0.0, 0.0);
}
}
/**
* Used to remember the last game piece picked up to apply some holding power.
*/
static final int CONE = 1;
static final int CUBE = 2;
static final int NOTHING = 3;
int lastGamePiece;
@Override
public void teleopInit() { // changed from kCoast
driveLeftLeader.setIdleMode(IdleMode.kCoast);
driveLeftFollower.setIdleMode(IdleMode.kCoast);
driveRightLeader.setIdleMode(IdleMode.kCoast);
driveRightFollower.setIdleMode(IdleMode.kCoast);
lastGamePiece = NOTHING;
}
@Override
public void teleopPeriodic() {
//if (j.getRawButton(0) System.out.println(0);
double armPower;
if (j.getRawButton(5)) { // might be 6 or 7
// lower the arm
armPower = -ARM_OUTPUT_POWER;
System.out.println("Lowering the arm");
} else if (j.getRawButton(6)) { // might be 4 or 5
// raise the arm
armPower = ARM_OUTPUT_POWER;
System.out.println("Raising the arm");
} else {
// do nothing and let it sit where it is
armPower = 0.0;
}
setArmMotor(armPower);
double intakePower;
int intakeAmps;
if (j.getRawAxis(3) > 0.7) { // right trigger
// cube in or cone out
intakePower = INTAKE_OUTPUT_POWER;
intakeAmps = INTAKE_CURRENT_LIMIT_A;
lastGamePiece = CUBE;
} else if (j.getRawAxis(2) > 0.7) { // left trigger
// cone in or cube out
intakePower = -INTAKE_OUTPUT_POWER;
intakeAmps = INTAKE_CURRENT_LIMIT_A;
lastGamePiece = CONE;
} else if (lastGamePiece == CUBE) {
intakePower = INTAKE_HOLD_POWER;
intakeAmps = INTAKE_HOLD_CURRENT_LIMIT_A;
} else if (lastGamePiece == CONE) {
intakePower = -INTAKE_HOLD_POWER;
intakeAmps = INTAKE_HOLD_CURRENT_LIMIT_A;
} else {
intakePower = 0.0;
intakeAmps = 0;
}
setIntakeMotor(intakePower, intakeAmps);
/*
* Negative signs here because the values from the analog sticks are backwards
* from what we want. Forward returns a negative when we want it positive.
*/
// left stick up & down, right stick up and downs
//setDriveMotors(filter.calculate(j.getRawAxis(1)),filter.calculate(j.getRawAxis(5)));
//setDriveMotors(-j.getRawAxis(1),j.getRawAxis(5)); //original working
double leftDesiredSpeed = -j.getRawAxis(1) * kMaxSpeed;
double leftActualSpeed = driveLeftLeader.getEncoder().getVelocity();
double rightDesiredSpeed = j.getRawAxis(5) * kMaxSpeed;
double rightActualSpeed = driveRightLeader.getEncoder().getVelocity();
//nullPointerException on leftController/rightController
setDriveMotors(leftController.calculate(leftActualSpeed, leftDesiredSpeed) - (.2*j.getRawAxis(1)),
rightController.calculate(rightActualSpeed, rightDesiredSpeed) + (.2*j.getRawAxis(5))
);
SmartDashboard.putNumber("Left Desired", leftDesiredSpeed);
SmartDashboard.putNumber("Left Actual", leftActualSpeed);
SmartDashboard.putNumber("Left Output", driveLeftLeader.get());
SmartDashboard.putNumber("Right Desired", rightDesiredSpeed);
SmartDashboard.putNumber("Right Actual", rightActualSpeed);
SmartDashboard.putNumber("Right Output", driveRightLeader.get());
SmartDashboard.putNumber("Drive kP", driveKp);
driveKp = SmartDashboard.getNumber("Drive kP", driveKp);
}
}