Hello I was wondering if anyone knew how I could make multiple auto modes using Java. I know how to make the basic auto mode but right now it runs automatically after autonomous mode is activated. I would like to make multiple autonomous modes that I can name. Help would be greatly appreciated!
Can you post your code? What framework are you using?
We create a sendable chooser on the dashboard or shuffleboard which we can use to select which autonomous program will be running. This year we are directly adding commands for our different sequences to the sendable chooser but in the past we just used numbers and a switch case to actually select the autonomous command. Either way it shows on the dashboard with a text name instead of needing to know what number is what. I can try to post some example code tomorrow from our current year if no one gives you any before then. There is probably some in our code from 2018 and 2017 on the github site for how we used to do it https://github.com/pyro4207 but probably not great examples and it does depend on whether you are using command based exactly how to implement it but I recommend the chooser either way.
Sorry meant to post this yesterday.
We create a sendable chooser in the robot class
public static SendableChooser sendablechooser;
then in the robotInit function
sendablechooser = new SendableChooser();
sendablechooser.setDefaultOption(“Do Nothing”, new DoNothing());
sendablechooser.addOption(“Drive”, new AutoDrive());
sendablechooser.addOption(“Drive And Shoot”, new AutoDriveandShoot());
SmartDashboard.putData(“Autonomous”, sendablechooser);
That last line is what actually adds it to the dashboard with the options showing up with the text in quotes. With shuffleboard it will show up as a drop box but you can switch modes to be a series of buttons where only one can be selected at a time. Note that you can use just a command or a they can be command groups. Add as many as you want
The autonomousInit function then becomes
m_autonomousCommand = sendablechooser.getSelected();
if (m_autonomousCommand != null) {
m_autonomousCommand.schedule();
}
Things are different if you are not using a Command based structure but mostly it is the same concept except you add something like integers to the chooser and then use if/else or switch type logic to where your autonomous is running. Personally I do recommend the command based structure and I think this year’s new version made it better.
We have a rotary switch connected to the DIO ports on the Rio. In robot.java we get the DIO value and “switch” based on it to select the auto command group.
In command-based java, we used an if, else, else, else to return our various autoroutine command groups, linked to the little sliders on the bottom of the joysticks our drivers use. They just flip the correct slider up and the others down to run the desired routine.
After looking at all of your responses I am still very confused. I know how to select the wanted autonomous in smart dashboard but I’m having a hard time figuring out how to program each auto in the code. So sorry this is my first time programming autonomous mode in java.
We do this as well!
There are really two ways to declare a different auto routine. You can use recursive composition or you can use command groups that you make with basic commands (ex: a command that makes the robot move forward for a predetermined amount of time)
WPI has a page on command groups that you might find useful
https://docs.wpilib.org/en/latest/docs/software/commandbased/command-groups.html
Here’s 2815’s code, which has 4 base command groups for reference
What you are looking for is the “SendableChooser”. The SendableChooser allows you to create a dropdown menu on your SmartDashboard/Shuffleboard and be able to select a value that is sent back to the robot code.
The way that you will need to implement this varies based on which framework you are using (Command-Based Robot or TimedRobot). For the former, you can make it so when a particular value is chosen in the dropdown, a CommandGroup is run (like @Hitch suggested). Make one CommandGroup for each different auto mode you want. You’ll also need to implement the code that @GKannel detailed. If you are not using command-based, you’ll need to make some methods for each of the different auto modes you want, and use if-statements in the autoInit method to call the methods.
So I understand how to create multiple autos that show up in the smart dashboard. However I do not know how to edit the autonomous modes.
Here is what I have right now:
public void autonomousInit() {
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
m_chooser.addOption("Custom", kCustomAuto);
SmartDashboard.putData("Auto choices", m_chooser);
resetEncoders();
m_autoSelected = m_chooser.getSelected();
}
public void autonomousPeriodic() {
left_drive1.setIdleMode(IdleMode.kBrake);
if (m_encoder1L.getPosition() < 2000) {
left_drive1.set(1);
} else {
left_drive1.set(0);
}
}
Have you looked at how the Timed Robot template code does it?
I think what you missed is the behavior of SendableChooser(). GKannel posted the code. Notice in the code in robotInit(), each time you add an option to the sendableChooser, you are creating an instance of a command (eg like “new AutoDrive()”), and passing that in with the string name.
Then, in autonomousInit(), you fetch the selected item from sendableChooser. The returned item is a Java Command, which you then run (with “schedule()”).
So, create a few different Commands which do different things. Put them into a SendableChooser. Select the choice on the dashboard when setting up. When auto starts, fetch the selected command out of the sendableChooser and schedule it.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.