-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot.java
More file actions
265 lines (225 loc) · 10.7 KB
/
Robot.java
File metadata and controls
265 lines (225 loc) · 10.7 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
package org.techvalleyhigh.frc5881.deepspace.robot;
import com.kauailabs.navx.frc.AHRS;
import edu.wpi.first.wpilibj.SPI;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.command.Scheduler;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import org.techvalleyhigh.frc5881.deepspace.robot.commands.elevator.LiftSave;
import org.techvalleyhigh.frc5881.deepspace.robot.commands.drive.ArcadeDrive;
import org.techvalleyhigh.frc5881.deepspace.robot.commands.drive.DriveSave;
import org.techvalleyhigh.frc5881.deepspace.robot.subsystem.*;
/**
* The VM is configured to automatically run this class, and to call the
* functions corresponding to each mode, as described in the TimedRobot
* documentation. If you change the name of this class or the package after
* creating this project, you must also update the build.gradle file in the
* project.
*/
public class Robot extends TimedRobot {
private static final String kDefaultAuto = "Default";
private static final String kCustomAuto = "My Auto";
private int ticks = 0;
private long lastTime = System.currentTimeMillis();
private String m_autoSelected;
private final SendableChooser<String> m_chooser = new SendableChooser<>();
// Define OI and subsystems
public static OI oi;
public static TestSubsystem testSubsystem;
public static Climber climber;
public static DriveControl driveControl;
public static Elevator elevator;
public static Intake intake;
public static UpsideDown upsideDown;
public static LED led;
public static AHRS navX;
// Commands
public static ArcadeDrive driveCommand;
/**
* This function is run when the robot is first started up and should be
* used for any initialization code.
*/
@Override
public void robotInit() {
// Init subsystems
testSubsystem = new TestSubsystem();
climber = new Climber();
driveControl = new DriveControl();
elevator = new Elevator();
intake = new Intake();
upsideDown = new UpsideDown();
/*
OI must be constructed after subsystems. If the OI creates Commands
(which it very likely will), subsystems are not guaranteed to be
constructed yet. Thus, their requires() statements may grab null
pointers. Bad news. Don't move it.
*/
oi = new OI();
driveCommand = new ArcadeDrive();
try {
SPI.Port port = SPI.Port.kMXP;
navX = new AHRS(port);
} catch (RuntimeException ex) {
System.err.println(ex);
}
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
m_chooser.addOption("My Auto", kCustomAuto);
SmartDashboard.putData("Auto choices", m_chooser);
}
/**
* This function is called every robot packet, no matter the mode. Use
* this for items like diagnostics that you want ran during disabled,
* autonomous, teleoperated and test.
*
* <p>This runs after the mode specific periodic functions, but before
* LiveWindow and SmartDashboard integrated updating.
*/
@Override
public void robotPeriodic() {
Scheduler.getInstance().run();
SmartDashboard.putNumber("X accel", navX.getRawAccelX());
SmartDashboard.putNumber("Y accel", navX.getRawAccelY());
SmartDashboard.putNumber("Z accel", navX.getRawAccelZ());
/* Display 6-axis Processed Angle Data */
SmartDashboard.putBoolean("IMU_Connected", navX.isConnected());
SmartDashboard.putBoolean("IMU_IsCalibrating", navX.isCalibrating());
SmartDashboard.putNumber("IMU_Yaw", navX.getYaw());
SmartDashboard.putNumber("IMU_Pitch", navX.getPitch());
SmartDashboard.putNumber("IMU_Roll", navX.getRoll());
/* Display tilt-corrected, Magnetometer-based heading (requires */
/* magnetometer calibration to be useful) */
SmartDashboard.putNumber("IMU_CompassHeading", navX.getCompassHeading());
/* Display 9-axis Heading (requires magnetometer calibration to be useful) */
SmartDashboard.putNumber("IMU_FusedHeading", navX.getFusedHeading());
/* These functions are compatible w/the WPI Gyro Class, providing a simple */
/* path for upgrading from the Kit-of-Parts gyro to the navx-MXP */
SmartDashboard.putNumber("IMU_TotalYaw", navX.getAngle());
SmartDashboard.putNumber("IMU_YawRateDPS", navX.getRate());
/* Display Processed Acceleration Data (Linear Acceleration, Motion Detect) */
SmartDashboard.putNumber("IMU_Accel_X", navX.getWorldLinearAccelX());
SmartDashboard.putNumber("IMU_Accel_Y", navX.getWorldLinearAccelY());
SmartDashboard.putBoolean("IMU_IsMoving", navX.isMoving());
SmartDashboard.putBoolean("IMU_IsRotating", navX.isRotating());
/* Display estimates of velocity/displacement. Note that these values are */
/* not expected to be accurate enough for estimating robot position on a */
/* FIRST FRC Robotics Field, due to accelerometer noise and the compounding */
/* of these errors due to single (velocity) integration and especially */
/* double (displacement) integration. */
SmartDashboard.putNumber("Velocity_X", navX.getVelocityX());
SmartDashboard.putNumber("Velocity_Y", navX.getVelocityY());
SmartDashboard.putNumber("Displacement_X", navX.getDisplacementX());
SmartDashboard.putNumber("Displacement_Y", navX.getDisplacementY());
/* Display Raw Gyro/Accelerometer/Magnetometer Values */
/* NOTE: These values are not normally necessary, but are made available */
/* for advanced users. Before using this data, please consider whether */
/* the processed data (see above) will suit your needs. */
SmartDashboard.putNumber("RawGyro_X", navX.getRawGyroX());
SmartDashboard.putNumber("RawGyro_Y", navX.getRawGyroY());
SmartDashboard.putNumber("RawGyro_Z", navX.getRawGyroZ());
SmartDashboard.putNumber("RawAccel_X", navX.getRawAccelX());
SmartDashboard.putNumber("RawAccel_Y", navX.getRawAccelY());
SmartDashboard.putNumber("RawAccel_Z", navX.getRawAccelZ());
SmartDashboard.putNumber("RawMag_X", navX.getRawMagX());
SmartDashboard.putNumber("RawMag_Y", navX.getRawMagY());
SmartDashboard.putNumber("RawMag_Z", navX.getRawMagZ());
SmartDashboard.putNumber("IMU_Temp_C", navX.getTempC());
/* Omnimount Yaw Axis Information */
/* For more info, see http://navx-mxp.kauailabs.com/installation/omnimount */
AHRS.BoardYawAxis yaw_axis = navX.getBoardYawAxis();
SmartDashboard.putString("YawAxisDirection", yaw_axis.up ? "Up" : "Down");
SmartDashboard.putNumber("YawAxis", yaw_axis.board_axis.getValue());
/* Sensor Board Information */
SmartDashboard.putString("FirmwareVersion", navX.getFirmwareVersion());
/* Quaternion Data */
/* Quaternions are fascinating, and are the most compact representation of */
/* orientation data. All of the Yaw, Pitch and Roll Values can be derived */
/* from the Quaternions. If interested in motion processing, knowledge of */
/* Quaternions is highly recommended. */
SmartDashboard.putNumber("QuaternionW", navX.getQuaternionW());
SmartDashboard.putNumber("QuaternionX", navX.getQuaternionX());
SmartDashboard.putNumber("QuaternionY", navX.getQuaternionY());
SmartDashboard.putNumber("QuaternionZ", navX.getQuaternionZ());
/* Connectivity Debugging Support */
SmartDashboard.putNumber("IMU_Byte_Count", navX.getByteCount());
SmartDashboard.putNumber("IMU_Update_Count", navX.getUpdateCount());
// Puts the Elevator encoder position into Smart Dashboard
SmartDashboard.putNumber("Elevator Encoder", elevator.getElevatorEncoderPosition());
// Puts the Elevator error value into the Smart Dashboard
SmartDashboard.putNumber("Elevator Error", elevator.getElevatorError());
// Puts the Elevator set point value into the Smart Dashboard
SmartDashboard.putNumber("Elevator Set Point", elevator.getElevatorSetpoint());
// Puts the Bar encoder position into Smart Dashboard
SmartDashboard.putNumber("Bar Encoder", elevator.getBarEncoderPosition());
// Puts the Bar error value into the Smart Dashboard
SmartDashboard.putNumber("Bar Error", elevator.getBarError());
// Puts the Bar set point value into the Smart Dashboard
SmartDashboard.putNumber("Bar Set Point", elevator.getBarSetpoint());
if (ticks % 100 == 0) {
SmartDashboard.putNumber("ultra Distance", driveControl.getUltrasonicRange());
SmartDashboard.putBoolean("is range valid", DriveControl.ultra.isRangeValid());
}
ticks++;
}
/**
* 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
*
* <p>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.
*/
@Override
public void autonomousInit() {
m_autoSelected = m_chooser.getSelected();
// m_autoSelected = SmartDashboard.getString("Auto Selector", kDefaultAuto);
System.out.println("Auto selected: " + m_autoSelected);
}
/**
* This function is called periodically during autonomous.
*/
@Override
public void autonomousPeriodic() {
switch (m_autoSelected) {
case kCustomAuto:
// Put custom auto code here
break;
case kDefaultAuto:
default:
// Put default auto code here
break;
}
}
/**
* Initialization code for teleop mode should go here.
*/
@Override
public void teleopInit() {
// Start the drive command
driveCommand.start();
}
/**
* This function is called periodically during operator control.
*/
@Override
public void teleopPeriodic() {
// If the bot is at an angle of greater than 30 degrees then run stop tipping
if (Math.abs(navX.getRawGyroY()) > 30) {
DriveSave driveSave = new DriveSave();
driveSave.start();
}
// If the bot is at an angle of greater than 30 degrees then do elevator save.
if (navX.getRawGyroY() > 30) {
LiftSave liftSave = new LiftSave();
liftSave.start();
}
}
/**
* This function is called periodically during test mode.
*/
@Override
public void testPeriodic() {
}
}