Skip to content

Commit

Permalink
Add abstract Elevator
Browse files Browse the repository at this point in the history
Co-authored-by: reyamiller <[email protected]>
Co-authored-by: ambers7 <[email protected]>
  • Loading branch information
colyic committed Dec 6, 2023
1 parent fe2be7a commit c83c68c
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 115 deletions.
8 changes: 8 additions & 0 deletions simgui.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
"/SmartDashboard/Elevator": "Mechanism2d"
},
"windows": {
"/SmartDashboard/Autonomous": {
"window": {
"visible": true
}
},
"/SmartDashboard/Elevator": {
"window": {
"visible": true
Expand All @@ -26,5 +31,8 @@
"open": true
}
}
},
"NetworkTables View": {
"visible": false
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/stuypulse/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RobotContainer {
public final Gamepad operator = new AutoGamepad(Ports.Gamepad.OPERATOR);

// Subsystem
ElevatorVisualizer elevator = new ElevatorVisualizer();
ElevatorVisualizer elevatorVisualizer = new ElevatorVisualizer();

// Autons
private static SendableChooser<Command> autonChooser = new SendableChooser<>();
Expand Down Expand Up @@ -56,7 +56,6 @@ public void configureAutons() {
autonChooser.setDefaultOption("Do Nothing", new DoNothingAuton());

SmartDashboard.putData("Autonomous", autonChooser);
// SmartDashboard.putData("elevator", elevator);
}

public Command getAutonomousCommand() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/stuypulse/robot/commands/ElevatorDrive.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.stuypulse.robot.commands;

import com.stuypulse.robot.subsystems.Elevator;
import com.stuypulse.robot.subsystems.ElevatorImpl;
import com.stuypulse.stuylib.input.Gamepad;
import com.stuypulse.stuylib.streams.IStream;

import edu.wpi.first.wpilibj2.command.CommandBase;

public class ElevatorDrive extends CommandBase {
private final Elevator elevator;
private final ElevatorImpl elevator;
private IStream velocity;

public ElevatorDrive(Elevator elevator, Gamepad gamepad) {
public ElevatorDrive(ElevatorImpl elevator, Gamepad gamepad) {
this.elevator = elevator;

velocity = IStream.create(gamepad::getLeftY); // left Y is elevator in controls
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.stuypulse.robot.commands;

import com.stuypulse.robot.subsystems.Elevator;
import com.stuypulse.robot.subsystems.ElevatorImpl;

import static com.stuypulse.robot.constants.Settings.Elevator.*;

public class ElevatorToBottom extends ElevatorToHeight {
public ElevatorToBottom(Elevator elevator) {
public ElevatorToBottom(ElevatorImpl elevator) {
super(elevator, MIN_HEIGHT);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.stuypulse.robot.commands;

import com.stuypulse.robot.subsystems.Elevator;
import com.stuypulse.robot.subsystems.ElevatorImpl;

import edu.wpi.first.wpilibj2.command.CommandBase;

import static com.stuypulse.robot.constants.Settings.Elevator.*;

public class ElevatorToHeight extends CommandBase {
private final Elevator elevator;
private final ElevatorImpl elevator;
private final double height;

private boolean instant;

public ElevatorToHeight(Elevator elevator, double height) {
public ElevatorToHeight(ElevatorImpl elevator, double height) {
this.elevator = elevator;
this.height = height;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/stuypulse/robot/commands/ElevatorToTop.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.stuypulse.robot.commands;

import com.stuypulse.robot.subsystems.Elevator;
import com.stuypulse.robot.subsystems.ElevatorImpl;

import static com.stuypulse.robot.constants.Settings.Elevator.*;

public class ElevatorToTop extends ElevatorToHeight {
public ElevatorToTop(Elevator elevator) {
public ElevatorToTop(ElevatorImpl elevator) {
super(elevator, MAX_HEIGHT);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/stuypulse/robot/constants/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
public interface Settings {
public interface Elevator {
double GEARING = 0.0;
double CARRIAGE_MASS = 0.0; // kg
double DRUM_RADIUS = 0.0; // meters

double MIN_HEIGHT = 0.0;
double MAX_HEIGHT = 0.0;

Expand Down
106 changes: 22 additions & 84 deletions src/main/java/com/stuypulse/robot/subsystems/Elevator.java
Original file line number Diff line number Diff line change
@@ -1,57 +1,43 @@
package com.stuypulse.robot.subsystems;

import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;
import static com.stuypulse.robot.constants.Settings.Elevator.*;
import static com.stuypulse.robot.constants.Settings.FeedForward.kG;
import static com.stuypulse.robot.constants.Settings.PID.*;

import com.stuypulse.robot.constants.Ports;
import com.stuypulse.robot.util.ElevatorVisualizer;
import com.stuypulse.stuylib.control.Controller;
import com.stuypulse.stuylib.control.feedback.PIDController;
import com.stuypulse.stuylib.control.feedforward.ElevatorFeedforward;
import com.stuypulse.stuylib.network.SmartNumber;
import com.stuypulse.stuylib.streams.filters.MotionProfile;

import com.revrobotics.RelativeEncoder;

import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.RobotBase;
import edu.wpi.first.wpilibj2.command.SubsystemBase;

import static com.stuypulse.robot.constants.Settings.Elevator.*;
import static com.stuypulse.robot.constants.Settings.Elevator.Encoder.*;
import static com.stuypulse.robot.constants.Settings.PID.*;

import static com.stuypulse.robot.constants.Settings.FeedForward.*;
public abstract class Elevator extends SubsystemBase {
// singleton
private static final Elevator instance;

public class Elevator extends SubsystemBase {
// hardware
public CANSparkMax leftMotor;
public CANSparkMax rightMotor;
static {
if (RobotBase.isReal()) {
instance = new ElevatorImpl();
} else {
instance = new SimElevator();
}
}

public RelativeEncoder leftEncoder;
public RelativeEncoder rightEncoder;
public static Elevator getInstance() {
return instance;
}

public DigitalInput topLimit;
public DigitalInput bottomLimit;
// elevator visualizer
ElevatorVisualizer elevatorVisualizer = new ElevatorVisualizer();

// control
public Controller position;

public SmartNumber targetHeight;

public Elevator() {
// motors
leftMotor = new CANSparkMax(Ports.Elevator.LEFT, MotorType.kBrushless);
rightMotor = new CANSparkMax(Ports.Elevator.RIGHT, MotorType.kBrushless);

// encoders
leftEncoder = leftMotor.getEncoder();
rightEncoder = rightMotor.getEncoder();

// limit switches
topLimit = new DigitalInput(Ports.Elevator.TOP_LIMIT_SWITCH);
bottomLimit = new DigitalInput(Ports.Elevator.BOTTOM_LIMIT_SWITCH);

// control
position = new PIDController(kP, kI, kD)
.add(new ElevatorFeedforward(kG))
Expand All @@ -60,14 +46,6 @@ public Elevator() {
targetHeight = new SmartNumber("Target Height", MIN_HEIGHT);
}

public boolean atTop() {
return !topLimit.get();
}

public boolean atBottom() {
return !bottomLimit.get();
}

public double getTargetHeight() {
return targetHeight.get();
}
Expand All @@ -76,50 +54,10 @@ public void setTargetHeight(double height) {
targetHeight.set(height);
}

public double getVelocity() { // average of the two?
return (rightEncoder.getVelocity() + leftEncoder.getVelocity()) / 2 * ENCODER_MULTIPLIER;
}

public double getHeight() {
return (leftEncoder.getPosition() + rightEncoder.getPosition()) / 2 * ENCODER_MULTIPLIER;
}

public void setVoltage(double voltage) {
if (atTop() && voltage > 0) {
DriverStation.reportWarning("Top Limit Reached", false);

voltage = 0.0;

leftEncoder.setPosition(MAX_HEIGHT);
rightEncoder.setPosition(MAX_HEIGHT);
} else if (atBottom() && voltage < 0) {
DriverStation.reportWarning("Bottom Limit Reached", false);

voltage = 0.0;

leftEncoder.setPosition(MIN_HEIGHT);
rightEncoder.setPosition(MIN_HEIGHT);
}

rightMotor.setVoltage(voltage);
leftMotor.setVoltage(voltage);
}

public final boolean isReady(double error) {
return Math.abs(getTargetHeight() - getHeight()) < error;
}

public void addTargetHeight(double delta) {
setTargetHeight(getTargetHeight() + delta);
}

public void periodic() {
setVoltage(position.update(getHeight(), getTargetHeight()));

SmartDashboard.putBoolean("Elevator/at Top", atTop());
SmartDashboard.putBoolean("Elevator/at Bottom", atBottom());

SmartDashboard.putNumber("Elevator/Height", getHeight());
SmartDashboard.putNumber("Elevator/Velocity", getVelocity());
}
public abstract double getVelocity();
public abstract double getHeight();
}
81 changes: 81 additions & 0 deletions src/main/java/com/stuypulse/robot/subsystems/ElevatorImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.stuypulse.robot.subsystems;

import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMaxLowLevel.MotorType;

import com.stuypulse.robot.constants.Ports;
import com.revrobotics.RelativeEncoder;

import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DriverStation;
import static com.stuypulse.robot.constants.Settings.Elevator.*;
import static com.stuypulse.robot.constants.Settings.Elevator.Encoder.*;

public class ElevatorImpl extends Elevator {
// hardware
public CANSparkMax leftMotor;
public CANSparkMax rightMotor;

public RelativeEncoder leftEncoder;
public RelativeEncoder rightEncoder;

public DigitalInput topLimit;
public DigitalInput bottomLimit;

public ElevatorImpl() {
// motors
leftMotor = new CANSparkMax(Ports.Elevator.LEFT, MotorType.kBrushless);
rightMotor = new CANSparkMax(Ports.Elevator.RIGHT, MotorType.kBrushless);

// encoders
leftEncoder = leftMotor.getEncoder();
rightEncoder = rightMotor.getEncoder();

// limit switches
topLimit = new DigitalInput(Ports.Elevator.TOP_LIMIT_SWITCH);
bottomLimit = new DigitalInput(Ports.Elevator.BOTTOM_LIMIT_SWITCH);
}

public boolean atTop() {
return !topLimit.get();
}

public boolean atBottom() {
return !bottomLimit.get();
}

@Override
public double getVelocity() { // average of the two?
return (rightEncoder.getVelocity() + leftEncoder.getVelocity()) / 2 * ENCODER_MULTIPLIER;
}

@Override
public double getHeight() {
return (leftEncoder.getPosition() + rightEncoder.getPosition()) / 2 * ENCODER_MULTIPLIER;
}

public void setVoltage(double voltage) {
if (atTop() && voltage > 0) {
DriverStation.reportWarning("Top Limit Reached", false);

voltage = 0.0;

leftEncoder.setPosition(MAX_HEIGHT);
rightEncoder.setPosition(MAX_HEIGHT);
} else if (atBottom() && voltage < 0) {
DriverStation.reportWarning("Bottom Limit Reached", false);

voltage = 0.0;

leftEncoder.setPosition(MIN_HEIGHT);
rightEncoder.setPosition(MIN_HEIGHT);
}

rightMotor.setVoltage(voltage);
leftMotor.setVoltage(voltage);
}

public void addTargetHeight(double delta) {
setTargetHeight(getTargetHeight() + delta);
}
}
28 changes: 10 additions & 18 deletions src/main/java/com/stuypulse/robot/subsystems/SimElevator.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,29 @@
package com.stuypulse.robot.subsystems;

import static com.stuypulse.robot.constants.Settings.Elevator.*;
import static com.stuypulse.robot.constants.Settings.FeedForward.*;
import static com.stuypulse.robot.constants.Settings.PID.*;

import com.stuypulse.stuylib.control.Controller;
import com.stuypulse.stuylib.control.feedback.PIDController;
import com.stuypulse.stuylib.control.feedforward.ElevatorFeedforward;
import com.stuypulse.stuylib.network.SmartNumber;
import com.stuypulse.stuylib.streams.filters.MotionProfile;

import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.wpilibj.simulation.ElevatorSim;

public class SimElevator {
public class SimElevator extends Elevator {
private ElevatorSim sim;

private double height;
private double velocity;

private Controller position;
private SmartNumber targetHeight;

public SimElevator() {
height = 0.0;
velocity = 0.0;

targetHeight = new SmartNumber("Target Height", MIN_HEIGHT);
sim = new ElevatorSim(DCMotor.getNEO(2), GEARING, CARRIAGE_MASS, DRUM_RADIUS, MIN_HEIGHT, MAX_HEIGHT, true);
}

position = new PIDController(kP, kI, kD)
.add(new ElevatorFeedforward(kG))
.setOutputFilter(new MotionProfile(VEL_LIMIT, ACC_LIMIT));
@Override
public double getVelocity() {
return sim.getVelocityMetersPerSecond();
}

// sim = new ElevatorSim(DCMotor.getCIM(4), 106.94, 2.5, 1.435, MIN_HEIGHT, MAX_HEIGHT, false);
@Override
public double getHeight() {
return sim.getPositionMeters();
}
}
Loading

0 comments on commit c83c68c

Please sign in to comment.