-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotContainer.java
More file actions
85 lines (73 loc) · 3.84 KB
/
RobotContainer.java
File metadata and controls
85 lines (73 loc) · 3.84 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
package frc.robot;
import java.util.List;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.controller.PIDController;
import edu.wpi.first.wpilibj.controller.ProfiledPIDController;
import edu.wpi.first.wpilibj.geometry.Pose2d;
import edu.wpi.first.wpilibj.geometry.Rotation2d;
import edu.wpi.first.wpilibj.geometry.Translation2d;
import edu.wpi.first.wpilibj.trajectory.Trajectory;
import edu.wpi.first.wpilibj.trajectory.TrajectoryConfig;
import edu.wpi.first.wpilibj.trajectory.TrajectoryGenerator;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import edu.wpi.first.wpilibj2.command.SwerveControllerCommand;
import edu.wpi.first.wpilibj2.command.button.JoystickButton;
import frc.robot.Constants.AutoConstants;
import frc.robot.Constants.DriveConstants;
import frc.robot.Constants.OIConstants;
import frc.robot.commands.SwerveJoystickCmd;
import frc.robot.subsystems.SwerveSubsystem;
public class RobotContainer {
private final SwerveSubsystem swerveSubsystem = new SwerveSubsystem();
private final Joystick driverJoytick = new Joystick(OIConstants.kDriverControllerPort);
public RobotContainer() {
swerveSubsystem.setDefaultCommand(new SwerveJoystickCmd(
swerveSubsystem,
() -> -driverJoytick.getRawAxis(OIConstants.kDriverYAxis),
() -> driverJoytick.getRawAxis(OIConstants.kDriverXAxis),
() -> driverJoytick.getRawAxis(OIConstants.kDriverRotAxis),
() -> !driverJoytick.getRawButton(OIConstants.kDriverFieldOrientedButtonIdx)));
configureButtonBindings();
}
private void configureButtonBindings() {
new JoystickButton(driverJoytick, 2).whenPressed(() -> swerveSubsystem.zeroHeading());
}
public Command getAutonomousCommand() {
// 1. Create trajectory settings
TrajectoryConfig trajectoryConfig = new TrajectoryConfig(
AutoConstants.kMaxSpeedMetersPerSecond,
AutoConstants.kMaxAccelerationMetersPerSecondSquared)
.setKinematics(DriveConstants.kDriveKinematics);
// 2. Generate trajectory
Trajectory trajectory = TrajectoryGenerator.generateTrajectory(
new Pose2d(0, 0, new Rotation2d(0)),
List.of(
new Translation2d(1, 0),
new Translation2d(1, -1)),
new Pose2d(2, -1, Rotation2d.fromDegrees(180)),
trajectoryConfig);
// 3. Define PID controllers for tracking trajectory
PIDController xController = new PIDController(AutoConstants.kPXController, 0, 0);
PIDController yController = new PIDController(AutoConstants.kPYController, 0, 0);
ProfiledPIDController thetaController = new ProfiledPIDController(
AutoConstants.kPThetaController, 0, 0, AutoConstants.kThetaControllerConstraints);
thetaController.enableContinuousInput(-Math.PI, Math.PI);
// 4. Construct command to follow trajectory
SwerveControllerCommand swerveControllerCommand = new SwerveControllerCommand(
trajectory,
swerveSubsystem::getPose,
DriveConstants.kDriveKinematics,
xController,
yController,
thetaController,
swerveSubsystem::setModuleStates,
swerveSubsystem);
// 5. Add some init and wrap-up, and return everything
return new SequentialCommandGroup(
new InstantCommand(() -> swerveSubsystem.resetOdometry(trajectory.getInitialPose())),
swerveControllerCommand,
new InstantCommand(() -> swerveSubsystem.stopModules()));
}
}