-
Notifications
You must be signed in to change notification settings - Fork 1
/
wireless.pde
337 lines (317 loc) · 7.83 KB
/
wireless.pde
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* wireless.pde
Handles most communication with base station through XBEEs
Assumes the XBEE modules have been set up properly
(correct addresses, baud rates, etc)
*/
static void checkWireless()
{
uint8_t inByte;
// check timeout
if (micros()-commtimer > 500 && wirelessLength > 0)
{
#if DEBUG
SERIAL_DEBUG.println("WARNING: comm timeout");
#endif
wirelessLength = 0;
wirelessOpcode = OPCODE_NOP;
}
while (SERIAL_WIRELESS.available())
{
inByte = SERIAL_WIRELESS.read();
if ((wirelessLength == 0 && inByte == WIRELESS_START) || wirelessLength > 0)
{
wirelessPackage[wirelessLength] = inByte;
commtimer = micros();
if (inByte == WIRELESS_END)
{
if (wirelessLength > 1)
wirelessOpcode = wirelessPackage[1];
heartbeat = 1;
lastHeartbeat = millis();
} else {
wirelessLength++;
// check past bounds
if (wirelessLength == WIRELESS_BYTELIMIT)
{
#if DEBUG
SERIAL_DEBUG.println("WARNING: comm buffer full");
#endif
wirelessLength = 0;
}
}
}
}
}
// check wireless data availability
// attempt to parse incoming data as opcode + data package
static void checkWireless_old()
{
uint8_t ii = 0, done = 0;
uint8_t inByte;
if (SERIAL_WIRELESS.available())
{
// check for start of wireless message
if (SERIAL_WIRELESS.read() == WIRELESS_START)
{
done = 0;
ii = 0;
wirelessLength = 0;
// read the whole message
while (!done && SERIAL_WIRELESS.available() && ii<WIRELESS_BYTELIMIT)
{
// if first byte, read into opcode space
if (!ii)
{
wirelessOpcode = SERIAL_WIRELESS.read();
} else { // else, read message
inByte = SERIAL_WIRELESS.read();
SERIAL_DEBUG.println(inByte);
// check for end
if (inByte == WIRELESS_END)
{
done = 1;
} else {
// if we still have space for values
if (wirelessLength < WIRELESS_BYTELIMIT)
{
wirelessPackage[wirelessLength] = inByte;
wirelessLength++;
} else {
// hit limit of package, no end
done = 1; // ?
#if DEBUG
SERIAL_DEBUG.println("hit limit of buffer");
#endif
}
}
}
ii++;
}
// check for early termination
if (!done)
{
// clear out the operation
SERIAL_DEBUG.println(wirelessOpcode);
wirelessOpcode = OPCODE_NOP;
wirelessLength = 0;
#if DEBUG
SERIAL_DEBUG.println("unfinished command, clearing buffer");
#endif
}
}
}
}
// parse the 1 byte opcode and execute some code
// reset the opcode at the end
static void parseCommand()
{
//#if DEBUG
// SERIAL_DEBUG.print("parsing command: ");
// SERIAL_DEBUG.println(wirelessOpcode, HEX);
//#endif
switch (wirelessOpcode)
{
case OPCODE_HEARTBEAT:
heartbeat = 1;
lastHeartbeat = micros();
#if DEBUG
// SERIAL_DEBUG.println("HEARTBEAT");
#endif
break;
case OPCODE_ARM:
if (!armed) arm_motors();
break;
case OPCODE_KILL:
disarm_motors();
break;
case OPCODE_CALIB:
calibrate_motors();
break;
case OPCODE_THROTTLE:
throttle = wirelessPackage[2];
break;
case OPCODE_FLIGHTMODE:
changeFlightmode(wirelessPackage[2]);
break;
case OPCODE_USERINPUT:
if (wirelessLength >= 7)
{
if (verify(wirelessPackage[6]))
{
userPitch = wirelessPackage[2];
userRoll = wirelessPackage[3];
userYaw = wirelessPackage[4];
userLift = wirelessPackage[5];
}
#if DEBUG
/*
SERIAL_DEBUG.print((uint8_t)userPitch);
SERIAL_DEBUG.print("\t");
SERIAL_DEBUG.print((uint8_t)userRoll);
SERIAL_DEBUG.print("\t");
SERIAL_DEBUG.print((uint8_t)userYaw);
SERIAL_DEBUG.print("\t");
SERIAL_DEBUG.print((uint8_t)userLift);
SERIAL_DEBUG.print("\t");
SERIAL_DEBUG.println((uint8_t)wirelessPackage[6]);
*/
#endif
}
break;
case OPCODE_SENDSTATS:
debugmode = 1;
break;
// start PID stuff
case OPCODE_PID_KP:
if (wirelessLength >= 6)
{
memcpy(&kftemp, wirelessPackage+2, 4);
if (kftemp >= 0.0 && kftemp < 2.0)
{
kp_roll = kp_pitch = kftemp;
savePIDvals();
}
}
break;
case OPCODE_PID_KD:
if (wirelessLength >= 6)
{
memcpy(&kftemp, wirelessPackage+2, 4);
if (kftemp >= 0.0 && kftemp < 500.0) {
kd_roll = kd_pitch = kftemp;
savePIDvals();
}
}
break;
case OPCODE_PID_KI:
if (wirelessLength >= 6)
{
memcpy(&kftemp, wirelessPackage+2, 4);
if (kftemp >= 0.0 && kftemp < 10.0) {
ki_roll = ki_pitch = kftemp;
savePIDvals();
}
}
break;
case OPCODE_PID_CHECK:
dosendPID = 1;
break;
case OPCODE_MOTORDEBUG:
motordebug++;
if (motordebug==6) motordebug = 0;
break;
case OPCODE_STARTLOG:
reset_log();
logging = 1;
break;
case OPCODE_STOPLOG:
logging = 0;
break;
case OPCODE_CLEARLOG:
reset_log();
break;
case OPCODE_PRINTLOG:
print_log();
break;
}
// at the end of execution, reset opcode
wirelessOpcode = OPCODE_NOP;
wirelessLength = 0;
}
/* verify checksum for userinput command */
uint8_t verify (uint8_t chk)
{
uint8_t chk2, ii;
chk2 = wirelessPackage[0];
for (ii=1; ii<6; ii++)
chk2 ^= wirelessPackage[ii];
return (chk == chk2);
}
// send a caution message back to the basestation
void caution(uint8_t message)
{
#ifdef SEND_CAUTIONS
if (millis() - cautiontimer > 1000)
{
SERIAL_WIRELESS.write(COMM_START);
SERIAL_WIRELESS.write(COMM_MODE_CAUTION);
SERIAL_WIRELESS.write(message);
SERIAL_WIRELESS.write(COMM_END);
cautiontimer = millis();
}
#endif
}
// send current PID gain values to basestation
static void sendPID()
{
#ifdef DEBUG
SERIAL_DEBUG.println("sending PID info..");
#endif
SERIAL_WIRELESS.write(COMM_START);
SERIAL_WIRELESS.write(COMM_MODE_PID);
SERIAL_WIRELESS.write((uint8_t*)&kp_roll,4);
SERIAL_WIRELESS.write((uint8_t*)&kd_roll,4);
SERIAL_WIRELESS.write((uint8_t*)&ki_roll,4);
SERIAL_WIRELESS.write(COMM_END);
dosendPID = 0;
}
static void sendHeartbeat()
{
SERIAL_WIRELESS.write(COMM_START);
SERIAL_WIRELESS.write(OPCODE_HEARTBEAT);
SERIAL_WIRELESS.write(COMM_END);
}
// send debug info over wireless
// there are flags deciding what to send and how often
static void sendDebug()
{
// only enter loop if the debug flag bit is set
if ((debugFlag>>debugmode)&0x01 || 1)
{
#ifdef DEBUG
SERIAL_DEBUG.println("sending stats");
SERIAL_DEBUG.println(debugmode);
#endif
SERIAL_WIRELESS.write(COMM_START);
// now figure out what to send
switch (debugmode)
{
case 1: // IMU
SERIAL_WIRELESS.write(COMM_MODE_IMU);
SERIAL_WIRELESS.write((uint8_t*)&pitch,4);
SERIAL_WIRELESS.write((uint8_t*)&roll,4);
SERIAL_WIRELESS.write((uint8_t*)&yaw,4);
break;
case 2: // position
SERIAL_WIRELESS.write(COMM_MODE_POS);
SERIAL_WIRELESS.write((uint8_t*)&gps_xpos,4);
SERIAL_WIRELESS.write((uint8_t*)&gps_ypos,4);
SERIAL_WIRELESS.write((uint8_t*)&altitude,4);
break;
case 3: // motor values
SERIAL_WIRELESS.write(COMM_MODE_MOTOR);
SERIAL_WIRELESS.write(motorval,6);
break;
case 4: // battery levels
SERIAL_WIRELESS.write(COMM_MODE_BATT);
SERIAL_WIRELESS.write(batterylevel,6);
break;
case 5: // flight stats
SERIAL_WIRELESS.write(COMM_MODE_STATS);
SERIAL_WIRELESS.write(flightMode);
SERIAL_WIRELESS.write(armed);
SERIAL_WIRELESS.write((uint8_t*)&flighttime,4);
break;
case 6: // PID integral values
SERIAL_WIRELESS.write(COMM_MODE_PID_INT);
SERIAL_WIRELESS.write((uint8_t*)&intPitch,4);
SERIAL_WIRELESS.write((uint8_t*)&intRoll,4);
SERIAL_WIRELESS.write((uint8_t*)&intYaw,4);
break;
}
SERIAL_WIRELESS.write(COMM_END);
SERIAL_WIRELESS.write('\r');
}
debugmode++;
if (debugmode >= 7) debugmode = 0;
}