Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libdragonfly / bom.c @ 1051

History | View | Annotate | Download (8.82 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 "serial.h"
40
#include "analog.h"
41

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

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

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

    
61

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

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

71
*/
72

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

    
82
#define BOM_VALUE_THRESHOLD 200
83
#define NUM_BOM_LEDS 16
84

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

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

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

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

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

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

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

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

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

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

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

    
254

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

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

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

    
312
/** @} **/ //end group
313

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

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

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

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