Skip to content

Commit

Permalink
Two
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachOrr committed Feb 18, 2022
1 parent e7e571a commit 7c36e22
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 119 deletions.
8 changes: 6 additions & 2 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.I2C;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
Expand Down Expand Up @@ -40,10 +41,13 @@ public class RobotContainer {
private final AutoDrive autoDrive = new AutoDrive();
// private final Delivery delivery = new Delivery();
private final Drivetrain drivetrain = new Drivetrain(pigeon);
private final Heading heading = new Heading(drivetrain::getGyroscopeRotation);
private final Heading heading = new Heading(drivetrain::getGyroscopeRotation, drivetrain::getChassisSpeeds);

private final SendableChooser<Command> autonChooser = new SendableChooser<>();

// private final ColorSensorREV c1 = new ColorSensorREV(I2C.Port.kOnboard);
private final ColorSensorTCS c2 = new ColorSensorTCS(I2C.Port.kMXP);

public RobotContainer() {
drivetrain.setDefaultCommand(new SwerveDriveCommand(driverController, autoDrive, heading, drivetrain));
heading.setDefaultCommand(new HeadingToTargetCommand(() -> drivetrain.getPose().getTranslation(), heading));
Expand All @@ -59,7 +63,7 @@ public RobotContainer() {
public void resetRobot() {
pigeon.setYaw(0, 250);
drivetrain.resetPosition(new Pose2d(
Constants.Auto.kBall2.toFieldCoordinate(),
Constants.Auto.kBallR2.toFieldCoordinate(),
drivetrain.getGyroscopeRotation()
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.littletonrobotics.junction.Logger;

import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.controller.ProfiledPIDController;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.trajectory.TrapezoidProfile;
Expand All @@ -26,7 +27,9 @@ public class DistanceToTargetCommand extends HeadingToTargetCommand implements A
private double distanceMeters;
private AutoDrive autoDrive;

private ProfiledPIDController distanceController;
private TrapezoidProfile profile;
// private ProfiledPIDController distanceController;
private PIDController distanceController;
private double output = 0.0;

public DistanceToTargetCommand(double distanceMeters, Supplier<Translation2d> translationSupplier, AutoDrive autoDrive, Heading heading) {
Expand All @@ -39,13 +42,16 @@ public DistanceToTargetCommand(Translation2d targetMeters, double distanceMeters
this.distanceMeters = distanceMeters;
this.autoDrive = autoDrive;

distanceController = new PIDController(1.5, 0.0, 0.0);
/*
distanceController = new ProfiledPIDController(1.5, 0.0, 0.0,
// Speeds have been tuned for 143" move (Ball 2 -> 10in from Hub)
new TrapezoidProfile.Constraints(
Units.inchesToMeters(160),
Units.inchesToMeters(80)
Units.inchesToMeters(50)
)
);
*/
// Tolerance for our distance is within ~2 inches (should be Good Enough™️)
distanceController.setTolerance(Units.inchesToMeters(2));

Expand All @@ -58,11 +64,22 @@ public void initialize() {

autoDrive.setDelegate(this);

// Set our initial setpoint for our profiled PID controllers
// to avoid a JUMP to their starting values on first run
// Set our initial setpoint for our profile to avoid
// a JUMP to their starting values on first run
PolarCoordinate robotCoordinate = getRobotCoordinate();
distanceController.reset(
robotCoordinate.getRadiusMeters()
profile = new TrapezoidProfile(
new TrapezoidProfile.Constraints(
Units.inchesToMeters(160),
Units.inchesToMeters(60)
),
new TrapezoidProfile.State(
distanceMeters,
0
),
new TrapezoidProfile.State(
robotCoordinate.getRadiusMeters(),
0
)
);
}

Expand All @@ -71,15 +88,23 @@ public void execute() {
super.execute();

PolarCoordinate robotCoordinate = getRobotCoordinate();
var m_setpoint = profile.calculate(distanceController.getPeriod());
output = distanceController.calculate(
robotCoordinate.getRadiusMeters(),
distanceMeters
m_setpoint.position
);

Logger.getInstance().recordOutput("Flare/output", output);
Logger.getInstance().recordOutput("Flare/Error", distanceController.getPositionError());
Logger.getInstance().recordOutput("Flare/Velocity Error", distanceController.getVelocityError());
/*
Logger.getInstance().recordOutput("Flare/atTarget", distanceController.atGoal());
Logger.getInstance().recordOutput("Flare/Robot Distance", robotCoordinate.getRadiusMeters());
Logger.getInstance().recordOutput("Flare/Goal Position", distanceController.getGoal().position);
Logger.getInstance().recordOutput("Flare/Goal Velocity", distanceController.getGoal().velocity);
Logger.getInstance().recordOutput("Flare/Setpoint Position", distanceController.getSetpoint().position);
Logger.getInstance().recordOutput("Flare/Setpoint Velocity", distanceController.getSetpoint().velocity);
*/

// Clamp to some max speed (should be between [0.0, 1.0])
final double maxSpeed = 1.0;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/frc/robot/subsystems/Drivetrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ public void drive(ChassisSpeeds chassisSpeeds) {
this.chassisSpeeds = chassisSpeeds;
}

public ChassisSpeeds getChassisSpeeds() {
return chassisSpeeds;
}

public void resetOdometry() {
odometry.resetPosition(new Pose2d(0, 0, Rotation2d.fromDegrees(0)), Rotation2d.fromDegrees(0));
}
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/frc/robot/subsystems/Heading.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import edu.wpi.first.math.MathUtil;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.nerdyfiles.utilities.Utilities;

Expand All @@ -29,13 +30,16 @@ public class Heading extends SubsystemBase {
* NOTE: Currently disabled by default as not to cause confusion when
* we're testing things on the robot.
*/
private boolean enabled = false;
private boolean enabled = true;

/**
* Supplier to provide the gyro angle of the robot. Should come from
* the gyro on the Drivetrain. Used to calculate our maintain heading calculation.
*/
private Supplier<Rotation2d> gyroAngleSupplier;

private Supplier<ChassisSpeeds> chassisSpeedsSupplier;

/**
* The maintain heading is the heading we would like the robot to maintain.
* Can be null if the robot should not maintain any specific heading.
Expand All @@ -52,16 +56,20 @@ public class Heading extends SubsystemBase {
/**
* PID used to converge the robot to the maintainHeading from it's current heading.
*/
private PIDController rotationController = new PIDController(0.01, 0.0, 0.0);
private PIDController rotationController = new PIDController(0.004, 0.0, 0.0);
private static double kFeedForward = 0.015;
private static double kNominalStationaryOutput = 0.03;
private static double kNominalMovingOutput = 0.02;

/**
* Heading subsystem to maintain a static heading of the robot.
*
* @param gyroAngleSupplier A Supplier to provide the gyro value of the robot.
* Should come from the Pigeon.
*/
public Heading(Supplier<Rotation2d> gyroAngleSupplier) {
public Heading(Supplier<Rotation2d> gyroAngleSupplier, Supplier<ChassisSpeeds> chassisSpeedsSupplier) {
this.gyroAngleSupplier = gyroAngleSupplier;
this.chassisSpeedsSupplier = chassisSpeedsSupplier;

rotationController.enableContinuousInput(-180, 180);
// +/- 1 degree position error tolerance
Expand Down Expand Up @@ -201,15 +209,21 @@ public double calculateRotation() {
currentHeading.getDegrees(),
maintainHeading.getDegrees()
);
Logger.getInstance().recordOutput("Heading/Rotation Controller Output", output);
if (rotationController.atSetpoint()) {
return 0.0;
}
// Clamp to some max speed (should be between [0.0, 1.0])
final double maxSpeed = 0.3;
double clamedOutput = MathUtil.clamp(
double clampedOutput = MathUtil.clamp(
output,
-maxSpeed,
maxSpeed
);
return clamedOutput;
);
ChassisSpeeds chassisSpeeds = chassisSpeedsSupplier.get();
double velocity = Math.hypot(chassisSpeeds.vxMetersPerSecond, chassisSpeeds.vyMetersPerSecond);
double adjustedClampedOutput = Math.copySign(Math.max(Math.abs(clampedOutput), velocity > 0 ? kNominalMovingOutput : kNominalStationaryOutput), clampedOutput);
Logger.getInstance().recordOutput("Heading/Rotation Controller Output", adjustedClampedOutput);
return adjustedClampedOutput;
}

/**
Expand Down
Loading

0 comments on commit 7c36e22

Please sign in to comment.