2022-01-11 11:05:52 -07:00
|
|
|
// 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.
|
2021-11-15 16:26:16 -07:00
|
|
|
|
|
|
|
|
package frc4388.robot;
|
|
|
|
|
|
2022-02-15 11:05:59 -07:00
|
|
|
import java.io.File;
|
2022-02-11 18:53:13 -07:00
|
|
|
import java.io.IOException;
|
2022-03-01 20:23:01 -07:00
|
|
|
import java.io.StringWriter;
|
2022-02-16 22:31:00 -07:00
|
|
|
import java.nio.file.FileSystems;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.StandardWatchEventKinds;
|
|
|
|
|
import java.nio.file.WatchEvent;
|
|
|
|
|
import java.nio.file.WatchKey;
|
2022-03-01 11:57:49 -07:00
|
|
|
import java.time.Clock;
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
2022-02-10 21:18:00 -07:00
|
|
|
import java.util.ArrayList;
|
2022-02-15 11:05:59 -07:00
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Comparator;
|
2022-02-16 22:31:00 -07:00
|
|
|
import java.util.List;
|
2022-02-27 22:59:32 -07:00
|
|
|
import java.util.Objects;
|
2022-02-15 11:05:59 -07:00
|
|
|
import java.util.Optional;
|
2022-02-16 22:31:00 -07:00
|
|
|
import java.util.function.Function;
|
|
|
|
|
import java.util.logging.Level;
|
2022-02-15 11:05:59 -07:00
|
|
|
import java.util.logging.Logger;
|
2022-02-16 22:31:00 -07:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
2022-02-27 22:59:32 -07:00
|
|
|
import java.util.stream.Collectors;
|
2022-01-21 16:50:26 -07:00
|
|
|
|
2022-03-10 16:47:32 -07:00
|
|
|
import com.ctre.phoenix.motorcontrol.can.WPI_TalonFX;
|
2022-02-16 22:31:00 -07:00
|
|
|
import com.diffplug.common.base.Errors;
|
2022-01-29 14:39:46 -07:00
|
|
|
import com.pathplanner.lib.PathPlanner;
|
2022-02-04 19:02:41 -07:00
|
|
|
import com.pathplanner.lib.PathPlannerTrajectory;
|
2022-02-07 19:49:27 -07:00
|
|
|
import com.pathplanner.lib.PathPlannerTrajectory.PathPlannerState;
|
2022-02-04 19:02:41 -07:00
|
|
|
import com.pathplanner.lib.commands.PPSwerveControllerCommand;
|
|
|
|
|
|
2022-03-10 16:47:32 -07:00
|
|
|
import org.ejml.dense.row.decomposition.hessenberg.TridiagonalDecompositionHouseholderOrig_DDRM;
|
|
|
|
|
|
2022-02-27 22:59:32 -07:00
|
|
|
import edu.wpi.first.math.Pair;
|
2022-01-29 14:39:46 -07:00
|
|
|
import edu.wpi.first.math.controller.PIDController;
|
|
|
|
|
import edu.wpi.first.math.controller.ProfiledPIDController;
|
2022-01-22 15:55:04 -07:00
|
|
|
import edu.wpi.first.math.geometry.Pose2d;
|
2022-01-29 14:39:46 -07:00
|
|
|
import edu.wpi.first.math.geometry.Rotation2d;
|
2022-02-16 22:31:00 -07:00
|
|
|
import edu.wpi.first.math.geometry.Translation2d;
|
2022-03-01 20:23:01 -07:00
|
|
|
import edu.wpi.first.networktables.NetworkTable;
|
|
|
|
|
import edu.wpi.first.networktables.NetworkTableInstance;
|
2022-02-15 11:05:59 -07:00
|
|
|
import edu.wpi.first.wpilibj.Filesystem;
|
2022-02-11 18:53:13 -07:00
|
|
|
import edu.wpi.first.wpilibj.GenericHID;
|
2022-03-01 20:23:01 -07:00
|
|
|
import edu.wpi.first.wpilibj.RobotBase;
|
2022-01-29 01:16:58 -07:00
|
|
|
import edu.wpi.first.wpilibj.XboxController;
|
2022-02-16 22:31:00 -07:00
|
|
|
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
|
2021-11-15 16:26:16 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.Command;
|
2022-02-03 20:53:43 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.InstantCommand;
|
2022-02-16 22:31:00 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.NotifierCommand;
|
2022-03-14 15:16:21 -06:00
|
|
|
import edu.wpi.first.wpilibj2.command.ParallelCommandGroup;
|
2021-11-15 16:26:16 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.RunCommand;
|
2022-02-03 20:53:43 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
|
2021-11-15 16:26:16 -07:00
|
|
|
import edu.wpi.first.wpilibj2.command.button.JoystickButton;
|
2022-02-11 18:53:13 -07:00
|
|
|
import frc4388.robot.Constants.OIConstants;
|
2022-03-05 22:57:55 -07:00
|
|
|
import frc4388.robot.Constants.StorageConstants;
|
2022-02-11 18:59:00 -07:00
|
|
|
import frc4388.robot.Constants.SwerveDriveConstants;
|
2022-02-24 18:12:57 -07:00
|
|
|
import frc4388.robot.commands.AimToCenter;
|
2022-03-14 15:16:21 -06:00
|
|
|
import frc4388.robot.commands.ExtenderIntakeGroup;
|
2022-03-12 19:01:16 -07:00
|
|
|
import frc4388.robot.commands.RunMiddleSwitch;
|
2022-03-05 11:07:54 -07:00
|
|
|
import frc4388.robot.commands.Shoot;
|
2022-03-06 00:24:51 -07:00
|
|
|
import frc4388.robot.commands.TrackTarget;
|
2022-01-20 18:08:05 -07:00
|
|
|
import frc4388.robot.subsystems.BoomBoom;
|
2022-03-10 16:47:32 -07:00
|
|
|
import frc4388.robot.subsystems.Climber;
|
2022-03-12 15:05:32 -07:00
|
|
|
import frc4388.robot.subsystems.Extender;
|
2022-01-20 18:08:05 -07:00
|
|
|
import frc4388.robot.subsystems.Hood;
|
2022-03-05 15:32:48 -07:00
|
|
|
import frc4388.robot.subsystems.Intake;
|
2021-11-15 16:26:16 -07:00
|
|
|
import frc4388.robot.subsystems.LED;
|
2022-01-15 14:20:49 -07:00
|
|
|
import frc4388.robot.subsystems.Serializer;
|
2022-02-26 15:41:54 -07:00
|
|
|
import frc4388.robot.subsystems.Storage;
|
2021-12-02 17:51:06 -07:00
|
|
|
import frc4388.robot.subsystems.SwerveDrive;
|
2022-01-20 18:08:05 -07:00
|
|
|
import frc4388.robot.subsystems.Turret;
|
|
|
|
|
import frc4388.robot.subsystems.Vision;
|
2022-03-05 11:29:40 -07:00
|
|
|
import frc4388.robot.subsystems.VisionOdometry;
|
2021-11-15 16:26:16 -07:00
|
|
|
import frc4388.utility.LEDPatterns;
|
2022-02-27 22:59:32 -07:00
|
|
|
import frc4388.utility.ListeningSendableChooser;
|
2022-02-16 22:31:00 -07:00
|
|
|
import frc4388.utility.PathPlannerUtil;
|
2022-03-12 14:35:47 -07:00
|
|
|
import frc4388.utility.Vector2D;
|
2022-02-16 22:31:00 -07:00
|
|
|
import frc4388.utility.PathPlannerUtil.Path.Waypoint;
|
2022-03-12 17:33:38 -07:00
|
|
|
import frc4388.utility.controller.ButtonBox;
|
2022-01-29 01:16:58 -07:00
|
|
|
import frc4388.utility.controller.DeadbandedXboxController;
|
2021-11-15 16:26:16 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 {
|
2022-02-25 01:33:32 -07:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(RobotContainer.class.getSimpleName());
|
2022-01-11 11:05:52 -07:00
|
|
|
/* RobotMap */
|
2022-03-07 15:26:05 -07:00
|
|
|
public final RobotMap m_robotMap = new RobotMap();
|
2022-01-11 11:05:52 -07:00
|
|
|
|
2022-03-05 14:56:59 -07:00
|
|
|
// Subsystems
|
2022-03-05 15:32:48 -07:00
|
|
|
public final SwerveDrive m_robotSwerveDrive = new SwerveDrive(m_robotMap.leftFront, m_robotMap.leftBack, m_robotMap.rightFront, m_robotMap.rightBack, m_robotMap.gyro);
|
2022-03-07 15:26:05 -07:00
|
|
|
public final Serializer m_robotSerializer = new Serializer(m_robotMap.serializerBelt, /*m_robotMap.serializerShooterBelt,*/ m_robotMap.serializerBeam);
|
2022-03-12 15:05:32 -07:00
|
|
|
public final Intake m_robotIntake = new Intake(m_robotMap.intakeMotor, m_robotSerializer);
|
|
|
|
|
public final Extender m_robotExtender = new Extender(m_robotMap.extenderMotor);
|
|
|
|
|
|
2022-03-07 15:26:05 -07:00
|
|
|
public final Storage m_robotStorage = new Storage(m_robotMap.storageMotor);
|
2022-03-06 11:14:30 -07:00
|
|
|
// private final LED m_robotLED = new LED(m_robotMap.LEDController);
|
2022-03-12 20:39:05 -07:00
|
|
|
public final BoomBoom m_robotBoomBoom = new BoomBoom(m_robotMap.shooterFalconLeft, m_robotMap.shooterFalconRight);
|
2022-03-12 22:19:42 -07:00
|
|
|
public final Hood m_robotHood = new Hood(m_robotMap.angleAdjusterMotor);
|
2022-03-07 15:26:05 -07:00
|
|
|
public final Turret m_robotTurret = new Turret(m_robotMap.shooterTurret);
|
2022-03-13 19:59:55 -06:00
|
|
|
public final VisionOdometry m_robotVisionOdometry = new VisionOdometry(m_robotSwerveDrive, m_robotTurret);
|
2022-03-10 16:47:32 -07:00
|
|
|
private final WPI_TalonFX testElbowMotor = new WPI_TalonFX(30);
|
2022-03-10 17:46:51 -07:00
|
|
|
private final WPI_TalonFX testShoulderMotor = new WPI_TalonFX(31);
|
2022-03-12 11:36:10 -07:00
|
|
|
public final Climber m_robotClimber = new Climber(testElbowMotor);
|
2022-03-12 11:37:33 -07:00
|
|
|
|
2022-01-11 11:05:52 -07:00
|
|
|
/* Controllers */
|
2022-01-29 01:16:58 -07:00
|
|
|
private final XboxController m_driverXbox = new DeadbandedXboxController(OIConstants.XBOX_DRIVER_ID);
|
|
|
|
|
private final XboxController m_operatorXbox = new DeadbandedXboxController(OIConstants.XBOX_OPERATOR_ID);
|
2022-03-12 19:02:37 -07:00
|
|
|
private final ButtonBox m_buttonBox = new ButtonBox(OIConstants.BUTTON_BOX_ID);
|
2022-01-11 11:05:52 -07:00
|
|
|
|
2022-03-01 20:23:01 -07:00
|
|
|
/* Autonomous */
|
2022-02-16 22:31:00 -07:00
|
|
|
private PathPlannerTrajectory loadedPathTrajectory = null;
|
2022-02-27 22:59:32 -07:00
|
|
|
private final ListeningSendableChooser<File> autoChooser = new ListeningSendableChooser<>(this::loadPath);
|
2022-03-01 11:57:49 -07:00
|
|
|
private final List<Waypoint> pathPoints = new ArrayList<>();
|
2022-03-01 20:23:01 -07:00
|
|
|
private final NetworkTableInstance networkTableInstance = NetworkTableInstance.getDefault();
|
|
|
|
|
private final NetworkTable recordingNetworkTable = networkTableInstance.getTable("Recording");
|
2022-02-27 22:59:32 -07:00
|
|
|
|
2022-03-05 11:12:33 -07:00
|
|
|
private static final DateTimeFormatter RECORDING_FILE_NAME_FORMATTER = DateTimeFormatter
|
|
|
|
|
.ofPattern("'Recording' yyyy-MM-dd HH-mm-ss.SSS'.path'");
|
2022-03-01 11:57:49 -07:00
|
|
|
private static final Clock SYSTEM_CLOCK = Clock.system(ZoneId.systemDefault());
|
|
|
|
|
private static final Path PATHPLANNER_DIRECTORY = Filesystem.getDeployDirectory().toPath().resolve("pathplanner");
|
2022-03-03 23:56:08 -07:00
|
|
|
// Function that removes the ".path" from the end of a string.
|
2022-03-05 11:12:33 -07:00
|
|
|
private static final Function<CharSequence, String> PATH_EXTENSION_REMOVER = ((Function<CharSequence, Matcher>) Pattern
|
|
|
|
|
.compile(".path")::matcher).andThen(m -> m.replaceFirst(""));
|
2022-03-12 17:43:17 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean manual = false;
|
2022-03-12 22:19:42 -07:00
|
|
|
public static boolean softLimits = true;
|
2022-02-16 22:31:00 -07:00
|
|
|
|
2022-01-11 11:05:52 -07:00
|
|
|
/**
|
|
|
|
|
* The container for the robot. Contains subsystems, OI devices, and commands.
|
|
|
|
|
*/
|
|
|
|
|
public RobotContainer() {
|
|
|
|
|
configureButtonBindings();
|
2022-03-12 14:59:21 -07:00
|
|
|
// testShoulderMotor.setNeutralMode(NeutralMode.Brake);
|
2022-01-11 11:05:52 -07:00
|
|
|
/* Default Commands */
|
2022-03-05 11:12:33 -07:00
|
|
|
|
2022-03-06 00:48:38 -07:00
|
|
|
// Swerve Drive with Input
|
2022-03-07 15:26:05 -07:00
|
|
|
m_robotSwerveDrive.setDefaultCommand(
|
2022-01-15 11:31:15 -07:00
|
|
|
new RunCommand(() -> m_robotSwerveDrive.driveWithInput(
|
2022-02-11 18:53:13 -07:00
|
|
|
getDriverController().getLeftX(),
|
|
|
|
|
getDriverController().getLeftY(),
|
2022-03-07 15:26:05 -07:00
|
|
|
//getDriverController().getRightX(),
|
2022-02-11 18:53:13 -07:00
|
|
|
getDriverController().getRightX(),
|
2022-03-10 16:47:32 -07:00
|
|
|
// getDriverController().getRightY(),
|
2022-03-12 20:39:05 -07:00
|
|
|
true),
|
2022-03-07 15:26:05 -07:00
|
|
|
m_robotSwerveDrive).withName("Swerve driveWithInput defaultCommand"));
|
2022-03-06 00:48:38 -07:00
|
|
|
// Intake with Triggers
|
2022-03-05 22:57:55 -07:00
|
|
|
m_robotIntake.setDefaultCommand(
|
|
|
|
|
new RunCommand(() -> m_robotIntake.runWithTriggers(
|
2022-03-07 15:26:05 -07:00
|
|
|
getOperatorController().getLeftTriggerAxis(),
|
|
|
|
|
getOperatorController().getRightTriggerAxis()),
|
2022-03-05 22:57:55 -07:00
|
|
|
m_robotIntake).withName("Intake runWithTriggers defaultCommand"));
|
2022-03-12 14:59:21 -07:00
|
|
|
// m_robotClimber.setDefaultCommand(
|
|
|
|
|
// new RunCommand(() -> m_robotClimber.runWithInput(getOperatorController().getRightY() * 0.6), m_robotClimber)
|
|
|
|
|
// );
|
2022-03-06 00:48:38 -07:00
|
|
|
// Storage Management
|
2022-03-06 11:14:30 -07:00
|
|
|
/*m_robotStorage.setDefaultCommand(
|
2022-03-05 22:57:55 -07:00
|
|
|
new RunCommand(() -> m_robotStorage.manageStorage(),
|
2022-03-06 11:14:30 -07:00
|
|
|
m_robotStorage).withName("Storage manageStorage defaultCommand"));*/
|
2022-03-06 00:48:38 -07:00
|
|
|
// Serializer Management
|
2022-03-08 16:14:34 -07:00
|
|
|
m_robotSerializer.setDefaultCommand(
|
|
|
|
|
new RunCommand(() -> m_robotSerializer.setSerializer(getOperatorController().getLeftTriggerAxis() * 0.8),//m_robotSerializer.setSerializerStateWithBeam(),
|
|
|
|
|
m_robotSerializer).withName("Serializer setSerializerStateWithBeam defaultCommand"));
|
2022-03-06 00:48:38 -07:00
|
|
|
// Turret Manual
|
2022-03-06 18:14:49 -07:00
|
|
|
m_robotTurret.setDefaultCommand(
|
2022-03-07 15:26:05 -07:00
|
|
|
new RunCommand(() -> m_robotTurret.runTurretWithInput(getOperatorController().getLeftX()),
|
2022-03-06 18:14:49 -07:00
|
|
|
m_robotTurret).withName("Turret runTurretWithInput defaultCommand"));
|
2022-03-14 12:16:32 -06:00
|
|
|
|
|
|
|
|
|
2022-03-12 22:19:42 -07:00
|
|
|
m_robotHood.setDefaultCommand(
|
2022-03-13 17:57:36 -06:00
|
|
|
new RunCommand(() -> m_robotHood.runHood(getOperatorController().getRightY()), m_robotHood));
|
2022-03-06 00:48:38 -07:00
|
|
|
// m_robotTurret.setDefaultCommand(
|
|
|
|
|
// new AimToCenter(m_robotTurret, m_robotSwerveDrive, m_robotVisionOdometry));
|
2022-01-11 11:05:52 -07:00
|
|
|
|
|
|
|
|
// continually sends updates to the Blinkin LED controller to keep the lights on
|
2022-03-10 17:46:51 -07:00
|
|
|
// m_robotLED.setDefaultCommand(new RunCommand(m_robotLED::updateLED, m_robotLED).withName("LED update defaultCommand"));
|
|
|
|
|
|
|
|
|
|
// autoInit();
|
|
|
|
|
// recordInit();
|
2022-01-11 11:05:52 -07:00
|
|
|
}
|
2022-01-15 11:31:15 -07:00
|
|
|
|
2022-01-11 11:05:52 -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() {
|
2022-03-05 22:57:55 -07:00
|
|
|
|
2022-01-11 11:05:52 -07:00
|
|
|
/* Driver Buttons */
|
2022-03-06 00:48:38 -07:00
|
|
|
// Start > Calibrate Odometry
|
2022-03-06 11:23:40 -07:00
|
|
|
new JoystickButton(getDriverController(), XboxController.Button.kBack.value)
|
2022-03-06 00:48:38 -07:00
|
|
|
.whenPressed(() -> resetOdometry(new Pose2d(0, 0, new Rotation2d(0))));
|
2022-03-06 11:23:40 -07:00
|
|
|
// Start > Calibrate Odometry
|
|
|
|
|
new JoystickButton(getDriverController(), XboxController.Button.kStart.value)
|
|
|
|
|
.whenPressed(m_robotSwerveDrive::resetGyro);
|
2022-03-06 00:48:38 -07:00
|
|
|
// Left Bumper > Shift Down
|
2022-01-29 01:16:58 -07:00
|
|
|
new JoystickButton(getDriverController(), XboxController.Button.kLeftBumper.value)
|
2022-02-11 18:53:13 -07:00
|
|
|
.whenPressed(() -> m_robotSwerveDrive.highSpeed(false));
|
2022-03-06 00:48:38 -07:00
|
|
|
// Right Bumper > Shift Up
|
2022-01-29 01:16:58 -07:00
|
|
|
new JoystickButton(getDriverController(), XboxController.Button.kRightBumper.value)
|
2022-02-11 18:53:13 -07:00
|
|
|
.whenPressed(() -> m_robotSwerveDrive.highSpeed(true));
|
2022-03-10 16:47:32 -07:00
|
|
|
|
2022-03-12 14:59:21 -07:00
|
|
|
// new JoystickButton(getDriverController(), XboxController.Button.kA.value)
|
|
|
|
|
// .whenPressed(() -> m_robotBoomBoom.increaseSpeed(0.025));
|
2022-03-10 16:47:32 -07:00
|
|
|
|
2022-03-12 14:59:21 -07:00
|
|
|
// new JoystickButton(getDriverController(), XboxController.Button.kB.value)
|
|
|
|
|
// .whenPressed(() -> m_robotBoomBoom.increaseSpeed(-0.025));
|
2022-03-10 16:47:32 -07:00
|
|
|
// .whileHeld(new RunCommand(() -> testElbowMotor.set(-0.2)))
|
|
|
|
|
// .whenReleased(new RunCommand(() -> testElbowMotor.set(0)));
|
2022-02-11 18:53:13 -07:00
|
|
|
|
2022-03-06 00:48:38 -07:00
|
|
|
// new JoystickButton(getDriverController(), XboxController.Button.kA.value)
|
|
|
|
|
// .whenPressed(() -> resetOdometry(new Pose2d(0, 0, new Rotation2d(0))));
|
2022-03-05 22:57:55 -07:00
|
|
|
|
2022-03-08 16:14:34 -07:00
|
|
|
// new JoystickButton(getDriverController(), XboxController.Button.kX.value) //Temp
|
|
|
|
|
// .whenPressed(() -> m_robotMap.leftFront.reset())
|
|
|
|
|
// .whenPressed(() -> m_robotMap.rightFront.reset())
|
|
|
|
|
// .whenPressed(() -> m_robotMap.leftBack.reset())
|
2022-03-10 16:47:32 -07:00
|
|
|
// .whenPressed(() -> m_robotMap.rightBack.reset());
|
2022-01-15 14:55:07 -07:00
|
|
|
|
2022-01-11 11:05:52 -07:00
|
|
|
/* Operator Buttons */
|
2022-03-05 22:57:55 -07:00
|
|
|
|
2022-03-06 00:48:38 -07:00
|
|
|
// X > Extend Intake
|
2022-03-12 22:19:42 -07:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kX.value)
|
|
|
|
|
// .whenPressed(new InstantCommand(() -> m_robotVisionOdometry.setLEDs(true), m_robotVisionOdometry));
|
|
|
|
|
|
|
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kX.value)
|
|
|
|
|
// .whenPressed(new InstantCommand(() -> m_robotTurret.m_boomBoomRotateEncoder.setPosition(0), m_robotTurret));
|
|
|
|
|
|
2022-03-14 15:16:21 -06:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kY.value)
|
|
|
|
|
// .whenPressed(new InstantCommand(() -> m_robotVisionOdometry.setLEDs(false), m_robotVisionOdometry));
|
2022-03-12 22:19:42 -07:00
|
|
|
|
2022-03-12 20:22:42 -07:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kY.value)
|
2022-03-12 22:19:42 -07:00
|
|
|
// .whenPressed(new InstantCommand(() -> m_robotVisionOdometry.setLEDs(false), m_robotVisionOdometry));
|
2022-03-06 18:02:04 -07:00
|
|
|
|
2022-03-10 17:46:51 -07:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kA.value) //8ft
|
|
|
|
|
// .whileHeld(new RunCommand(() -> m_robotBoomBoom.runDrumShooter(0.425)))
|
|
|
|
|
// .whileHeld(new RunCommand(() -> m_robotHood.runAngleAdjustPID(-25)))
|
|
|
|
|
// .whenReleased(new RunCommand(() -> m_robotBoomBoom.runDrumShooter(0.0)));
|
2022-03-08 16:14:34 -07:00
|
|
|
|
|
|
|
|
|
2022-03-10 17:46:51 -07:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kB.value) //12ft
|
|
|
|
|
// .whileHeld(new RunCommand(() -> m_robotBoomBoom.runDrumShooter(0.475)))
|
|
|
|
|
// .whileHeld(new RunCommand(() -> m_robotHood.runAngleAdjustPID(-47.57)))
|
|
|
|
|
// .whenReleased(new RunCommand(() -> m_robotBoomBoom.runDrumShooter(0.0)));
|
2022-03-08 16:14:34 -07:00
|
|
|
|
2022-03-06 18:02:04 -07:00
|
|
|
|
2022-03-10 17:46:51 -07:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kY.value) //16ft
|
|
|
|
|
// .whileHeld(new RunCommand(() -> m_robotBoomBoom.runDrumShooter(0.5)))
|
|
|
|
|
// .whileHeld(new RunCommand(() -> m_robotHood.runAngleAdjustPID(-55.55)))
|
|
|
|
|
// .whenReleased(new RunCommand(() -> m_robotBoomBoom.runDrumShooter(0.0)));
|
2022-03-06 18:14:49 -07:00
|
|
|
|
2022-03-14 15:16:21 -06:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kX.value) //20ft
|
|
|
|
|
// .whileHeld(new RunCommand(() -> m_robotBoomBoom.runDrumShooterVelocityPID(10000)))
|
|
|
|
|
// .whenReleased(new RunCommand(() -> m_robotBoomBoom.runDrumShooterVelocityPID(0)));
|
2022-03-07 15:26:05 -07:00
|
|
|
|
|
|
|
|
new JoystickButton(getOperatorController(), XboxController.Button.kLeftBumper.value)
|
2022-03-12 19:01:16 -07:00
|
|
|
.whenPressed(new RunCommand(() -> m_robotStorage.runStorage(-0.9), m_robotStorage))
|
2022-03-07 15:26:05 -07:00
|
|
|
.whenReleased(new RunCommand(() -> m_robotStorage.runStorage(0.0), m_robotStorage));
|
|
|
|
|
|
|
|
|
|
new JoystickButton(getOperatorController(), XboxController.Button.kRightBumper.value)
|
2022-03-08 16:14:34 -07:00
|
|
|
.whileHeld(new RunCommand(() -> m_robotStorage.runStorage(0.9), m_robotStorage))
|
2022-03-07 15:26:05 -07:00
|
|
|
.whenReleased(new RunCommand(() -> m_robotStorage.runStorage(0.0), m_robotStorage));
|
2022-03-12 14:59:21 -07:00
|
|
|
|
2022-03-14 15:16:21 -06:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kX.value)
|
|
|
|
|
// .whileHeld(new RunExtender(m_robotIntake, m_robotExtender, 0.5))
|
|
|
|
|
// .whenReleased(new RunCommand(() -> RunExtender.changeDirection(), m_robotIntake, m_robotExtender));
|
|
|
|
|
|
|
|
|
|
new JoystickButton(getOperatorController(), XboxController.Button.kY.value)
|
|
|
|
|
.whenPressed(new ExtenderIntakeGroup(m_robotIntake, m_robotExtender));
|
|
|
|
|
// .whenReleased(new InstantCommand(() -> ExtenderIntakeGroup.changeDirection()));
|
|
|
|
|
|
|
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kX.value)
|
|
|
|
|
// .whenPressed(new ParallelCommandGroup(
|
|
|
|
|
// new RunCommand(() -> m_robotIntake.m_intakeMotor.set(ExtenderIntakeGroup.direction * -0.2), m_robotIntake),
|
|
|
|
|
// new RunCommand(() -> m_robotExtender.m_extenderMotor.set(ExtenderIntakeGroup.direction * 0.5), m_robotExtender)))
|
|
|
|
|
// .whenReleased(new InstantCommand(() -> ExtenderIntakeGroup.changeDirection()));
|
|
|
|
|
|
|
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kY.value)
|
|
|
|
|
// .whenPressed(new InstantCommand(() -> m_robotExtender.m_extenderMotor.getEncoder().setPosition(0)));
|
|
|
|
|
|
2022-03-14 12:16:32 -06:00
|
|
|
new JoystickButton(getOperatorController(), XboxController.Button.kA.value)
|
|
|
|
|
.whileHeld(new RunCommand(() -> m_robotExtender.runExtender(1.0), m_robotExtender))
|
2022-03-14 15:16:21 -06:00
|
|
|
.whenReleased(new RunCommand(() -> m_robotExtender.runExtender(0.0), m_robotExtender));
|
2022-03-13 17:02:55 -06:00
|
|
|
|
2022-03-14 12:16:32 -06:00
|
|
|
new JoystickButton(getOperatorController(), XboxController.Button.kB.value)
|
|
|
|
|
.whileHeld(new RunCommand(() -> m_robotExtender.runExtender(-1.0), m_robotExtender))
|
|
|
|
|
.whenReleased(new RunCommand(() -> m_robotExtender.runExtender(0.0), m_robotExtender));
|
2022-03-13 17:02:55 -06:00
|
|
|
|
2022-03-13 17:57:36 -06:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kA.value)
|
|
|
|
|
// .whenPressed(new InstantCommand(() -> m_robotBoomBoom.updateOffset(500), m_robotBoomBoom));
|
|
|
|
|
|
|
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kB.value)
|
|
|
|
|
// .whenPressed(new InstantCommand(() -> m_robotBoomBoom.updateOffset(-500), m_robotBoomBoom));
|
2022-03-13 17:02:55 -06:00
|
|
|
|
2022-03-06 00:48:38 -07:00
|
|
|
// Right Bumper > Storage In
|
2022-03-06 13:02:17 -07:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kRightBumper.value)
|
|
|
|
|
// .whileHeld(new RunCommand(() -> m_robotStorage.runStorage(StorageConstants.STORAGE_SPEED)))
|
|
|
|
|
// .whenReleased(new RunCommand(() -> m_robotStorage.runStorage(0.0)));
|
|
|
|
|
// // Left Bumper > Storage Out (note: neccessary?)
|
|
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kLeftBumper.value)
|
|
|
|
|
// .whileHeld(new RunCommand(() -> m_robotStorage.runStorage(-StorageConstants.STORAGE_SPEED)))
|
|
|
|
|
// .whenReleased(new RunCommand(() -> m_robotStorage.runStorage(0.0)));
|
2022-03-10 16:47:32 -07:00
|
|
|
|
2022-03-06 00:48:38 -07:00
|
|
|
// A > Shoot with Odo
|
2022-03-06 14:51:20 -07:00
|
|
|
/*new JoystickButton(getOperatorController(), XboxController.Button.kA.value)
|
2022-03-10 16:47:32 -07:00
|
|
|
.whenPressed(new Shoot(m_robotSwerveDrive, m_robotBoomBoom, m_robotTurret, m_robotHood));*/
|
|
|
|
|
|
|
|
|
|
//B > Shoot with Lime
|
2022-03-14 12:16:32 -06:00
|
|
|
// new JoystickButton(getOperatorController(), XboxController.Button.kB.value)
|
|
|
|
|
// .whileHeld(new TrackTarget(m_robotTurret, m_robotBoomBoom, m_robotHood, m_robotVisionOdometry));
|
|
|
|
|
|
2022-03-12 17:33:38 -07:00
|
|
|
|
|
|
|
|
/* Button Box Buttons */
|
2022-03-12 22:19:42 -07:00
|
|
|
|
2022-03-12 19:02:37 -07:00
|
|
|
new JoystickButton(getButtonBox(), ButtonBox.Button.kLeftSwitch.value)
|
2022-03-12 22:19:42 -07:00
|
|
|
.whenPressed(new InstantCommand(() -> m_robotTurret.setTurretSoftLimits(false), m_robotTurret))
|
|
|
|
|
.whenPressed(new InstantCommand(() -> m_robotHood.setHoodSoftLimits(false), m_robotHood))
|
2022-03-14 15:16:21 -06:00
|
|
|
.whenPressed(new InstantCommand(() -> m_robotExtender.setExtenderSoftLimits(false), m_robotExtender))
|
|
|
|
|
|
2022-03-12 22:19:42 -07:00
|
|
|
.whenReleased(new InstantCommand(() -> m_robotTurret.setTurretSoftLimits(true), m_robotTurret))
|
|
|
|
|
.whenReleased(new InstantCommand(() -> m_robotHood.setHoodSoftLimits(true), m_robotHood))
|
2022-03-14 15:16:21 -06:00
|
|
|
.whenReleased(new InstantCommand(() -> m_robotExtender.setExtenderSoftLimits(true), m_robotExtender))
|
|
|
|
|
|
2022-03-12 22:19:42 -07:00
|
|
|
.whenReleased(new InstantCommand(() -> m_robotTurret.m_boomBoomRotateEncoder.setPosition(0), m_robotTurret))
|
2022-03-14 15:16:21 -06:00
|
|
|
.whenReleased(new InstantCommand(() -> m_robotHood.m_angleEncoder.setPosition(0), m_robotHood))
|
|
|
|
|
.whenReleased(new InstantCommand(() -> m_robotExtender.m_extenderMotor.getEncoder().setPosition(0), m_robotExtender));
|
2022-03-12 22:19:42 -07:00
|
|
|
|
|
|
|
|
// new JoystickButton(getButtonBox(), ButtonBox.Button.kLeftSwitch.value)
|
|
|
|
|
// .whileHeld(new RunCommand(() -> RunMiddleSwitch.setManual(true)))
|
|
|
|
|
// .whenReleased(new RunCommand(() -> RunMiddleSwitch.setManual(false)));
|
2022-03-12 17:33:38 -07:00
|
|
|
|
2022-03-12 22:19:42 -07:00
|
|
|
// new JoystickButton(getButtonBox(), ButtonBox.Button.kMiddleSwitch.value)
|
|
|
|
|
// .whileHeld(new RunMiddleSwitch());
|
2022-03-12 17:33:38 -07:00
|
|
|
|
2022-03-12 19:02:37 -07:00
|
|
|
new JoystickButton(getButtonBox(), ButtonBox.Button.kRightSwitch.value)
|
2022-03-12 17:33:38 -07:00
|
|
|
.whileHeld(new RunCommand(() -> System.out.println("RightSwitch")));
|
|
|
|
|
|
2022-03-12 19:02:37 -07:00
|
|
|
new JoystickButton(getButtonBox(), ButtonBox.Button.kLeftButton.value)
|
2022-03-12 17:33:38 -07:00
|
|
|
.whileHeld(new RunCommand(() -> System.out.println("LeftButton")));
|
|
|
|
|
|
2022-03-12 19:02:37 -07:00
|
|
|
new JoystickButton(getButtonBox(), ButtonBox.Button.kRightButton.value)
|
2022-03-12 17:33:38 -07:00
|
|
|
.whileHeld(new RunCommand(() -> System.out.println("RightButton")));
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 11:05:52 -07:00
|
|
|
/**
|
|
|
|
|
* Use this to pass the autonomous command to the main {@link Robot} class.
|
|
|
|
|
*
|
|
|
|
|
* @return the command to run in autonomous
|
|
|
|
|
*/
|
2022-02-16 22:31:00 -07:00
|
|
|
public Command getAutonomousCommand() {
|
|
|
|
|
if (loadedPathTrajectory != null) {
|
|
|
|
|
PIDController xController = SwerveDriveConstants.X_CONTROLLER;
|
|
|
|
|
PIDController yController = SwerveDriveConstants.Y_CONTROLLER;
|
|
|
|
|
ProfiledPIDController thetaController = SwerveDriveConstants.THETA_CONTROLLER;
|
|
|
|
|
thetaController.enableContinuousInput(-Math.PI, Math.PI);
|
|
|
|
|
|
|
|
|
|
PathPlannerState initialState = loadedPathTrajectory.getInitialState();
|
|
|
|
|
Pose2d initialPosition = new Pose2d(initialState.poseMeters.getTranslation(), initialState.holonomicRotation);
|
|
|
|
|
return new SequentialCommandGroup(
|
|
|
|
|
new InstantCommand(m_robotSwerveDrive.m_gyro::reset),
|
|
|
|
|
new InstantCommand(() -> m_robotSwerveDrive.resetOdometry(initialPosition)),
|
|
|
|
|
new PPSwerveControllerCommand(loadedPathTrajectory, m_robotSwerveDrive::getOdometry,
|
|
|
|
|
m_robotSwerveDrive.m_kinematics, xController, yController, thetaController,
|
|
|
|
|
m_robotSwerveDrive::setModuleStates, m_robotSwerveDrive),
|
|
|
|
|
new InstantCommand(m_robotSwerveDrive::stopModules)).withName("Run Autonomous Path");
|
|
|
|
|
} else {
|
2022-02-25 01:33:32 -07:00
|
|
|
LOGGER.severe("No auto selected.");
|
2022-03-05 11:12:33 -07:00
|
|
|
return new RunCommand(() -> {
|
|
|
|
|
}).withName("No Autonomous Path");
|
2022-02-15 11:05:59 -07:00
|
|
|
}
|
2022-01-11 11:05:52 -07:00
|
|
|
}
|
|
|
|
|
|
2022-01-29 01:16:58 -07:00
|
|
|
public XboxController getDriverController() {
|
2022-01-11 11:05:52 -07:00
|
|
|
return m_driverXbox;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 17:33:38 -07:00
|
|
|
public XboxController getOperatorController() {
|
|
|
|
|
return m_operatorXbox;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 19:02:37 -07:00
|
|
|
public ButtonBox getButtonBox() {
|
|
|
|
|
return m_buttonBox;
|
2022-03-12 17:33:38 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-12 17:43:17 -07:00
|
|
|
public void setManual(boolean set) {
|
|
|
|
|
this.manual = set;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-12 22:19:42 -07:00
|
|
|
public static void setSoftLimits(boolean set) {
|
|
|
|
|
softLimits = set;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 11:05:52 -07:00
|
|
|
/**
|
2022-02-17 19:52:05 -07:00
|
|
|
* Get odometry.
|
2022-03-05 11:12:33 -07:00
|
|
|
*
|
2022-02-17 19:52:05 -07:00
|
|
|
* @return Odometry
|
2022-01-11 11:05:52 -07:00
|
|
|
*/
|
2022-01-22 15:55:04 -07:00
|
|
|
public Pose2d getOdometry() {
|
|
|
|
|
return m_robotSwerveDrive.getOdometry();
|
|
|
|
|
}
|
2022-01-24 17:00:06 -07:00
|
|
|
|
2022-01-11 11:05:52 -07:00
|
|
|
/**
|
2022-02-17 19:52:05 -07:00
|
|
|
* Set odometry to given pose.
|
2022-03-05 11:12:33 -07:00
|
|
|
*
|
2022-02-17 19:52:05 -07:00
|
|
|
* @param pose Pose to set odometry to.
|
2022-01-11 11:05:52 -07:00
|
|
|
*/
|
2022-02-05 11:50:49 -07:00
|
|
|
public void resetOdometry(Pose2d pose) {
|
2022-01-29 14:39:46 -07:00
|
|
|
m_robotSwerveDrive.resetOdometry(pose);
|
2022-01-24 17:00:06 -07:00
|
|
|
}
|
2022-02-11 18:53:13 -07:00
|
|
|
|
2022-03-03 23:56:08 -07:00
|
|
|
/**
|
2022-03-05 11:12:33 -07:00
|
|
|
* Creates a WatchKey for the path planner directory and registers it with the
|
|
|
|
|
* WatchService.
|
|
|
|
|
* Then creates a NotifierCommand that will update the auto chooser with the
|
|
|
|
|
* latest path files.
|
2022-03-03 23:56:08 -07:00
|
|
|
* Finally, adds the existing path files to the auto chooser
|
|
|
|
|
*/
|
2022-03-01 11:57:49 -07:00
|
|
|
private void autoInit() {
|
|
|
|
|
try {
|
2022-03-05 11:12:33 -07:00
|
|
|
WatchKey watchKey = PATHPLANNER_DIRECTORY.register(FileSystems.getDefault().newWatchService(),
|
|
|
|
|
StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY,
|
|
|
|
|
StandardWatchEventKinds.ENTRY_DELETE);
|
2022-03-01 11:57:49 -07:00
|
|
|
// TODO: Store this and other commands as fields so they can be rescheduled.
|
|
|
|
|
new NotifierCommand(() -> updateAutoChooser(watchKey), 0.5) {
|
2022-03-05 11:12:33 -07:00
|
|
|
@Override
|
|
|
|
|
public boolean runsWhenDisabled() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-03-01 11:57:49 -07:00
|
|
|
}.withName("Path Watcher").schedule();
|
|
|
|
|
} catch (IOException exception) {
|
|
|
|
|
LOGGER.log(Level.SEVERE, "Exception with path file watcher.", exception);
|
|
|
|
|
}
|
|
|
|
|
Arrays.stream(PATHPLANNER_DIRECTORY.toFile().listFiles())
|
|
|
|
|
.filter(file -> file.getName().endsWith(".path")).sorted(Comparator.comparingLong(File::lastModified))
|
|
|
|
|
.forEachOrdered(file -> autoChooser.addOption(file.getName(), file));
|
|
|
|
|
SmartDashboard.putData("Auto Chooser", autoChooser);
|
|
|
|
|
}
|
2022-02-16 22:31:00 -07:00
|
|
|
|
2022-03-03 23:56:08 -07:00
|
|
|
/**
|
2022-03-05 11:12:33 -07:00
|
|
|
* Creates a button on the SmartDashboard that will record the path of the
|
|
|
|
|
* robot.
|
2022-03-03 23:56:08 -07:00
|
|
|
*/
|
2022-02-16 22:31:00 -07:00
|
|
|
public void recordInit() {
|
|
|
|
|
SmartDashboard.putData("Recording",
|
|
|
|
|
new RunCommand(this::recordPeriodic) {
|
|
|
|
|
@Override
|
|
|
|
|
public void end(boolean interupted) {
|
|
|
|
|
new InstantCommand(RobotContainer.this::saveRecording) {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean runsWhenDisabled() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}.withName("Save Recording").schedule();
|
|
|
|
|
}
|
|
|
|
|
}.withName("Record Path (Cancel to Save)"));
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 23:56:08 -07:00
|
|
|
/**
|
|
|
|
|
* Called when a file is created, modified, or deleted.
|
|
|
|
|
* Adds newly created .path files to the SendableChooser.
|
|
|
|
|
* Reloads the path if the currently selected file is modified.
|
2022-03-05 11:12:33 -07:00
|
|
|
*
|
2022-03-03 23:56:08 -07:00
|
|
|
* @param watchKey The WatchKey that is being observed.
|
|
|
|
|
*/
|
2022-03-01 11:57:49 -07:00
|
|
|
private void updateAutoChooser(WatchKey watchKey) {
|
|
|
|
|
List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
|
|
|
|
|
if (!watchEvents.isEmpty()) {
|
2022-03-05 11:12:33 -07:00
|
|
|
List<WatchEvent<?>> pathWatchEvents = watchEvents.stream()
|
|
|
|
|
.filter(e -> e.kind().type().isAssignableFrom(Path.class)).collect(Collectors.toList());
|
2022-03-01 11:57:49 -07:00
|
|
|
for (WatchEvent<?> pathWatchEvent : pathWatchEvents) {
|
|
|
|
|
Path watchEventPath = (Path) pathWatchEvent.context();
|
|
|
|
|
File watchEventFile = watchEventPath.toFile();
|
|
|
|
|
String watchEventFileName = watchEventFile.getName();
|
|
|
|
|
if (watchEventFileName.endsWith(".path")) {
|
|
|
|
|
if (pathWatchEvent.kind().equals(StandardWatchEventKinds.ENTRY_CREATE)) {
|
2022-03-05 11:12:33 -07:00
|
|
|
LOGGER.log(Level.WARNING, "PathPlanner file {0} created. Options added to SendableChooser.",
|
|
|
|
|
watchEventFileName);
|
2022-03-01 11:57:49 -07:00
|
|
|
autoChooser.addOption(watchEventFile.getName(), watchEventFile);
|
|
|
|
|
} else if (pathWatchEvent.kind().equals(StandardWatchEventKinds.ENTRY_MODIFY)) {
|
2022-03-01 16:30:25 -07:00
|
|
|
LOGGER.log(Level.WARNING, "PathPlanner file {0} modified.", watchEventFileName);
|
2022-03-01 11:57:49 -07:00
|
|
|
if (watchEventFileName.equals(autoChooser.getSelected().getName())) {
|
2022-03-01 16:30:25 -07:00
|
|
|
LOGGER.log(Level.SEVERE, "PathPlanner file {0} already selected. Reloading path.", watchEventFileName);
|
2022-03-01 11:57:49 -07:00
|
|
|
loadPath(watchEventFileName);
|
|
|
|
|
}
|
|
|
|
|
} else if (pathWatchEvent.kind().equals(StandardWatchEventKinds.ENTRY_DELETE)) {
|
2022-03-05 11:12:33 -07:00
|
|
|
LOGGER.log(Level.SEVERE,
|
|
|
|
|
"PathPlanner file {0} deleted. Removing options from SendableChooser not yet implemented.",
|
|
|
|
|
watchEventFileName);
|
2022-03-01 11:57:49 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!watchKey.reset())
|
|
|
|
|
LOGGER.severe("File watch key invalid.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadPath(String pathName) {
|
|
|
|
|
LOGGER.warning("Loading path " + pathName);
|
|
|
|
|
loadedPathTrajectory = null;
|
2022-03-05 11:12:33 -07:00
|
|
|
loadedPathTrajectory = PathPlanner.loadPath(PATH_EXTENSION_REMOVER.apply(Objects.requireNonNullElse(pathName, "")),
|
|
|
|
|
SwerveDriveConstants.PATH_MAX_VELOCITY, SwerveDriveConstants.PATH_MAX_ACCELERATION);
|
2022-03-01 11:57:49 -07:00
|
|
|
LOGGER.info("Done loading");
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-16 22:31:00 -07:00
|
|
|
private void saveRecording() {
|
|
|
|
|
// IMPORTANT: Had to chown the pathplanner folder in order to save autos.
|
2022-03-05 11:12:33 -07:00
|
|
|
File outputFile = PATHPLANNER_DIRECTORY
|
|
|
|
|
.resolve(ZonedDateTime.now(SYSTEM_CLOCK).format(RECORDING_FILE_NAME_FORMATTER)).toFile();
|
2022-02-27 22:59:32 -07:00
|
|
|
LOGGER.log(Level.WARNING, "Creating path {0}.", outputFile.getPath());
|
2022-03-01 20:23:01 -07:00
|
|
|
if (!pathPoints.isEmpty() && Boolean.TRUE.equals(Errors.log().getWithDefault(outputFile::createNewFile, false))) {
|
2022-03-01 11:57:49 -07:00
|
|
|
// TODO: Change to use measured maximum velocity and acceleration.
|
2022-03-01 20:23:01 -07:00
|
|
|
var path = createPath(null, null, false);
|
|
|
|
|
if (RobotBase.isReal())
|
|
|
|
|
path.write(outputFile);
|
|
|
|
|
StringWriter writer = new StringWriter();
|
|
|
|
|
path.write(writer);
|
|
|
|
|
recordingNetworkTable.getEntry(outputFile.getName()).setString(writer.toString());
|
2022-02-16 22:31:00 -07:00
|
|
|
autoChooser.setDefaultOption(outputFile.getName(), outputFile);
|
2022-02-27 22:59:32 -07:00
|
|
|
LOGGER.log(Level.INFO, "Recorded path to {0}.", outputFile.getPath());
|
2022-02-16 22:31:00 -07:00
|
|
|
} else
|
|
|
|
|
LOGGER.log(Level.SEVERE, "Unable to record path to {0}", outputFile.getPath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void recordPeriodic() {
|
|
|
|
|
Translation2d position = m_robotSwerveDrive.m_poseEstimator.getEstimatedPosition().getTranslation();
|
|
|
|
|
Rotation2d rotation = m_robotSwerveDrive.m_gyro.getRotation2d();
|
2022-03-05 11:12:33 -07:00
|
|
|
// FIXME: Chassis speeds are created from joystick inputs and do not reflect
|
|
|
|
|
// actual robot velocity.
|
2022-03-06 18:02:04 -07:00
|
|
|
Translation2d velocity = new Translation2d(m_robotSwerveDrive.getChassisSpeeds()[0],
|
|
|
|
|
m_robotSwerveDrive.getChassisSpeeds()[1]);
|
2022-03-05 11:12:33 -07:00
|
|
|
Waypoint waypoint = new Waypoint(position, position, position, rotation.getDegrees(), false,
|
|
|
|
|
SwerveDriveConstants.PATH_RECORD_VELOCITY ? velocity.getNorm() : null, false);
|
2022-02-16 22:31:00 -07:00
|
|
|
pathPoints.add(waypoint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PathPlannerUtil.Path createPath(Double maxVelocity, Double maxAcceleration, Boolean isReversed) {
|
2022-03-05 11:12:33 -07:00
|
|
|
// Remove points whose angles to neighboring points are less than 10 degrees
|
|
|
|
|
// apart.
|
2022-02-27 22:59:32 -07:00
|
|
|
int j = 0;
|
|
|
|
|
for (int i = 1; i < pathPoints.size() - 1; i++) {
|
|
|
|
|
var prev = pathPoints.get(j).anchorPoint.orElseThrow();
|
|
|
|
|
var current = pathPoints.get(i).anchorPoint.orElseThrow();
|
|
|
|
|
var next = pathPoints.get(i + 1).anchorPoint.orElseThrow();
|
|
|
|
|
var fromPrevious = current.minus(prev);
|
|
|
|
|
var toNext = next.minus(current);
|
|
|
|
|
var angleFromPrevious = new Rotation2d(fromPrevious.getX(), fromPrevious.getY());
|
|
|
|
|
var angleToNext = new Rotation2d(toNext.getX(), toNext.getY());
|
2022-03-05 11:12:33 -07:00
|
|
|
if (Math.abs(angleFromPrevious.minus(angleToNext).getDegrees()) < SwerveDriveConstants.MIN_WAYPOINT_ANGLE
|
|
|
|
|
|| (next.getDistance(prev) < SwerveDriveConstants.MIN_WAYPOINT_DISTANCE
|
|
|
|
|
&& pathPoints.get(i).velOverride.map(v -> v < SwerveDriveConstants.MIN_WAYPOINT_VELOCITY).orElse(false)))
|
2022-02-27 22:59:32 -07:00
|
|
|
pathPoints.set(i, null);
|
2022-02-28 18:57:53 -07:00
|
|
|
else
|
|
|
|
|
j = i;
|
2022-02-27 22:59:32 -07:00
|
|
|
}
|
|
|
|
|
pathPoints.removeIf(Objects::isNull);
|
|
|
|
|
// Make control points
|
2022-03-05 11:12:33 -07:00
|
|
|
pathPoints.get(0).nextControl = Optional.of(makeControlPoints(null, pathPoints.get(0).anchorPoint.orElseThrow(),
|
|
|
|
|
pathPoints.get(1).anchorPoint.orElseThrow()).getSecond());
|
2022-02-27 22:59:32 -07:00
|
|
|
for (int i = 1; i < pathPoints.size() - 1; i++) {
|
2022-03-05 11:12:33 -07:00
|
|
|
var controls = makeControlPoints(pathPoints.get(i - 1).anchorPoint.orElseThrow(),
|
|
|
|
|
pathPoints.get(i).anchorPoint.orElseThrow(), pathPoints.get(i + 1).anchorPoint.orElseThrow());
|
2022-02-27 22:59:32 -07:00
|
|
|
pathPoints.get(i).prevControl = Optional.of(controls.getFirst());
|
|
|
|
|
pathPoints.get(i).nextControl = Optional.of(controls.getSecond());
|
|
|
|
|
}
|
2022-03-05 11:12:33 -07:00
|
|
|
pathPoints.get(pathPoints.size() - 1).prevControl = Optional
|
|
|
|
|
.of(makeControlPoints(pathPoints.get(pathPoints.size() - 2).anchorPoint.orElseThrow(),
|
|
|
|
|
pathPoints.get(pathPoints.size() - 1).anchorPoint.orElseThrow(), null).getFirst());
|
2022-02-27 22:59:32 -07:00
|
|
|
// Create the path
|
2022-02-16 22:31:00 -07:00
|
|
|
PathPlannerUtil.Path path = new PathPlannerUtil.Path();
|
|
|
|
|
path.waypoints = Optional.ofNullable(pathPoints.toArray(PathPlannerUtil.Path.Waypoint[]::new));
|
|
|
|
|
path.maxVelocity = Optional.ofNullable(maxVelocity);
|
|
|
|
|
path.maxAcceleration = Optional.ofNullable(maxAcceleration);
|
|
|
|
|
path.isReversed = Optional.ofNullable(isReversed);
|
|
|
|
|
pathPoints.clear();
|
|
|
|
|
return path;
|
|
|
|
|
}
|
2022-02-27 22:59:32 -07:00
|
|
|
|
2022-03-05 11:12:33 -07:00
|
|
|
private static Pair<Translation2d, Translation2d> makeControlPoints(Translation2d prev, Translation2d current,
|
|
|
|
|
Translation2d next) {
|
2022-02-27 22:59:32 -07:00
|
|
|
var line = Objects.requireNonNullElse(next, current).minus(Objects.requireNonNullElse(prev, current)).div(4);
|
|
|
|
|
return Pair.of(current.minus(line), current.plus(line));
|
|
|
|
|
}
|
2021-11-15 16:26:16 -07:00
|
|
|
}
|