Project

General

Profile

Statistics
| Revision:

root / branches / rbom / code / projects / libdragonfly / bom.c @ 594

History | View | Annotate | Download (8.55 KB)

1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

    
26

    
27
/**
28
 * @file bom.c
29
 * @brief Implementation for using the BOM
30
 *
31
 * Contains functions for using the Bearing and Orientation Module (BOM)
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35

    
36
#include <dragonfly_lib.h>
37
#include "bom.h"
38
#include "dio.h"
39
#include "analog.h"
40

    
41
//On the original BOM1.0, the emmitter angular order does not match the analog mux order
42
//so you need to iterate through the mux index in the following order if you want to get
43
//the detector readings in order:
44
static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
45

    
46
// internal function prototypes
47
static void bom_select(char which);
48

    
49
/*
50
 Bk R Y (Analog)
51
---------
52
 Green
53
 Blue
54
 White
55
---------
56
 Blue
57
 White
58
*/
59

    
60

    
61
/*
62
the analog pin definitions from dio.h DO NOT work here,
63
so we must use PF0 from avrgcc (as opposed to _PIN_F0).
64
BUT the dio pin definitions from dio.h must be used (no PE...).
65

66
also, _PIN_E2 is initialized to high for some reason,
67
which turns the BOM on when the robot is turned on.
68
WORK-AROUND: call digital_output(_PIN_E2,0) at some point.
69

70
*/
71

    
72
#define MONKI PF0         //analog (yellow)
73
//------------------------//
74
#define MONKL _PIN_E2     //green
75
#define MONK1 _PIN_E3     //blue
76
#define MONK0 _PIN_E4     //white
77
//------------------------//
78
#define MONK3 _PIN_E6     //blue
79
#define MONK2 _PIN_E7     //white
80

    
81
#define BOM_VALUE_THRESHOLD 200
82
#define NUM_BOM_LEDS 16
83

    
84
/*
85
  *The following pin definitions are for the BOM v1.5
86
  */
87

    
88
#define BOM_MODE        _PIN_E2        //dio0
89
#define BOM_STROBE        _PIN_E3        //dio1
90

    
91
#define BOM_DATA        _PIN_A0 //servo0
92
#define BOM_CLOCK        _PIN_A1        //servo1
93

    
94
#define BOM_S0                _PIN_E6        //dio3
95
#define BOM_S1                _PIN_E7        //dio2
96
#define BOM_S2                _PIN_E4        //dio1
97
#define BOM_S3                _PIN_E5        //dio0
98
#define BOM_OUT                PF0                //analog(yellow)
99

    
100
/**
101
 * @defgroup bom BOM (Bearing and Orientation Module)
102
 * @brief Functions for dealing with the BOM.
103
 *
104
 * The Bearing and Orientation Module / Barrel of Monkeys / BOM
105
 * is a custom sensor designed and built by the Colony Project.
106
 * It consists of a ring of 16 IR emitters and 16 IR detectors.
107
 * The BOM is most often use to determine the direction of other
108
 * robots. This module contains functions for controlling the BOM.
109
 *
110
 * Include bom.h to access these functions.
111
 *
112
 * @{
113
 **/
114

    
115
static unsigned int bom_val[NUM_BOM_LEDS];
116
static char bom_type = BOM;
117
static int select_pins[4];
118
static int analog_pin;
119

    
120
/**
121
 * Initializes the BOM.
122
 * Call bom_init before reading bom values or turning bom leds.
123
 *
124
 * @bugs INCOMPLETE - need to fill in init routine for BOM15
125
 * 
126
 * @see bom_refresh, bom_leds_on, bom_leds_off
127
 **/
128
void bom_init(char type) {
129
    bom_type = type;
130
    
131
    switch(bom_type) {
132
    case BOM:
133
                select_pins[0] = MONK0; 
134
                select_pins[1] = MONK1;
135
                select_pins[2] = MONK2;
136
                select_pins[3] = MONK3;
137
                analog_pin = MONKI;
138
        break;
139
    case BOM15:
140
        //Sets BOM1.5 to normal [BOM] mode
141
        digital_output(BOM_MODE, 0);
142
                select_pins[0] = BOM_S0; 
143
                select_pins[1] = BOM_S1;
144
                select_pins[2] = BOM_S2;
145
                select_pins[3] = BOM_S3;
146
                bom_set_leds(BOM_ALL);
147
                analog_pin = BOM_OUT;
148
        break;
149
    case RBOM:
150
        break;
151
    //default:
152
    }
153
}
154

    
155
/**
156
 * Iterates through each bit in the bit_field. For each set bit, sets the corresponding bom select bits
157
 *    and updates the corresponding bom value with an analog_get8 reading.  analog_init and bom_init
158
 *    must be called for this to work.
159
 *
160
 *
161
 * @param bit_field specifies which elements in bom_val[] should be updated. Use BOM_ALL to refresh all values.
162
 *    Ex. if 0x0003 is passed, bom_val[0] and bom_val[1] will be updated.
163
 *
164
 * @see bom_get
165
 **/
166
void bom_refresh(int bit_field) {
167
    int i;
168
    
169
    analog_stop_loop();
170
    
171
    for(i = 0; i < NUM_BOM_LEDS; i++) {
172
        if(bit_field & 0x1) {
173
            bom_select(i);
174
            bom_val[i] = analog_get8(analog_pin);
175
        }
176
        bit_field = bit_field >> 1;
177
    }
178
    
179
    analog_start_loop();
180
}
181

    
182
/**
183
 * Gets the bom reading from bom_val[which].  Call bom_refresh beforehand to read new bom values.
184
 *
185
 * @param which which bom value to return
186
 *
187
 * @return the bom value
188
 *
189
 * see bom_refresh
190
 **/
191
int bom_get(int which) {
192
    return bom_val[which];
193
}
194

    
195
/** 
196
 * Compares all the values in bom_val[] and returns the index to the lowest (max) value element.
197
 *
198
 * @return index to the lowest (max) bom value element.  -1 if no value is lower than
199
 *    BOM_VALUE_THRESHOLD
200
 **/
201
int bom_get_max(void) {
202
    int i, lowest_val, lowest_i;
203
    lowest_i = -1;
204
    lowest_val = 255;
205
    for(i = 0; i < NUM_BOM_LEDS; i++) {
206
        if(bom_val[i] < lowest_val) {
207
            lowest_val = bom_val[i];
208
            lowest_i = i;
209
        }
210
    }
211
    
212
    if(lowest_val < BOM_VALUE_THRESHOLD)
213
        return lowest_i;
214
    else
215
        return -1;
216
}
217

    
218
/**
219
 * Iterates through each bit in the bit_field. If the bit is set, the corresponding emitter will
220
 *    be enabled to turn on when bom_on() is called.
221
 *    bom_init must be called for this to work.
222
 *
223
 * @param bit_field specifies which leds should be turned on.  Use BOM_ALL to turn on all bom leds.
224
 *    Ex. if 0x0005 is passed, leds 0 and 2 will be turned on.
225
 **/
226
void bom_set_leds(int bit_field) {
227
    int i;
228
        unsigned int mask = 1<<(NUM_BOM_LEDS-1);
229
        switch(bom_type) {
230
    case BOM:
231
        if(bit_field == BOM_ALL)
232
            digital_output(MONKL, 1);
233
        break;
234
                
235
    case BOM15:
236
            for(i=NUM_BOM_LEDS; i>0; i--)
237
            {
238
                    //set the current bit, sending MSB first
239
                    digital_output(BOM_DATA, bit_field&mask);
240
                    //then pulse the clock
241
                    digital_output(BOM_CLOCK, 1);
242
                    digital_output(BOM_CLOCK, 0);
243
                        mask = mask>>1;
244
            }
245
        break;
246
                
247
    case RBOM:
248
        //add rbom code here
249
        break;
250
    }
251
}
252

    
253

    
254
/**
255
 * (DEPRECATED) Returns the direction of the maximum BOM reading,
256
 * as an integer in the range 0-15. 0 indicates to the
257
 * robot's right, while the rest of the sensors are
258
 * numbered counterclockwise. This is useful for determining
259
 * the direction of a robot flashing its BOM, of only one
260
 * robot is currently doing so. analog_init must be called
261
 * before this function can be used.
262
 *
263
 * @return the direction of the maximum BOM reading
264
 *
265
 * @see analog_init
266
 **/
267
int get_max_bom(void) {
268
    bom_refresh(BOM_ALL);
269
    return bom_get_max();
270
}
271

    
272
/**
273
 * Flashes the BOM.  If using a BOM1.5, only the emmitters that have been enabled using
274
 * bom_set_leds will turn on.
275
 * 
276
 * @see bom_off, bom_set_leds
277
 **/
278
void bom_on(void)
279
{
280
  switch(bom_type) {
281
  case BOM:
282
        digital_output(MONKL, 1);
283
        break;
284
  case BOM15:
285
        digital_output(BOM_STROBE, 1);
286
        break;
287
  case RBOM:
288
        break;
289
  }
290
}
291

    
292
/**
293
 * Turns off all bom leds.
294
 * 
295
 * @see bom_on
296
 **/
297
void bom_off(void)
298
{
299
  switch(bom_type) {
300
  case BOM:
301
        digital_output(MONKL, 0);
302
        break;
303
  case BOM15:
304
        digital_output(BOM_STROBE, 0);
305
        break;
306
  case RBOM:
307
        break;
308
  }
309
}
310

    
311
/** @} **/ //end group
312

    
313
//select an emmitter to read
314
static void bom_select(char which) {
315
        if(bom_type == BOM)
316
          which = lookup[(int)which];
317
        
318
    if (which&8)
319
      digital_output(select_pins[3], 1);
320
    else
321
      digital_output(select_pins[3], 0);
322

    
323
    if (which&4)
324
      digital_output(select_pins[2], 1);
325
    else
326
      digital_output(select_pins[2], 0);
327

    
328
    if (which&2)
329
      digital_output(select_pins[1], 1);
330
    else
331
      digital_output(select_pins[1], 0);
332

    
333
    if (which&1)
334
      digital_output(select_pins[0], 1);
335
    else
336
      digital_output(select_pins[0], 0);
337
        
338
}