Caddy
A 2005 Roborodentia entry with vision and path planning capability
 All Data Structures Files Functions Variables Typedefs Macros Pages
camera.c
Go to the documentation of this file.
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  */
18 #include "camera.h"
19 #include "ball_tracking.h"
20 #include "line_tracking.h"
21 #include "utility.h"
22 #include "lcd_16x2.h"
23 
24 // AVRLIB
25 #include "rprintf.h"
26 #include "uart.h"
27 
28 // avr-libc
29 #include <stdbool.h>
30 
31 // Packet types
32 #define NEW_PACKET 0
33 #define FE_RCV 1
34 #define T_RCV 2
35 
36 #define CMU_BAUD 38400
37 
38 static uint8_t mode;
39 static uint16_t byteNum;
40 
41 void packetRcv( uint8_t c );
42 inline void lineMode2Rcv( uint8_t c );
43 inline void trackColorRcv( uint8_t c );
44 
45 inline void cmuCamInit(void)
46 {
47  uartInit();
48  uartSetBaudRate(CMU_BAUD);
49  uartSetRxHandler(packetRcv);
50  rprintfInit(uartSendByte);
51 }
52 
53 inline void cameraWhiteBalance()
54 {
55  // turn auto white balance on
56 #if DEBUGGING
57  lcdWriteStr("white Bal ", 0, 6);
58 #endif
59  rprintf("CR 18 44\r");
60  myDelay(200);
61  // turn auto white balance off
62  rprintf("CR 18 40\r");
63 }
64 
65 inline void initCamera( void )
66 {
67  mode = NEW_PACKET;
68  byteNum = 0;
69 
70  /*
71  * Applies the following settings:
72  * - Output from the camera is in raw bytes
73  * - “ACK\r” and “NCK\r” confirmations are suppressed
74  * - Input to the camera is in ASCII
75  */
76  rprintf("RM 3\r");
77 }
78 
79 
80 void packetRcv(uint8_t c)
81 {
82  if (c == 0xff)
83  {
84  mode = NEW_PACKET;
85  byteNum = 0;
86  }
87  else
88  {
89  switch (mode)
90  {
91  case NEW_PACKET:
92  switch (c)
93  {
94  case 0xfe:
95  mode = FE_RCV;
96  break;
97  case 'T':
98  mode = T_RCV;
99  break;
100  default:
101  break;
102  }
103  break;
104  case FE_RCV:
105  lineMode2Rcv(c);
106  if (c == 0xfd)
107  {
108  mode = NEW_PACKET;
109  }
110  break;
111  case T_RCV:
112  trackColorRcv(c);
113  break;
114  }
115  }
116 }
117 
118 
119 inline void lineMode2Rcv(uint8_t c)
120 {
121  if (c == 0xfd)
122  {
123  lineStatsProcessed = false;
124  byteNum = 0;
125  }
126  else
127  {
128  lineStats[(byteNum - 1) / LINE_STATS_COLS]
129  [(byteNum - 1) % LINE_STATS_COLS] = c;
130  byteNum++;
131  }
132 }
133 
134 
135 inline void trackColorRcv(uint8_t c)
136 {
137  lineStats[0][byteNum] = c;
138  byteNum++;
139 
140  if (byteNum >= NUM_COLOR_STATS)
141  {
142  colorStatsProcessed = false;
143  }
144 }
145 
146 
147 inline void cameraStreamingOff( void )
148 {
149  rprintf("\r\r"); // add an extra return as recommended by CMUcam manual
150  msDelay(32); // wait for streaming to stop ( 16ms delay ok )
151 }
152 
153 
154 inline void setVirtualWindow(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)
155 {
156  rprintf("VW %d %d %d %d\r", x1, y1, x2, y2);
157 }
158 
159 inline void cameraHighResMode(void)
160 {
161  rprintf("HR 1\r");
162 }
163 
164 inline void cameraLowResMode(void)
165 {
166  rprintf("HR 0\r");
167 }