-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDFW.cpp
More file actions
257 lines (227 loc) · 5.89 KB
/
DFW.cpp
File metadata and controls
257 lines (227 loc) · 5.89 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
/* Serial controller library for DFWireless controllers with wired connection or XBee connection
written by Joe St. Germain
For RBE Class use
joest@wpi.edu
*/
#include "Arduino.h"
#include "DFW.h"
// used in the setup portion of your program
// baud = defualt is 9600
//port_num = serial1 lowercase s defualt is serial1
void DFW::run(void) {
update();
unsigned long tmp = 0;
long timeDiff = 0;
switch (state) {
case powerup:
digitalWrite(robot->getDebugLEDPin(), 1);
Serial.println("\r\nwaiting for DWF init...");
if (!start()) {
state = waitForAuto;
Serial.println("\r\nwaiting for auto (press start)...");
} else
break;
/* no break */
case waitForAuto:
if (start()) {
robot->robotStartup();
state = Autonomous;
autoStartTime = millis(); // sets start time of autonomous
Serial.println("\r\nRunning Auto...");
// fall through when a state changes
} else
break;
/* no break */
case Autonomous:
timeDiff = millis() - autoStartTime;
if (timeDiff > autoTime) {
state = waitForTeleop;
robot->robotShutdown();
Serial.println("\r\nwaiting for teleop (press start)...");
// fall through when a state changes
} else {
tmp = millis();
robot->autonomous(autoTime - timeDiff);
if (functionReturnTime < (millis() - tmp)) {
Serial.print(
"\r\n\r\nERROR!! user Functions should return in ");
Serial.print(functionReturnTime);
Serial.print(" ms, took ");
Serial.print((millis() - tmp));
Serial.println(" ms ");
}
break;
}
/* no break */
case waitForTeleop:
if (start()) {
state = Teleop;
teleopStartTime = millis(); // sets start time of autonomous
Serial.println("\r\nRunning Teleop...");
// fall through when a state changes
} else
break;
/* no break */
case Teleop:
timeDiff = millis() - teleopStartTime;
if (timeDiff > teleopTime) {
state = waitForAuto;
robot->robotShutdown();
Serial.println("\r\nwaiting for auto (press start)...");
} else {
tmp = millis();
robot->teleop(teleopTime - timeDiff);
if (functionReturnTime < (millis() - tmp)) {
Serial.print(
"\r\n\r\nERROR!! user Functions should return in ");
Serial.print(functionReturnTime);
Serial.print(" ms, took ");
Serial.print((millis() - tmp));
Serial.println(" ms ");
}
break;
}
}
}
void DFW::update(void) {
while (Serial1.peek() != 'A' && Serial1.available() >= packetSize) {
Serial1.read();
}
if (Serial1.peek() == 'A' && Serial1.available() >= packetSize) {
packet[0] = Serial1.read();
if (Serial1.peek() == 'c' && Serial1.available() >= (packetSize - 1)) {
for (int i = 1; i < packetSize; i++) {
packet[i] = Serial1.read();
}
if (packet[0] == 'A' && packet[1] == 'c' && packet[6] == 'a'
&& packet[9] == 'B' && packet[12] == 'b') {
for (int i = 0; i < 2; i++) {
byteBu[i] = packet[10 + i];
}
for (int i = 0; i < 4; i++) {
byteAn[i] = packet[2 + i];
}
}
lastHeartBeatTime = millis();
}
}
//Heartbeat need to read once every 1 seconds
hbTime = millis() - lastHeartBeatTime;
if (hbTime > 1000) {
Serial.print("\r\nHB tripped clearing data. Diff= ");
Serial.print(hbTime);
Serial.print(" lastTime = ");
Serial.print(lastHeartBeatTime);
Serial.print(" current Time = ");
Serial.print(millis());
digitalWrite(robot->getDebugLEDPin(), 1);
for (int i = 0; i < buttonBytes; i++) {
byteBu[i] = 127;
}
for (int i = 0; i < analogBytes; i++) {
byteAn[i] = 90;
}
return;
} else {
long timeSinceFlash = millis() - flashTime;
if (timeSinceFlash > 1000) {
flashTime = millis();
} else if (timeSinceFlash > 900) {
digitalWrite(robot->getDebugLEDPin(), 1);
} else {
digitalWrite(robot->getDebugLEDPin(), 0);
}
}
}
DFW::DFW(AbstractDFWRobot * myrobot){
robot=myrobot;
startup();
}
DFW::DFW(int debugpin){
robot=new DymmyDFWRobot(debugpin);
startup();
}
void DFW::startup(){
state = powerup;
pinMode(robot->getDebugLEDPin(), OUTPUT);
digitalWrite(robot->getDebugLEDPin(), 0);
};
CompetitionState DFW::getCompetitionState(void) {
return state;
}
void DFW::begin() //serial1 is the only functional input
{
Serial1.begin(9600);
Serial1.setTimeout(250);
}
int DFW::joystickrv(void) {
return (unsigned char) byteAn[0];
}
int DFW::joystickrh(void) {
return (unsigned char) byteAn[1];
}
int DFW::joysticklv(void) {
return (unsigned char) byteAn[2];
}
int DFW::joysticklh(void) {
return (unsigned char) byteAn[3];
}
bool DFW::start(void) // returns start button state
{
return ((byteBu[1] & 0b00000010) == 0);
}
bool DFW::select(void) // select button state
{
return ((byteBu[1] & 0b00000001) == 0);
}
bool DFW::one(void) // button one state
{
return ((byteBu[0] & 0b01000000) == 0);
}
bool DFW::two(void) // button two state
{
return ((byteBu[0] & 0b00100000) == 0);
}
bool DFW::three(void) //button three state
{
return ((byteBu[0] & 0b00010000) == 0);
}
bool DFW::four(void) //button four state
{
return ((byteBu[0] & 0b00001000) == 0);
}
bool DFW::up(void) //up button state
{
return ((byteBu[1] & 0b00001000) == 0);
}
bool DFW::down(void) //down button state
{
return ((byteBu[1] & 0b00000100) == 0);
}
bool DFW::left(void) //left button state
{
return ((byteBu[1] & 0b00100000) == 0);
}
bool DFW::right(void) //right button state
{
return ((byteBu[1] & 0b00010000) == 0);
}
bool DFW::l1(void) //L1 button state
{
return ((byteBu[0] & 0b00000100) == 0);
}
bool DFW::l2(void) //L2 button state
{
return ((byteBu[0] & 0b00000001) == 0);
}
bool DFW::r1(void) //R1 button state
{
return ((byteBu[0] & 0b00000010) == 0);
}
bool DFW::r2(void) //R2 button state
{
return ((byteBu[1] & 0b01000000) == 0);
}
void DFW::end(void) {
Serial1.end();
}