-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterativeRobot.cpp
More file actions
291 lines (261 loc) · 7.85 KB
/
Copy pathIterativeRobot.cpp
File metadata and controls
291 lines (261 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "IterativeRobot.h"
#include "NetworkCommunication/FRCComm.h"
#include "Utility.h"
/**
* Constructor for RobotIterativeBase
*
* The constructor initializes the instance variables for the robot to indicate
* the status of initialization for disabled, autonomous, and teleop code.
*/
IterativeRobot::IterativeRobot()
: m_packetDataAvailableSem (0)
{
printf("RobotIterativeBase Constructor Start\n");
// set status for initialization of disabled, autonomous, and teleop code.
m_disabledInitialized = false;
m_autonomousInitialized = false;
m_teleopInitialized = false;
// keep track of the number of continuous loops performed per period
m_disabledLoops = 0;
m_autonomousLoops = 0;
m_teleopLoops = 0;
// Create a new semaphore
m_packetDataAvailableSem = semBCreate (SEM_Q_PRIORITY, SEM_EMPTY);
// Register that semaphore with the network communications task.
// It will signal when new packet data is available.
setNewDataSem(m_packetDataAvailableSem);
m_period = kDefaultPeriod;
// Start the timer for the main loop
printf("RobotIterativeBase Constructor Finish\n");
}
/**
* Free the resources for a RobotIterativeBase class.
*/
IterativeRobot::~IterativeRobot()
{
// Unregister our semaphore.
setNewDataSem(0);
}
/**
* Set the period for the periodic functions.
*
* @deprecated The periodic functions are now synchronized with the receipt of packets from the Driver Station.
*/
void IterativeRobot::SetPeriod(double period)
{
wpi_assert(false);
}
/**
* Get the number of loops per second for the IterativeRobot
*
* Get the number of loops per second for the IterativeRobot. The default period of
* 0.005 seconds results in 200 loops per second. (200Hz iteration loop).
*/
double IterativeRobot::GetLoopsPerSec()
{
return 1.0 / m_period;
}
/**
* Provide an alternate "main loop" via StartCompetition().
*
* This specific StartCompetition() implements "main loop" behavior like that of the FRC
* control system in 2008 and earlier, with a primary (slow) loop that is
* called periodically, and a "fast loop" (a.k.a. "spin loop") that is
* called as fast as possible with no delay between calls.
*/
void IterativeRobot::StartCompetition()
{
printf("RobotIterativeBase StartCompetition() Commenced\n");
// first and one-time initialization
RobotInit();
// loop forever, calling the appropriate mode-dependent function
while (TRUE)
{
// Call the appropriate function depending upon the current robot mode
if (IsDisabled())
{
// call DisabledInit() if we are now just entering disabled mode from
// either a different mode or from power-on
if(!m_disabledInitialized)
{
DisabledInit();
m_disabledInitialized = true;
// reset the initialization flags for the other modes
m_autonomousInitialized = false;
m_teleopInitialized = false;
printf("Disabled_Init() completed\n");
}
if (NextPeriodReady())
{
DisabledPeriodic();
}
DisabledContinuous();
}
else if (IsAutonomous())
{
// call AutonomousInit() if we are now just entering autonomous mode from
// either a different mode or from power-on
if(!m_autonomousInitialized)
{
// KBS NOTE: old code reset all PWMs and relays to "safe values"
// whenever entering autonomous mode, before calling
// "Autonomous_Init()"
AutonomousInit();
m_autonomousInitialized = true;
// reset the initialization flags for the other modes
m_disabledInitialized = false;
m_teleopInitialized = false;
printf("Autonomous_Init() completed\n");
}
if (NextPeriodReady())
{
AutonomousPeriodic();
}
AutonomousContinuous();
}
else
{
// call TeleopInit() if we are now just entering teleop mode from
// either a different mode or from power-on
if(!m_teleopInitialized)
{
TeleopInit();
m_teleopInitialized = true;
// reset the initialization flags for the other modes
m_disabledInitialized = false;
m_autonomousInitialized = false;
printf("Teleop_Init() completed\n");
}
if (NextPeriodReady())
{
TeleopPeriodic();
}
TeleopContinuous();
}
}
printf("RobotIterativeBase StartCompetition() Ended\n");
}
/**
* Determine if the appropriate next periodic function should be called.
*
* This function makes adjust for lateness in the cycle by keeping track of how late
* it was and crediting the next period with that amount.
*/
//TODO: decide what this should do if it slips more than one cycle.
bool IterativeRobot::NextPeriodReady()
{
int success = semTake(m_packetDataAvailableSem, 0);
if(success == OK)
{
return true;
}
return false;
}
/**
* Robot-wide initialization code should go here.
*
* Users should override this method for default Robot-wide initialization which will
* be called when the robot is first powered on. It will be called exactly 1 time.
*/
void IterativeRobot::RobotInit()
{
printf("Default RobotIterativeBase::RobotInit() method running\n");
}
/**
* Initialization code for disabled mode should go here.
*
* Users should override this method for initialization code which will be called each time
* the robot enters disabled mode.
*/
void IterativeRobot::DisabledInit()
{
printf("Default RobotIterativeBase::DisabledInit() method running\n");
}
/**
* Initialization code for autonomous mode should go here.
*
* Users should override this method for initialization code which will be called each time
* the robot enters autonomous mode.
*/
void IterativeRobot::AutonomousInit()
{
printf("Default RobotIterativeBase::AutonomousInit() method running\n");
}
/**
* Initialization code for teleop mode should go here.
*
* Users should override this method for initialization code which will be called each time
* the robot enters teleop mode.
*/
void IterativeRobot::TeleopInit()
{
printf("Default RobotIterativeBase::TeleopInit() method running\n");
}
/**
* Periodic code for disabled mode should go here.
*
* Users should override this method for code which will be called periodically at a regular
* rate while the robot is in disabled mode.
*/
void IterativeRobot::DisabledPeriodic()
{
printf("D - %d\n", m_disabledLoops);
m_disabledLoops = 0;
}
/**
* Periodic code for autonomous mode should go here.
*
* Users should override this method for code which will be called periodically at a regular
* rate while the robot is in autonomous mode.
*/
void IterativeRobot::AutonomousPeriodic()
{
printf("A - %d\n", m_autonomousLoops);
m_autonomousLoops = 0;
}
/**
* Periodic code for teleop mode should go here.
*
* Users should override this method for code which will be called periodically at a regular
* rate while the robot is in teleop mode.
*/
void IterativeRobot::TeleopPeriodic()
{
printf("T - %d", m_teleopLoops);
m_teleopLoops = 0;
}
/**
* Continuous code for disabled mode should go here.
*
* Users should override this method for code which will be called repeatedly as frequently
* as possible while the robot is in disabled mode.
*/
void IterativeRobot::DisabledContinuous()
{
m_disabledLoops++;
}
/**
* Continuous code for autonomous mode should go here.
*
* Users should override this method for code which will be called repeatedly as frequently
* as possible while the robot is in autonomous mode.
*/
void IterativeRobot::AutonomousContinuous()
{
m_autonomousLoops++;
}
/**
* Continuous code for teleop mode should go here.
*
* Users should override this method for code which will be called repeatedly as frequently
* as possible while the robot is in teleop mode.
*/
void IterativeRobot::TeleopContinuous()
{
m_teleopLoops++;
}