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 factories #55

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5539322
woah these are actually kinda cool
Jan 23, 2025
7c3fb18
added FunctionalCommand, StartEndCommand, and RunOnceCommand examples
Jan 23, 2025
43cc9b3
added example triggers and a bit more stuff about command factories
Jan 24, 2025
91debed
moved auto stuff to robot.java, made it better(used choreo's autoChoo…
Jan 25, 2025
f0f9afd
vscode.dev changes so they r very jank
aridavidson001 Jan 27, 2025
293ee0e
made some changes
Jan 31, 2025
6d74f63
amazing cool new autos.java file yay
Feb 3, 2025
c513ba6
merge conflicts
Feb 3, 2025
6ed584f
cool
Feb 3, 2025
de943d6
starter paths for now
Feb 3, 2025
487b5ea
More paths but J heading is very wrong needs fixing
Feb 3, 2025
828ffe3
removed example auto
aridavidson001 Feb 4, 2025
0c93dee
Formatting fixes
github-actions[bot] Feb 4, 2025
fa2c140
use pose estimation for trajectories (#71)
Ishan1522 Feb 4, 2025
e8a10b2
w fixes
aridavidson001 Feb 4, 2025
e495961
added a constructor for Autos(), fixed some smaller things
aridavidson001 Feb 4, 2025
9cabc9a
oui oui fixed the constructor
Feb 4, 2025
2f7cf56
Created an AutoConstants class and moved TrajectoryConstants into it.…
aridavidson001 Feb 5, 2025
14358d9
removed example directory
aridavidson001 Feb 5, 2025
250138a
Formatting fixes
github-actions[bot] Feb 5, 2025
13c5624
actual computer changes yay
Feb 5, 2025
b2dcbdb
Merge branch 'flexiautos' of https://github.com/TitaniumTigers4829/ro…
Feb 5, 2025
6ad781a
renamed AutoPaths folder to Trajectories
Feb 5, 2025
c37e0d1
fixed string path in constants for trajectories
Feb 5, 2025
16de4ef
fix
Ishan1522 Feb 5, 2025
1e56d2e
one meter path exists
Feb 6, 2025
fa7dd55
Merge branch 'auto-factories' of https://github.com/TitaniumTigers482…
Feb 6, 2025
18d7a1d
deleted unused imports
Feb 6, 2025
3aa5a1d
added one meter test auto
Feb 6, 2025
340a39b
R
Feb 6, 2025
9f8a437
Merge branch 'main' of https://github.com/TitaniumTigers4829/robot-co…
Feb 6, 2025
5e2bc4f
more merge fixes
Feb 6, 2025
882ee8b
choreos autoChooser is broken I think
Feb 7, 2025
14651a8
something isn't working :(
Feb 7, 2025
ba7b4be
choreo changes
Feb 8, 2025
c49126b
huh
Ishan1522 Feb 8, 2025
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
Empty file added paths.chor
Empty file.
47 changes: 47 additions & 0 deletions src/main/java/frc/robot/Autos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package frc.robot;
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved

import choreo.auto.AutoFactory;
import choreo.auto.AutoRoutine;
import choreo.auto.AutoTrajectory;
import edu.wpi.first.wpilibj2.command.Commands;
import frc.robot.subsystems.example.ExampleSubsystem;
import frc.robot.subsystems.swerve.SwerveDrive;

public class Autos {
public AutoFactory autoFactory;
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved

public AutoRoutine exampleAutoRoutine(
ExampleSubsystem exampleSubsystem, SwerveDrive swerveDrive) {

AutoRoutine routine = autoFactory.newRoutine("exampleAutoRoutine");
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved

AutoTrajectory startToETraj = routine.trajectory("startToE");
AutoTrajectory eToPickupTraj = routine.trajectory("eToPickup");
AutoTrajectory cToPickupTraj = routine.trajectory("cToPickup");

Check warning on line 20 in src/main/java/frc/robot/Autos.java

View workflow job for this annotation

GitHub Actions / Lint

Avoid unused local variables such as 'cToPickupTraj'.

Detects when a local variable is declared and/or assigned, but not used. Variables whose name starts with `ignored` or `unused` are filtered out. UnusedLocalVariable (Priority: 3, Ruleset: Best Practices) https://docs.pmd-code.org/pmd-doc-7.10.0/pmd_rules_java_bestpractices.html#unusedlocalvariable
AutoTrajectory pickupToCTraj = routine.trajectory("pickupToC");

Check warning on line 21 in src/main/java/frc/robot/Autos.java

View workflow job for this annotation

GitHub Actions / Lint

Avoid unused local variables such as 'pickupToCTraj'.

Detects when a local variable is declared and/or assigned, but not used. Variables whose name starts with `ignored` or `unused` are filtered out. UnusedLocalVariable (Priority: 3, Ruleset: Best Practices) https://docs.pmd-code.org/pmd-doc-7.10.0/pmd_rules_java_bestpractices.html#unusedlocalvariable

// reset odometry with vision and start first trajectory
routine.active().onTrue(Commands.sequence(swerveDrive.getEstimatedPose(), startToETraj.cmd()));
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved

startToETraj
.active()
.onTrue(
exampleSubsystem
.exampleFunctionalCommand()); // TODO: replace with elevator to L4 command
startToETraj
.atTime("score")
.onTrue(
exampleSubsystem.exampleFunctionalCommand()); // TODO: replace with command for rollers
startToETraj
.done()
.onTrue(
eToPickupTraj
.cmd()
.alongWith(
exampleSubsystem
.exampleFunctionalCommand())); // TODO: replace with elevator to intake
// command

return routine;
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved
}
}
35 changes: 9 additions & 26 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package frc.robot;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import frc.robot.extras.simulation.field.SimulatedField;
import org.littletonrobotics.junction.LogFileUtil;
Expand All @@ -17,8 +16,7 @@
* project.
*/
public class Robot extends LoggedRobot {
private Command m_autonomousCommand;
private RobotContainer m_robotContainer;
private RobotContainer robotContainer;

Check warning on line 19 in src/main/java/frc/robot/Robot.java

View workflow job for this annotation

GitHub Actions / Lint

Field 'robotContainer' may be declared final

Reports non-final fields whose value never changes once object initialization ends, and hence may be marked final. Note that this rule does not enforce that the field value be deeply immutable itself. An object can still have mutable state, even if all its member fields are declared final. This is referred to as shallow immutability. For more information on mutability, see *Effective Java, 3rd Edition, Item 17: Minimize mutability*. Limitations: We can only check private fields for now. ImmutableField (Priority: 3, Ruleset: Design) https://docs.pmd-code.org/pmd-doc-7.10.0/pmd_rules_java_design.html#immutablefield

public Robot() {
// Record metadata
Expand All @@ -42,34 +40,33 @@
}

// Set up data receivers & replay source
switch (Constants.ROBOT_TYPE) {
case COMP_ROBOT:
// Running on a real robot, log to a USB stick ("/U/logs")
Logger.addDataReceiver(new WPILOGWriter());
// Gets data from network tables
Logger.addDataReceiver(new NT4Publisher());
break;

case SIM_ROBOT:
// Running a physics simulator, log to NT
Logger.addDataReceiver(new NT4Publisher());
break;

case DEV_ROBOT:
// Replaying a log, set up replay source
setUseTiming(false); // Run as fast as possible
String logPath = LogFileUtil.findReplayLog();
Logger.setReplaySource(new WPILOGReader(logPath));
Logger.addDataReceiver(new WPILOGWriter(LogFileUtil.addPathSuffix(logPath, "_sim")));
break;
}

Check warning on line 63 in src/main/java/frc/robot/Robot.java

View workflow job for this annotation

GitHub Actions / Lint

Switch statements or expressions should be exhaustive, add a default case (or missing enum branches)

Switch statements should be exhaustive, to make their control flow easier to follow. This can be achieved by adding a `default` case, or, if the switch is on an enum type, by ensuring there is one switch branch for each enum constant. This rule doesn't consider Switch Statements, that use Pattern Matching, since for these the compiler already ensures that all cases are covered. The same is true for Switch Expressions, which are also not considered by this rule. NonExhaustiveSwitch (Priority: 3, Ruleset: Best Practices) https://docs.pmd-code.org/pmd-doc-7.10.0/pmd_rules_java_bestpractices.html#nonexhaustiveswitch

// Start AdvantageKit logger
Logger.start();

// Instantiate our RobotContainer. This will perform all our button bindings,
// and put our autonomous chooser on the dashboard.
m_robotContainer = new RobotContainer();
robotContainer = new RobotContainer();
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved
}

/** This function is called periodically during all modes. */
Expand Down Expand Up @@ -98,33 +95,19 @@
@Override
public void disabledPeriodic() {}

/** This autonomous runs the autonomous command selected by your {@link RobotContainer} class. */
@Override
public void autonomousInit() {
m_autonomousCommand = m_robotContainer.getAutonomousCommand();

// schedule the autonomous command (example)
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
}
}

/** This function is called periodically during autonomous. */
@Override
public void autonomousPeriodic() {}

@Override
public void autonomousInit() {
robotContainer.getAutonomousCommand();
}

/** This function is called once when teleop is enabled. */
@Override
public void teleopInit() {
// This makes sure that the autonomous stops running when
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
if (m_autonomousCommand != null) {
m_autonomousCommand.cancel();
}

m_robotContainer.teleopInit();
robotContainer.teleopInit();
}

/** This function is called periodically during operator control. */
Expand All @@ -150,6 +133,6 @@
@Override
public void simulationPeriodic() {
SimulatedField.getInstance().simulationPeriodic();
m_robotContainer.updateFieldSimAndDisplay();
robotContainer.updateFieldSimAndDisplay();
}
}
43 changes: 36 additions & 7 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package frc.robot;

import choreo.auto.AutoChooser;
import choreo.auto.AutoFactory;
import choreo.trajectory.SwerveSample;
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.system.plant.DCMotor;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
import edu.wpi.first.wpilibj2.command.button.Trigger;
import frc.robot.Constants.SimulationConstants;
import frc.robot.commands.drive.DriveCommand;
import frc.robot.commands.drive.FollowChoreoTrajectory;
import frc.robot.extras.simulation.field.SimulatedField;
import frc.robot.extras.simulation.mechanismSim.swerve.GyroSimulation;
import frc.robot.extras.simulation.mechanismSim.swerve.SwerveDriveSimulation;
import frc.robot.extras.simulation.mechanismSim.swerve.SwerveModuleSimulation;
import frc.robot.extras.simulation.mechanismSim.swerve.SwerveModuleSimulation.WHEEL_GRIP;
import frc.robot.extras.util.AllianceFlipper;
import frc.robot.extras.util.JoystickUtil;
import frc.robot.subsystems.example.ExampleSubsystem;
import frc.robot.subsystems.swerve.SwerveConstants;
import frc.robot.subsystems.swerve.SwerveConstants.DriveConstants;
import frc.robot.subsystems.swerve.SwerveConstants.ModuleConstants;
Expand All @@ -37,25 +43,24 @@

private final VisionSubsystem visionSubsystem;
private final SwerveDrive swerveDrive;

public final ExampleSubsystem exampleSubsystem;
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved
private final CommandXboxController operatorController = new CommandXboxController(1);
private final CommandXboxController driverController = new CommandXboxController(0);

Check warning on line 49 in src/main/java/frc/robot/RobotContainer.java

View workflow job for this annotation

GitHub Actions / Lint

Avoid unused private fields such as 'operatorController'.

Detects when a private field is declared and/or assigned a value, but not used. Since PMD 6.50.0 private fields are ignored, if the fields are annotated with any annotation or the enclosing class has any annotation. Annotations often enable a framework (such as dependency injection, mocking or e.g. Lombok) which use the fields by reflection or other means. This usage can't be detected by static code analysis. Previously these frameworks where explicitly allowed by listing their annotations in the property "ignoredAnnotations", but that turned out to be prone of false positive for any not explicitly considered framework. UnusedPrivateField (Priority: 3, Ruleset: Best Practices) https://docs.pmd-code.org/pmd-doc-7.10.0/pmd_rules_java_bestpractices.html#unusedprivatefield
// Simulation, we store them here in the robot container
// private final SimulatedField simulatedArena;
private final SwerveDriveSimulation swerveDriveSimulation;
private final GyroSimulation gyroSimulation;

private final SendableChooser<Command> autoChooser;
public AutoFactory autoFactory;
public AutoChooser autoChooser;
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved
public Autos autos;

public RobotContainer() {
autoChooser = new SendableChooser<Command>();
autoChooser.setDefaultOption("Auto", null);

switch (Constants.ROBOT_TYPE) {
case COMP_ROBOT -> {
/* Real robot, instantiate hardware IO implementations */

/* Disable Simulations */
// this.simulatedArena = null;
this.gyroSimulation = null;
Expand Down Expand Up @@ -136,6 +141,30 @@
new ModuleInterface() {});
}
}

exampleSubsystem = new ExampleSubsystem();

autos = new Autos();
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved
// this adds an auto routine to the auto chooser
autoChooser.addRoutine(
"Example routine", () -> autos.exampleAutoRoutine(exampleSubsystem, swerveDrive));

// this updates the auto chooser
SmartDashboard.putData(autoChooser);

// this sets up the auto factory
autoFactory =
new AutoFactory(
swerveDrive::getEstimatedPose, // A function that returns the current robot pose
swerveDrive::resetEstimatedPose, // A function that resets the current robot pose to the
// provided Pose2d
(SwerveSample sample) -> {
FollowChoreoTrajectory command =
new FollowChoreoTrajectory(swerveDrive, visionSubsystem, sample);
command.execute();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might not be correct. idk

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uh it seems weird just doing command.execute? I don't think this is right probably

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah me too lol im gonna test it today

}, // The drive subsystem trajectory follower
AllianceFlipper.isRed(), // If alliance flipping should be enabled
swerveDrive); // The drive subsystem
}

private void resetFieldAndOdometryForAuto(Pose2d robotStartingPoseAtBlueAlliance) {
Expand Down Expand Up @@ -215,7 +244,7 @@
swerveDrive.getEstimatedPose().getX(),
swerveDrive.getEstimatedPose().getY(),
Rotation2d.fromDegrees(swerveDrive.getAllianceAngleOffset())));
return autoChooser.getSelected();
return autoChooser.selectedCommand();
}

public void updateFieldSimAndDisplay() {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/frc/robot/commands/drive/FollowChoreoTrajectory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package frc.robot.commands.drive;

import choreo.trajectory.SwerveSample;
import frc.robot.subsystems.swerve.SwerveDrive;
import frc.robot.subsystems.vision.VisionSubsystem;

public class FollowChoreoTrajectory extends DriveCommandBase {
private final SwerveDrive swerveDrive;
private SwerveSample sample;

// Constructor without TrajectorySample for AutoFactory
public FollowChoreoTrajectory(
SwerveDrive swerveDrive, VisionSubsystem vision, SwerveSample sample) {
super(swerveDrive, vision);
this.swerveDrive = swerveDrive;
this.sample = sample;
// Use addRequirements() here to declare subsystem dependencies.
addRequirements(swerveDrive, vision);
}

// Called when the command is initially scheduled.
@Override
public void initialize() {}

// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
if (sample != null) {
swerveDrive.followTrajectory(sample);
}
}

// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {}

// Returns true when the command should end.
@Override
public boolean isFinished() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package frc.robot.subsystems.example;
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved

public class ExampleConstants {
public static final double SHOOTER_PIVOT_ANGLE = 0 - 9;
}
102 changes: 102 additions & 0 deletions src/main/java/frc/robot/subsystems/example/ExampleSubsystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.subsystems.example;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.FunctionalCommand;
import edu.wpi.first.wpilibj2.command.StartEndCommand;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import edu.wpi.first.wpilibj2.command.button.Trigger;
import java.util.function.DoubleSupplier;

public class ExampleSubsystem extends SubsystemBase {
private DoubleSupplier DOUBLELOL;

/** Creates a new ExampleSubsystem. */
public ExampleSubsystem() {}

// this is the skeleton for the subsystem

@Override
public void periodic() {
// This method will be called once per scheduler run
}

private void exampleSubsystemFunction() {}

private void setExamplePivotAngle(double angle) {
// normal subsystem stuff
}

private double getExamplePivotAngle() {
return 0.0;
}

private boolean isAngleSet() {
return false;
}

private boolean isGamepieceinRobot() {
return false;
}

// Public Command Factory
// create these for everything you would need to access from a command
// so you don't input values or acces the internals of a subsystem in the commands themselves
public Command exampleFunctionalCommand() {
// the command is written here instead of in it's own seperate file.
// this saves space and makes the code more readable

return new FunctionalCommand(
// does on init
() -> this.setExamplePivotAngle(0),
// does on execute
() -> this.setExamplePivotAngle(1),
// does when command ends
interrupted -> this.setExamplePivotAngle(0),
// ends the command when this is true
() -> getExamplePivotAngle() >= 0,
// requirements for the command
this);
}

// Shared internal implementation
private Command exampleStartEndCommand() {
// the command is written here instead of in it's own seperate file.
// this saves space and makes the code more readable

return new StartEndCommand(
// Sets the pivot to 1 while the command is active
() -> this.setExamplePivotAngle(1),
// Sets the pivot to 0 when the command ends
() -> this.setExamplePivotAngle(0),
// requirements for the command
this);
}

// Shared internal implementation
private Command exampleRunOnceCommand(double angle) {
return this.runOnce(() -> setExamplePivotAngle(angle));
}

// Public Command Factory
public Command setPivotToScore() {
return exampleRunOnceCommand(ExampleConstants.SHOOTER_PIVOT_ANGLE);
}

// Public Command Factory
public Command setPivotToAngle(DoubleSupplier angle) {
// if you need to pass a value in do it this way
return exampleRunOnceCommand(angle.getAsDouble());
}

// this is a trigger
// if you need to give information from the subsystem do it like this
// must be a boolean
public final Trigger isAngleInRightSpot = new Trigger(() -> isAngleSet());

// another trigger example
public final Trigger hasGamepiece = new Trigger(() -> isGamepieceinRobot());
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ public static final class TrajectoryConstants {
public static final double MAX_ACCELERATION = 3;

public static final double AUTO_TRANSLATION_P = 1.5; // 1.7
public static final double AUTO_TRANSLATION_I = 0;
public static final double AUTO_TRANSLATION_D = 0.2;
public static final double AUTO_THETA_P = 4.5; // 5
public static final double AUTO_THETA_I = 0; // 5
public static final double AUTO_THETA_D = 0.4;

public static final double AUTO_SHOOT_HEADING_OFFSET = 2;
Expand Down
Loading
Loading