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