Skip to content

Commit

Permalink
Add a basic waypoint based auto, using 6328s FieldConstants
Browse files Browse the repository at this point in the history
  • Loading branch information
tervay committed Feb 11, 2022
1 parent 1b8b0d7 commit 679b139
Show file tree
Hide file tree
Showing 7 changed files with 333 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/main/java/frc/robot/Constants.java
Expand Up @@ -47,6 +47,13 @@ public static final class DriveConstants {
public static final double gearRatio = 60.0 / 10.0;
public static final double distPerPulse =
(1.0 / gearRatio) * Units.inchesToMeters(wheelDiameter) * Math.PI;

private static final double bumperlessRobotLength = Units.inchesToMeters(26);
private static final double bumperlessRobotWidth = Units.inchesToMeters(24);
private static final double bumperThickness = Units.inchesToMeters(3);

public static final double fullRobotWidth = bumperlessRobotWidth + bumperThickness * 2;
public static final double fullRobotLength = bumperlessRobotLength + bumperThickness * 2;
}

public static final class IntakeConstants {
Expand All @@ -73,7 +80,7 @@ public static final class AutoConstants {
// FF and FB gains; NEED TO BE DETERMINED ON THE FULLY BUILT ROBOT, WILL CHANGE
// WITH WEIGHT
public static final double ksVolts = 0.20541;
public static final double ksVoltSecondsPerMeter = 2.4361;
public static final double kvVoltSecondsPerMeter = 2.4361;
public static final double kaVoltSecondsSquaredPerMeter = 0.25946;

public static final double kPDriveVel = 3.95;
Expand All @@ -83,12 +90,15 @@ public static final class AutoConstants {
public static final DifferentialDriveKinematics kinematics =
new DifferentialDriveKinematics(trackWidth);

public static final double maxCentripetalAcceleration = 100;

// Ramsete constants; generally the same on all robots
public static final double RamseteZeta = 0.7;
public static final double RamseteB = 2;

// Max speeds
public static final double maxSpeed = 8;
public static final double maxAccel = 5;
public static final double maxSpeed = 1;
public static final double maxAccel = 1;
public static final double maxVoltageApplied = 6;
}
}
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/RobotContainer.java
Expand Up @@ -96,7 +96,7 @@ public Command getAutonomousCommand() {
Constants.AutoConstants.RamseteB, Constants.AutoConstants.RamseteZeta),
new SimpleMotorFeedforward(
Constants.AutoConstants.ksVolts,
Constants.AutoConstants.ksVoltSecondsPerMeter,
Constants.AutoConstants.kvVoltSecondsPerMeter,
Constants.AutoConstants.kaVoltSecondsSquaredPerMeter),
Constants.AutoConstants.kinematics,
driveSubsystem::getWheelSpeeds,
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/frc/robot/commands/RamsetA.java
Expand Up @@ -3,18 +3,42 @@
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.controller.RamseteController;
import edu.wpi.first.math.controller.SimpleMotorFeedforward;
import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.trajectory.Trajectory;
import edu.wpi.first.math.trajectory.TrajectoryConfig;
import edu.wpi.first.math.trajectory.TrajectoryGenerator;
import edu.wpi.first.math.trajectory.constraint.CentripetalAccelerationConstraint;
import edu.wpi.first.math.trajectory.constraint.DifferentialDriveVoltageConstraint;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.RamseteCommand;
import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import frc.robot.Constants;
import frc.robot.subsystems.DriveSubsystem;
import java.util.List;

public class RamsetA extends SequentialCommandGroup {

protected Trajectory autoTrajectory;

public static Trajectory makeTrajectory(
double startVelocity, List<Pose2d> waypoints, double endVelocity, boolean reversed) {
CentripetalAccelerationConstraint centripetalAccelerationConstraint =
new CentripetalAccelerationConstraint(Constants.AutoConstants.maxCentripetalAcceleration);
return TrajectoryGenerator.generateTrajectory(
waypoints,
new TrajectoryConfig(Constants.AutoConstants.maxSpeed, Constants.AutoConstants.maxAccel)
.addConstraint(centripetalAccelerationConstraint)
.addConstraint(
new DifferentialDriveVoltageConstraint(
new SimpleMotorFeedforward(
Constants.AutoConstants.ksVolts,
Constants.AutoConstants.kvVoltSecondsPerMeter,
Constants.AutoConstants.kaVoltSecondsSquaredPerMeter),
Constants.AutoConstants.kinematics,
Constants.AutoConstants.maxVoltageApplied)));
}

public static Command RamseteSchmoove(Trajectory autoTrajectory, DriveSubsystem driveSubsystem) {
RamseteCommand ramsete =
new RamseteCommand(
Expand All @@ -24,7 +48,7 @@ public static Command RamseteSchmoove(Trajectory autoTrajectory, DriveSubsystem
Constants.AutoConstants.RamseteB, Constants.AutoConstants.RamseteZeta),
new SimpleMotorFeedforward(
Constants.AutoConstants.ksVolts,
Constants.AutoConstants.ksVoltSecondsPerMeter,
Constants.AutoConstants.kvVoltSecondsPerMeter,
Constants.AutoConstants.kaVoltSecondsSquaredPerMeter),
Constants.AutoConstants.kinematics,
driveSubsystem::getWheelSpeeds,
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/frc/robot/commands/auto/TwoBall.java
@@ -0,0 +1,33 @@
package frc.robot.commands.auto;

import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import frc.robot.commands.DeployIntake;
import frc.robot.commands.RamsetA;
import frc.robot.subsystems.DriveSubsystem;
import frc.robot.subsystems.IntakeFourBar;
import frc.robot.subsystems.IntakeSubsystem;
import frc.robot.util.FieldConstants;
import java.util.List;

public class TwoBall extends SequentialCommandGroup {
public TwoBall(
DriveSubsystem driveSubsystem, IntakeSubsystem intakeSubsystem, IntakeFourBar fourBar) {
addCommands(
sequence(
new DeployIntake(intakeSubsystem, fourBar),
RamsetA.RamseteSchmoove(
RamsetA.makeTrajectory(
0,
List.of(FieldConstants.StartingPoints.tarmacD, FieldConstants.cargoE),
0.0,
false),
driveSubsystem),
RamsetA.RamseteSchmoove(
RamsetA.makeTrajectory(
0.0,
List.of(FieldConstants.cargoE, FieldConstants.StartingPoints.fenderB),
0.0,
false),
driveSubsystem)));
}
}
1 change: 1 addition & 0 deletions src/main/java/frc/robot/subsystems/DriveSubsystem.java
Expand Up @@ -95,6 +95,7 @@ public Pose2d getPose() {

public void resetOdometry(Pose2d pose) {
roboOdometry.resetPosition(pose, gyro.getRotation2d());
resetEncoders();
}

public DifferentialDriveWheelSpeeds getWheelSpeeds() {
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/frc/robot/util/FieldConstants.java
@@ -0,0 +1,136 @@
package frc.robot.util;

import edu.wpi.first.math.geometry.Pose2d;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.util.Units;

public class FieldConstants {
// Field dimensions
public static final double fieldLength = Units.inchesToMeters(54.0 * 12.0);
public static final double fieldWidth = Units.inchesToMeters(27.0 * 12.0);
public static final double hangarLength = Units.inchesToMeters(128.75);
public static final double hangarWidth = Units.inchesToMeters(116.0);

// Vision target
public static final double visionTargetDiameter = Units.inchesToMeters(4.0 * 12.0 + 5.375);

/* Bottom of tape */
public static final double visionTargetHeightLower = Units.inchesToMeters(8.0 * 12 + 5.625);

/* Top of tape */
public static final double visionTargetHeightUpper =
visionTargetHeightLower + Units.inchesToMeters(2.0);

// Dimensions of hub and tarmac
public static final Rotation2d centerLineAngle = Rotation2d.fromDegrees(66.0);
public static final Translation2d hubCenter =
new Translation2d(fieldLength / 2.0, fieldWidth / 2.0);
public static final double tarmacInnerDiameter = Units.inchesToMeters(219.25);
public static final double tarmacOuterDiameter = Units.inchesToMeters(237.31);
public static final double tarmacFenderToTip = Units.inchesToMeters(84.75);

/* If the tarmac formed a full octagon */
public static final double tarmacFullSideLength = tarmacInnerDiameter * (Math.sqrt(2.0) - 1.0);

/* Length of tape marking outside of tarmac */
public static final double tarmacMarkedSideLength = Units.inchesToMeters(82.83);

/* Length removed because of corner cutoff */
public static final double tarmacMissingSideLength =
tarmacFullSideLength - tarmacMarkedSideLength;

public static final double hubSquareLength = tarmacOuterDiameter - (tarmacFenderToTip * 2.0);

// Reference rotations (angle from hub to each reference point and fender side)
public static final Rotation2d referenceARotation =
Rotation2d.fromDegrees(180.0)
.minus(centerLineAngle)
.plus(Rotation2d.fromDegrees(360.0 / 16.0));
public static final Rotation2d referenceBRotation =
referenceARotation.rotateBy(Rotation2d.fromDegrees(360.0 / 8.0));
public static final Rotation2d referenceCRotation =
referenceBRotation.rotateBy(Rotation2d.fromDegrees(360.0 / 8.0));
public static final Rotation2d referenceDRotation =
referenceCRotation.rotateBy(Rotation2d.fromDegrees(360.0 / 8.0));
public static final Rotation2d fenderARotation =
referenceARotation.rotateBy(Rotation2d.fromDegrees(360.0 / 16.0));
public static final Rotation2d fenderBRotation =
fenderARotation.rotateBy(Rotation2d.fromDegrees(90.0));

// Reference points (centered of the sides of the tarmac if they formed a
// complete octagon, plus
// edges of fender)
public static final Pose2d referenceA =
new Pose2d(hubCenter, referenceARotation)
.transformBy(Util.Geometry.transformFromTranslation(tarmacInnerDiameter / 2.0, 0.0));
public static final Pose2d referenceB =
new Pose2d(hubCenter, referenceBRotation)
.transformBy(Util.Geometry.transformFromTranslation(tarmacInnerDiameter / 2.0, 0.0));
public static final Pose2d referenceC =
new Pose2d(hubCenter, referenceCRotation)
.transformBy(Util.Geometry.transformFromTranslation(tarmacInnerDiameter / 2.0, 0.0));
public static final Pose2d referenceD =
new Pose2d(hubCenter, referenceDRotation)
.transformBy(Util.Geometry.transformFromTranslation(tarmacInnerDiameter / 2.0, 0.0));
private static final Pose2d fenderA =
new Pose2d(hubCenter, fenderARotation)
.transformBy(Util.Geometry.transformFromTranslation(hubSquareLength / 2.0, 0.0));
private static final Pose2d fenderB =
new Pose2d(hubCenter, fenderBRotation)
.transformBy(Util.Geometry.transformFromTranslation(hubSquareLength / 2.0, 0.0));

// Cargo points
public static final double cornerToCargoY = Units.inchesToMeters(15.56);
public static final double referenceToCargoY = (tarmacFullSideLength / 2.0) - cornerToCargoY;
public static final double referenceToCargoX = Units.inchesToMeters(40.44);
public static final Pose2d cargoA =
referenceA.transformBy(
Util.Geometry.transformFromTranslation(referenceToCargoX, -referenceToCargoY));
public static final Pose2d cargoB =
referenceA.transformBy(
Util.Geometry.transformFromTranslation(referenceToCargoX, referenceToCargoY));
public static final Pose2d cargoC =
referenceB.transformBy(
Util.Geometry.transformFromTranslation(referenceToCargoX, referenceToCargoY));
public static final Pose2d cargoD =
referenceC.transformBy(
Util.Geometry.transformFromTranslation(referenceToCargoX, -referenceToCargoY));
public static final Pose2d cargoE =
referenceD.transformBy(
Util.Geometry.transformFromTranslation(referenceToCargoX, -referenceToCargoY));
public static final Pose2d cargoF =
referenceD.transformBy(
Util.Geometry.transformFromTranslation(referenceToCargoX, referenceToCargoY));

// Terminal cargo point
public static final Rotation2d terminalOuterRotation = Rotation2d.fromDegrees(133.75);
public static final double terminalLength = Units.inchesToMeters(324.0 - 256.42);
public static final double terminalWidth =
Math.tan(Rotation2d.fromDegrees(180.0).minus(terminalOuterRotation).getRadians())
* terminalLength;
public static final Pose2d terminalCenter =
new Pose2d(
new Translation2d(terminalLength / 2.0, terminalWidth / 2.0),
terminalOuterRotation.minus(Rotation2d.fromDegrees(90.0)));
public static final double terminalCargoOffset = Units.inchesToMeters(10.43);
public static final Pose2d cargoG =
terminalCenter.transformBy(Util.Geometry.transformFromTranslation(terminalCargoOffset, 0.0));

// Starting points
public static class StartingPoints {
public static final Pose2d tarmacA =
referenceA.transformBy(Util.Geometry.transformFromTranslation(-0.5, 0.7));
public static final Pose2d tarmacB =
referenceB.transformBy(Util.Geometry.transformFromTranslation(-0.5, -0.2));
public static final Pose2d tarmacC =
referenceC.transformBy(Util.Geometry.transformFromTranslation(-0.5, -0.1));
public static final Pose2d tarmacD =
referenceD.transformBy(Util.Geometry.transformFromTranslation(-0.5, -0.7));

public static final Pose2d fenderA =
FieldConstants.fenderA.transformBy(Util.Geometry.transformFromTranslation(0.5, 0.0));
public static final Pose2d fenderB =
Util.Geometry.offsetDrivetrainFromPose(FieldConstants.fenderB);
}
}

0 comments on commit 679b139

Please sign in to comment.