Files
2023WayOfTheRobot/src/main/java/frc4388/robot/RobotContainer.java
T

193 lines
8.9 KiB
Java
Raw Normal View History

2023-01-07 13:36:56 -07:00
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package frc4388.robot;
2023-02-07 23:32:19 -07:00
import java.util.ArrayList;
2023-02-07 18:50:33 -07:00
import java.util.List;
2023-02-09 18:48:43 -07:00
import org.opencv.objdetect.HOGDescriptor;
2023-02-09 18:25:57 -07:00
import edu.wpi.first.math.controller.HolonomicDriveController;
2023-02-07 18:50:33 -07:00
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.controller.ProfiledPIDController;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.trajectory.Trajectory;
2023-02-06 19:49:08 -07:00
import edu.wpi.first.math.trajectory.TrajectoryConfig;
2023-02-07 18:50:33 -07:00
import edu.wpi.first.math.trajectory.TrajectoryGenerator;
2023-02-07 23:32:19 -07:00
import edu.wpi.first.math.trajectory.Trajectory.State;
2023-01-07 13:36:56 -07:00
import edu.wpi.first.wpilibj.Joystick;
2023-02-09 18:48:43 -07:00
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
2023-01-07 13:36:56 -07:00
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
2023-02-04 15:06:42 -07:00
import edu.wpi.first.wpilibj2.command.RunCommand;
2023-02-07 18:50:33 -07:00
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import edu.wpi.first.wpilibj2.command.SwerveControllerCommand;
2023-01-07 13:36:56 -07:00
import edu.wpi.first.wpilibj2.command.button.JoystickButton;
import frc4388.robot.Constants.*;
2023-02-07 18:50:33 -07:00
import frc4388.robot.Constants.SwerveDriveConstants.AutoConstants;
import frc4388.robot.Constants.SwerveDriveConstants.PIDConstants;
2023-02-02 19:11:10 -07:00
import frc4388.robot.commands.AutoBalance;
2023-02-10 20:53:33 -07:00
import frc4388.robot.commands.JoystickPlayback;
import frc4388.robot.commands.JoystickRecorder;
import frc4388.robot.subsystems.SwerveDrive;
2023-02-04 15:06:42 -07:00
import frc4388.utility.controller.DeadbandedXboxController;
2023-01-07 13:36:56 -07:00
import frc4388.utility.controller.XboxController;
/**
* 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 {
/* RobotMap */
2023-02-02 19:43:47 -07:00
public final RobotMap m_robotMap = new RobotMap();
2023-01-07 13:36:56 -07:00
/* Subsystems */
2023-02-14 19:00:09 -07:00
public final SwerveDrive m_robotSwerveDrive = new SwerveDrive(m_robotMap.leftFront,
m_robotMap.rightFront,
m_robotMap.leftBack,
m_robotMap.rightBack,
m_robotMap.gyro);
2023-01-07 13:36:56 -07:00
/* Controllers */
2023-02-04 15:06:42 -07:00
private final DeadbandedXboxController m_driverXbox = new DeadbandedXboxController(OIConstants.XBOX_DRIVER_ID);
private final DeadbandedXboxController m_operatorXbox = new DeadbandedXboxController(OIConstants.XBOX_OPERATOR_ID);
2023-01-07 13:36:56 -07:00
/**
* The container for the robot. Contains subsystems, OI devices, and commands.
*/
public RobotContainer() {
configureButtonBindings();
2023-02-14 19:00:09 -07:00
// * Default Commands
m_robotSwerveDrive.setDefaultCommand(new RunCommand(() -> {
m_robotSwerveDrive.driveWithInput(getDeadbandedDriverController().getLeft(),
getDeadbandedDriverController().getRight(),
true);
}, m_robotSwerveDrive)
.withName("SwerveDrive DefaultCommand"));
2023-01-07 13:36:56 -07:00
}
2023-01-24 17:57:48 -07:00
2023-01-07 13:36:56 -07:00
/**
* 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() {
2023-02-14 19:00:09 -07:00
// * Driver Buttons
2023-02-04 15:06:42 -07:00
new JoystickButton(getDeadbandedDriverController(), XboxController.A_BUTTON)
.onTrue(new InstantCommand(() -> m_robotSwerveDrive.resetGyro(), m_robotSwerveDrive));
2023-02-10 17:12:53 -07:00
// new JoystickButton(getDeadbandedDriverController(), XboxController.X_BUTTON)
// .onTrue(new InstantCommand(() -> m_robotSwerveDrive.resetOdometry(), m_robotSwerveDrive));
2023-02-04 15:06:42 -07:00
// // .onFalse()
2023-01-07 13:36:56 -07:00
2023-02-04 15:06:42 -07:00
new JoystickButton(getDeadbandedDriverController(), XboxController.Y_BUTTON)
2023-02-02 19:11:10 -07:00
.onTrue(new AutoBalance(m_robotMap.gyro, m_robotSwerveDrive));
2023-02-10 20:53:33 -07:00
new JoystickButton(getDeadbandedDriverController(), XboxController.RIGHT_BUMPER_BUTTON)
.whileTrue(new JoystickRecorder(m_robotSwerveDrive,
() -> getDeadbandedDriverController().getLeftX(),
() -> getDeadbandedDriverController().getLeftY(),
() -> getDeadbandedDriverController().getRightX(),
2023-02-14 19:00:09 -07:00
() -> getDeadbandedDriverController().getRightY()))
2023-02-10 20:53:33 -07:00
.onFalse(new InstantCommand());
new JoystickButton(getDeadbandedDriverController(), XboxController.LEFT_BUMPER_BUTTON)
.onTrue(new JoystickPlayback(m_robotSwerveDrive));
2023-02-14 19:00:09 -07:00
// * Operator Buttons
2023-01-07 13:36:56 -07:00
}
/**
* Use this to pass the autonomous command to the main {@link Robot} class.
*
* @return the command to run in autonomous
*/
public Command getAutonomousCommand() {
2023-02-07 23:32:19 -07:00
2023-02-07 18:50:33 -07:00
//Create Trajectory Settings
TrajectoryConfig trajectoryConfig = new TrajectoryConfig(SwerveDriveConstants.AutoConstants.PATH_MAX_VEL,
SwerveDriveConstants.AutoConstants.PATH_MAX_ACC).setKinematics(m_robotSwerveDrive.getKinematics());
2023-02-07 23:32:19 -07:00
// generate trajectory
2023-02-09 19:34:59 -07:00
Trajectory trajectory = TrajectoryGenerator.generateTrajectory(
2023-02-07 23:32:19 -07:00
2023-02-09 19:34:59 -07:00
new Pose2d(0, 0, new Rotation2d(0)),
List.of(
new Translation2d(0, 1)),
new Pose2d(0, 2, Rotation2d.fromDegrees(0)),
2023-02-07 23:32:19 -07:00
2023-02-09 19:34:59 -07:00
trajectoryConfig);
2023-02-07 18:50:33 -07:00
2023-02-09 18:25:57 -07:00
ArrayList<Pose2d> simplePath = new ArrayList<Pose2d>();
simplePath.add(new Pose2d(0, 0, new Rotation2d(0)));
2023-02-09 18:48:43 -07:00
simplePath.add(new Pose2d(0, -1, new Rotation2d(0)));
2023-02-07 23:32:19 -07:00
2023-02-09 19:34:59 -07:00
// Trajectory trajectory = TrajectoryGenerator.generateTrajectory(simplePath, trajectoryConfig);
// Trajectory trajectory = TrajectoryGenerator.generateTrajectory(
// new Pose2d(0, 0, new Rotation2d(0),
// List.of(new Translation2d(0, 1)),
// new Pose2d(0, 2, new Rotation2d(0)),
// trajectoryConfig
// );
// ArrayList<double[]> states = new ArrayList<double[]>();
2023-02-07 23:32:19 -07:00
2023-02-09 18:48:43 -07:00
2023-02-07 18:50:33 -07:00
//Defining PID Controller for tracking trajectory
2023-02-07 23:32:19 -07:00
PIDController xController = new PIDController(SwerveDriveConstants.AutoConstants.X_CONTROLLER.kP, 0, 0);
PIDController yController = new PIDController(SwerveDriveConstants.AutoConstants.Y_CONTROLLER.kP, 0, 0);
2023-02-07 18:50:33 -07:00
ProfiledPIDController thetaController = new ProfiledPIDController(AutoConstants.THETA_CONTROLLER.kP,
2023-02-07 23:32:19 -07:00
AutoConstants.THETA_CONTROLLER.kI, AutoConstants.THETA_CONTROLLER.kD, AutoConstants.THETA_CONSTRAINTS);
2023-02-07 18:50:33 -07:00
thetaController.enableContinuousInput(-Math.PI, Math.PI);
2023-02-09 18:48:43 -07:00
HolonomicDriveController holoController = new HolonomicDriveController(xController, yController, thetaController);
2023-02-07 18:50:33 -07:00
//Command to follow trajectory
2023-02-10 17:12:53 -07:00
// SwerveControllerCommand swerveControllerCommand = new SwerveControllerCommand(
// trajectory,
// m_robotSwerveDrive::getOdometry,
// m_robotSwerveDrive.getKinematics(),
// holoController,
// m_robotSwerveDrive::setModuleStates,
// m_robotSwerveDrive);
2023-02-07 18:50:33 -07:00
SwerveControllerCommand swerveControllerCommand = new SwerveControllerCommand(
trajectory,
2023-02-10 17:12:53 -07:00
m_robotSwerveDrive::getPoseEstimator,
2023-02-07 18:50:33 -07:00
m_robotSwerveDrive.getKinematics(),
2023-02-09 18:48:43 -07:00
holoController,
2023-02-07 18:50:33 -07:00
m_robotSwerveDrive::setModuleStates,
m_robotSwerveDrive);
//Init and wrap-up, return everything
return new SequentialCommandGroup(
2023-02-10 17:12:53 -07:00
new InstantCommand(() -> m_robotSwerveDrive.resetPoseEstimator(), m_robotSwerveDrive),
2023-02-09 18:25:57 -07:00
// new InstantCommand(() -> m_robotSwerveDrive.setOdometry(trajectory.getInitialPose())),
2023-02-07 18:50:33 -07:00
swerveControllerCommand,
new InstantCommand(() -> m_robotSwerveDrive.stopModules()));
2023-01-07 13:36:56 -07:00
}
2023-02-04 15:06:42 -07:00
public DeadbandedXboxController getDeadbandedDriverController() {
return this.m_driverXbox;
}
public DeadbandedXboxController getDeadbandedOperatorController() {
return this.m_operatorXbox;
2023-01-07 13:36:56 -07:00
}
}