How to Install
Download
Latest Release
Links
Detailed WPI Lib Install Guide
V1
V2
Timed Robot
This code base is the most basic, and well used code base that most programmers start off within FIRST robotics. This code has everything contained in one file. The downside to having it in all one file is that complex code can make code hard to organize. This should only be used for simple robotics code, or testing ideas.
Looking at the template, we will dig into how this code works:
Java
// 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 edu.wpi.first.wpilibj.templates.timed;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
/**
* The methods in this class are called automatically corresponding to each mode, as described in
* the TimedRobot documentation. If you change the name of this class or the package after creating
* this project, you must also update the Main.java file in the project.
*/
public class Robot extends TimedRobot {
private static final String kDefaultAuto = "Default";
private static final String kCustomAuto = "My Auto";
private String m_autoSelected;
private final SendableChooser<String> m_chooser = new SendableChooser<>();
/**
* This function is run when the robot is first started up and should be used for any
* initialization code.
*/
public Robot() {
m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
m_chooser.addOption("My Auto", kCustomAuto);
SmartDashboard.putData("Auto choices", m_chooser);
}
/**
* This function is called every 20 ms, no matter the mode. Use this for items like diagnostics
* that you want ran during disabled, autonomous, teleoperated and test.
*
* <p>This runs after the mode specific periodic functions, but before LiveWindow and
* SmartDashboard integrated updating.
*/
@Override
public void robotPeriodic() {}
/**
* This autonomous (along with the chooser code above) shows how to select between different
* autonomous modes using the dashboard. The sendable chooser code works with the Java
* SmartDashboard. If you prefer the LabVIEW Dashboard, remove all of the chooser code and
* uncomment the getString line to get the auto name from the text box below the Gyro
*
* <p>You can add additional auto modes by adding additional comparisons to the switch structure
* below with additional strings. If using the SendableChooser make sure to add them to the
* chooser code above as well.
*/
@Override
public void autonomousInit() {
m_autoSelected = m_chooser.getSelected();
// m_autoSelected = SmartDashboard.getString("Auto Selector", kDefaultAuto);
System.out.println("Auto selected: " + m_autoSelected);
}
/** This function is called periodically during autonomous. */
@Override
public void autonomousPeriodic() {
switch (m_autoSelected) {
case kCustomAuto:
// Put custom auto code here
break;
case kDefaultAuto:
default:
// Put default auto code here
break;
}
}
/** This function is called once when teleop is enabled. */
@Override
public void teleopInit() {}
/** This function is called periodically during operator control. */
@Override
public void teleopPeriodic() {}
/** This function is called once when the robot is disabled. */
@Override
public void disabledInit() {}
/** This function is called periodically when disabled. */
@Override
public void disabledPeriodic() {}
/** This function is called once when test mode is enabled. */
@Override
public void testInit() {}
/** This function is called periodically during test mode. */
@Override
public void testPeriodic() {}
/** This function is called once when the robot is first started up. */
@Override
public void simulationInit() {}
/** This function is called periodically whilst in simulation. */
@Override
public void simulationPeriodic() {}
}
Init and Periodic
There are two sections for each mode Periodic, and Init.
Init is code that runs once just before the Periodic runs. This allows to reset veriables and such.
Periodic is code that loops every 20ms (milliseconds). This is where we want to place things like motors, sensors and such.
Modes
Each mode is ran depending on what is chosen in the dashboard. Not every mode is accessable from the dashboard though.
Autonomous is code that is ran for the first 15 seconds of a match. This code is expected to run things without any operator control.
Teleop (Or Teleoperated) is code that runs with operator control (using a gamepad, xbox controller, joystick, etc). This is ran for the rest of the match after autonomous. This can run both autonomous tasks and operator controls.
Robot, this mode is ran all the time. Very useful for diagnostics, or running other things that are not physically actuated.
Disabled is a mode that runs when the robot does not have any modes running. This is great for disabling motor movement.
We will not cover the other modes here, but they do have a use. Just not imparitive to run a robot.
Later, we will explore this template more for simple robot drives like tank drive and mecanum.
Links
WPI Lib Template
Command