-
Notifications
You must be signed in to change notification settings - Fork 1
/
Alex-pi_v2.cpp
275 lines (226 loc) · 5.19 KB
/
Alex-pi_v2.cpp
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
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdint.h>
#include "packet.h"
#include "serial.h"
#include "serialize.h"
#include "constants.h"
#define PORT_NAME "/dev/ttyACM0"
#define BAUD_RATE B57600
int exitFlag=0;
sem_t _xmitSema;
void handleError(TResult error)
{
switch(error)
{
case PACKET_BAD:
printf("ERROR: Bad Magic Number\n");
break;
case PACKET_CHECKSUM_BAD:
printf("ERROR: Bad checksum\n");
break;
default:
printf("ERROR: UNKNOWN ERROR\n");
}
}
void handleStatus(TPacket *packet)
{
printf("\n ------- ALEX STATUS REPORT ------- \n\n");
printf("Left Forward Ticks:\t\t%d\n", packet->params[0]);
printf("Right Forward Ticks:\t\t%d\n", packet->params[1]);
printf("Left Reverse Ticks:\t\t%d\n", packet->params[2]);
printf("Right Reverse Ticks:\t\t%d\n", packet->params[3]);
printf("Left Forward Ticks Turns:\t%d\n", packet->params[4]);
printf("Right Forward Ticks Turns:\t%d\n", packet->params[5]);
printf("Left Reverse Ticks Turns:\t%d\n", packet->params[6]);
printf("Right Reverse Ticks Turns:\t%d\n", packet->params[7]);
printf("Forward Distance:\t\t%d\n", packet->params[8]);
printf("Reverse Distance:\t\t%d\n", packet->params[9]);
printf("Color_red (red = 0, green = 1):\t\t%d\n", packet->params[10]);
printf("IR (front = 0, back = 1; left = 2, right = 3):\t\t%d\n", packet->params[11]);
printf("\n---------------------------------------\n\n");
}
void handleResponse(TPacket *packet)
{
// The response code is stored in command
switch(packet->command)
{
case RESP_OK:
printf("Command OK\n");
break;
case RESP_STATUS:
handleStatus(packet);
break;
default:
printf("Alex is confused.\n");
}
}
void handleErrorResponse(TPacket *packet)
{
// The error code is returned in command
switch(packet->command)
{
case RESP_BAD_PACKET:
printf("Arduino received bad magic number\n");
break;
case RESP_BAD_CHECKSUM:
printf("Arduino received bad checksum\n");
break;
case RESP_BAD_COMMAND:
printf("Arduino received bad command\n");
break;
case RESP_BAD_RESPONSE:
printf("Arduino received unexpected response\n");
break;
default:
printf("Arduino reports a weird error\n");
}
}
void handleMessage(TPacket *packet)
{
printf("Message from Alex: %s\n", packet->data);
}
void handlePacket(TPacket *packet)
{
switch(packet->packetType)
{
case PACKET_TYPE_COMMAND:
// Only we send command packets, so ignore
break;
case PACKET_TYPE_RESPONSE:
handleResponse(packet);
break;
case PACKET_TYPE_ERROR:
handleErrorResponse(packet);
break;
case PACKET_TYPE_MESSAGE:
handleMessage(packet);
break;
}
}
void sendPacket(TPacket *packet)
{
char buffer[PACKET_SIZE];
int len = serialize(buffer, packet, sizeof(TPacket));
serialWrite(buffer, len);
}
void *receiveThread(void *p)
{
char buffer[PACKET_SIZE];
int len;
TPacket packet;
TResult result;
int counter=0;
while(1)
{
len = serialRead(buffer);
counter+=len;
if(len > 0)
{
result = deserialize(buffer, len, &packet);
if(result == PACKET_OK)
{
counter=0;
handlePacket(&packet);
}
else
if(result != PACKET_INCOMPLETE)
{
printf("PACKET ERROR\n");
handleError(result);
}
}
}
}
void flushInput()
{
char c;
while((c = getchar()) != '\n' && c != EOF);
}
void getParams(TPacket *commandPacket)
{
flushInput();
}
void sendCommand(char command)
{
TPacket commandPacket;
commandPacket.packetType = PACKET_TYPE_COMMAND;
switch(command)
{
case 'w':
case 'W':
getParams(&commandPacket);
commandPacket.command = COMMAND_FORWARD;
sendPacket(&commandPacket);
break;
case 's':
case 'S':
getParams(&commandPacket);
commandPacket.command = COMMAND_REVERSE;
sendPacket(&commandPacket);
break;
case 'a':
case 'A':
getParams(&commandPacket);
commandPacket.command = COMMAND_TURN_LEFT;
sendPacket(&commandPacket);
break;
case 'd':
case 'D':
getParams(&commandPacket);
commandPacket.command = COMMAND_TURN_RIGHT;
sendPacket(&commandPacket);
break;
case 'x':
case 'X':
commandPacket.command = COMMAND_STOP;
sendPacket(&commandPacket);
break;
case 'c':
case 'C':
commandPacket.command = COMMAND_CLEAR_STATS;
commandPacket.params[0] = 0;
sendPacket(&commandPacket);
break;
case 'g':
case 'G':
commandPacket.command = COMMAND_GET_STATS;
sendPacket(&commandPacket);
break;
case 'q':
case 'Q':
exitFlag=1;
break;
default:
printf("Bad command\n");
}
}
int main()
{
// Connect to the Arduino
startSerial(PORT_NAME, BAUD_RATE, 8, 'N', 1, 5);
// Sleep for two seconds
printf("WAITING TWO SECONDS FOR ARDUINO TO REBOOT\n");
sleep(2);
printf("DONE\n");
// Spawn receiver thread
pthread_t recv;
pthread_create(&recv, NULL, receiveThread, NULL);
// Send a hello packet
TPacket helloPacket;
helloPacket.packetType = PACKET_TYPE_HELLO;
sendPacket(&helloPacket);
while(!exitFlag)
{
char ch;
printf("Command (f=forward, b=reverse, l=turn left, r=turn right, s=stop, c=clear stats, g=get stats q=exit)\n");
scanf("%c", &ch);
// Purge extraneous characters from input stream
flushInput();
sendCommand(ch);
}
printf("Closing connection to Arduino.\n");
endSerial();
}