Caddy
A 2005 Roborodentia entry with vision and path planning capability
 All Data Structures Files Functions Variables Typedefs Macros Pages
test_routines.c
1 /*
2  * This file is part of Caddy.
3  *
4  * Caddy is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Caddy is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Caddy. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include "test_routines.h"
18 #include "robot_control.h"
19 #include "motor_control.h"
20 #include "ball_tracking.h"
21 #include "line_tracking.h"
22 #include "path_planning.h"
23 #include "node_list.h"
24 #include "servos.h"
25 #include "buttons.h"
26 #include "tweak_data.h"
27 #include "lcd_16x2.h"
28 #include "utility.h"
29 
30 // avr-libc
31 #include <stdbool.h>
32 
33 inline void runTest(void)
34 {
35  switch (testMode)
36  {
37  case SEEK_BALL_TEST_MODE:
38  seekBallTest();
39  break;
40  case FIXED_PATH_TEST_MODE:
41  runFixedPath();
42  break;
43  case UPDATE_PATH_TEST_MODE:
44  updatePathTest();
45  break;
46  case RUN_BOT_TEST_MODE:
47  default:
49  break;
50  }
51 }
52 
53 void toggleTestMode(int8_t i)
54 {
55  // advance test mode up or down
56  testMode += i;
57  if (testMode == NUM_TEST_MODES)
58  testMode = 0;
59  else if (testMode == 255)
60  testMode = NUM_TEST_MODES - 1;
61 
62  switch (testMode)
63  {
64  case SEEK_BALL_TEST_MODE:
65 #if DEBUGGING
66  lcdWriteStr("Seek ball test ", 1, 0);
67 #endif
68  break;
69  case FIXED_PATH_TEST_MODE:
70 #if DEBUGGING
71  lcdWriteStr("Fixed path test ", 1, 0);
72 #endif
73  break;
74  case UPDATE_PATH_TEST_MODE:
75 #if DEBUGGING
76  lcdWriteStr("Update path test", 1, 0);
77 #endif
78  break;
79  case RUN_BOT_TEST_MODE:
80  default:
81 #if DEBUGGING
82  lcdWriteStr("Run bot. ", 1, 0);
83 #endif
84  break;
85  }
86 }
87 
88 void runFixedPath( void )
89 {
90  // From start to zig-zag
91  moveToJunction(1, false);
92  tractorTurn(255, 64 - turnSubtract);
93  moveToJunction(1, true);
94  moveToJunction(1, true);
95  setServo(LIFT, LIFT_OPEN);
96  moveToJunction(1, true);
97  setServo(LIFT, LIFT_UP);
98 
99  tractorTurn(255, 64 - turnSubtract);
100 
101  moveToJunction(1, true);
102 
103  setServo(LIFT, LIFT_OPEN);
104  moveToJunction(1, true);
105  setServo(LIFT, LIFT_UP);
106 
107  // Through zig-zag
108  tractorTurn(255, 64 - turnSubtract);
109  moveToJunction(1, true);
110  tractorTurn(255, -64 + turnSubtract);
111  moveToJunction(1, true);
112  tractorTurn(255, 64 - turnSubtract);
113  moveToJunction(1, true);
114  tractorTurn(255, -64 + turnSubtract);
115  moveToJunction(1, true);
116  tractorTurn(255, 64 - turnSubtract);
117 
118  // To bonus ball 1
119  moveToJunction(2, true);
120  tractorTurn(255, 64 - turnSubtract);
121  moveToJunction(1, true);
122  tractorTurn(255, -64 + turnSubtract);
123  moveToJunction(1, true);
124  tractorTurn(255, 64 - turnSubtract);
125  moveToJunction(1, true);
126 
127  botHeading = -64; // Pick up Bonus Ball 1
129 
130  // To bonus ball 2
131  moveToJunction(1, true);
132  tractorTurn(255, -64 + turnSubtract);
133  moveToJunction(1, true);
134  tractorTurn(255, 64 - turnSubtract);
135  moveToJunction(1, true);
136  tractorTurn(255, 64 - turnSubtract);
137  moveToJunction(1, true);
138 
139  botHeading = 64; // Pick up Bonus Ball 2
140  bonusBallPickUpManeuver(-96, -128);
141 
142  // To nest
143  moveToJunction(1, true);
144  tractorTurn(255, -64 + turnSubtract);
145  moveToJunction(1, true);
146  tractorTurn(255, -64 + turnSubtract);
147  setServo(LIFT, LIFT_OPEN);
148  moveToJunction(1, true);
149  setServo(LIFT, LIFT_UP);
150  tractorTurn(255, 64 - turnSubtract);
151  moveToJunction(1, true);
152  tractorTurn(255, 64 - turnSubtract);
153  moveToJunction(1, true);
154 
155  nestSequence();
156 }
157 
158 
159 void seekBallTest( void )
160 {
161  /*
162  trackColorInit(LOOK_LEFT);
163 
164  while( 1 )
165  {
166  labelColorStats();
167  if (cameraSeekLeft() )
168  {
169  //msDelay(1000);
170  //lcdWriteStr("BALL ", 0, 0);
171  }
172  else
173  {
174  //msDelay(1000);
175  //lcdWriteStr("NO BALL ", 0, 0);
176  }
177  msDelay(1000);
178  }
179  */
180 
181  bool justTurned = true;
182  GraphNodeType node;
183 
184  botNode = tempTweak1; // set path to next junction
185  botHeading = tempTweak2; // tempTweak1 must be adjacent to junction at heading in tempTweak2
186 
187  pathListIndex = 0;
188  pathList[0] = botNode;
189  getNode(botNode, &node);
190  pathList[1] = getNodeAtHeading(&node, botHeading);
191 
192 #if DEBUGGING
193  lcdWriteStr("Junction Code ", 0, 0);
194  lcdWriteStr("bn: bh: ", 1, 0);
195  lcdPrintDecU08(botNode, 1, 3);
196  lcdPrintDecS08(botHeading, 1, 9);
197  waitFor(RED_BUTTON);
198 #endif
199 
200  moveToJunction(1, justTurned);
201  brake(BOTH_MOTORS);
202  msDelay(1000);
203  updatePath(); // sets path to nest, b/c goalList is empty
204  junctionCode(); // updates path to ball, if ball found
205  /*
206  #if DEBUGGING
207  lcdWriteStr("Junction Code ",0,0);
208  printGoalList();
209  #endif
210 
211  justTurned = positionBot(); // continues to nest or picks up ball
212  moveToJunction(1 , justTurned);
213 
214  //junctionCode();
215  //justTurned = positionBot();
216  //moveToJunction(1 , justTurned);
217  */
218 
219  printGoalList();
220 
221  brake(BOTH_MOTORS);
222 }
223 
224 
225 inline void refreshColorStats( void )
226 {
227 #if DEBUGGING
228  lcdPrintHex(lineStats[0][MX_NDX], 0, 1);
229  lcdPrintHex(lineStats[0][MY_NDX], 0, 4);
230  lcdPrintHex(lineStats[0][X1_NDX], 0, 9);
231  lcdPrintHex(lineStats[0][Y1_NDX], 0, 12);
232  lcdPrintHex(lineStats[0][PIXEL_CNT_NDX], 1, 1);
233  lcdPrintHex(lineStats[0][CONFIDENCE_NDX], 1, 5);
234  lcdPrintHex(lineStats[0][X2_NDX], 1, 10);
235  lcdPrintHex(lineStats[0][Y2_NDX], 1, 13);
236 #endif
237 }
238 
239 
240 inline void clearColorStats( void )
241 {
242 #if DEBUGGING
243  lcdWriteStr(" ", 0, 1);
244  lcdWriteStr(" ", 0, 4);
245  lcdWriteStr(" ", 0, 9);
246  lcdWriteStr(" ", 0, 12);
247  lcdWriteStr(" ", 1, 1);
248  lcdWriteStr(" ", 1, 5);
249  lcdWriteStr(" ", 1, 10);
250  lcdWriteStr(" ", 1, 13);
251 #endif
252 }
253 
254 
255 inline void labelColorStats( void )
256 {
257 #if DEBUGGING
258  lcdWriteStr("( , ) ( , ) ", 0, 0);
259  lcdWriteStr("p c ( , )", 1, 0);
260 #endif
261 }
262 
263 
264 void updatePathTest( void )
265 {
266  /*
267  //Break beam test
268  while(1)
269  {
270  #if DEBUGGING
271  if( BREAK_BEAM_TRIGGERED )
272  {
273  lcdWriteStr("Triggered ", 0, 0);
274  }
275  else
276  {
277  lcdWriteStr("Not triggered ", 0, 0);
278  }
279  #endif
280  }
281  */
282 
283  /*
284  #if DEBUGGING
285  // Uniform Cost Search Test
286  lcdPrintDecU08( uniformCostSearch(tempTweak1,tempTweak2), 1, 0 );
287  while(1) ;
288  #endif
289  */
290 
291  /*
292  // reconstruct path test
293  updatePathTo(tempTweak1);
294  runRoborodentiaCourse();
295  */
297  // updatePathWithPerms test
298  addToGoalList(tempTweak1);
299  addToGoalList(tempTweak2);
300  addToGoalList(tempTweak3);
301  printGoalList();
302  waitFor(RED_BUTTON);
303 
304 #if DEBUGGING
305  lcdWriteStr(" ", 1, 0);
306  lcdPrintDecU08(updatePath(), 1, 0);
307  printGoalList();
308  waitFor(RED_BUTTON);
309 #endif
310 
312 //*/
313 
314 }
315 
316 void printPathList( void )
317 {
318 #if DEBUGGING
319  lcdPrintDecU08(botNode, 1, 0);
320  lcdPrintDecS08(botHeading, 1, 3);
321  lcdPrintDecU08(pathListIndex, 1, 6);
322 #endif
323 
324  // print pathList
325 #if DEBUGGING
326  lcdWriteStr("Printing... ", 0, 0);
327  waitFor(RED_BUTTON);
328 #endif
329  uint8_t i;
330  for (i = pathListIndex; i < MAX_PATH_LIST_SIZE; i++)
331  {
332  if (i % 5 == 0)
333  {
334  waitFor(RED_BUTTON);
335  lcdWriteStr(" ", 0, 0);
336  }
337 
338  lcdPrintDecU08(pathList[i], 0, 3 * (i % 5));
339  }
340  waitFor(RED_BUTTON);
341 }