-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComms.h
More file actions
232 lines (203 loc) · 7.54 KB
/
Comms.h
File metadata and controls
232 lines (203 loc) · 7.54 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
#ifndef Comms_h
#define Comms_h
/*-----------------
Includes
-----------------*/
#if ARDUINO < 100
#include "WProgram.h"
#else
#include "Arduino.h"
#endif
#include "Qconfig.h"
/// --- VARS --- ///
unsigned char cntrl_str[COMMAND_SIZE-1];
bool data_received = false;
bool clientStarted = false;
unsigned long last_recv;
unsigned long last_cmd;
/// --- RECEIVE --- ///
void receiveData()
{
if(SERIAL_AVAILABLE() >= COMMAND_SIZE)
{
unsigned char recv_head = SERIAL_READ(); // Read first byte.
// Wait till HEAD received
while(recv_head != SERIAL_HEAD)
{
QDEBUG_PRINT(F("Head: "));
QDEBUG_PRINTLN(recv_head);
QDEBUG_PRINT(F("Expected: "));
QDEBUG_PRINTLN(SERIAL_HEAD);
if(SERIAL_AVAILABLE() >= COMMAND_SIZE)
recv_head = SERIAL_READ();
else
QDEBUG_PRINT(F("Clearing read buffer."));
while(SERIAL_AVAILABLE() > 0) // Clear read buffer
SERIAL_READ();
return;
}
// Read up command:
SERIAL_READ_BYTES(cntrl_str, COMMAND_SIZE - 1);
// If matching "signature": (means that it is really a command)
if(cntrl_str[COMMAND_SIZE-2] == SERIAL_TAIL)
{
int32_t temp_PARAMS[MAX_CNTRL_PARAMS];
int32_t cmd = -1;
for(int i = 0; i < (MAX_CNTRL_PARAMS + 1) * 4; i=i+4)
{
// Little-endian byte array to signed integer conversion
int32_t st = (cntrl_str[i+3] << 24) | (cntrl_str[i+2] << 16) | (cntrl_str[i+1] << 8) | (cntrl_str[i]);
if(i == 0)
cmd = st;
if(!(cmd >= 0 && cmd <= NUM_STATES-1))
break;
else
temp_PARAMS[i/4 - 1] = st;
}
if(cmd >= 0 && cmd <= NUM_STATES-1)
{
STATE = cmd;
memcpy(PARAMS, temp_PARAMS, MAX_CNTRL_PARAMS); // Copy temp params to real params
// Debug
QDEBUG_PRINT(F("Changing STATE to: "));
QDEBUG_PRINTLN(STATE);
QDEBUG_PRINT(F("Params are: "));
for(int i=0; i < MAX_CNTRL_PARAMS - 1; i++)
{
QDEBUG_PRINT(PARAMS[i]);
QDEBUG_PRINT(F("; "));
}
QDEBUG_PRINTLN(PARAMS[MAX_CNTRL_PARAMS - 1]);
}
else if (cmd == Q_ACK)
{
last_recv = uptime;
data_received = true;
}
else if (cmd == Q_SYN)
{
// Clear params
for (int c = 0; c < MAX_CNTRL_PARAMS; c++)
PARAMS[c] = 0;
STATE = FLY;
QDEBUG_PRINTLN(F("Client connected, setting STATE to FLY."));
clientStarted = true;
data_received = true;
}
else
{
QDEBUG_PRINT(F("Unknown command: "));
QDEBUG_PRINTLN(cmd);
}
if (cmd != Q_ACK)
{
last_recv = uptime;
QDEBUG_PRINTLN(F("Acknowledging command."));
SERIAL_PRINTLN(Q_ACK, 0, 0);
}
}
else
{
QDEBUG_PRINT(F("Tail: "));
QDEBUG_PRINTLN(cntrl_str[COMMAND_SIZE-2]);
QDEBUG_PRINT(F("Expected Tail: "));
QDEBUG_PRINTLN(SERIAL_TAIL);
}
// Update time since inactive:
zero_time = uptime;
}
else if(SERIAL_AVAILABLE() > 0)
{
/*QDEBUG_PRINT(F("Has: "));
QDEBUG_PRINT(SERIAL_AVAILABLE());
QDEBUG_PRINT(F(" ; Client started: "));
QDEBUG_PRINTLN(clientStarted); */
}
}
/// --- SEND --- ///
void sendStatus()
{
if(!clientStarted)
last_recv = uptime;
if(!data_received && ((uptime - last_recv) / 1000 > 5)) // Go to sleep if no acknowledge for more than 5 secs.
{
QDEBUG_PRINT(F("Client did not acknowledge message "));
QDEBUG_PRINT(last_cmd);
QDEBUG_PRINTLN(F(" within 5secs - going to sleep."));
STATE = SLEEP;
return;
}
if(STATE == FLY && clientStarted && data_received)
{
int* thrusts = VoCopter.GetMotors();
// FORMAT: ' UP_TIME ; STATE ; YAW,PITCH,ROLL,TEMP,HEADING,ALTITUDE ; FRONT_LEFT,FRONT_RIGHT,BACK_LEFT,BACK_RIGHT ; BAT_LVL '
SERIAL_PRINTLN(Q_STATUS, 13,
static_cast<int32_t>((uptime / 1000)),
static_cast<int32_t>(STATE),
static_cast<int32_t>(VoCopter.GetYawI()),
static_cast<int32_t>(VoCopter.GetPitchI()),
static_cast<int32_t>(VoCopter.GetRollI()),
static_cast<int32_t>(VoCopter.GetTemp()),
static_cast<int32_t>(VoCopter.GetHeading()),
static_cast<int32_t>(VoCopter.GetAltitude()),
static_cast<int32_t>(thrusts[0]),
static_cast<int32_t>(thrusts[1]),
static_cast<int32_t>(thrusts[2]),
static_cast<int32_t>(thrusts[3]),
static_cast<int32_t>(BatLvl)
);
}
}
/*------------------------------
Serial command builder
------------------------------*/
void insertToCmdBuffer (unsigned char buffer[], int dataLen, const unsigned char data[], int startingFrom)
{
for(int i = 0; i < dataLen; i++)
if(startingFrom + i < COMMAND_SIZE) // Not overflowing
buffer[startingFrom + i] = data[i];
}
void SERIAL_PRINTLN(unsigned long cmd, int len, ...)
{
//Declare a va_list macro and initialize it with va_start
va_list argList;
va_start(argList, len);
if(clientStarted && (data_received || cmd == Q_ACK))
{
unsigned char buffer[COMMAND_SIZE];
int count;
int32_t param;
//Send head bytes
buffer[0] = SERIAL_HEAD;
//Send CMD in Little-endian
param = static_cast<int32_t>(cmd);
insertToCmdBuffer(buffer, 4, reinterpret_cast<const unsigned char*>(¶m), 1);
//Send arguments in Little-endian
for(count = 0; count < len; count++)
{
if(count < MAX_CNTRL_PARAMS)
{
param = va_arg(argList, int32_t);
insertToCmdBuffer(buffer, 4, reinterpret_cast<const unsigned char*>(¶m), 5 + 4 * count);
}
}
//Fill up buffer
for(; count < MAX_CNTRL_PARAMS; count++)
insertToCmdBuffer(buffer, 4, SERIAL_EMPTY, 5 + 4 * count);
//Send end bytes
buffer[COMMAND_SIZE-1] = SERIAL_TAIL;
// Make sure all previous data was sent:
SERIAL_FLUSH();
// Send the data:
SERIAL_WRITE(buffer, COMMAND_SIZE);
if (cmd != Q_ACK)
{
last_cmd = cmd;
data_received = false;
}
}
else if (!clientStarted)
QDEBUG_PRINTLN(F("Client not started, cannot send command."));
va_end(argList);
}
#endif