root / trunk / code / projects / traffic_navigation / validTurns.c @ 1966
History | View | Annotate | Download (3.55 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 |
switch (intersect_type)
|
68 |
{ |
69 |
case DOUBLE_C:
|
70 |
{ |
71 |
intersect_pos = getIntersectPos(barcode); |
72 |
if (0<=intersect_pos && intersect_pos<=3) |
73 |
return turn_type;
|
74 |
break;
|
75 |
} |
76 |
case DOUBLE_T:
|
77 |
{ |
78 |
intersect_pos = getIntersectPos(barcode); |
79 |
switch (intersect_pos)
|
80 |
{ |
81 |
case TLEFT:
|
82 |
{ |
83 |
if (turn_type == ILEFT) turn_type = ISTRAIGHT;
|
84 |
return turn_type;
|
85 |
break;
|
86 |
} |
87 |
case TRIGHT:
|
88 |
{ |
89 |
if (turn_type == IRIGHT) turn_type = ISTRAIGHT;
|
90 |
return turn_type;
|
91 |
break;
|
92 |
} |
93 |
case TMIDDLE:
|
94 |
{ |
95 |
if (turn_type == ISTRAIGHT) turn_type = IUTURN;
|
96 |
return turn_type;
|
97 |
break;
|
98 |
} |
99 |
default:
|
100 |
return -1; |
101 |
} |
102 |
break;
|
103 |
} |
104 |
case SINGLE:
|
105 |
{ |
106 |
intersect_pos = getIntersectPos(barcode); |
107 |
switch (intersect_pos)
|
108 |
{ |
109 |
case SACROSS:
|
110 |
{ |
111 |
if (turn_type == IRIGHT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
112 |
return turn_type;
|
113 |
break;
|
114 |
} |
115 |
case SUP:
|
116 |
{ |
117 |
if (turn_type == ILEFT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
118 |
return turn_type;
|
119 |
break;
|
120 |
} |
121 |
default:
|
122 |
return -1; |
123 |
} |
124 |
break;
|
125 |
} |
126 |
case ON_RAMP:
|
127 |
{ |
128 |
intersect_pos = getIntersectPos(barcode); |
129 |
switch (intersect_pos)
|
130 |
{ |
131 |
case R_LEFT:
|
132 |
{ |
133 |
if (turn_type == ILEFT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
134 |
return turn_type;
|
135 |
break;
|
136 |
} |
137 |
case R_RIGHT:
|
138 |
{ |
139 |
if (turn_type == IRIGHT || turn_type == IUTURN) turn_type = ISTRAIGHT;
|
140 |
return turn_type;
|
141 |
break;
|
142 |
} |
143 |
default:
|
144 |
return -1; |
145 |
} |
146 |
break;
|
147 |
} |
148 |
case OFF_RAMP:
|
149 |
{ |
150 |
intersect_pos = getIntersectPos(barcode); |
151 |
switch (intersect_pos)
|
152 |
{ |
153 |
case R_LEFT:
|
154 |
{ |
155 |
int turn_type = ISTRAIGHT;
|
156 |
return turn_type;
|
157 |
break;
|
158 |
} |
159 |
case R_RIGHT:
|
160 |
{ |
161 |
int turn_type = ISTRAIGHT;
|
162 |
return turn_type;
|
163 |
break;
|
164 |
} |
165 |
case R_RAMP:
|
166 |
{ |
167 |
if (turn_type == ISTRAIGHT || turn_type == IUTURN) turn_type = ILEFT;
|
168 |
return turn_type;
|
169 |
break;
|
170 |
} |
171 |
default:
|
172 |
return -1; |
173 |
} |
174 |
break;
|
175 |
} |
176 |
default:
|
177 |
return -1; |
178 |
} |
179 |
|
180 |
return -1; |
181 |
} |