-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
282 lines (242 loc) · 5.63 KB
/
functions.c
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
#include "functions.h"
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
int uitgangen[12][2] =
{
{1,0}, //1
{2,0}, //2
{3,0}, //3
{4,1}, //4
{4,2}, //5
{4,3}, //6
{3,4}, //
{2,4}, //
{1,4}, //9
{0,3}, //
{0,2}, //
{0,1} //12
};
int direction_exit[12] = {2,2,2,1,1,1,0,0,0,3,3,3};
int curr_direction = 0; //0 = north
//int curr_position[2]= {1,0};
int curr_position[2];
int checkpoints[3];
int res[50];
int curr_command = 0;
int mod (int a, int b)
{
int ret = a % b;
if(ret < 0)
ret+=b;
return ret;
}
void turn(int control)
{
//control:
//1 counter clockwise
//2 U turn
//3 clockwise
switch(control)
{
case CCLOCKWISE :
printf("turn(%d) ccw (rightwheel only)\n",control);
//todo
res[curr_command] = LEFTDATA;
printf("%x \n", res[curr_command]);
curr_command++;
curr_direction--;
break;
case UTURN :
printf("turn(%d) U-turn",control);
//todo
res[curr_command] = RIGHTDATA;
printf("%x \n", res[curr_command]);
curr_command++;
res[curr_command] = RIGHTDATA;
printf("%x \n", res[curr_command]);
curr_command++;
curr_direction += 2;
break;
case CLOCKWISE :
printf("turn(%d) cw (leftwheel only)\n",control);
//todo
res[curr_command] = RIGHTDATA;
printf("%x \n", res[curr_command]);
curr_command++;
curr_direction++;
break;
}
curr_direction = mod(curr_direction,4);
print_data();
}
void drive(int distance)
{
int temp_distance = 0;
distance = abs(distance);
switch(curr_direction)
{
case NORTH :
curr_position[1] +=distance;
break;
case EAST:
curr_position[0] +=distance;
break;
case SOUTH :
curr_position[1] -=distance;
break;
case WEST :
curr_position[0] -=distance;
break;
}
//printf("drive(%d)\t(%d)\n", distance,temp);
printf("drive(%d)\n", distance);
//todo
temp_distance = distance *2;
while(temp_distance){
res[curr_command] = FORWARDDATA;
printf("%x \n", res[curr_command]);
curr_command++;
temp_distance--;
}
print_data();
}
void print_curr_position()
{
printf("curr_position:(%d,%d)\n\n",curr_position[0],curr_position[1]);
}
void print_data()
{
printf("\tcurr_position:(%d,%d)\tcurr_direction:%d\n\n\n\n",curr_position[0],curr_position[1],curr_direction);
}
void send_commands()
{
int i =0;
//initSerial(globalArgs.comport);
initSerial("COM3");
char byteBuffer[32] = {0};
for (i = 0; res[i] != 0; i++)
{
while (byteBuffer[0] != BLACKDATA)
{
readByte(byteBuffer);
printf("waiting for data. byteBuffer[0] = %x\n", byteBuffer[0]);
}
while (byteBuffer[0] == BLACKDATA)
{
writeByte(&res[i]);
printf("Written data = %x\n", res[i]);
memset(byteBuffer, 0, sizeof(byteBuffer));
readByte(byteBuffer);
}
}
}
void print_commands()
{
//TODO
for(curr_command = 0 ; res[curr_command]!=0 ; curr_command++)
{
printf("%x \n", res[curr_command]);
}
}
void drive_exit(int direction)
{
printf("Direction exit: %d\t curr_direction:%d\n",direction,curr_direction);
//Need a turn
if(curr_direction%2 != direction%2)
{
//printf("Make a turn for Exit!\n");
if(curr_direction<direction)
{
turn(CLOCKWISE); // Right
}
else
{
turn(CCLOCKWISE); // Left
}
}
else
{
//printf("Else\n");
if(curr_direction == (direction+2) % 4)
{
turn(UTURN); // U-turn
}
}
printf("Drove 12cm for an exit\n");
res[curr_command] = FORWARDDATA;
printf("%x \n", res[curr_command]);
curr_command++;
print_data();
}
void drive_to_intersection(int uitgang)
{
// uitgang is # in array
int driveX,driveY;
if(curr_direction%2)
{
//HORIZONTAL
//printf("drive horizontal first\n\n");
driveX = uitgangen[uitgang][0] - curr_position[0];
//printf("driveX now");
if(driveX==0)
{
printf("drive(NULL)\n\n");
}
else
{
drive(driveX);
}
//printf("driveY now");
driveY = uitgangen[uitgang][1]- curr_position[1];
if(driveY==0)
{
printf("drive(NULL)\n\n");
}
else if(driveY >0)
{
if(curr_direction==3) turn(CLOCKWISE);
if(curr_direction==1) turn(CCLOCKWISE);
drive(driveY);
}
else
{
if(curr_direction==3) turn(CCLOCKWISE);
if(curr_direction==1) turn(CLOCKWISE);
drive(-driveY);
}
}
else
{
//VERTICAL:
printf("drive vertical first\n\n");
driveY = uitgangen[uitgang][1]- curr_position[1];
if(driveY==0)
{
printf("drive(NULL)\n\n");
//return;
}
else
{
drive(driveY);
}
driveX = uitgangen[uitgang][0] - curr_position[0];
if(driveX==0)
{
printf("drive(NULL)\n\n");
//return;
}
else if(driveX >0)
{
if(curr_direction==0) turn(CLOCKWISE);
if(curr_direction==2) turn(CCLOCKWISE);
drive(driveX);
}
else
{
if(curr_direction==0) turn(CCLOCKWISE);
if(curr_direction==2) turn(CLOCKWISE);
drive(-driveX);
}
}
}