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