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