My team has been working on setting up swerve this year and an issue we ran into is programming multiple autonomous programs. We are used to creating a Sendable Chooser and making a switch/case function for the different auto’s, but that does not seem to work with how the auto code for swerve is set up. So we are curious how other teams do it? Does anyone have an easy solution for it? Any tips or clarification on this would help!
I am not sure what you mean exactly. Our swerve code, for instance, has trajectory driving commands (we use PathPlanner). That means we have commands like “dirve trajectory …” (saved in a file and drawn with PP GUI tool), or “generate trajectory on a fly between coordinates blah and blah1 and drive that”.
All that is perfectly compatible with Sendable Chooser.
Can you share your current code for the Senable Chooser? Might help us spot something.
In the example code we are using, it has a separate folder in the robot folder with autos, where there is an exampleAuto.java file. There, apart from the trajectory, are a lot of different stuff, like swerveControllerCommand and thetaController. So, naturally, we thought we could add more .java files with different autos to the auto folder and do a sendable choose in the Robot.java or RobotContainer.java files, where we could choose between the different auto files. I hope that makes sense. Is that possible or do we have to do something differently?
Let’s start a bit earlier than that - do you use CommandRobot or TimedRobot template?
If it’s CommandRobot, you need to create Commands, either as command files or methods usable with InstantCommand or other Command-related types. If you share a link to your code, I can take a look.
Here is our code: https://github.com/Tofety/vault2024/blob/main/Kettering_v1/BaseTalonFXSwerve-main/src/main/java/frc/robot/autos/exampleAuto.java
Your code isn’t public
What you want to do is possible, but not in the way you describe it. You aren’t able to just say “run this file” super easily. That leaves you with 2 (very similar) options. If you are using command-based, then it should be as easy a creating a sendable chooser with a list of command objects. If you are not using command based, then I would look into doing that for next year, but in order to avoid switching to command based, as long as each of the .java files has a class that inherits from a common class or interface (and have common method required by the class or interface), then you can use a sendable chooser with a list of objects, each one being one of the classes from the java files.
Alternatively, you could just do your old method, except instead of writing all of your code inside of the switch statement, you can simply just call the class (.java file) that you want to use instead.
Just made it public. My bad.
It looks like you are using the 364 swerve base code. I would advise using pathplanner and its autobuilder to create and switch between multiple autos. It is really easy to use. 364 has an example code implementing this here.
I second the opinion of another person earlier in a thread that using WPI libraries for swerve navigation is not the best choice. PathPlanner makes is very easy, and I highly recommend switching.
I looked at the code and I see how they set up the chooser, but where does the main path planner gets set up? I am not very familiar with path planner, so if someone could orient me in the code or point me to the right place, that would be helpful!
PathPlanner doesn’t have much to set up besides the auto builder, setting up named commands and sendable chooser. You configure PathPlanner on their UI and all the settings get saved in the deploy directory. After that you just draw paths and then put together an auto with all your named commands and everything saves automatically so there’s no code you have to do besides the initial setup with auto builder, named commands, and sendable chooser.
I’d also look at https://pathplanner.dev for examples and API reference
We implemented the pathplanner and autobuilder, but we have been having issues with deploying code to the robot. It seems to be stuck in a boot loop. You can access our code with the changes here.
I do not think you configured the autobuilder correctly. You would usually have something like this in the Swerve Subsystem:
// Configure the AutoBuilder last
AutoBuilder.configureHolonomic(
this::getPose, // Robot pose supplier
this::resetOdometry, // Method to reset odometry (will be called if your auto has a starting pose)
this::getRobotRelativeSpeeds, // ChassisSpeeds supplier. MUST BE ROBOT RELATIVE
this::driveRobotRelative, // Method that will drive the robot given ROBOT RELATIVE ChassisSpeeds
new HolonomicPathFollowerConfig( // HolonomicPathFollowerConfig, this should likely live in your Constants class
new PIDConstants(5.0, 0.0, 0.0), // Translation PID constants
new PIDConstants(5.0, 0.0, 0.0), // Rotation PID constants
4.5, // Max module speed, in m/s
0.4, // Drive base radius in meters. Distance from robot center to furthest module.
new ReplanningConfig() // Default path replanning config. See the API for the options here
),
() -> {
Optional<Alliance> alliance = DriverStation.getAlliance();
if (alliance.isPresent()) {
return alliance.get() == DriverStation.Alliance.Red;
}
return false;
},
this // Reference to this subsystem to set requirements
);
// Set up custom logging to add the current path to a field 2d widget
PathPlannerLogging.setLogActivePathCallback((poses) -> field.getObject("path").setPoses(poses));
SmartDashboard.putData("Field", field);
}
Take a look at how I implemented it in my code.
We implemented this in our code and it got rid of the issues we were having with the boot loop for now! Thank you! We’ll see whether it functions well though.
@Toefty did you get to make your autonomous code work?
Just pay attention to building the auto builder and you will almost not have any problem
@atsekhan @BTYWAR @EndrewBortoli9458 Here is the updated code. When we run it, the chooser on the dashboard doesn’t show any options for auto, except for the default “none” option. Don’t know what is wrong or missing in the code. But other than that, doesn’t seem like there are any other issues for now. Any ideas?
Your chooser is a SendableChooser type. Don’t you need to add options to it via .addOption ?
From the instructions in the Path Planner docs, it seemed like I just need to set up the AutoBuilder to be connected to the chooser, but I might need to do the .addOption() method. What do you think should I put into it? Do I do use the AutoBuilder.followPath or something similar? I can’t really connect it to the PathPlanner part of the code with Auto’s and Paths, can I?