Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008. All Rights Reserved. |
| 3 | */ |
| 4 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 5 | /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "SampleRobot.h" |
| 9 | |
| 10 | #include "DriverStation.h" |
| 11 | #include "Timer.h" |
| 12 | #include "SmartDashboard/SmartDashboard.h" |
| 13 | #include "LiveWindow/LiveWindow.h" |
| 14 | #include "networktables/NetworkTable.h" |
| 15 | |
| 16 | SampleRobot::SampleRobot() : m_robotMainOverridden(true) {} |
| 17 | |
| 18 | /** |
| 19 | * Robot-wide initialization code should go here. |
| 20 | * |
| 21 | * Users should override this method for default Robot-wide initialization which |
| 22 | * will be called when the robot is first powered on. It will be called exactly |
| 23 | * one time. |
| 24 | * |
| 25 | * Warning: the Driver Station "Robot Code" light and FMS "Robot Ready" |
| 26 | * indicators will be off until RobotInit() exits. Code in RobotInit() that |
| 27 | * waits for enable will cause the robot to never indicate that the code is |
| 28 | * ready, causing the robot to be bypassed in a match. |
| 29 | */ |
| 30 | void SampleRobot::RobotInit() { |
| 31 | printf("Default %s() method... Override me!\n", __FUNCTION__); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Disabled should go here. |
| 36 | * Programmers should override this method to run code that should run while the |
| 37 | * field is |
| 38 | * disabled. |
| 39 | */ |
| 40 | void SampleRobot::Disabled() { |
| 41 | printf("Default %s() method... Override me!\n", __FUNCTION__); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Autonomous should go here. |
| 46 | * Programmers should override this method to run code that should run while the |
| 47 | * field is |
| 48 | * in the autonomous period. This will be called once each time the robot enters |
| 49 | * the |
| 50 | * autonomous state. |
| 51 | */ |
| 52 | void SampleRobot::Autonomous() { |
| 53 | printf("Default %s() method... Override me!\n", __FUNCTION__); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Operator control (tele-operated) code should go here. |
| 58 | * Programmers should override this method to run code that should run while the |
| 59 | * field is |
| 60 | * in the Operator Control (tele-operated) period. This is called once each time |
| 61 | * the robot |
| 62 | * enters the teleop state. |
| 63 | */ |
| 64 | void SampleRobot::OperatorControl() { |
| 65 | printf("Default %s() method... Override me!\n", __FUNCTION__); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Test program should go here. |
| 70 | * Programmers should override this method to run code that executes while the |
| 71 | * robot is |
| 72 | * in test mode. This will be called once whenever the robot enters test mode |
| 73 | */ |
| 74 | void SampleRobot::Test() { |
| 75 | printf("Default %s() method... Override me!\n", __FUNCTION__); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Robot main program for free-form programs. |
| 80 | * |
| 81 | * This should be overridden by user subclasses if the intent is to not use the |
| 82 | * Autonomous() and |
| 83 | * OperatorControl() methods. In that case, the program is responsible for |
| 84 | * sensing when to run |
| 85 | * the autonomous and operator control functions in their program. |
| 86 | * |
| 87 | * This method will be called immediately after the constructor is called. If it |
| 88 | * has not been |
| 89 | * overridden by a user subclass (i.e. the default version runs), then the |
| 90 | * Autonomous() and |
| 91 | * OperatorControl() methods will be called. |
| 92 | */ |
| 93 | void SampleRobot::RobotMain() { m_robotMainOverridden = false; } |
| 94 | |
| 95 | /** |
| 96 | * Start a competition. |
| 97 | * This code needs to track the order of the field starting to ensure that |
| 98 | * everything happens |
| 99 | * in the right order. Repeatedly run the correct method, either Autonomous or |
| 100 | * OperatorControl |
| 101 | * or Test when the robot is enabled. After running the correct method, wait for |
| 102 | * some state to |
| 103 | * change, either the other mode starts or the robot is disabled. Then go back |
| 104 | * and wait for the |
| 105 | * robot to be enabled again. |
| 106 | */ |
| 107 | void SampleRobot::StartCompetition() { |
| 108 | LiveWindow *lw = LiveWindow::GetInstance(); |
| 109 | |
| 110 | HALReport(HALUsageReporting::kResourceType_Framework, |
| 111 | HALUsageReporting::kFramework_Sample); |
| 112 | |
| 113 | SmartDashboard::init(); |
| 114 | NetworkTable::GetTable("LiveWindow") |
| 115 | ->GetSubTable("~STATUS~") |
| 116 | ->PutBoolean("LW Enabled", false); |
| 117 | |
| 118 | RobotInit(); |
| 119 | |
| 120 | // Tell the DS that the robot is ready to be enabled |
| 121 | HALNetworkCommunicationObserveUserProgramStarting(); |
| 122 | |
| 123 | RobotMain(); |
| 124 | |
| 125 | if (!m_robotMainOverridden) { |
| 126 | // first and one-time initialization |
| 127 | lw->SetEnabled(false); |
| 128 | |
| 129 | while (true) { |
| 130 | if (IsDisabled()) { |
| 131 | m_ds.InDisabled(true); |
| 132 | Disabled(); |
| 133 | m_ds.InDisabled(false); |
| 134 | while (IsDisabled()) m_ds.WaitForData(); |
| 135 | } else if (IsAutonomous()) { |
| 136 | m_ds.InAutonomous(true); |
| 137 | Autonomous(); |
| 138 | m_ds.InAutonomous(false); |
| 139 | while (IsAutonomous() && IsEnabled()) m_ds.WaitForData(); |
| 140 | } else if (IsTest()) { |
| 141 | lw->SetEnabled(true); |
| 142 | m_ds.InTest(true); |
| 143 | Test(); |
| 144 | m_ds.InTest(false); |
| 145 | while (IsTest() && IsEnabled()) m_ds.WaitForData(); |
| 146 | lw->SetEnabled(false); |
| 147 | } else { |
| 148 | m_ds.InOperatorControl(true); |
| 149 | OperatorControl(); |
| 150 | m_ds.InOperatorControl(false); |
| 151 | while (IsOperatorControl() && IsEnabled()) m_ds.WaitForData(); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |