-
Notifications
You must be signed in to change notification settings - Fork 12
/
JavaExample.java
199 lines (153 loc) · 5.41 KB
/
JavaExample.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
package frc.robot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.GenericHID.Hand;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
/**
* 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 TimedRobot {
/*** Initializes all classes ***/
private boolean driverVision, tapeVision, cargoVision, cargoSeen, tapeSeen;
private NetworkTableEntry tapeDetected, cargoDetected, tapeYaw, cargoYaw,
videoTimestamp;
private XboxController driverJoy;
private double targetAngle;
NetworkTableInstance instance;
NetworkTable chickenVision;
/*** EDIT TO CURRENT CONFIGURATION ***/
Spark leftMotor, rightMotor;
DifferentialDrive drive;
Hand leftSide, rightSide;
public Robot() {
super();
}
/**
* This function is run when the robot is first started up and should be used
* for any initialization code.
*/
@Override
public void robotInit() {
instance = NetworkTableInstance.getDefault();
chickenVision = instance.getTable("ChickenVision");
tapeDetected = chickenVision.getEntry("tapeDetected");
cargoDetected = chickenVision.getEntry("cargoDetected");
tapeYaw = chickenVision.getEntry("tapeYaw");
cargoYaw = chickenVision.getEntry("cargoYaw");
driveWanted = chickenVision.getEntry("Driver");
tapeWanted = chickenVision.getEntry("Tape");
cargoWanted = chickenVision.getEntry("Cargo");
videoTimestamp = chickenVision.getEntry("VideoTimestamp");
tapeVision = cargoVision = false;
driverVision = true;
driverJoy = new XboxController(0);
/*** EDIT TO CURRENT CONFIGURATION ***/
leftMotor = new Spark(0);
rightMotor = new Spark(1);
drive = new DifferentialDrive(leftMotor, rightMotor);
leftSide = Hand.kLeft;
rightSide = Hand.kRight;
targetAngle = 0;
/*** Update Dashboard ***/
}
/**
* This function is run once each time the robot enters autonomous mode
*/
@Override
public void autonomousInit() {
}
/**
* This function is called periodically during autonomous, but is mainly used
* for logging
*/
@Override
public void autonomousPeriodic() {
}
/**
* This function is called once each time the robot enters tele-operated mode
*/
@Override
public void teleopInit() {
}
/**
* This function is called periodically during operator control Arcade drive
* Left Y controls forward and back Right X controls rotation
*
* If want to auto align with cargo, you hold down left bumper
*
* If want to auto align with vision tapes, you hold down right bumper
*
* You still have forward and backwards control no matter what button you press
*/
@Override
public void teleopPeriodic() {
// Change this to alter how quick or slow the feedback loop is
double kP = 1.2;
double forward = driverJoy.getY(leftSide);
double turn = driverJoy.getX(rightSide);
boolean cargoDesired = driverJoy.getBumper(leftSide);
boolean tapeDesired = driverJoy.getBumper(rightSide);
// If button 1 is pressed, then it will track cargo
if (cargoDesired) {
driveWanted.setBoolean(false);
tapeWanted.setBoolean(false);
cargoWanted.setBoolean(true);
cargoSeen = cargoDetected.getBoolean(false);
if (cargoSeen)
targetAngle = cargoYaw.getDouble(0);
else
targetAngle = 0;
} else if (tapeDesired) {
driveWanted.setBoolean(false);
tapeWanted.setBoolean(true);
cargoWanted.setBoolean(false);
// Checks if vision sees cargo or vision targets. This may not get called unless
// cargo vision detected
tapeSeen = tapeDetected.getBoolean(false);
if (tapeSeen)
targetAngle = tapeYaw.getDouble(0);
else
targetAngle = 0;
} else {
driveWanted.setBoolean(true);
tapeWanted.setBoolean(false);
cargoWanted.setBoolean(false);
targetAngle = 0;
}
// Limit output to 0.3
double output = limitOutput(-kP * targetAngle, 0.3);
if (cargoDesired || tapeDesired)
drive.arcadeDrive(forward, output);
else
drive.arcadeDrive(forward, turn);
}
/**
* This function is called periodically during test mode
*/
@Override
public void testPeriodic() {
}
public void disabledInit() {
}
public void disabledPeriodic() {
}
public double limitOutput(double number, double maxOutput) {
if (number > 1.0) {
number = 1.0;
}
if (number < -1.0) {
number = -1.0;
}
if (number > maxOutput) {
return maxOutput;
}
if (number < -maxOutput) {
return -maxOutput;
}
return number;
}
}