001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2008-2012. All Rights Reserved.                        */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpilibj;
009
010import edu.wpi.first.wpilibj.communication.UsageReporting;
011import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tInstances;
012import edu.wpi.first.wpilibj.communication.FRCNetworkCommunicationsLibrary.tResourceType;
013import edu.wpi.first.wpilibj.Timer;
014import edu.wpi.first.wpilibj.livewindow.LiveWindow;
015
016/**
017 * A simple robot base class that knows the standard FRC competition states (disabled, autonomous, or operator controlled).
018 *
019 * You can build a simple robot program off of this by overriding the
020 * robotinit(), disabled(), autonomous() and operatorControl() methods. The startCompetition() method will calls these methods
021 * (sometimes repeatedly).
022 * depending on the state of the competition.
023 *
024 * Alternatively you can override the robotMain() method and manage all aspects of the robot yourself.
025 */
026public class SampleRobot extends RobotBase {
027
028    private boolean m_robotMainOverridden;
029
030    /**
031     * Create a new SampleRobot
032     */
033    public SampleRobot() {
034        super();
035        m_robotMainOverridden = true;
036    }
037
038    /**
039     * Robot-wide initialization code should go here.
040     *
041     * Users should override this method for default Robot-wide initialization which will
042     * be called when the robot is first powered on.
043     *
044     * Called exactly 1 time when the competition starts.
045     */
046    protected void robotInit() {
047        System.out.println("Default robotInit() method running, consider providing your own");
048    }
049
050    /**
051     * Disabled should go here.
052     * Users should overload this method to run code that should run while the field is
053     * disabled.
054     *
055     * Called once each time the robot enters the disabled state.
056     */
057    protected void disabled() {
058        System.out.println("Default disabled() method running, consider providing your own");
059    }
060
061    /**
062     * Autonomous should go here.
063     * Users should add autonomous code to this method that should run while the field is
064     * in the autonomous period.
065     *
066     * Called once each time the robot enters the autonomous state.
067     */
068    public void autonomous() {
069        System.out.println("Default autonomous() method running, consider providing your own");
070    }
071
072    /**
073     * Operator control (tele-operated) code should go here.
074     * Users should add Operator Control code to this method that should run while the field is
075     * in the Operator Control (tele-operated) period.
076     *
077     * Called once each time the robot enters the operator-controlled state.
078     */
079    public void operatorControl() {
080        System.out.println("Default operatorControl() method running, consider providing your own");
081    }
082
083    /**
084     * Test code should go here.
085     * Users should add test code to this method that should run while the robot is in test mode.
086     */
087    public void test() {
088        System.out.println("Default test() method running, consider providing your own");
089    }
090
091    /**
092     * Robot main program for free-form programs.
093     *
094     * This should be overridden by user subclasses if the intent is to not use the autonomous() and
095     * operatorControl() methods. In that case, the program is responsible for sensing when to run
096     * the autonomous and operator control functions in their program.
097     *
098     * This method will be called immediately after the constructor is called. If it has not been
099     * overridden by a user subclass (i.e. the default version runs), then the robotInit(), disabled(), autonomous() and
100     * operatorControl() methods will be called.
101     */
102    public void robotMain() {
103        m_robotMainOverridden = false;
104    }
105
106    /**
107     * Start a competition.
108     * This code tracks the order of the field starting to ensure that everything happens
109     * in the right order. Repeatedly run the correct method, either Autonomous or OperatorControl
110     * when the robot is enabled. After running the correct method, wait for some state to change,
111     * either the other mode starts or the robot is disabled. Then go back and wait for the robot
112     * to be enabled again.
113     */
114    public void startCompetition() {
115        UsageReporting.report(tResourceType.kResourceType_Framework, tInstances.kFramework_Simple);
116
117        robotMain();
118        if (!m_robotMainOverridden) {
119            // first and one-time initialization
120            LiveWindow.setEnabled(false);
121            robotInit();
122
123            while (true) {
124                if (isDisabled()) {
125                    m_ds.InDisabled(true);
126                    disabled();
127                    m_ds.InDisabled(false);
128                    while (isDisabled()) {
129                        Timer.delay(0.01);
130                    }
131                } else if (isAutonomous()) {
132                    m_ds.InAutonomous(true);
133                    autonomous();
134                    m_ds.InAutonomous(false);
135                    while (isAutonomous() && !isDisabled()) {
136                        Timer.delay(0.01);
137                    }
138                } else if (isTest()) {
139                    LiveWindow.setEnabled(true);
140                    m_ds.InTest(true);
141                    test();
142                    m_ds.InTest(false);
143                    while (isTest() && isEnabled())
144                        Timer.delay(0.01);
145                    LiveWindow.setEnabled(false);
146                } else {
147                    m_ds.InOperatorControl(true);
148                    operatorControl();
149                    m_ds.InOperatorControl(false);
150                    while (isOperatorControl() && !isDisabled()) {
151                        Timer.delay(0.01);
152                    }
153                }
154            } /* while loop */
155        }
156    }
157}