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