-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.java
More file actions
338 lines (293 loc) · 11.4 KB
/
Copy pathRobot.java
File metadata and controls
338 lines (293 loc) · 11.4 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
package frc.robot;
//hi
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.drive.MecanumDrive;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
import com.ctre.phoenix.motorcontrol.can.*;
import edu.wpi.first.wpilibj.*;
//import edu.wpi.first.wpilibj.SensorBase;
//import edu.wpi.first.wpilibj.PWM;
//import edu.wpi.first.wpilibj.SafePWM;
//import edu.wpi.first.wpilibj.PWMSpeedController;
//import edu.wpi.first.wpilibj.PWMTalonSRX;
//import edu.wpi.first.wpilibj.TalonSRX;
//import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.wpilibj.PIDController;
import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.I2C.Port;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj.SolenoidBase;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.Compressor;
// import com.ctre.phoenix.motorcontrol.FeedbackDevice;
// import com.ctre.phoenix.motorcontrol.StatusFrameEnhanced;
import edu.wpi.cscore.*;
import edu.wpi.cscore.CvSink;
import edu.wpi.cscore.CvSource;
import edu.wpi.cscore.UsbCamera;
import edu.wpi.first.wpilibj.CameraServer;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.cscore.VideoSource;
public class Robot extends IterativeRobot {
MecanumDrive robotDrive;
WPI_TalonSRX frontLeft = new WPI_TalonSRX(RobotMap.talonFrontLeftChannel);
WPI_TalonSRX rearLeft = new WPI_TalonSRX(RobotMap.talonRearLeftChannel);
WPI_TalonSRX frontRight = new WPI_TalonSRX(RobotMap.talonFrontRightChannel);
WPI_TalonSRX rearRight = new WPI_TalonSRX(RobotMap.talonRearRightChannel);
Encoder testCoder;
double motorSpeed = 0.7;
double deadzone = 0.15;
double joyY = 0;
double joyX = 0;
double joyZ = 0;
double rightTrigger = 0;
boolean buttonA;
boolean buttonB;
boolean buttonX;
boolean buttonY;
boolean buttonRight;
boolean buttonLeft;
double target = 0.5;
double correctSpeed = 0.2;
XboxController controller = new XboxController(RobotMap.xBoxControllerChannel);
int frames = 30;
double currentData;
public double preTime = 0.0;
int ledCode = 1;
// DoubleSolenoid doubleSolenoid = new DoubleSolenoid(RobotMap.doubleSolenoidForwardChannel, RobotMap.doubleSolenoidReverseChannel);
// Compressor compressor = new Compressor();
//instantiate output of PIDout
PIDout output = new PIDout(this);
PIDoutX strafeOutput = new PIDoutX(this);
PIDoutY forwardOutput = new PIDoutY(this);
EncoderPID encoderPID = new EncoderPID(this);
//Ints. Class
CameraSource limelight = new CameraSource(this);
CameraSourceX limelightX = new CameraSourceX(this);
CameraSourceY limelightY = new CameraSourceY(this);
EncoderSource encoder = new EncoderSource(this);
//Set PIDs
PIDController visionLoop = new PIDController(0.03, 0.0, 0.0, limelight, output);
PIDController strafeLoop = new PIDController(0.1, 0.0, 0.0, limelightX, strafeOutput);
PIDController forwardLoop = new PIDController(0.04, 0.0, 0.0, limelightY, forwardOutput);
PIDController encoderLoop = new PIDController(0.02, 0.0, 0.0, encoder, encoderPID);
//Global Varable for CameraValues
public double CameraValue = 0;
public double StrafeValue = 0;
public double ForwardValue = 0;
public double encoderValue = 0;
public double rotations = 0;
public double circumfrence = 0;
public double distance = 0;
double actualSkew;
LED Leds = new LED();
@Override
public void robotInit() {
//Setup Drive Train
frontLeft.setInverted(false);
rearLeft.setInverted(true);
frontRight.setInverted(false);
rearRight.setInverted(true);
robotDrive = new MecanumDrive(frontLeft, rearLeft, frontRight, rearRight);
//Start Compressor
//compressor.start();
//Setup Encoders
testCoder = new Encoder(RobotMap.encoderAChannel, RobotMap.encoderBChannel, false, Encoder.EncodingType.k4X);
testCoder.setDistancePerPulse((Math.PI * 8) / 360);
// rearRight.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, 0, 0);
//Seting Camera value Ranges and Setpoints
strafeLoop.setSetpoint(0.0);
strafeLoop.setOutputRange(-1,1);
strafeLoop.setInputRange(-25.0, 25.0);
visionLoop.setSetpoint(0.0);
visionLoop.setOutputRange(-1,1);
visionLoop.setInputRange(-25.0, 25.0);
forwardLoop.setSetpoint(6.0);
forwardLoop.setOutputRange(-1,1);
forwardLoop.setInputRange(-25.0, 25.0);
encoderLoop.setSetpoint(24.0); //distance
encoderLoop.setOutputRange(-0.5,0.5); //max speed
encoderLoop.setInputRange(-25.0, 25.0); //the minimum or maximum percentage to write to the output
/*new Thread(() -> {
UsbCamera camera = CameraServer.getInstance().startAutomaticCapture();
camera.setVideoMode(VideoMode.PixelFormat.kMJPEG, 360, 320, frames);
CvSink cvSink = CameraServer.getInstance().getVideo();
CvSource outputStream = CameraServer.getInstance().putVideo("Blur", 480, 320);
Mat source = new Mat();
Mat output = new Mat();
while(!Thread.interrupted()) {
cvSink.grabFrame(source);
currentData = camera.getActualDataRate();
if (currentData > 3.5 && frames > 8){
frames = frames - 2;
camera.setVideoMode(VideoMode.PixelFormat.kMJPEG, 360, 320, frames);
}else if (currentData < 1.5 && frames < 30){
frames = frames + 2;
camera.setVideoMode(VideoMode.PixelFormat.kMJPEG, 360, 320, frames);
}
Imgproc.cvtColor(source, output, Imgproc.COLOR_BGR2GRAY);
outputStream.putFrame(output);
}).start();*/
/*new Thread(() -> {
Timer frameTimer = new Timer();
frameTimer.start();
preTime = frameTimer.get();
UsbCamera camera = CameraServer.getInstance().startAutomaticCapture();
camera.setVideoMode(VideoMode.PixelFormat.kMJPEG , 320, 240, 30);
//camera.setResolution(320, 240);
//camera.setFPS(30);
CvSink cvSink = CameraServer.getInstance().getVideo();
CvSource outputStream = CameraServer.getInstance().putVideo("Blur", 200, 200);
Mat source = new Mat();
Mat output = new Mat();
while(!Thread.interrupted()) {
double currentTime = frameTimer.get();
double diffTime = currentTime - preTime;
SmartDashboard.putNumber("FPSTimer", (1/diffTime));
preTime = currentTime;
cvSink.grabFrame(source);
Imgproc.cvtColor(source, output, Imgproc.COLOR_BGR2GRAY);
outputStream.putFrame(output);
}
}).start();*/
}
@Override
public void teleopPeriodic() {
//Gather encoder position, post to smartDashboard. Chech to see if B is pressed to reset encoder.
encoderValue = -rearRight.getSelectedSensorPosition(0);
rotations = encoderValue/4000;
circumfrence = Math.PI*8;
distance = circumfrence * rotations;
SmartDashboard.putNumber("Rotations", rotations);
SmartDashboard.putNumber("Encoder Value", encoderValue);
SmartDashboard.putNumber("Distance", distance);
if (buttonX) {
rearRight.setSelectedSensorPosition(0, 0, 0);
}
SmartDashboard.putNumber("Encoder Distance:" , testCoder.getDistance());
SmartDashboard.putNumber("Encoder Value:" , testCoder.get());
// Use the joystick X axis for lateral movement, Y axis for forward
// movement, and Z axis for rotation.
NetworkTable table = NetworkTableInstance.getDefault().getTable("limelight");
NetworkTableEntry tx = table.getEntry("tx");
NetworkTableEntry ty = table.getEntry("ty");
NetworkTableEntry ta = table.getEntry("ta");
NetworkTableEntry ts = table.getEntry("ts");
//read values periodically
double x = tx.getDouble(0.0);
double y = ty.getDouble(0.0);
double area = ta.getDouble(0.0);
double skew = ts.getDouble(0.0);
CameraValue = x;
ForwardValue = area;
if (skew > -45){
actualSkew = Math.abs(skew);
} else {
actualSkew = Math.abs(skew) - 90;
}
StrafeValue = actualSkew;
//post to smart dashboard periodically
SmartDashboard.putNumber("LimelightX", x);
SmartDashboard.putNumber("LimelightY", y);
SmartDashboard.putNumber("LimelightArea", area);
SmartDashboard.putNumber("LimelightSkew", actualSkew);
rightTrigger = controller.getRawAxis(RobotMap.xBoxRightTriggerChannel);
buttonA = controller.getRawButton(RobotMap.xBoxButtonAChannel);
buttonB = controller.getRawButton(RobotMap.xBoxButtonBChannel);
buttonX = controller.getRawButton(RobotMap.xBoxButtonXChannel);
buttonRight = controller.getRawButton(RobotMap.xBoxButtonRightChannel);
joyY = controller.getRawAxis(RobotMap.xBoxLeftStickYChannel);
joyX = controller.getRawAxis(RobotMap.xBoxLeftStickXChannel);
joyZ = controller.getRawAxis(RobotMap.xBoxRightStickXChannel);
//Read Values on Smartdashboard
SmartDashboard.putNumber("JoyX", joyX);
SmartDashboard.putNumber("JoyY x number", joyY*motorSpeed);
SmartDashboard.putNumber("JoyY", joyY);
SmartDashboard.putNumber("JoyZ", joyZ);
SmartDashboard.putNumber("timer", ledCode);
//Deadzone
if (Math.abs(joyY) < (deadzone)) {
joyY = 0;
}
if (Math.abs(joyX) < (deadzone)) {
joyX = 0;
}
if (Math.abs(joyZ) < (deadzone)) {
joyZ = 0;
}
//Controlling PID Loops
if (buttonA){
visionLoop.enable();
strafeLoop.enable();
forwardLoop.enable();
robotDrive.driveCartesian(-strafeOutput.outputX, forwardOutput.outputY, output.outputSkew, 0.0);
} else if (rightTrigger > 0.6){
encoderLoop.enable();
robotDrive.driveCartesian(-0.0, encoderPID.outputEncoder, 0.0, 0.0);
}else{
visionLoop.disable();
strafeLoop.disable();
forwardLoop.disable();
encoderLoop.disable();
robotDrive.driveCartesian(-joyX * motorSpeed, joyY * motorSpeed, -joyZ * motorSpeed, 0.0);
}
//Checking if reflective tape area is less and change LED lights
if(y < 4)
{
//Lower Hatches
if(area >= 4.0 && x > -4 && x < 4)
{
Leds.sendCode(1);
}
else
{
Leds.sendCode(2);
}
}
else if(y > 4)
{
//Ball Hatches
if(area >= 2.5 && x > -3.5 && x < 3.5)
{
Leds.sendCode(1);
}
else
{
Leds.sendCode(2);
}
}
else
{
Leds.sendCode(2);
}
if (buttonB){
Leds.sendCode(9);
}
if (buttonRight){
// doubleSolenoid.set(Value.kForward);
} else{
// doubleSolenoid.set(Value.kReverse);
}
}
public void testPeriodic(){
/*Proper Code On Robot
Ping Talons(Drive Base, Arms)
Limelight Sees Vision Targets
Vision Camera Sends Feed
LEDs Work
Compressor Runs
Indicator Light Works
Encoders Returning Values
Motors Run
Joysticks Return Value*/
}
}