Choosing a Base Class

IterativeRobot

C++
RobotTemplate::RobotTemplate() { } void RobotTemplate::RobotInit() { } void RobotTemplate::AutonomousInit() { } void RobotTemplate::AutonomousPeriodic() { }

Java
public class RobotTemplate extends IterativeRobot { public void robotInit() { } public void autonomousInit() { } public void autonomousPeriodic() { \ }

The Iterative Robot base class assists with the most common code structure by handling the state transitions and looping in the base class instead of in the robot code. For each state (autonomous, teleop, disabled, test) there are two methods that are called:

  • Init methods - The init method for a given state is called each time the corresponding state is entered (for example, a transition from disabled to teleop will call teleopInit()). Any initialization code or resetting of variables between modes should be placed here.
  • Periodic methods - The periodic method for a given state is called each time the robot receives a Driver Station packet in the corresponding state, approximately every 20ms. This means that all of the code placed in each periodic method should finish executing in 20ms or less. The idea is to put code here that gets values from the driver station and updates the motors. You can read the joysticks and other Driver Station inputs more often, but you’ll only get the previous value until a new update is received. By synchronizing with the received updates your program will put less of a load on the roboRIO CPU leaving more time for other tasks such as camera processing.

SampleRobot

C++
RobotTemplate::RobotTemplate() { } //This function is called once each time the robot enters autonomous mode. void RobotTemplate::Autonomous() { \ while (IsAutonomous() && IsEnable()) { // Put code here \ Wait(0.05); } } // This function is called once each time the robot enters teleop mode. void RobotTemplate::OperatorControl() { while (isOperatorControl() && isEnabled()) { // Put code here Wait(0.05); \ } }

Java
public class RobotTemplate extends SampleRobot { //This function is called once each time the robot enters autonomous mode. public void autonomous() { // Put code here Timer.delay(0.05); } // This function is called once each time the robot enters teleop mode. public void operatorControl() { while(isOperatorControl() && isEnabled()) { //Put code here //Timer.delay(0.05); } \ } }

public class RobotTemplate extends SampleRobot {

    /**
     * This function is called once each time the robot enters autonomous mode.
     */
    public void autonomous() {    
        while (isAutonomous() && isEnable()) {
            // Put code here
            Timer.delay(0.05);
        }
    }

    /**
     * This function is called once each time the robot enters teleop mode.
     */
    public void operatorControl() {
        while (isOperatorControl() && isEnabled()) {
            // Put code here
            Timer.delay(0.05);
        }
    }
}

The SampleRobot class is the simplest template as most of the state flow is directly visible in your program and not hidden in the WPILib code. The downside is that implementing this state flow incorrectly can lead to problems. Your robot program overrides the operatorControl() and autonomous() methods that are called by the base at the appropriate time. Note that these methods are called only called once each time the robot enters the appropriate mode and are not automatically terminated. Your code in the operatorControl method must contain a loop that checks the robot mode in order to keep running and taking new input from the Driver Station. The autonomous code shown uses a similar loop.

It is recommended for beginners to choose the Iterative Template or Command Based robot. SampleRobot can be used by advanced users wishing to have more control over the flow of their program.

Command-Based Robot

While not strictly a base class, the Command-based robot model is a method for creating larger programs, more easily, that are easier to extend. There is built in support with a number of classes to make it easy to design your robot, build subsystems, and control interactions between the robot and the operator interface. In addition it provides a simple mechanism for writing autonomous programs. The command-based model is described in detail in the Command-Based Programming section of the C++ and Java manuals.

0 Report Errors

Use this form to report any errors with the documentation. For help with WPILib, please use the FIRST Forums at http://forums.usfirst.org For reporting WPILib bugs, please submit an issue on GitHub at https://github.com/wpilibsuite/allwpilib/issues/new

Ignore