root / trunk / code / projects / traffic_navigation / validTurns.c @ 1958
History | View | Annotate | Download (3.47 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 |
|
14 |
|
15 |
#define GET16(s)( ((s)&0x8000)>>16 ) /*gets the 16 bit*/ |
16 |
#define GET14(s)( ((s)&0x2000)>>14 ) /*gets the 14thbit*/ |
17 |
#define GET13(s)( ((s)&0x1000)>>13 ) /*you get the idea*/ |
18 |
#define GET11(s)( ((s)&0x400)>>11 ) |
19 |
#define CYCLES 10 /*the number of reseeds i perform before finally extracting my ranodm number*/ |
20 |
#define SHIFT(s)( (s)<<1) |
21 |
#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*/ |
22 |
|
23 |
|
24 |
unsigned int seed = 0xC0FFEE; |
25 |
int randomNumGen(int max){ |
26 |
int a = 0; |
27 |
int b = 0xBEEF; |
28 |
seed++; |
29 |
return seed%4; |
30 |
} |
31 |
|
32 |
/*
|
33 |
int randomNumGen(int max){
|
34 |
return rtc_get() % max;
|
35 |
}
|
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 |
int getTurnType(int barcode) |
57 |
{ |
58 |
return randomNumGen(4); |
59 |
} |
60 |
|
61 |
|
62 |
#include "intersectData.h" |
63 |
#include "lineDrive.h" |
64 |
#include "validTurns.h" |
65 |
|
66 |
int validateTurn(int barcode, int turn_type) |
67 |
{ |
68 |
int intersect_type;
|
69 |
int intersect_pos;
|
70 |
intersect_type = getIntersectType(barcode); |
71 |
switch (intersect_type)
|
72 |
{ |
73 |
case DOUBLE_C:
|
74 |
{ |
75 |
intersect_pos = getIntersectPos(barcode); |
76 |
if (0<=intersect_pos && intersect_pos<=3) |
77 |
return turn_type;
|
78 |
break;
|
79 |
} |
80 |
case DOUBLE_T:
|
81 |
{ |
82 |
intersect_pos = getIntersectPos(barcode); |
83 |
switch (intersect_pos)
|
84 |
{ |
85 |
case TLEFT:
|
86 |
{ |
87 |
if (turn_type == ILEFT) turn_type = ISTRAIGHT;
|
88 |
return turn_type;
|
89 |
break;
|
90 |
} |
91 |
case TRIGHT:
|
92 |
{ |
93 |
if (turn_type == IRIGHT) turn_type = ISTRAIGHT;
|
94 |
return turn_type;
|
95 |
break;
|
96 |
} |
97 |
case TMIDDLE:
|
98 |
{ |
99 |
if (turn_type == ISTRAIGHT) turn_type = IUTURN;
|
100 |
return turn_type;
|
101 |
break;
|
102 |
} |
103 |
default:
|
104 |
return -1; |
105 |
} |
106 |
break;
|
107 |
} |
108 |
case SINGLE:
|
109 |
{ |
110 |
intersect_pos = getIntersectPos(barcode); |
111 |
switch (intersect_pos)
|
112 |
{ |
113 |
case SACROSS:
|
114 |
{ |
115 |
if (turn_type == IRIGHT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
116 |
return turn_type;
|
117 |
break;
|
118 |
} |
119 |
case SUP:
|
120 |
{ |
121 |
if (turn_type == ILEFT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
122 |
return turn_type;
|
123 |
break;
|
124 |
} |
125 |
default:
|
126 |
return -1; |
127 |
} |
128 |
break;
|
129 |
} |
130 |
case ON_RAMP:
|
131 |
{ |
132 |
intersect_pos = getIntersectPos(barcode); |
133 |
switch (intersect_pos)
|
134 |
{ |
135 |
case R_LEFT:
|
136 |
{ |
137 |
if (turn_type == ILEFT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
138 |
return turn_type;
|
139 |
break;
|
140 |
} |
141 |
case R_RIGHT:
|
142 |
{ |
143 |
if (turn_type == IRIGHT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
144 |
return turn_type;
|
145 |
break;
|
146 |
} |
147 |
default:
|
148 |
return -1; |
149 |
} |
150 |
break;
|
151 |
} |
152 |
case OFF_RAMP:
|
153 |
{ |
154 |
intersect_pos = getIntersectPos(barcode); |
155 |
switch (intersect_pos)
|
156 |
{ |
157 |
case R_LEFT:
|
158 |
{ |
159 |
int turn_type = ISTRAIGHT;
|
160 |
return turn_type;
|
161 |
break;
|
162 |
} |
163 |
case R_RIGHT:
|
164 |
{ |
165 |
int turn_type = ISTRAIGHT;
|
166 |
return turn_type;
|
167 |
break;
|
168 |
} |
169 |
case R_RAMP:
|
170 |
{ |
171 |
if (turn_type == ISTRAIGHT || turn_type == IUTURN) turn_type = ILEFT;
|
172 |
return turn_type;
|
173 |
break;
|
174 |
} |
175 |
default:
|
176 |
return -1; |
177 |
} |
178 |
break;
|
179 |
} |
180 |
default:
|
181 |
return -1; |
182 |
} |
183 |
} |