-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
450eb8e
commit 40c0d9e
Showing
6 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/frc/robot/Commands/SetElevatorHeightCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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.robot.commands; | ||
|
||
import edu.wpi.first.wpilibj2.command.CommandBase; | ||
import frc.robot.subsystems.ElevatorSubsystem; | ||
|
||
public class SetElevatorHeightCommand extends CommandBase { | ||
|
||
ElevatorSubsystem elevatorSubsystem; | ||
double height; | ||
|
||
/** Creates a new SetElevatorHeightCommand. */ | ||
public SetElevatorHeightCommand(ElevatorSubsystem elevatorSubsystem, double height) { | ||
|
||
this.elevatorSubsystem = elevatorSubsystem; | ||
this.height = height; | ||
|
||
// Use addRequirements() here to declare subsystem dependencies. | ||
addRequirements(elevatorSubsystem); | ||
} | ||
|
||
// Called when the command is initially scheduled. | ||
@Override | ||
public void initialize() { | ||
//set the elevator height | ||
elevatorSubsystem.setHeight(height); | ||
|
||
} | ||
|
||
// 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() { | ||
return elevatorSubsystem.nearTargetHeight(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/frc/robot/Subsystems/ElevatorSubsystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// 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.robot.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.Shuffleboard; | ||
import edu.wpi.first.wpilibj.shuffleboard.ShuffleboardTab; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
import frc.robot.Constants; | ||
|
||
public class ElevatorSubsystem extends SubsystemBase { | ||
/** leader */ | ||
private TalonFX left_motor; | ||
/** follower */ | ||
private TalonFX right_motor; | ||
|
||
private final ShuffleboardTab ElevatorTab = Shuffleboard.getTab("Elevator"); | ||
|
||
private double currentHeight; | ||
private double targetHeight; | ||
|
||
private double motorPower; | ||
|
||
private PIDController controller; | ||
|
||
/** Creates a new ElevatorSubsystem. */ | ||
public ElevatorSubsystem() { | ||
|
||
left_motor = new TalonFX(14); | ||
right_motor = new TalonFX(15); | ||
|
||
right_motor.configFactoryDefault(); | ||
left_motor.configFactoryDefault(); | ||
|
||
right_motor.clearStickyFaults(); | ||
left_motor.clearStickyFaults(); | ||
|
||
right_motor.configForwardSoftLimitThreshold( | ||
0, 0); // this is the bottom limit, we stop AT the bottom | ||
right_motor.configReverseSoftLimitThreshold(-inchesToTicks(Constants.Elevator.MAX_ELEVATOR_EXTENSION), 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); | ||
|
||
// make sure we hold our height when we get disabled | ||
right_motor.setNeutralMode(NeutralMode.Coast); | ||
left_motor.setNeutralMode(NeutralMode.Coast); | ||
|
||
right_motor.follow(left_motor); | ||
|
||
targetHeight = 0; | ||
|
||
motorPower = 0; | ||
|
||
controller = new PIDController(.02, 0, .01); | ||
|
||
ElevatorTab.addNumber("Current Motor Power", () -> this.motorPower); | ||
|
||
ElevatorTab.addNumber("Left Motor Speed", left_motor::getSelectedSensorVelocity); | ||
ElevatorTab.addNumber("Right Motor Speed", right_motor::getSelectedSensorVelocity); | ||
|
||
ElevatorTab.addNumber("height", () -> this.currentHeight); | ||
ElevatorTab.addNumber("target height", () -> this.targetHeight); | ||
ElevatorTab.addNumber("right motor sensor value", this::getHeight); | ||
|
||
} | ||
|
||
public void setMotorPower(double motorPower){ | ||
this.motorPower = MathUtil.clamp(motorPower, -0.25, 0.25); | ||
} | ||
|
||
public double inchesToTicks(double inches){ | ||
|
||
return (inches / (Constants.Elevator.GEAR_RATIO * Constants.Elevator.GEAR_CIRCUMFERENCE * Constants.Elevator.CARRIAGE_RATIO)) * Constants.Elevator.TICKS_PER_REVOLUTION; | ||
} | ||
|
||
public double ticksToInches(double ticks){ | ||
return (ticks/ Constants.Elevator.TICKS_PER_REVOLUTION) * Constants.Elevator.GEAR_RATIO * Constants.Elevator.GEAR_CIRCUMFERENCE * Constants.Elevator.CARRIAGE_RATIO; | ||
} | ||
|
||
public void setHeight(double targetHeight){ | ||
this.targetHeight = targetHeight; | ||
|
||
controller.setSetpoint(targetHeight); | ||
} | ||
|
||
public double getHeight(){ | ||
return currentHeight; | ||
} | ||
|
||
public boolean nearTargetHeight(){ | ||
return (targetHeight - Constants.Elevator.NEAR_TARGET_HEIGHT_THRESHOLD <= getHeight() | ||
&& getHeight() <= targetHeight + Constants.Elevator.NEAR_TARGET_HEIGHT_THRESHOLD); | ||
} | ||
|
||
@Override | ||
public void periodic() { | ||
|
||
currentHeight = ticksToInches(-left_motor.getSelectedSensorPosition()); | ||
|
||
motorPower = controller.calculate(currentHeight); | ||
|
||
|
||
left_motor.set(TalonFXControlMode.PercentOutput, motorPower); | ||
|
||
} | ||
} |