Project

General

Profile

Statistics
| Revision:

root / branches / analog / code / projects / libdragonfly / bom.c @ 1634

History | View | Annotate | Download (10.3 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
        bom_select(which);
206
    return analog_get8(analog_pin);
207
}
208

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

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

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

    
265
    if (dist)
266
    {
267
        *dist = 50 - sum/48;
268
    }
269

    
270
    return mean;
271
}
272

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

    
307

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

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

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

    
365
/** @} **/ //end group
366

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

    
377
    if (which&4)
378
      digital_output(select_pins[2], 1);
379
    else
380
      digital_output(select_pins[2], 0);
381

    
382
    if (which&2)
383
      digital_output(select_pins[1], 1);
384
    else
385
      digital_output(select_pins[1], 0);
386

    
387
    if (which&1)
388
      digital_output(select_pins[0], 1);
389
    else
390
      digital_output(select_pins[0], 0);
391
        
392
}