Subversion Repositories Projects

[/] [avrra/] [library/] [avrcam.h] - Blame information for rev 48

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1 mikef
/******************************************************************************
2
 * AVRRA: The AVR Robotics API
3 48 mikef
 * avrcam.h - driver for AVRCam from JRobot.net. Uses serial1, Mega324P only
4 1 mikef
 *      NOTE: serial1.h should not be used when using this file...
5
 *
6
 * Copyright (c) 2004-2008, Michael E. Ferguson
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 *
12
 * - Redistributions of source code must retain the above copyright notice,
13
 *   this list of conditions and the following disclaimer.
14
 * - Redistributions in binary form must reproduce the above copyright notice,
15
 *   this list of conditions and the following disclaimer in the documentation
16
 *   and/or other materials provided with the distribution.
17
 * - Neither the name of AVRRA nor the names of its contributors
18
 *   may be used to endorse or promote products derived from this software
19
 *   without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31
 * THE POSSIBILITY OF SUCH DAMAGE.
32
 *****************************************************************************/
33
 
34
// we now externally define this, and use it to include the file
35
#ifdef TGT_HAS_CAMERA
36
 
37
// Only one camera per robot (currently)
38
#ifndef XR_VISION
39
#define XR_VISION
40
 
41
#include <avr/io.h>
42
#include <stdio.h>
43
#include <avr/interrupt.h>
44
#include "utils.h"
45
#include "serial.h"
46
 
47
// this allows use of the AVRcamVIEW software to see tracking
48
//#define AVR_CAM_INTERFACE
49
 
50
// Vision driver modes of operation
51
#define VMODE_NONE      0
52
#define VMODE_PING      1  
53
#define VMODE_A         2   // A recieved, looking for C
54
#define VMODE_C         3      
55
#define VMODE_K         4
56
#define VMODE_N         5
57
#define VMODE_RAW       6   // pass a raw image
58
#define VMODE_ET        7   // tracking enabled
59
#define VMODE_TFRAME    8   // recieving a tracking frame.
60
#define VMODE_TCOLOR    9  
61
#define VMODE_TXMIN     10
62
#define VMODE_TXMAX     11
63
#define VMODE_TYMIN     12
64
#define VMODE_TYMAX     13
65
static int visionMode;
66
 
67
typedef struct{
68
    int color;
69
    int xmin;
70
    int xmax;
71
    int ymin;
72
    int ymax;
73
}t_object;
74
 
75
// tracked data
76
t_object objects[8];
77
int numObjects;
78
int newVisData;
79
 
80
// tracking reception data
81
int numObjRec;              // #of objects to recieve
82
 
83
// camera status
84
int camStatus;
85
#define CAM_ERROR       0
86
#define CAM_OK          1
87
#define CAM_TRACKING    2
88
 
89
static int rawPackets;
90
 
91 40 mikef
/** Starts the vision system and serial port 1 */
92 1 mikef
void visionInit(){
93
    // initialise visual system
94
    numObjects = 0;
95
    visionMode = VMODE_NONE;
96
    newVisData = 0;
97
    // start serial port 1
98
    UBRR1H = 0;
99
    UBRR1L = 7;
100
    // enable rx and tx
101
    SetBit(UCSR1B, RXEN1);
102
    SetBit(UCSR1B, TXEN1);
103
    // enable interrupt on complete reception of a byte
104
    SetBit(UCSR1B, RXCIE1);
105
}
106
 
107 40 mikef
/** Sends a character out the serial port. */
108 1 mikef
void serial1Write(byte data){
109
    while (bit_is_clear(UCSR1A, UDRE1))
110
        ;
111
    UDR1 = data;
112
}
113
 
114 40 mikef
/** This will decide if camera is functioning. */
115 1 mikef
int pingCam(){
116
    int timeouts=0;
117
    visionMode = VMODE_PING;
118
    camStatus = CAM_ERROR;
119
    serial1Write('P');
120
    serial1Write('G');
121
    serial1Write('\r');
122
    // Wait max of half second
123
    while(timeouts<20){
124
        delayms(25);
125
        if(camStatus > CAM_ERROR){
126
            return 1;
127
        }
128
        timeouts++;
129
    }
130
    return 0;
131
}  
132
 
133 40 mikef
/** Pass a raw camera image back to the AVRCamView program */
134 1 mikef
void passRawCam(){
135
    if(camStatus==CAM_OK){ 
136
        visionMode = VMODE_RAW;
137
        rawPackets = 0;
138
        serial1Write('D');
139
        serial1Write('F');
140
        serial1Write('\r');
141
        // quick hack to make it work...
142
        //Print("ACK\r");
143
    }else{
144
        Print("NCK\r");
145
    }
146
}
147
 
148
void passSegCam(){
149
    // this function will send seg cam info back to pc
150
}
151
 
152 40 mikef
/** Enable tracking, interrupt driven response */
153 1 mikef
void enableTracking(){
154
    visionMode = VMODE_ET;
155
    camStatus=CAM_TRACKING;
156
    serial1Write('E');
157
    serial1Write('T');
158
    serial1Write('\r');
159
    numObjects = 0;
160
    newVisData = 0;
161
}
162
 
163 40 mikef
/** Disable tracking */
164 1 mikef
void disableTracking(){
165
    visionMode = VMODE_NONE;
166
    camStatus=CAM_OK;
167
    serial1Write('D');
168
    serial1Write('T');
169
    serial1Write('\r');
170
  #ifdef AVR_CAM_INTERFACE
171
    Print("ACK\r    numObjects = 0;");
172
  #endif
173
 
174
}
175
 
176 40 mikef
/** Set color map for AVRCAM */
177 1 mikef
void setColorMap(){
178
    // 16 bytes for red, green and blue
179 32 mikef
    int bytes = 48;
180
    serial1Write('S');
181
    serial1Write('M');
182 1 mikef
    while(bytes > 0){
183
        while(serialAvailable() == 0)
184
            ;
185
        serial1Write(serialRead());
186
        bytes--;
187
    }    
188
    // Send ACK
189
    Print("ACK\r");
190
}
191
 
192 40 mikef
/** interrupt driven camera handler... */
193 1 mikef
ISR(USART1_RX_vect){
194
    unsigned char c = UDR1;
195
 
196
    switch(visionMode){
197
        case VMODE_RAW:
198
            // pass raw image upwards
199
            serialWrite(c);
200
            if(c == 0x0F){
201
                rawPackets++;
202
            }
203
            if(rawPackets > 72){
204
                visionMode = VMODE_NONE;
205
            }
206
            break;
207
        case VMODE_PING:
208
            // check to see if Ack or Nck.
209
            if(c == 'A'){
210
                visionMode = VMODE_A;
211
                camStatus = CAM_OK;
212
            }else{
213
                visionMode = VMODE_N;
214
            }
215
            serialWrite(c);
216
            break;
217
        case VMODE_A:
218
        case VMODE_N:
219
            // C recieved
220
            serialWrite(c);
221
            visionMode = VMODE_C;
222
            break;
223
        case VMODE_C:
224
            // K recieved
225
            serialWrite(c);
226
            visionMode = VMODE_K;
227
            break;
228
        case VMODE_K:
229
            // \r recieved
230
            serialWrite(c);
231
            visionMode = VMODE_NONE;
232
            break;
233
        case VMODE_ET:
234
            // beginning of ET
235
            if(c == 0x0A){
236
                visionMode = VMODE_TFRAME;
237
            }
238
        #ifdef AVR_CAM_INTERFACE
239
            serialWrite(c);
240
        #endif         
241
            break;
242
        case VMODE_TFRAME:
243
            // this is our number of tracked objects
244
            numObjRec = c;
245
            if(numObjRec > 8){
246
                numObjRec = 0;
247
                visionMode = VMODE_ET;
248
            }else{
249
                numObjects = 0;
250
                visionMode = VMODE_TCOLOR;
251
            }
252
        #ifdef AVR_CAM_INTERFACE
253
            serialWrite(c);
254
        #endif         
255
            break;
256
        case VMODE_TCOLOR:
257
            objects[numObjects].color = c;
258
            visionMode = VMODE_TXMIN;
259
        #ifdef AVR_CAM_INTERFACE
260
            serialWrite(c);
261
        #endif         
262
            break;
263
        case VMODE_TXMIN:
264
            objects[numObjects].xmin = c;
265
            visionMode = VMODE_TYMAX;
266
        #ifdef AVR_CAM_INTERFACE
267
            serialWrite(c);
268
        #endif         
269
            break;
270
        case VMODE_TYMAX:
271
            objects[numObjects].ymax = c;
272
            visionMode = VMODE_TXMAX;
273
        #ifdef AVR_CAM_INTERFACE
274
            serialWrite(c);
275
        #endif
276
            break;
277
        case VMODE_TXMAX:
278
            objects[numObjects].xmax = c;
279
            visionMode = VMODE_TYMIN;
280
        #ifdef AVR_CAM_INTERFACE
281
            serialWrite(c);
282
        #endif
283
            break;
284
        case VMODE_TYMIN:
285
            objects[numObjects].ymin = c;
286
            if(numObjects < numObjRec){
287
                numObjects++;
288
                visionMode = VMODE_TCOLOR;
289
            }else{
290
                //sysMsg("gtf");
291
                visionMode = VMODE_ET;
292
                newVisData = 1;
293
            }      
294
        #ifdef AVR_CAM_INTERFACE
295
            serialWrite(c);
296
        #endif         
297
            break;
298
        default:
299
            // do nothing with extra input
300
            break;
301
    }
302
}
303
 
304
#endif
305
 
306
#endif