-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotContainer.java
More file actions
178 lines (140 loc) · 7.15 KB
/
RobotContainer.java
File metadata and controls
178 lines (140 loc) · 7.15 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
// 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 java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.button.JoystickButton;
import frc.robot.Constants.ControllerConstants;
import frc.robot.commands.BalanceCommand;
import frc.robot.commands.DriveCommand;
import frc.robot.commands.DriveStraightCommand;
import frc.robot.commands.ExampleCommand;
import frc.robot.subsystems.BlinkinSubsystem;
import frc.robot.subsystems.DrivetrainSubsystem;
import frc.robot.subsystems.ExampleSubsystem;
import frc.robot.subsystems.NavXGyroSubsystem;
import frc.robot.util.Logger;
/**
* This class is where the bulk of the robot should be declared. Since Command-based is a
* "declarative" paradigm, very little robot logic should actually be handled in the {@link Robot}
* periodic methods (other than the scheduler calls). Instead, the structure of the robot (including
* subsystems, commands, and button mappings) should be declared here.
*/
public class RobotContainer {
// The robot's subsystems and commands are defined here...
private final BlinkinSubsystem m_blinkinSubsystem = new BlinkinSubsystem();
private final ExampleSubsystem m_exampleSubsystem = new ExampleSubsystem();
private final ExampleCommand m_autoCommand = new ExampleCommand(m_exampleSubsystem);
private final BalanceCommand m_balancecommand;
private final DriveStraightCommand m_drivestraightcommand;
private DriveCommand teleopDriveCmd;
private DrivetrainSubsystem drivetrainSubsystem;
private Joystick rightJoystick;
private Joystick leftJoystick;
private final NavXGyroSubsystem navxGyroSubsystem = new NavXGyroSubsystem();
private XboxController xboxController;
private Logger m_logger;
public double getRightY() {
if (!DriverStation.isJoystickConnected(ControllerConstants.kXboxControllerPort)) {
return Math.abs(rightJoystick.getY()) > ControllerConstants.kDeadZoneRadius ? -rightJoystick.getY() : 0;
}
else {
return Math.abs(xboxController.getRightY()) > ControllerConstants.kXboxDeadZoneRadius ? -xboxController.getRightY() : 0;
}
}
public double getLeftY() {
if (!DriverStation.isJoystickConnected(ControllerConstants.kXboxControllerPort)) {
return Math.abs(leftJoystick.getY()) > ControllerConstants.kDeadZoneRadius ? -leftJoystick.getY() : 0;
}
else {
return Math.abs(xboxController.getLeftY()) > ControllerConstants.kDeadZoneRadius ? -xboxController.getLeftY() : 0;
}
}
private double getThrottle() {
if (!DriverStation.isJoystickConnected(ControllerConstants.kXboxControllerPort)) {
return this.rightJoystick.getRawButton(ControllerConstants.kThrottleButton) ? ControllerConstants.kSlowThrottle : ControllerConstants.kMaxThrottle;
}
else {
return this.xboxController.getBButton() ? ControllerConstants.kSlowThrottle : ControllerConstants.kMaxThrottle;
}
}
/** The container for the robot. Contains subsystems, OI devices, and commands. */
public RobotContainer() {
this.drivetrainSubsystem = new DrivetrainSubsystem();
this.rightJoystick = new Joystick(ControllerConstants.kRightJoystickPort);
this.leftJoystick = new Joystick(ControllerConstants.kLeftJoystickPort);
this.xboxController = new XboxController(ControllerConstants.kXboxControllerPort);
this.teleopDriveCmd = new DriveCommand(this.drivetrainSubsystem, this::getRightY, this::getLeftY, this::getThrottle, this.m_blinkinSubsystem);
this.drivetrainSubsystem.setDefaultCommand(this.teleopDriveCmd);
this.m_balancecommand = new BalanceCommand(navxGyroSubsystem, drivetrainSubsystem);
this.m_drivestraightcommand = new DriveStraightCommand(navxGyroSubsystem, drivetrainSubsystem, m_blinkinSubsystem, this::getLeftY,this::getRightY, this::getThrottle);
// this.colorSensorSubsystem.setDefaultCommand(colorSensorCommand); <--- Causes an error right now
// Creates the logger
m_logger = new Logger("/home/lvuser/logs/", "ezlog");
// Sets the data to
{
//DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//LocalDateTime now = LocalDateTime.now();
//m_logger.createStaticField("Time", dtf.format(now)); //DOESN'T WORK
m_logger.createStaticField("Event", "CORI Preliminaries 1");
m_logger.recordFrequency(.250);
/*
* m_leftB(0) m_rightB(1)
*
* m_leftA(2) m_rightA(3)
*/
m_logger.createDynamicFieldDouble("Left B Motor Speed", drivetrainSubsystem.getMotorSpeed(0), () -> drivetrainSubsystem.getMotorSpeed(0));
m_logger.createDynamicFieldDouble("Right B Motor Speed", drivetrainSubsystem.getMotorSpeed(1), () -> drivetrainSubsystem.getMotorSpeed(1));
m_logger.createDynamicFieldDouble("Left A Motor Speed", drivetrainSubsystem.getMotorSpeed(2), () -> drivetrainSubsystem.getMotorSpeed(2));
m_logger.createDynamicFieldDouble("Right A Motor Speed", drivetrainSubsystem.getMotorSpeed(3), () -> drivetrainSubsystem.getMotorSpeed(3));
m_logger.setup();
}
// NOTE: The logs can be accessed through File Explorer by typing into the bar:
// ftp://roborio-1014-frc.local/home/lvuser/logs/
// Configure the button bindings
configureButtonBindings();
}
/**
* Use this method to define your button->command mappings. Buttons can be created by
* instantiating a {@link GenericHID} or one of its subclasses ({@link
* edu.wpi.first.wpilibj.Joystick} or {@link XboxController}), and then passing it to a {@link
* edu.wpi.first.wpilibj2.command.button.JoystickButton}.
*/
private void configureButtonBindings() {
if (!DriverStation.isJoystickConnected(ControllerConstants.kXboxControllerPort)) {
JoystickButton balanceButton = new JoystickButton(this.rightJoystick, ControllerConstants.kBalanceButton);
balanceButton.whileTrue(this.m_balancecommand);
JoystickButton driveStraightButton = new JoystickButton(this.leftJoystick, ControllerConstants.kDriveStraightButton);
driveStraightButton.whileTrue(this.m_drivestraightcommand);//drivestraight button
driveStraightButton.whileFalse(this.teleopDriveCmd);
balanceButton.whileTrue(this.m_balancecommand);
}
else {
JoystickButton balanceButton = new JoystickButton(this.xboxController, XboxController.Button.kLeftBumper.value);
balanceButton.whileTrue(this.m_balancecommand);
}
}
/**
* Use this to pass the autonomous command to the main {@link Robot} class.
*
* @return the command to run in autonomous
*/
public Command getAutonomousCommand() {
// An ExampleCommand will run in autonomous
return m_autoCommand;
}
/**
* Use this to pass the logger to the main {@link Robot} class to schedule updates.
*
* @return the logger
*/
public Logger getLogger() {
return m_logger;
}
}