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 5 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
43 changes: 17 additions & 26 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package frc.robot;

import edu.wpi.first.wpilibj2.command.Command;
import choreo.auto.AutoChooser;
import choreo.auto.AutoFactory;
import choreo.auto.AutoRoutine;
import choreo.auto.AutoTrajectory;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.button.RobotModeTriggers;
import frc.robot.extras.simulation.field.SimulatedField;
import org.littletonrobotics.junction.LogFileUtil;
import org.littletonrobotics.junction.LoggedRobot;
Expand All @@ -17,8 +23,8 @@
* project.
*/
public class Robot extends LoggedRobot {
private Command m_autonomousCommand;
private RobotContainer m_robotContainer;
private RobotContainer robotContainer;

Check warning on line 26 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.9.0/pmd_rules_java_design.html#immutablefield


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

// Set up data receivers & replay source
switch (Constants.CURRENT_MODE) {
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 71 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.9.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 +103,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() {
autoChooser.selectedCommandScheduler();

}
/** 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 +141,6 @@
@Override
public void simulationPeriodic() {
SimulatedField.getInstance().simulationPeriodic();
m_robotContainer.updateFieldSimAndDisplay();
robotContainer.updateFieldSimAndDisplay();
}
}
104 changes: 98 additions & 6 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package frc.robot;

import choreo.auto.AutoChooser;
import choreo.auto.AutoFactory;
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.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.InstantCommand;
import edu.wpi.first.wpilibj2.command.button.CommandXboxController;
Expand All @@ -17,6 +18,8 @@
import frc.robot.extras.simulation.mechanismSim.swerve.SwerveModuleSimulation;
import frc.robot.extras.simulation.mechanismSim.swerve.SwerveModuleSimulation.WHEEL_GRIP;
import frc.robot.extras.util.JoystickUtil;
import frc.robot.extras.util.AllianceFlipper;
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 Down Expand Up @@ -46,13 +49,21 @@ public class RobotContainer {
private final SwerveDriveSimulation swerveDriveSimulation;
private final GyroSimulation gyroSimulation;

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

public RobotContainer() {
autoChooser = new SendableChooser<Command>();
autoChooser.setDefaultOption("Auto", null);
// this will put our autonomous chooser on the dashboard.
autoChooser = new AutoChooser();
autoChooser.addRoutine("Example routine", this::exampleAutoRoutine);
SmartDashboard.putData(autoChooser);

switch (Constants.CURRENT_MODE) {
// this will put our autonomous chooser on the dashboard.
autoChooser = new AutoChooser();
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved
autoChooser.addRoutine("Example routine", this::exampleAutoRoutine);
SmartDashboard.putData(autoChooser);

case COMP_ROBOT -> {
/* Real robot, instantiate hardware IO implementations */

Expand All @@ -69,12 +80,36 @@ public RobotContainer() {
new CompModule(SwerveConstants.moduleConfigs[2]),
new CompModule(SwerveConstants.moduleConfigs[3]));
visionSubsystem = new VisionSubsystem(new PhysicalVision());
exampleSubsystem = new ExampleSubsystem();

// 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
swerveDrive::followTrajectory, // The drive subsystem trajectory follower
AllianceFlipper.isRed(), // If alliance flipping should be enabled
swerveDrive); // The drive subsystem

}
case DEV_ROBOT -> {
swerveDrive = new SwerveDrive(null, null, null, null, null);
gyroSimulation = null;
swerveDriveSimulation = null;
visionSubsystem = null;
exampleSubsystem = new ExampleSubsystem();

// 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
swerveDrive::followTrajectory, // The drive subsystem trajectory follower
AllianceFlipper.isRed(), // If alliance flipping should be enabled
swerveDrive); // The drive subsystem
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved

}

case SIM_ROBOT -> {
Expand Down Expand Up @@ -113,6 +148,18 @@ public RobotContainer() {
new SimulatedModule(swerveDriveSimulation.getModules()[3]));

visionSubsystem = null;
exampleSubsystem = new ExampleSubsystem();

// 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
swerveDrive::followTrajectory, // The drive subsystem trajectory follower
AllianceFlipper.isRed(), // If alliance flipping should be enabled
swerveDrive); // The drive subsystem

// new VisionSubsystem(
// new SimulatedVision(() -> swerveDriveSimulation.getSimulatedDriveTrainPose()));

Expand All @@ -136,10 +183,56 @@ public RobotContainer() {
new ModuleInterface() {},
new ModuleInterface() {},
new ModuleInterface() {});
exampleSubsystem = new ExampleSubsystem();

// 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
swerveDrive::followTrajectory, // The drive subsystem trajectory follower
AllianceFlipper.isRed(), // If alliance flipping should be enabled
swerveDrive); // The drive subsystem

}
}
}

public AutoRoutine exampleAutoRoutine() {
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved

AutoRoutine routine = autoFactory.newRoutine("exampleAutoRoutine");

AutoTrajectory startToETraj = routine.trajectory("startToE");
AutoTrajectory eToPickupTraj = routine.trajectory("eToPickup");
AutoTrajectory cToPickupTraj = routine.trajectory("cToPickup");
AutoTrajectory pickupToCTraj = routine.trajectory("pickupToC");
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved

// reset odometry and start first trajectory
routine.active().onTrue(Commands.sequence(startToETraj.resetOdometry(), startToETraj.cmd()));

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;
}

private void resetFieldAndOdometryForAuto(Pose2d robotStartingPoseAtBlueAlliance) {
final Pose2d startingPose = robotStartingPoseAtBlueAlliance;

Expand Down Expand Up @@ -215,8 +308,7 @@ public Command getAutonomousCommand() {
new Pose2d(
swerveDrive.getEstimatedPose().getX(),
swerveDrive.getEstimatedPose().getY(),
Rotation2d.fromDegrees(swerveDrive.getAllianceAngleOffset())));
return autoChooser.getSelected();
Rotation2d.fromDegrees(swerveDrive.getAllianceAngleOffset())));
}

public void updateFieldSimAndDisplay() {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/frc/robot/commands/auto/ExampleAuto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) FIRST and other WPILib contributors.
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved
// 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.commands.auto;

import edu.wpi.first.wpilibj2.command.SequentialCommandGroup;
import frc.robot.subsystems.example.ExampleSubsystem;

// NOTE: Consider using this command inline, rather than writing a subclass. For more
// information, see:
// https://docs.wpilib.org/en/stable/docs/software/commandbased/convenience-features.html
public class ExampleAuto extends SequentialCommandGroup {

/** Creates a new ExampleAuto. */
public ExampleAuto(ExampleSubsystem exampleSubsystem) {
// Add your commands in the addCommands() call, e.g.
// addCommands(new FooCommand(), new BarCommand());
addCommands(exampleSubsystem.setPivotToScore().alongWith(exampleSubsystem.setPivotToScore()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.commands.example;

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

/* You should consider using the more terse Command factories API instead https://docs.wpilib.org/en/stable/docs/software/commandbased/organizing-command-based.html#defining-commands */
public class ExampleScoreL4Command extends Command {
/** Creates a new ExampleScoreL4Command. */
public ExampleScoreL4Command() {
aridavidson001 marked this conversation as resolved.
Show resolved Hide resolved
// Use addRequirements() here to declare subsystem dependencies.
}

// 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() {}

// 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;
}
Loading
Loading