From 9be99be0c303a376eb7917da3d1f182e4987c7b0 Mon Sep 17 00:00:00 2001 From: Aarav <91212@psdschools.org> Date: Thu, 16 Jan 2020 20:22:50 -0700 Subject: [PATCH] Add PID and Motion Magic Commands --- .../java/frc4388/robot/RobotContainer.java | 4 ++ .../robot/commands/DriveToDistanceMM.java | 57 +++++++++++++++++++ .../robot/commands/DriveToDistancePID.java | 4 +- .../java/frc4388/robot/subsystems/Drive.java | 12 ++-- 4 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 src/main/java/frc4388/robot/commands/DriveToDistanceMM.java diff --git a/src/main/java/frc4388/robot/RobotContainer.java b/src/main/java/frc4388/robot/RobotContainer.java index a06bda1..6469cc8 100644 --- a/src/main/java/frc4388/robot/RobotContainer.java +++ b/src/main/java/frc4388/robot/RobotContainer.java @@ -15,6 +15,7 @@ import edu.wpi.first.wpilibj2.command.InstantCommand; import edu.wpi.first.wpilibj2.command.RunCommand; import edu.wpi.first.wpilibj2.command.button.JoystickButton; import frc4388.robot.Constants.*; +import frc4388.robot.commands.DriveToDistanceMM; import frc4388.robot.commands.DriveToDistancePID; import frc4388.robot.subsystems.Drive; import frc4388.robot.subsystems.LED; @@ -74,6 +75,9 @@ public class RobotContainer { /* PID Test Command */ new JoystickButton(getDriverJoystick(), XboxController.B_BUTTON) .whenPressed(new DriveToDistancePID(m_robotDrive, 5000)); + + new JoystickButton(getDriverJoystick(), XboxController.X_BUTTON) + .whenPressed(new DriveToDistanceMM(m_robotDrive, 5000)); } /** diff --git a/src/main/java/frc4388/robot/commands/DriveToDistanceMM.java b/src/main/java/frc4388/robot/commands/DriveToDistanceMM.java new file mode 100644 index 0000000..c259e54 --- /dev/null +++ b/src/main/java/frc4388/robot/commands/DriveToDistanceMM.java @@ -0,0 +1,57 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 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.commands; + +import edu.wpi.first.wpilibj2.command.CommandBase; +import frc4388.robot.subsystems.Drive; + +public class DriveToDistanceMM extends CommandBase { + Drive m_drive; + double m_distance; + double m_leftTarget; + double m_rightTarget; + + /** + * Creates a new DriveToDistancePID. + */ + public DriveToDistanceMM(Drive subsystem, double distance) { + // Use addRequirements() here to declare subsystem dependencies. + m_drive = subsystem; + m_distance = distance; + addRequirements(m_drive); + } + + // Called when the command is initially scheduled. + @Override + public void initialize() { + m_leftTarget = m_drive.m_leftFrontMotor.getActiveTrajectoryPosition() + m_distance; + m_rightTarget = m_drive.m_rightFrontMotor.getActiveTrajectoryPosition() + m_distance; + } + + // Called every time the scheduler runs while the command is scheduled. + @Override + public void execute() { + m_drive.runMotionMagicPID(m_drive.m_leftFrontMotor, m_leftTarget); + m_drive.runMotionMagicPID(m_drive.m_rightFrontMotor, m_rightTarget); + } + + // Called once the command ends or is interrupted. + @Override + public void end(boolean interrupted) { + } + + // Returns true when the command should end. + @Override + public boolean isFinished() { + if (Math.abs(m_drive.m_leftFrontMotor.getActiveTrajectoryPosition() - m_leftTarget) < 100){ + return true; + } else { + return false; + } + } +} diff --git a/src/main/java/frc4388/robot/commands/DriveToDistancePID.java b/src/main/java/frc4388/robot/commands/DriveToDistancePID.java index 6512150..e0bbbe8 100644 --- a/src/main/java/frc4388/robot/commands/DriveToDistancePID.java +++ b/src/main/java/frc4388/robot/commands/DriveToDistancePID.java @@ -36,8 +36,8 @@ public class DriveToDistancePID extends CommandBase { // Called every time the scheduler runs while the command is scheduled. @Override public void execute() { - m_drive.gotoPositionPID(m_drive.m_leftFrontMotor, m_leftTarget); - m_drive.gotoPositionPID(m_drive.m_rightFrontMotor, m_rightTarget); + m_drive.runPositionPID(m_drive.m_leftFrontMotor, m_leftTarget); + m_drive.runPositionPID(m_drive.m_rightFrontMotor, m_rightTarget); } // Called once the command ends or is interrupted. diff --git a/src/main/java/frc4388/robot/subsystems/Drive.java b/src/main/java/frc4388/robot/subsystems/Drive.java index d411fa1..3b81cfc 100644 --- a/src/main/java/frc4388/robot/subsystems/Drive.java +++ b/src/main/java/frc4388/robot/subsystems/Drive.java @@ -74,7 +74,7 @@ public class Drive extends SubsystemBase { //m_leftFrontMotor.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, DriveConstants.DRIVE_PID_LOOP_IDX, DriveConstants.DRIVE_TIMEOUT_MS); //m_rightFrontMotor.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, DriveConstants.DRIVE_PID_LOOP_IDX, DriveConstants.DRIVE_TIMEOUT_MS); - m_leftFrontMotor.setStatusFramePeriod(StatusFrameEnhanced.Status_10_MotionMagic, 10, DriveConstants.DRIVE_TIMEOUT_MS); + /*m_leftFrontMotor.setStatusFramePeriod(StatusFrameEnhanced.Status_10_MotionMagic, 10, DriveConstants.DRIVE_TIMEOUT_MS); m_rightFrontMotor.setStatusFramePeriod(StatusFrameEnhanced.Status_10_MotionMagic, 10, DriveConstants.DRIVE_TIMEOUT_MS); m_leftFrontMotor.configNominalOutputForward(0, DriveConstants.DRIVE_TIMEOUT_MS); @@ -84,7 +84,7 @@ public class Drive extends SubsystemBase { m_rightFrontMotor.configNominalOutputForward(0, DriveConstants.DRIVE_TIMEOUT_MS); m_rightFrontMotor.configNominalOutputReverse(0, DriveConstants.DRIVE_TIMEOUT_MS); m_rightFrontMotor.configPeakOutputForward(1, DriveConstants.DRIVE_TIMEOUT_MS); - m_rightFrontMotor.configPeakOutputReverse(-1, DriveConstants.DRIVE_TIMEOUT_MS); + m_rightFrontMotor.configPeakOutputReverse(-1, DriveConstants.DRIVE_TIMEOUT_MS);*/ setDriveTrainGains(); @@ -179,14 +179,18 @@ public class Drive extends SubsystemBase { m_driveTrain.arcadeDrive(move, steer); } - public void gotoPositionPID(WPI_TalonFX talon, double targetPos) { + public void runPositionPID(WPI_TalonFX talon, double targetPos) { talon.set(TalonFXControlMode.Position, targetPos); } - public void gotoVelocityPID(WPI_TalonFX talon, double targetVel) { + public void runVelocityPID(WPI_TalonFX talon, double targetVel) { talon.set(TalonFXControlMode.Velocity, targetVel); } + public void runMotionMagicPID(WPI_TalonFX talon, double targetPos){ + talon.set(TalonFXControlMode.MotionMagic, targetPos); + } + public double getGyroYaw() { double[] ypr = new double[3]; m_pigeon.getYawPitchRoll(ypr);