-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.java
229 lines (198 loc) · 7.16 KB
/
Robot.java
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
package org.usfirst.frc.team6013.robot;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import org.usfirst.frc.team6013.robot.commands.DriveForward;
import org.usfirst.frc.team6013.robot.commands.Turn;
import org.usfirst.frc.team6013.robot.interfaces.ICommandGroup;
import edu.wpi.cscore.CvSink;
import edu.wpi.cscore.CvSource;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.first.wpilibj.ADXRS450_Gyro;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.CounterBase.EncodingType;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.Victor;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the IterativeRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the manifest file in the resource
* directory.
*/
public class Robot extends IterativeRobot {
final String defaultAuto = "No Move";
final String leftAuto = "Left";
final String centerAuto = "Center";
final String rightAuto = "Right";
final String testAuto = "Test";
String autoSelected;
double autoStartTime;
SendableChooser<String> autoChooser = new SendableChooser<>();
Victor leftDrive;
Victor rightDrive;
Spark climbMotor;
Joystick driverController;
public static RobotDrive driveTrain;
public static Encoder leftEncoder;
public static Encoder rightEncoder;
public static ADXRS450_Gyro gyro;
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
@Override
public void robotInit() {
//zero equals joystick port 1 on the driver station
driverController = new Joystick(0);
//left and right drive channels, maps to PWM 0 and 1 on the robot
leftDrive = new Victor(0);
rightDrive = new Victor(1);
climbMotor = new Spark(7);
//drivetrain object to run tank/arcade drive for us
driveTrain = new RobotDrive(leftDrive, rightDrive);
//24" = 1790 * 4
leftEncoder = new Encoder(0, 1, true, EncodingType.k4X);
rightEncoder = new Encoder(2, 3, false, EncodingType.k4X);
leftEncoder.setDistancePerPulse(0.053631);
rightEncoder.setDistancePerPulse(0.053631);
gyro = new ADXRS450_Gyro();
autoChooser.addObject("No Move", defaultAuto);
autoChooser.addObject("Left Side", leftAuto);
autoChooser.addDefault("Center", centerAuto);
autoChooser.addObject("Right Side", rightAuto);
autoChooser.addObject("Test Mode", testAuto);
//camera code to display on the dashboard, runs on an independent thread as the robot code (dual core micro)
new Thread( () -> {
try {
UsbCamera cam = CameraServer.getInstance().startAutomaticCapture();
cam.setResolution(640, 480);
CvSink cvSink = CameraServer.getInstance().getVideo();
CvSource output = CameraServer.getInstance().putVideo("Camera", 640, 480);
Mat source = new Mat();
Mat outputFrame = new Mat();
//main loop of camera code
while(true) {
//read frame
cvSink.grabFrame(source);
//process image? (part of the default code, maybe gives some time so this loop doesn't go crazy
Imgproc.cvtColor(source, outputFrame, Imgproc.COLOR_BGR2GRAY);
//send to output buffer
output.putFrame(outputFrame);
}
} catch (Exception e) {
System.out.println("Camera exception");
}
}).start();
}
@Override
public void disabledInit() {
}
@Override
public void disabledPeriodic() {
}
/**
* This autonomous (along with the chooser code above) shows how to select
* between different autonomous modes using the dashboard. The sendable
* chooser code works with the Java SmartDashboard. If you prefer the
* LabVIEW Dashboard, remove all of the chooser code and uncomment the
* getString line to get the auto name from the text box below the Gyro
*
* You can add additional auto modes by adding additional comparisons to the
* switch structure below with additional strings. If using the
* SendableChooser make sure to add them to the chooser code above as well.
*/
ICommandGroup autoGroup;
@Override
public void autonomousInit() {
//read what the selector says at the beginning of auto mode
autoSelected = autoChooser.getSelected();
System.out.println("Auto selected: " + autoSelected);
autoGroup = new ICommandGroup();
switch (autoSelected) {
case leftAuto:
autoGroup.addSequential(new DriveForward(100));
autoGroup.addSequential(new Turn(43));
autoGroup.addSequential(new DriveForward(24));
break;
case centerAuto:
autoGroup.addSequential(new DriveForward(80));
break;
case rightAuto:
autoGroup.addSequential(new DriveForward(96));
autoGroup.addSequential(new Turn(-53));
autoGroup.addSequential(new DriveForward(41));
break;
case defaultAuto:
default:
driveTrain.arcadeDrive(0, 0);
break;
}
}
/**
* This function is called periodically during autonomous
*/
@Override
public void autonomousPeriodic() {
autoGroup.execute();
}
final double SPEED_RATIO = 0.8;
/**
* This function is called periodically during operator control
*/
@Override
public void teleopPeriodic() {
//axis 1 is left stick forward/backwards, axis 4 is right stick left/right
//constants added to slow down the robot to make it more controllable, can be tuned for drivers
double speed = deadband(driverController.getRawAxis(1)) * SPEED_RATIO;
double turn = -deadband(driverController.getRawAxis(4)) * 0.7;
//boost goes from 0-0.3 (trigger is 0-1, no negative)
double boost = driverController.getRawAxis(3);
speed = speed * (SPEED_RATIO + ((1-SPEED_RATIO) * boost));
//command the motors to drive, drivetrain object handles all the turning logic for us
driveTrain.arcadeDrive(speed, turn);
//climb code
double climbSpeed = driverController.getRawAxis(2);
if (driverController.getRawButton(5) == true) {
//drive climber in reverse
climbMotor.set(-0.6);
} else {
climbMotor.set(climbSpeed);
}
}
/**
* This function is called periodically during test mode
*/
@Override
public void testPeriodic() {
//test mode is basically the same as teleop, only used for practice
teleopPeriodic();
}
/**
* This function is called periodically during operator control
*/
public void robotPeriodic() {
SmartDashboard.putNumber("LeftEncoder", leftEncoder.getDistance());
SmartDashboard.putNumber("RightEncoder", rightEncoder.getDistance());
SmartDashboard.putNumber("Gyro Angle", Robot.gyro.getAngle());
SmartDashboard.putData("Auto choices", autoChooser);
String autoMode = autoChooser.getSelected();
if(autoMode == null) {
autoMode = "(null)";
}
SmartDashboard.putString("Selected Auto Mode", autoMode);
}
final double DEADBAND = 0.2;
private double deadband(double input) {
if((-DEADBAND < input) && (input < DEADBAND)) {
return 0;
} else {
return input;
}
}
}