Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto shifting #4

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/frc/robot/OI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private static class PORT {
private static class DRIVER_MAP {
private static final int SHIFT_UP_BUTTON = 3;
private static final int SHIFT_DOWN_BUTTON = 2;
private static final int LOW_SHIFT_BUTTON = 1;
}

/** Operator Button Map */
Expand All @@ -34,6 +35,8 @@ private static class OPERATOR_MAP {
public static final JoystickButton shiftUpButton = new JoystickButton(rightJoystick, DRIVER_MAP.SHIFT_UP_BUTTON);
public static final JoystickButton shiftDownButton = new JoystickButton(rightJoystick, DRIVER_MAP.SHIFT_DOWN_BUTTON);

public static final JoystickButton lowShiftButton = new JoystickButton(leftJoystick, DRIVER_MAP.LOW_SHIFT_BUTTON);


public static DoubleSupplier leftDriveSupplier = () -> {
double raw = leftJoystick.getY();
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import frc.robot.commands.instant.ShiftDownCommand;
import frc.robot.commands.instant.ShiftUpCommand;
import frc.robot.subsystems.Drivetrain;
import frc.robot.subsystems.Drivetrain.Gear;

public class RobotContainer {

Expand All @@ -22,8 +23,16 @@ public RobotContainer() {
}

private void configureBindings() {
OI.shiftUpButton.onTrue(new ShiftUpCommand(drivetrain));
OI.shiftDownButton.onTrue(new ShiftDownCommand(drivetrain));
//OI.shiftUpButton.onTrue(new ShiftUpCommand(drivetrain));
//OI.shiftDownButton.onTrue(new ShiftDownCommand(drivetrain));

// Not requiring drivetrain because we dont want to interupt DriveCommand
OI.lowShiftButton.whileTrue(Commands.runOnce(() -> {
drivetrain.disableAutoShifting();
drivetrain.setGear(Gear.LOW);
})).onFalse(Commands.runOnce(() -> {
drivetrain.enableAutoShifting();
}));
}

public Command getAutonomousCommand() {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/frc/robot/commands/DriveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void initialize() {
@Override
public void execute() {
drivetrain.setOutput(leftSupplier.getAsDouble(), rightSupplier.getAsDouble());
drivetrain.shiftGears();
}

@Override
Expand Down
42 changes: 41 additions & 1 deletion src/main/java/frc/robot/subsystems/Drivetrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.revrobotics.RelativeEncoder;

import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.SubsystemBase;

public class Drivetrain extends SubsystemBase {
Expand All @@ -15,6 +16,8 @@ public class Drivetrain extends SubsystemBase {

Gear currentGear;

boolean autoShifting;

/**
* Encoder to inches conversion factor
* <pre>
Expand Down Expand Up @@ -42,6 +45,8 @@ public Drivetrain(CANSparkMax leftLeadController, CANSparkMax rightLeadControlle
rightDriveEncoder.setPositionConversionFactor(CONVERSION_FACTOR);
rightDriveEncoder.setPosition(0);
rightDriveEncoder.setInverted(false);

autoShifting = true;
}

public enum Gear {
Expand Down Expand Up @@ -78,5 +83,40 @@ public void setGear(Gear gear) {
public Gear getGear() {
return currentGear;
}

public void disableAutoShifting() {
autoShifting = false;
}

public void enableAutoShifting() {
autoShifting = true;
}

/**
* Pulls speed to figure out when to shift gear
* MUST BE RUN IN COMMAND excute()
*/
public void shiftGears() {

if(!autoShifting) return;

double UPSHIFT_SPEED = 1200;
double DOWNSHIFT_SPEED = 800;

double currentSpeed = (leftDriveEncoder.getVelocity() + rightDriveEncoder.getVelocity()) / 2;

SmartDashboard.putNumber("Avg Speed", currentSpeed);

}
if(currentSpeed >= UPSHIFT_SPEED) {
currentGear = Gear.HIGH;
shiftSolenoid.set(Gear.HIGH.value);
}

if(currentSpeed <= DOWNSHIFT_SPEED) {
currentGear = Gear.LOW;
shiftSolenoid.set(Gear.LOW.value);
}


}
}
Loading