Skip to content

Commit

Permalink
Add commands
Browse files Browse the repository at this point in the history
Co-authored-by: ambers7 <[email protected]>
Co-authored-by: ilovemarmot <[email protected]>
  • Loading branch information
3 people committed Nov 17, 2023
1 parent 9338d74 commit 90a28b9
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/main/java/com/stuypulse/robot/commands/ElevatorDrive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.stuypulse.robot.commands;

import com.stuypulse.robot.subsystems.Elevator;
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 IStream velocity;

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

velocity = IStream.create(gamepad::getLeftY); // left Y is elevator in controls

addRequirements(elevator);
}

@Override
public void execute() {
elevator.addTargetHeight(velocity.get());
}

@Override
public boolean isFinished() {
return false;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/stuypulse/robot/commands/ElevatorToBottom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.stuypulse.robot.commands;

import com.stuypulse.robot.subsystems.Elevator;

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

public class ElevatorToBottom extends ElevatorToHeight {
public ElevatorToBottom(Elevator elevator) {
super(elevator, MIN_HEIGHT);
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/stuypulse/robot/commands/ElevatorToHeight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.stuypulse.robot.commands;

import com.stuypulse.robot.subsystems.Elevator;

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 double height;

private boolean instant;

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

instant = true;

addRequirements(elevator);
}

public final ElevatorToHeight untilReady() {
instant = false;
return this;
}

@Override
public void initialize() {
elevator.setTargetHeight(height);
}

@Override
public boolean isFinished() {
if (!instant) {
elevator.isReady(MAX_HEIGHT_ERROR);
}
return true;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/stuypulse/robot/commands/ElevatorToTop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.stuypulse.robot.commands;

import com.stuypulse.robot.subsystems.Elevator;

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

public class ElevatorToTop extends ElevatorToHeight {
public ElevatorToTop(Elevator elevator) {
super(elevator, MAX_HEIGHT);
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/stuypulse/robot/subsystems/Elevator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
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 class Elevator {
public class Elevator extends SubsystemBase {
// hardware
public CANSparkMax leftMotor;
public CANSparkMax rightMotor;
Expand Down Expand Up @@ -104,6 +105,14 @@ public void setVoltage(double 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()));

Expand Down

0 comments on commit 90a28b9

Please sign in to comment.