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