π€Basics of Autonomous
Note: Command Based Programming
Introduction to the Robot Class
The Robot class is the entry point of any FRC program, and is housed in Robot.java, which is included in every newly generated FRC project. This class typically extends the TimedRobot class, and houses 10 methods:
|
For the purposes of this article, we will be focusing on the logic contained with the autonomousInit() and autonomousPeriodic() methods. The autonomousInit() command begins by retrieving the main autonomous command, which is housed in the RobotContainer class, and storing it in the m_autonomousCommand variable. That command is then scheduled in the CommandScheduler.
The main autonomous command is typically a SequentialCommandGroup, which contains all of the steps needed to run the autonomous routine. If input from the Driver Station is needed to select the proper autonomous, this should be done in the getAutonomousCommand() method within the RobotContainer class. This way, the Robot class does not manage the selection of autonomous based on driveteam input.
Structure of a Command
Letβs quickly review the structure of a command. Most typical commands consist of 4 methods: initialize(), execute(), end(), and isFinished().
initialize() will only run once when the command is scheduled and used to set the state of the robot needed for the task.
execute() will run every 20ms as the command is scheduled and is used to perform the actual task at hand. Methods to move the robot and retrieve necessary values from subsystems should be called from execute()
end() is used to set the robot back to its original state and is called when the command is removed from the command scheduler
isFinished() runs every 20 milliseconds and can be used to remove the command from the command scheduler preemptively. In autonomous commands, isFinished() is often used to obtain sensor input and determine if this step of the autonomous has reached its desired target.
What makes up an Autonomous Command
Because this is an autonomous command, there will be no controller input to remove the command from the CommandScheduler.
This is where isFinished() comes into play. If isFinished() returns true, end() is called and the command is removed from the CommandScheduler.
There are several conditions you can set inside of isFinished(). For the purposes of this article, we will be examining time-based or integrated encoder conditions.
Types
In order to create a time-based autonomous, first initialize a Timer in the constructor of the Command and call the start() method of the Timer inside of the initialize().
isFinished() will be used to constantly retrieve the current time from the Timer object by calling the Timerβs get() method and checking if it has exceeded the desired time. If the conditional returns true, the Command will be removed from the CommandScheduler.
In order to create an encoder-based autonomous, you should have a method in your subsystem to retrieve the current encoder value. Similarly to the timer, this can be checked against a target in the isFinished() method.
Resources
Last updated