Skip to content

Commit

Permalink
made a ton of progress on vision subsystem, requires some refining an…
Browse files Browse the repository at this point in the history
…d testing
  • Loading branch information
1e-ipi committed Feb 21, 2024
1 parent 8660b58 commit a1baa88
Show file tree
Hide file tree
Showing 11 changed files with 849 additions and 603 deletions.
140 changes: 70 additions & 70 deletions src/main/java/frc/robot/AutonomousManager.java
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
package frc.robot;
// package frc.robot;

import com.choreo.lib.Choreo;
import com.choreo.lib.ChoreoTrajectory;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.math.geometry.Translation2d;
import edu.wpi.first.math.kinematics.ChassisSpeeds;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import frc.robot.subsystems.SwerveSubsystem;
// import com.choreo.lib.Choreo;
// import com.choreo.lib.ChoreoTrajectory;
// import edu.wpi.first.math.controller.PIDController;
// import edu.wpi.first.math.geometry.Translation2d;
// import edu.wpi.first.math.kinematics.ChassisSpeeds;
// import edu.wpi.first.wpilibj.DriverStation;
// import edu.wpi.first.wpilibj2.command.Command;
// import edu.wpi.first.wpilibj2.command.Commands;
// import frc.robot.subsystems.SwerveSubsystem;

import java.util.Optional;
// import java.util.Optional;

// autonomous path planning this year relies on Choreo
// download planning app from here: https://github.com/SleipnirGroup/Choreo/releases/latest
// it is similar to pathplanner but the splines are automatically
// configured for the most efficient route while avoiding obstacles
// // autonomous path planning this year relies on Choreo
// // download planning app from here: https://github.com/SleipnirGroup/Choreo/releases/latest
// // it is similar to pathplanner but the splines are automatically
// // configured for the most efficient route while avoiding obstacles

public class AutonomousManager {
private final SwerveSubsystem SwerveSubsystem;
private ChoreoTrajectory trajectory;
// public class AutonomousManager {
// private final SwerveSubsystem SwerveSubsystem;
// private ChoreoTrajectory trajectory;

public AutonomousManager(SwerveSubsystem subsystem) {
SwerveSubsystem = subsystem;
}
// public AutonomousManager(SwerveSubsystem subsystem) {
// SwerveSubsystem = subsystem;
// }

public Command getCommand() {
trajectory = Choreo.getTrajectory(Constants204.Autonomous.CHOREO_PATH_FILE);
// public Command getCommand() {
// trajectory = Choreo.getTrajectory(Constants204.Autonomous.CHOREO_PATH_FILE);

var thetaController = new PIDController(Constants.AutoConstants.kPThetaController, 0, 0);
thetaController.enableContinuousInput(-Math.PI, Math.PI);
// var thetaController = new PIDController(Constants.AutoConstants.kPThetaController, 0, 0);
// thetaController.enableContinuousInput(-Math.PI, Math.PI);

SwerveSubsystem.resetOdometry(trajectory.getInitialPose());
// SwerveSubsystem.resetOdometry(trajectory.getInitialPose());

Command swerveCommand = Choreo.choreoSwerveCommand(
trajectory, // Choreo trajectory from above
SwerveSubsystem::getPose, // A function that returns the current field-relative pose of the robot: your
// wheel or vision odometry
new PIDController(Constants.AutoConstants.kPXController, 0.0, 0.005), // PIDController for field-relative X
// translation (input: X error in meters,
// output: m/s).
new PIDController(Constants.AutoConstants.kPYController, 0.0, 0.005), // PIDController for field-relative Y
// translation (input: Y error in meters,
// output: m/s).
thetaController, // PID constants to correct for rotation
// error
/*(ChassisSpeeds speeds) -> SwerveSubsystem.drive( // needs to be robot-relative
speeds.vxMetersPerSecond,
speeds.vyMetersPerSecond,
speeds.omegaRadiansPerSecond,
false),*/
(ChassisSpeeds speeds) -> SwerveSubsystem.drive(
new Translation2d(speeds.vyMetersPerSecond, speeds.vxMetersPerSecond), // x and y are reversed somewhere else in the code
speeds.omegaRadiansPerSecond,
true, // robot oriented swerve
true),
this::isRedAlliance, // Whether or not to mirror the path based on alliance (this assumes the path is created for the blue alliance)
SwerveSubsystem // The subsystem(s) to require, typically your drive subsystem only
);
// Command swerveCommand = Choreo.choreoSwerveCommand(
// trajectory, // Choreo trajectory from above
// SwerveSubsystem::getPose, // A function that returns the current field-relative pose of the robot: your
// // wheel or vision odometry
// new PIDController(Constants.AutoConstants.kPXController, 0.0, 0.005), // PIDController for field-relative X
// // translation (input: X error in meters,
// // output: m/s).
// new PIDController(Constants.AutoConstants.kPYController, 0.0, 0.005), // PIDController for field-relative Y
// // translation (input: Y error in meters,
// // output: m/s).
// thetaController, // PID constants to correct for rotation
// // error
// /*(ChassisSpeeds speeds) -> SwerveSubsystem.drive( // needs to be robot-relative
// speeds.vxMetersPerSecond,
// speeds.vyMetersPerSecond,
// speeds.omegaRadiansPerSecond,
// false),*/
// (ChassisSpeeds speeds) -> SwerveSubsystem.drive(
// new Translation2d(speeds.vyMetersPerSecond, speeds.vxMetersPerSecond), // x and y are reversed somewhere else in the code
// speeds.omegaRadiansPerSecond,
// true, // robot oriented swerve
// true),
// this::isRedAlliance, // Whether or not to mirror the path based on alliance (this assumes the path is created for the blue alliance)
// SwerveSubsystem // The subsystem(s) to require, typically your drive subsystem only
// );

return Commands.sequence(
Commands.runOnce(() -> SwerveSubsystem.resetOdometry(trajectory.getInitialPose())),
swerveCommand,
SwerveSubsystem.run(() -> SwerveSubsystem.drive(new Translation2d(0, 0), 0, false, false))
);
}
// return Commands.sequence(
// Commands.runOnce(() -> SwerveSubsystem.resetOdometry(trajectory.getInitialPose())),
// swerveCommand,
// SwerveSubsystem.run(() -> SwerveSubsystem.drive(new Translation2d(0, 0), 0, false, false))
// );
// }

private boolean isRedAlliance() {
Optional<DriverStation.Alliance> alliance = DriverStation.getAlliance();
if (alliance.isPresent()) {
DriverStation.Alliance a = alliance.get();
if (a == DriverStation.Alliance.Red) {
return true;
} else if (a == DriverStation.Alliance.Blue) {
return false;
}
}
return false;
}
}
// private boolean isRedAlliance() {
// Optional<DriverStation.Alliance> alliance = DriverStation.getAlliance();
// if (alliance.isPresent()) {
// DriverStation.Alliance a = alliance.get();
// if (a == DriverStation.Alliance.Red) {
// return true;
// } else if (a == DriverStation.Alliance.Blue) {
// return false;
// }
// }
// return false;
// }
// }
108 changes: 54 additions & 54 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public static final class Shooter {
/* Motor Inverts */
public static final boolean driveInvert = true;

public static final int shooterID_1 = 22;
public static final int shooterID_2 = 25;
public static final int bumpID = 26;
// public static final int shooterID_1 = 22;
// public static final int shooterID_2 = 25;
// public static final int bumpID = 26;


/* Swerve Current Limiting */
Expand Down Expand Up @@ -139,65 +139,65 @@ public static final class Swerve {

/* Module Specific Constants */
/* Front Left Module - Module 0 */
public static final class Mod0 {
public static final int driveMotorID = 2;
public static final int angleMotorID = 9;
//public static final int canCoderID = 4;
//public static final Rotation2d angleOffset = Rotation2d.fromDegrees(42.58);
public static final Rotation2d angleOffset = Rotation2d.fromDegrees(62.99);
public static final SwerveModuleConstants constants =
new SwerveModuleConstants(driveMotorID, angleMotorID, angleOffset);
}
// public static final class Mod0 {
// // public static final int driveMotorID = 2;
// // public static final int angleMotorID = 9;
// //public static final int canCoderID = 4;
// //public static final Rotation2d angleOffset = Rotation2d.fromDegrees(42.58);
// public static final Rotation2d angleOffset = Rotation2d.fromDegrees(62.99);
// public static final SwerveModuleConstants constants =
// new SwerveModuleConstants(driveMotorID, angleMotorID, angleOffset);
// }

/* Front Right Module - Module 1 */
public static final class Mod1 {
public static final int driveMotorID = 4;
public static final int angleMotorID = 3;
//public static final int canCoderID = 1;
//public static final Rotation2d angleOffset = Rotation2d.fromDegrees(296.67);
public static final Rotation2d angleOffset = Rotation2d.fromDegrees(312.58);
public static final SwerveModuleConstants constants =
new SwerveModuleConstants(driveMotorID, angleMotorID, angleOffset);
}
// public static final class Mod1 {
// // public static final int driveMotorID = 4;
// // public static final int angleMotorID = 3;
// //public static final int canCoderID = 1;
// //public static final Rotation2d angleOffset = Rotation2d.fromDegrees(296.67);
// public static final Rotation2d angleOffset = Rotation2d.fromDegrees(312.58);
// public static final SwerveModuleConstants constants =
// new SwerveModuleConstants(driveMotorID, angleMotorID, angleOffset);
// }

/* Back Left Module - Module 2 */
public static final class Mod2 {
public static final int driveMotorID = 8;
public static final int angleMotorID = 7;
//public static final int canCoderID = 3;
public static final Rotation2d angleOffset = Rotation2d.fromDegrees(470.5);
//public static final Rotation2d angleOffset = Rotation2d.fromDegrees(204.7);
public static final SwerveModuleConstants constants =
new SwerveModuleConstants(driveMotorID, angleMotorID, angleOffset);
}
// public static final class Mod2 {
// public static final int driveMotorID = 8;
// public static final int angleMotorID = 7;
// //public static final int canCoderID = 3;
// public static final Rotation2d angleOffset = Rotation2d.fromDegrees(470.5);
// //public static final Rotation2d angleOffset = Rotation2d.fromDegrees(204.7);
// public static final SwerveModuleConstants constants =
// new SwerveModuleConstants(driveMotorID, angleMotorID, angleOffset);
// }

/* Back Right Module - Module 3 */
public static final class Mod3 {
public static final int driveMotorID = 6;
public static final int angleMotorID = 5;
//public static final int canCoderID = 2;
public static final Rotation2d angleOffset = Rotation2d.fromDegrees(296.67);
//public static final Rotation2d angleOffset = Rotation2d.fromDegrees(9.2);
public static final SwerveModuleConstants constants =
new SwerveModuleConstants(driveMotorID, angleMotorID, angleOffset);
}
// public static final class Mod3 {
// public static final int driveMotorID = 6;
// public static final int angleMotorID = 5;
// //public static final int canCoderID = 2;
// public static final Rotation2d angleOffset = Rotation2d.fromDegrees(296.67);
// //public static final Rotation2d angleOffset = Rotation2d.fromDegrees(9.2);
// public static final SwerveModuleConstants constants =
// new SwerveModuleConstants(driveMotorID, angleMotorID, angleOffset);
// }
}

public static final class AutoConstants {
public static final double kMaxSpeedMetersPerSecond = 4; // 4
public static final double kMaxAccelerationMetersPerSecondSquared = 3; // 3
public static final double kMaxAngularSpeedRadiansPerSecond = 2 * Math.PI;
public static final double kMaxAngularSpeedRadiansPerSecondSquared = 2 * Math.PI;

public static final double kPXController = 0.3;
public static final double kPYController = 0.3;
public static final double kPThetaController = 0.3;

// Constraint for the motion profilied robot angle controller
public static final TrapezoidProfile.Constraints kThetaControllerConstraints =
new TrapezoidProfile.Constraints(
kMaxAngularSpeedRadiansPerSecond, kMaxAngularSpeedRadiansPerSecondSquared);
}
// public static final class AutoConstants {
// public static final double kMaxSpeedMetersPerSecond = 4; // 4
// public static final double kMaxAccelerationMetersPerSecondSquared = 3; // 3
// public static final double kMaxAngularSpeedRadiansPerSecond = 2 * Math.PI;
// public static final double kMaxAngularSpeedRadiansPerSecondSquared = 2 * Math.PI;

// public static final double kPXController = 0.3;
// public static final double kPYController = 0.3;
// public static final double kPThetaController = 0.3;

// // Constraint for the motion profilied robot angle controller
// public static final TrapezoidProfile.Constraints kThetaControllerConstraints =
// new TrapezoidProfile.Constraints(
// kMaxAngularSpeedRadiansPerSecond, kMaxAngularSpeedRadiansPerSecondSquared);
// }

}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void autonomousInit() {
teleopCommand.cancel();
}

autonomousCommand = robotContainer.getAutonomousCommand();
// autonomousCommand = robotContainer.getAutonomousCommand();

// schedule the autonomous command (example)
if (autonomousCommand != null) {
Expand Down
Loading

0 comments on commit a1baa88

Please sign in to comment.