root / trunk / code / projects / traffic_navigation / validTurns.c @ 1988
History | View | Annotate | Download (3.85 KB)
1 |
/*
|
---|---|
2 |
* Deterministic turning implemented using table lookup. Using a
|
3 |
* table, the function looks up a value from the table using the
|
4 |
* barcode value, and then the robot decides to go straight,
|
5 |
* left, right, or u-turn depending on the valid turn types
|
6 |
* for the intersection it is at.
|
7 |
*
|
8 |
*/
|
9 |
|
10 |
|
11 |
/******************************Random Num Gen Version of validTurns*/
|
12 |
|
13 |
#include "intersectData.h" |
14 |
#include "../linefollowing/lineDrive.h" |
15 |
#include "validTurns.h" |
16 |
|
17 |
#define GET16(s)( ((s)&0x8000)>>16 ) /*gets the 16 bit*/ |
18 |
#define GET14(s)( ((s)&0x2000)>>14 ) /*gets the 14thbit*/ |
19 |
#define GET13(s)( ((s)&0x1000)>>13 ) /*you get the idea*/ |
20 |
#define GET11(s)( ((s)&0x400)>>11 ) |
21 |
#define CYCLES 10 /*the number of reseeds i perform before finally extracting my ranodm number*/ |
22 |
#define SHIFT(s)( (s)<<1) |
23 |
#define RESEED(s)( (s)=( ( ((GET16(s)^GET14(s))^GET13(s))^GET11(s) ) | SHIFT(s) ) )/*reseeding the first bit of the number with bits from the number*/ |
24 |
|
25 |
unsigned int seed = 0xC0DE; |
26 |
int randomNumGen(int max){ |
27 |
//int a = 0; // not currently in use
|
28 |
//int b = 0xBEEF;
|
29 |
seed++; |
30 |
return seed%4; |
31 |
} |
32 |
|
33 |
/*
|
34 |
int randomNumGen(int max){
|
35 |
return rtc_get() % max;
|
36 |
}
|
37 |
int randomNumGen(int max){
|
38 |
int a = 1337;
|
39 |
int b = 0xDEADBEEF;
|
40 |
seed++;
|
41 |
return seed%max;
|
42 |
}
|
43 |
*/
|
44 |
|
45 |
/*
|
46 |
int getIntersectType(int barcode)
|
47 |
{
|
48 |
return randomNumGen(5);
|
49 |
}
|
50 |
|
51 |
int getIntersectPos(int barcode, int max)
|
52 |
{
|
53 |
return randomNumGen(max);
|
54 |
}
|
55 |
*/
|
56 |
|
57 |
int getTurnType(int barcode) |
58 |
{ |
59 |
return randomNumGen(4); |
60 |
} |
61 |
|
62 |
int validateTurn(int barcode, int turn_type) |
63 |
{ |
64 |
int intersect_type;
|
65 |
int intersect_pos;
|
66 |
intersect_type = getIntersectType(barcode); |
67 |
//TEMPORARY CODE FOR TEST MAP
|
68 |
//INTERSECTION 2 (barcodes 13 and 16) was made incorrectly
|
69 |
//If the bot reads barcode 16, it must turn left
|
70 |
//If the bot reads barcode 13, it must go straight
|
71 |
if(barcode == 16){ |
72 |
return ILEFT;
|
73 |
} |
74 |
if(barcode == 13){ |
75 |
return ISTRAIGHT;
|
76 |
} |
77 |
//END TEMP CODE
|
78 |
|
79 |
switch (intersect_type)
|
80 |
{ |
81 |
case DOUBLE_C:
|
82 |
{ |
83 |
intersect_pos = getIntersectPos(barcode); |
84 |
if (0<=intersect_pos && intersect_pos<=3) |
85 |
return turn_type;
|
86 |
break;
|
87 |
} |
88 |
case DOUBLE_T:
|
89 |
{ |
90 |
intersect_pos = getIntersectPos(barcode); |
91 |
switch (intersect_pos)
|
92 |
{ |
93 |
case TLEFT:
|
94 |
{ |
95 |
if (turn_type == ILEFT) turn_type = ISTRAIGHT;
|
96 |
return turn_type;
|
97 |
break;
|
98 |
} |
99 |
case TRIGHT:
|
100 |
{ |
101 |
if (turn_type == IRIGHT) turn_type = ISTRAIGHT;
|
102 |
return turn_type;
|
103 |
break;
|
104 |
} |
105 |
case TMIDDLE:
|
106 |
{ |
107 |
if (turn_type == ISTRAIGHT) turn_type = IUTURN;
|
108 |
return turn_type;
|
109 |
break;
|
110 |
} |
111 |
default:
|
112 |
return -1; |
113 |
} |
114 |
break;
|
115 |
} |
116 |
case SINGLE:
|
117 |
{ |
118 |
intersect_pos = getIntersectPos(barcode); |
119 |
switch (intersect_pos)
|
120 |
{ |
121 |
case SACROSS:
|
122 |
{ |
123 |
if (turn_type == IRIGHT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
124 |
return turn_type;
|
125 |
break;
|
126 |
} |
127 |
case SUP:
|
128 |
{ |
129 |
if (turn_type == ILEFT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
130 |
return turn_type;
|
131 |
break;
|
132 |
} |
133 |
default:
|
134 |
return -1; |
135 |
} |
136 |
break;
|
137 |
} |
138 |
case ON_RAMP:
|
139 |
{ |
140 |
intersect_pos = getIntersectPos(barcode); |
141 |
switch (intersect_pos)
|
142 |
{ |
143 |
case R_LEFT:
|
144 |
{ |
145 |
if (turn_type == ILEFT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
146 |
return turn_type;
|
147 |
break;
|
148 |
} |
149 |
case R_RIGHT:
|
150 |
{ |
151 |
if (turn_type == IRIGHT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
152 |
return turn_type;
|
153 |
break;
|
154 |
} |
155 |
default:
|
156 |
return -1; |
157 |
} |
158 |
break;
|
159 |
} |
160 |
case OFF_RAMP:
|
161 |
{ |
162 |
intersect_pos = getIntersectPos(barcode); |
163 |
switch (intersect_pos)
|
164 |
{ |
165 |
case R_LEFT:
|
166 |
{ |
167 |
int turn_type = ISTRAIGHT;
|
168 |
return turn_type;
|
169 |
break;
|
170 |
} |
171 |
case R_RIGHT:
|
172 |
{ |
173 |
int turn_type = ISTRAIGHT;
|
174 |
return turn_type;
|
175 |
break;
|
176 |
} |
177 |
case R_RAMP:
|
178 |
{ |
179 |
if (turn_type == ISTRAIGHT || turn_type == IUTURN) turn_type = ILEFT;
|
180 |
return turn_type;
|
181 |
break;
|
182 |
} |
183 |
default:
|
184 |
return -1; |
185 |
} |
186 |
break;
|
187 |
} |
188 |
default:
|
189 |
return -1; |
190 |
} |
191 |
|
192 |
return -1; |
193 |
} |