Statistics
| Revision:

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

History | View | Annotate | Download (10.2 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 150 //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_E7        //dio4
98
#define BOM_S3                _PIN_E6        //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. Must call this before reading BOM values!
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
        int loop_was_running = 0;
171
    
172
        //Check analog loop status
173
    if(analog_loop_status() == ADC_LOOP_RUNNING) {
174
                loop_was_running = 1;
175
                analog_stop_loop();
176
        }
177
    
178
        //Read BOM values
179
    for(i = 0; i < NUM_BOM_LEDS; i++) {
180
        if(bit_field & 0x1) {
181
            bom_select(i);
182
            bom_val[i] = analog_get8(analog_pin);
183
        }
184
        bit_field = bit_field >> 1;
185
    }
186
    
187
        //Restore analog loop status
188
        if(loop_was_running)
189
                analog_start_loop();
190
}
191

    
192
/**
193
 * Gets the bom reading from bom_val[which].  Call bom_refresh beforehand to read new bom values.
194
 *
195
 * @pre must call bom refresh first
196
 *
197
 * @param which which bom value to return
198
 *
199
 * @return the bom value
200
 *
201
 * see bom_refresh
202
 **/
203
int bom_get(int which) {
204
    return bom_val[which];
205
}
206

    
207
/** 
208
 * Compares all the values in bom_val[] and returns the index to the lowest (max) value element.
209
 *
210
 * @pre must call bom refresh
211
 * @return index to the lowest (max) bom value element.  -1 if no value is lower than
212
 *    BOM_VALUE_THRESHOLD
213
 **/
214
int bom_get_max(void) {
215
    int i, lowest_val, lowest_i;
216
    lowest_i = -1;
217
    lowest_val = 255;
218
    for(i = 0; i < NUM_BOM_LEDS; i++) {
219
        if(bom_val[i] < lowest_val) {
220
            lowest_val = bom_val[i];
221
            lowest_i = i;
222
        }
223
    }
224
    
225
    if(lowest_val < BOM_VALUE_THRESHOLD)
226
        return lowest_i;
227
    else
228
        return -1;
229
}
230

    
231
/** 
232
 * Computes the weighted average of all the bom readings to estimate the position (and distance) of another robot.
233
 *
234
 * @pre must call bom refresh
235
 * @param dist  pointer to int in which to return the estimated distance to the other robot
236
 * @return estimated position of the max bom value element as a fixed point value analogous to 10 times the
237
 *        index of the max bom value.  -1 if no value is lower than BOM_VALUE_THRESHOLD.
238
 **/
239
int bom_get_max10(int *dist) {
240
    int i, max;
241
    long long mean, sum;
242

    
243
    max = bom_get_max();
244
    if (max < 0)
245
    {
246
        if (dist)
247
        {
248
            *dist = -1;
249
        }
250
        return -1;
251
    }
252
    /* Record values into an array */
253
    for (i = 0; i < NUM_BOM_LEDS; i++) {
254
        int idx = ((i + (NUM_BOM_LEDS/2 - max) + NUM_BOM_LEDS) % NUM_BOM_LEDS) - (NUM_BOM_LEDS/2 - max);
255
        int val = 255 - bom_val[i];
256
        mean += idx * val;
257
        sum += val;
258
    }
259
    mean = (mean * 10) / sum;
260
    mean = (mean + NUM_BOM_LEDS*10) % (NUM_BOM_LEDS*10);
261

    
262
    if (dist)
263
    {
264
        *dist = 50 - sum/48;
265
    }
266

    
267
    return mean;
268
}
269

    
270
/**
271
 * Iterates through each bit in the bit_field. If the bit is set, the corresponding emitter will
272
 *    be enabled to turn on when bom_on() is called.
273
 *    bom_init must be called for this to work. Does nothing if a BOM1.0 is installed
274
 *
275
 * @param bit_field specifies which leds should be turned on when bom_on is called.  Use BOM_ALL to turn on all bom leds.
276
 *    Ex. if 0x0005 is passed, leds 0 and 2 will be turned on.
277
 **/
278
void bom_set_leds(int bit_field) {
279
    int i;
280
        unsigned int mask = 1<<(NUM_BOM_LEDS-1);
281
        switch(bom_type) {
282
    case BOM10:
283
        //TODO: put an assert here to alert the user that this should not be called
284
        break;
285
                
286
    case BOM15:
287
            for(i=NUM_BOM_LEDS; i>0; i--)
288
            {
289
                    //set the current bit, sending MSB first
290
                    digital_output(BOM_DATA, bit_field&mask);
291
                    //then pulse the clock
292
                    digital_output(BOM_CLOCK, 1);
293
                    digital_output(BOM_CLOCK, 0);
294
                        mask = mask>>1;
295
            }
296
        break;
297
                
298
    case RBOM:
299
        //add rbom code here
300
        break;
301
    }
302
}
303

    
304

    
305
/**
306
 * (DEPRECATED) Returns the direction of the maximum BOM reading,
307
 * as an integer in the range 0-15. 0 indicates to the
308
 * robot's right, while the rest of the sensors are
309
 * numbered counterclockwise. This is useful for determining
310
 * the direction of a robot flashing its BOM, of only one
311
 * robot is currently doing so. analog_init must be called
312
 * before this function can be used.
313
 *
314
 * @return the direction of the maximum BOM reading
315
 *
316
 * @see analog_init
317
 **/
318
int get_max_bom(void) {
319
    bom_refresh(BOM_ALL);
320
    return bom_get_max();
321
}
322

    
323
/**
324
 * Flashes the BOM.  If using a BOM1.5, only the emitters that have been enabled using
325
 * bom_set_leds will turn on.
326
 * 
327
 * @see bom_off, bom_set_leds
328
 **/
329
void bom_on(void)
330
{
331
  switch(bom_type) {
332
  case BOM10:
333
        digital_output(MONKL, 1);
334
        break;
335
  case BOM15:
336
        digital_output(BOM_STROBE, 1);
337
        break;
338
  case RBOM:
339
        break;
340
  }
341
}
342

    
343
/**
344
 * Turns off all bom leds.
345
 * 
346
 * @see bom_on
347
 **/
348
void bom_off(void)
349
{
350
  switch(bom_type) {
351
  case BOM10:
352
        digital_output(MONKL, 0);
353
        break;
354
  case BOM15:
355
        digital_output(BOM_STROBE, 0);
356
        break;
357
  case RBOM:
358
        break;
359
  }
360
}
361

    
362
/** @} **/ //end group
363

    
364
//select a detector to read
365
static void bom_select(char which) {
366
        if(bom_type == BOM10)
367
          which = lookup[(int)which];
368
        
369
    if (which&8)
370
      digital_output(select_pins[3], 1);
371
    else
372
      digital_output(select_pins[3], 0);
373

    
374
    if (which&4)
375
      digital_output(select_pins[2], 1);
376
    else
377
      digital_output(select_pins[2], 0);
378

    
379
    if (which&2)
380
      digital_output(select_pins[1], 1);
381
    else
382
      digital_output(select_pins[1], 0);
383

    
384
    if (which&1)
385
      digital_output(select_pins[0], 1);
386
    else
387
      digital_output(select_pins[0], 0);
388
        
389
}