Skip to content

Commit

Permalink
Fixed merge conflicts for pr
Browse files Browse the repository at this point in the history
Merge branch 'main' of https://github.com/StuyPulse/BigWang into se/swerve-commands
  • Loading branch information
Keobkeig committed Jan 28, 2024
1 parent 7476a2d commit 25bf3d9
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 33 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/stuypulse/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@
import com.stuypulse.stuylib.input.gamepads.AutoGamepad;
import com.stuypulse.stuylib.input.gamepads.Xbox;

import com.stuypulse.robot.subsystems.climber.*;

import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;

public class RobotContainer {

// Gamepads
public final Gamepad driver = new Xbox(Ports.Gamepad.DRIVER);
public final Gamepad driver = new AutoGamepad(Ports.Gamepad.DRIVER);
public final Gamepad operator = new AutoGamepad(Ports.Gamepad.OPERATOR);

// Subsystem
public final Climber climber = Climber.getInstance();
public final Amper amper = Amper.getInstance();
public final SwerveDrive swerve = SwerveDrive.getInstance();
public final Odometry odometry = Odometry.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import com.stuypulse.robot.constants.Settings;
import com.stuypulse.robot.constants.Settings.Driver.Drive;
import com.stuypulse.robot.constants.Settings.Driver.Turn;
import com.stuypulse.robot.constants.Settings.Driver.Turn.GyroFeedback;
import com.stuypulse.robot.subsystems.swerve.SwerveDrive;
import com.stuypulse.stuylib.control.angle.AngleController;
import com.stuypulse.stuylib.input.Gamepad;
import com.stuypulse.stuylib.math.Angle;
import com.stuypulse.stuylib.math.SLMath;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
package com.stuypulse.robot.commands.swerve;

import com.stuypulse.robot.constants.Field;
import com.stuypulse.robot.constants.Settings;
import com.stuypulse.robot.constants.Settings.Alignment;
import com.stuypulse.robot.constants.Settings.Alignment.Rotation;
import com.stuypulse.robot.constants.Settings.Alignment.Translation;
import com.stuypulse.robot.subsystems.odometry.Odometry;
import com.stuypulse.robot.subsystems.swerve.SwerveDrive;
import com.stuypulse.robot.util.Fiducial;
import com.stuypulse.robot.util.HolonomicController;
import com.stuypulse.stuylib.control.angle.feedback.AnglePIDController;
import com.stuypulse.stuylib.control.feedback.PIDController;
import com.stuypulse.stuylib.math.Angle;
import com.stuypulse.stuylib.math.Vector2D;

import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Pose3d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Rotation3d;
import edu.wpi.first.math.geometry.Translation3d;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.smartdashboard.FieldObject2d;
import edu.wpi.first.wpilibj2.command.Command;

public class SwerveDriveToShoot extends Command {
/*
* swerve
* holonomi controller
* targetpose pose2d
* targetPose2d as a field object 2d
*
* contrusctor (init them, add requirements)
* initialize (do the math to get the target pose)
* execute (set the target pose to the controller and then setChassisSpeeds to it)
* isFinished (check if the controller is done within the tolerances)
* end (stop the swerve)
*
*/

private final SwerveDrive swerve;
private Pose2d targetPose;
private final HolonomicController controller;
Expand All @@ -62,13 +44,12 @@ private Pose2d getSpeakerTargetPose() {
Vector2D speakerCenterVec = new Vector2D(Field.getAllianceSpeakerPose().getTranslation());
Vector2D robotVec = new Vector2D(Odometry.getInstance().getPose().getTranslation());

double speakerOpeningX = Units.inchesToMeters(13.6);
// the distances between the robot and the target
double Dx = speakerCenterVec.x - robotVec.x;
double Dy = speakerCenterVec.y - robotVec.y;

// the offset of the speakers opening width from center using similar triangles
double dy = (Dy / Dx) * speakerOpeningX;
double dy = (Dy / Dx) * Field.SPEAKER_OPENING_X;

// gets the new speaker target vector to aim at
Vector2D speakerTargetVec= new Vector2D(Field.getAllianceSpeakerPose().getX(), Field.getAllianceSpeakerPose().getY() + dy);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.stuypulse.robot.commands.swerve;

import com.stuypulse.robot.constants.Settings.Swerve.BackLeft;
import com.stuypulse.robot.constants.Settings.Swerve.BackRight;
import com.stuypulse.robot.constants.Settings.Swerve.FrontLeft;
import com.stuypulse.robot.constants.Settings.Swerve.FrontRight;
import com.stuypulse.robot.subsystems.swerve.SwerveDrive;
import com.stuypulse.robot.subsystems.swerve.modules.SimModule;

import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.SwerveModuleState;
Expand All @@ -22,6 +17,7 @@ public SwerveDriveXMode() {
@Override
public void execute() {
SwerveModuleState[] states = new SwerveModuleState[] {
//{front right, front left, back right, back left}
new SwerveModuleState(0,Rotation2d.fromDegrees(225)),
new SwerveModuleState(0,Rotation2d.fromDegrees(315)),
new SwerveModuleState(0,Rotation2d.fromDegrees(45)),
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/stuypulse/robot/constants/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ public static Pose2d getAllianceSpeakerPose() {
boolean isBlue = DriverStation.getAlliance().isPresent() && DriverStation.getAlliance().get() == DriverStation.Alliance.Blue;
return SPEAKER_POSES[isBlue ? 0 : 1];
}

double SPEAKER_OPENING_X = Units.inchesToMeters(13.6);
}
5 changes: 5 additions & 0 deletions src/main/java/com/stuypulse/robot/constants/Motors.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public interface Conveyor {
CANSparkMaxConfig SHOOTER_FEEDER_MOTOR = new CANSparkMaxConfig(false, IdleMode.kCoast);
}

public interface Climber {
CANSparkMaxConfig LEFT_MOTOR = new CANSparkMaxConfig(false, IdleMode.kBrake, 80);
CANSparkMaxConfig RIGHT_MOTOR = new CANSparkMaxConfig(false, IdleMode.kBrake, 80);
}

public static class TalonSRXConfig {
public final boolean INVERTED;
public final NeutralMode NEUTRAL_MODE;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/stuypulse/robot/constants/Ports.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ public interface Gamepad {
int DEBUGGER = 2;
}

public interface Climber {
int LEFT_MOTOR = 60;
int RIGHT_MOTOR = 61;

int TOP_RIGHT_LIMIT = 8;
int TOP_LEFT_LIMIT = 7;
int BOTTOM_RIGHT_LIMIT = 6;
int BOTTOM_LEFT_LIMIT = 5;
}

public interface Amper {
int SCORE = 31;
int LIFT = 30;
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/stuypulse/robot/constants/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,23 @@
public interface Settings {

double DT = 1.0 / 50.0;


public interface Climber {
double MIN_HEIGHT = 0.0;
double MAX_HEIGHT = 0.0;

double AT_HEIGHT_THRESHOLD = Units.inchesToMeters(1);

double BANGBANG_VOLTAGE = 8;

public interface Encoder {
double GEAR_RATIO = 0.0;

double POSITION_CONVERSION = 0.0;
double VELOCITY_CONVERSION = POSITION_CONVERSION / 60.0;
}
}

public interface Amper {
public interface Score {
SmartNumber ROLLER_SPEED = new SmartNumber("Amper/Score/Roller Speed", 1.0); // change later
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/stuypulse/robot/subsystems/climber/Climber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.stuypulse.robot.subsystems.climber;

import com.stuypulse.stuylib.network.SmartNumber;

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

public abstract class Climber extends SubsystemBase {
private static final Climber instance;

private final SmartNumber targetHeight;

static {
instance = new ClimberImpl();
}

public static Climber getInstance() {
return instance;
}

public Climber() {
targetHeight = new SmartNumber("Climber/Target Height", 0.0);
}

public void setTargetHeight(double height) {
targetHeight.set(height);
}

public double getTargetHeight() {
return targetHeight.get();
}

public abstract double getHeight();
public abstract double getVelocity();

public abstract void setVoltage(double voltage);

public abstract boolean atTop();
public abstract boolean atBottom();
}
105 changes: 105 additions & 0 deletions src/main/java/com/stuypulse/robot/subsystems/climber/ClimberImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.stuypulse.robot.subsystems.climber;

import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkLowLevel.MotorType;
import com.revrobotics.RelativeEncoder;

import com.stuypulse.robot.constants.Motors;
import com.stuypulse.robot.constants.Ports;
import com.stuypulse.robot.constants.Settings;

import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class ClimberImpl extends Climber {
private final CANSparkMax rightMotor;
private final CANSparkMax leftMotor;

private final RelativeEncoder rightEncoder;
private final RelativeEncoder leftEncoder;

private final DigitalInput topRightLimit;
private final DigitalInput topLeftLimit;
private final DigitalInput bottomRightLimit;
private final DigitalInput bottomLeftLimit;

protected ClimberImpl() {
rightMotor = new CANSparkMax(Ports.Climber.RIGHT_MOTOR, MotorType.kBrushless);
leftMotor = new CANSparkMax(Ports.Climber.LEFT_MOTOR, MotorType.kBrushless);

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

rightEncoder.setPositionConversionFactor(Settings.Climber.Encoder.POSITION_CONVERSION);
leftEncoder.setPositionConversionFactor(Settings.Climber.Encoder.POSITION_CONVERSION);

rightEncoder.setVelocityConversionFactor(Settings.Climber.Encoder.VELOCITY_CONVERSION);
leftEncoder.setVelocityConversionFactor(Settings.Climber.Encoder.VELOCITY_CONVERSION);

topRightLimit = new DigitalInput(Ports.Climber.TOP_RIGHT_LIMIT);
topLeftLimit = new DigitalInput(Ports.Climber.TOP_LEFT_LIMIT);
bottomRightLimit = new DigitalInput(Ports.Climber.BOTTOM_RIGHT_LIMIT);
bottomLeftLimit = new DigitalInput(Ports.Climber.BOTTOM_LEFT_LIMIT);

Motors.Climber.LEFT_MOTOR.configure(leftMotor);
Motors.Climber.RIGHT_MOTOR.configure(rightMotor);
}

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

@Override
public double getVelocity() {
return (leftEncoder.getVelocity() + rightEncoder.getVelocity()) / 2;
}

@Override
public boolean atTop() {
return !topRightLimit.get() || !topLeftLimit.get();
}

@Override
public boolean atBottom() {
return !bottomRightLimit.get() || !bottomLeftLimit.get();
}

@Override
public void setVoltage(double voltage) {
if (atTop() && voltage > 0) {
DriverStation.reportWarning("Climber Top Limit Reached", false);
voltage = 0.0;
} else if (atBottom() && voltage < 0) {
DriverStation.reportWarning("Climber Bottom Limit Reached", false);
voltage = 0.0;
}

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

@Override
public void periodic() {
super.periodic();

if (Math.abs(getHeight() - getTargetHeight()) < Settings.Climber.AT_HEIGHT_THRESHOLD) {
setVoltage(0.0);
} else if (getHeight() > getTargetHeight()) {
setVoltage(-Settings.Climber.BANGBANG_VOLTAGE);
} else {
setVoltage(+Settings.Climber.BANGBANG_VOLTAGE);
}

SmartDashboard.putNumber("Climber/Target Height", getTargetHeight());
SmartDashboard.putNumber("Climber/Height", getHeight());

SmartDashboard.putNumber("Climber/Velocity", getVelocity());

if (atBottom()) {
leftEncoder.setPosition(Settings.Climber.MIN_HEIGHT);
rightEncoder.setPosition(Settings.Climber.MIN_HEIGHT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public Translation2d[] getModuleOffsets() {
}

public ChassisSpeeds getChassisSpeeds() {
return getKinematics().toChassisSpeeds(getModuleStates());
return kinematics.toChassisSpeeds(getModuleStates());
}

/** Setters **/
Expand Down Expand Up @@ -266,7 +266,6 @@ public void periodic() {

public void simulationPeriodic() {
//show gyro angle in simulation
ChassisSpeeds speeds = getChassisSpeeds();
gyro.setAngleAdjustment(gyro.getAngleAdjustment()- Math.toDegrees(speeds.omegaRadiansPerSecond * Settings.DT));
gyro.setAngleAdjustment(gyro.getAngle() - Math.toDegrees(getChassisSpeeds().omegaRadiansPerSecond * Settings.DT));
}
}

0 comments on commit 25bf3d9

Please sign in to comment.