From d29b983de99709bf997e382d82621d05534073ef Mon Sep 17 00:00:00 2001 From: Ethan Li Date: Thu, 30 Nov 2023 16:24:37 -0800 Subject: [PATCH] Cleaned up code a bit Got rid of unnecessary spaces, made it easier to look at --- .../frc/Subsystems/ElevatorSubsystem.java | 32 +++---------------- .../robot/Commands/ElevatorBaseCommand.java | 12 ------- src/main/java/frc/robot/RobotContainer.java | 7 ---- 3 files changed, 5 insertions(+), 46 deletions(-) diff --git a/src/main/java/frc/Subsystems/ElevatorSubsystem.java b/src/main/java/frc/Subsystems/ElevatorSubsystem.java index e36a0f7..82b2eef 100644 --- a/src/main/java/frc/Subsystems/ElevatorSubsystem.java +++ b/src/main/java/frc/Subsystems/ElevatorSubsystem.java @@ -1,32 +1,13 @@ // 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.Subsystems; - - - import com.ctre.phoenix.motorcontrol.NeutralMode; import com.ctre.phoenix.motorcontrol.TalonFXControlMode; import com.ctre.phoenix.motorcontrol.can.TalonFX; - import edu.wpi.first.math.MathUtil; import edu.wpi.first.math.controller.PIDController; import edu.wpi.first.wpilibj.shuffleboard.BuiltInLayouts; @@ -36,7 +17,6 @@ import frc.robot.Constants.Elevator; import edu.wpi.first.wpilibj2.command.SubsystemBase; - public class ElevatorSubsystem extends SubsystemBase { /** Creates a new ExampleSubsystem. */ private TalonFX left_motor; @@ -58,18 +38,16 @@ public ElevatorSubsystem() { right_motor.clearStickyFaults(); left_motor.clearStickyFaults(); - right_motor.configForwardSoftLimitThreshold( 0, 0); // this is the bottom limit, we stop AT the bottom + // right_motor.configReverseSoftLimitThreshold( // -heightToTicks(24), 0); // this is the top limit, we stop at the very top right_motor.configForwardSoftLimitEnable(true, 0); right_motor.configReverseSoftLimitEnable(true, 0); - right_motor.configOpenloopRamp(.5); - left_motor.setSelectedSensorPosition(0); right_motor.setSelectedSensorPosition(0); @@ -85,7 +63,9 @@ public ElevatorSubsystem() { targetHeight = 0; motorPower = 0; + pidController = new PIDController(0.34, 0, 0.02); + //pidController.setTolerance(0.7,0.001); ElevatorTab.addNumber("Current Motor Power", () -> this.motorPower); ElevatorTab.addNumber("Target Height", () -> this.targetHeight); @@ -110,13 +90,11 @@ public static double inchesToTicks(double height) { public static double ticksToInches(double ticks) { return (ticks * Elevator.GEAR_CIRCUMFERENCE) / (Elevator.TICKS_PER_REVOLUTION * Elevator.GEAR_RATIO); } - - + public void setTargetHeight(double targetHeight) { this.targetHeight = targetHeight; pidController.setSetpoint(targetHeight); } - public boolean nearTargetHeight(){ if(targetHeight -0.5 <= currentHeight && currentHeight <= targetHeight + 0.5){ return true; @@ -124,13 +102,13 @@ public boolean nearTargetHeight(){ return false; } } + @Override public void periodic() { // This method will be called once per scheduler run motorPower = pidController.calculate(currentHeight); left_motor.set(TalonFXControlMode.PercentOutput, -(MathUtil.clamp(motorPower + 0.02, -0.5,0.5))); currentHeight = ticksToInches(-left_motor.getSelectedSensorPosition()); - } // left_motor.set(TalonFXControlMode.PercentOutput, -(0.1)); diff --git a/src/main/java/frc/robot/Commands/ElevatorBaseCommand.java b/src/main/java/frc/robot/Commands/ElevatorBaseCommand.java index 1979f79..bfcdd86 100644 --- a/src/main/java/frc/robot/Commands/ElevatorBaseCommand.java +++ b/src/main/java/frc/robot/Commands/ElevatorBaseCommand.java @@ -2,21 +2,17 @@ // 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.Commands; - import frc.Subsystems.ElevatorSubsystem; import edu.wpi.first.wpilibj2.command.CommandBase; - /** An example command that uses an example subsystem. */ public class ElevatorBaseCommand extends CommandBase { @SuppressWarnings({"PMD.UnusedPrivateField", "PMD.SingularField"}) private final ElevatorSubsystem elevatorSubsystem; public double targetHeight; - /** * Creates a new ExampleCommand. * @@ -29,28 +25,20 @@ public ElevatorBaseCommand(ElevatorSubsystem elevatorSubsystem, double targetHei addRequirements(elevatorSubsystem); } - // Called when the command is initially scheduled. @Override public void initialize() { elevatorSubsystem.setTargetHeight(targetHeight); - - - - } - // Called every time the scheduler runs while the command is scheduled. @Override public void execute() {} - // 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() { diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 7702232..ee25db9 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -6,7 +6,6 @@ import edu.wpi.first.wpilibj2.command.button.CommandXboxController; import frc.Subsystems.ElevatorSubsystem; - /** * 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} @@ -17,14 +16,11 @@ public class RobotContainer { // The robot's subsystems and commands are defined here... private final ElevatorSubsystem elevatorSubsystem = new ElevatorSubsystem(); - private ElevatorBaseCommand ElevatorBaseCommand = new ElevatorBaseCommand(elevatorSubsystem, 0); - private final CommandXboxController driverA = new CommandXboxController(0); private double targetHeight; - /** The container for the robot. Contains subsystems, OI devices, and commands. */ public RobotContainer() { @@ -36,7 +32,6 @@ public RobotContainer() { driverA.a().onTrue(new ElevatorBaseCommand(elevatorSubsystem, 3)); } - /** * Use this method to define your button->command mappings. Buttons can be created by * instantiating a {@link GenericHID} or one of its subclasses ({@link @@ -44,8 +39,6 @@ public RobotContainer() { * edu.wpi.first.wpilibj2.command.button.JoystickButton}. */ private void configureButtonBindings() {} - - /** * Use this to pass the autonomous command to the main {@link Robot} class. *