Project

General

Profile

Statistics
| Revision:

root / branches / wireless / code / projects / libdragonfly / bom.c @ 1513

History | View | Annotate | Download (10.8 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_defs.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
unsigned char bom_initd=0;
117

    
118
static unsigned int bom_val[NUM_BOM_LEDS];
119
static volatile char bom_type = BOM10;
120
static int select_pins[4];
121
static int analog_pin;
122

    
123
/**
124
 * Initializes the BOM.
125
 * Call bom_init before reading bom values or turning bom leds.
126
 *
127
 * @bugs INCOMPLETE - No utilization of BOM1.5 RSSI capability. Probably leave this out
128
 * until Cornell and Pras return
129
 *
130
 * @return 0 if init succesfull, an error code otherwise 
131
 *
132
 * @see bom_refresh, bom_leds_on, bom_leds_off
133
 **/
134
int bom_init(char type) {
135

    
136
    if(bom_initd)
137
      return ERROR_INIT_ALREADY_INITD;
138

    
139

    
140
    bom_type = type;
141
    
142
    switch(bom_type) {
143
    case BOM10:
144
                select_pins[0] = MONK0; 
145
                select_pins[1] = MONK1;
146
                select_pins[2] = MONK2;
147
                select_pins[3] = MONK3;
148
                analog_pin = MONKI;
149
        break;
150
    case BOM15:
151
        //Sets BOM1.5 to normal [BOM] mode
152
        digital_output(BOM_MODE, 0);
153
                select_pins[0] = BOM_S0; 
154
                select_pins[1] = BOM_S1;
155
                select_pins[2] = BOM_S2;
156
                select_pins[3] = BOM_S3;
157
                bom_set_leds(BOM_ALL);
158
                analog_pin = BOM_OUT;
159
        break;
160
    case RBOM:
161
        break;
162
    default:
163
      return -1;        
164
    //default:
165
    }
166

    
167
    bom_initd=1;
168
    return 0;
169
}
170

    
171
/**
172
 * Iterates through each bit in the bit_field. For each set bit, sets the corresponding bom select bits
173
 *    and updates the corresponding bom value with an analog_get8 reading.  analog_init and bom_init
174
 *    must be called for this to work. Must call this before reading BOM values!
175
 *
176
 *
177
 * @param bit_field specifies which elements in bom_val[] should be updated. Use BOM_ALL to refresh all values.
178
 *    Ex. if 0x0003 is passed, bom_val[0] and bom_val[1] will be updated.
179
 *
180
 * @return 0 if init succesfull, an error code otherwise
181
 *
182
 * @see bom_get
183
 **/
184
int bom_refresh(int bit_field) {
185
    int i;
186
    int loop_was_running = 0;
187

    
188
    if(!bom_initd)
189
      return ERROR_LIBRARY_NOT_INITD;
190

    
191
    
192
    //Check analog loop status
193
    if(analog_loop_status() == ADC_LOOP_RUNNING) {
194
      loop_was_running = 1;
195
      analog_stop_loop();
196
    }
197
    
198
    //Read BOM values
199
    for(i = 0; i < NUM_BOM_LEDS; i++) {
200
      if(bit_field & 0x1) {
201
        bom_select(i);
202
        bom_val[i] = analog_get8(analog_pin);
203
      }
204
      bit_field = bit_field >> 1;
205
    }
206
    
207
    //Restore analog loop status
208
    if(loop_was_running)
209
      analog_start_loop();
210

    
211
    return 0;
212
}
213

    
214
/**
215
 * Gets the bom reading from bom_val[which].  Call bom_refresh beforehand to read new bom values.
216
 *
217
 * @pre must call bom refresh first
218
 *
219
 * @param which which bom value to return
220
 *
221
 * @return the bom value, -1 on error
222
 *
223
 * see bom_refresh
224
 **/
225
int bom_get(int which) {
226
    if(!bom_initd)
227
      return -1;
228

    
229
    return bom_val[which];
230
}
231

    
232
/** 
233
 * Compares all the values in bom_val[] and returns the index to the lowest (max) value element.
234
 *
235
 * @pre must call bom refresh
236
 * @return index to the lowest (max) bom value element.  -1 if no value is lower than
237
 *    BOM_VALUE_THRESHOLD, -2 if the bom is not initialized
238
 **/
239
int bom_get_max(void) {
240
    int i, lowest_val, lowest_i;
241

    
242
    if(!bom_initd)
243
      return -2;
244

    
245
    lowest_i = -1;
246
    lowest_val = 255;
247
    for(i = 0; i < NUM_BOM_LEDS; i++) {
248
        if(bom_val[i] < lowest_val) {
249
            lowest_val = bom_val[i];
250
            lowest_i = i;
251
        }
252
    }
253
    
254
    if(lowest_val < BOM_VALUE_THRESHOLD)
255
        return lowest_i;
256
    else
257
        return -1;
258
}
259

    
260
/** 
261
 * Computes the weighted average of all the bom readings to estimate the position (and distance) of another robot.
262
 *
263
 * @pre must call bom refresh
264
 * @param dist  pointer to int in which to return the estimated distance to the other robot
265
 * @return estimated position of the max bom value element as a fixed point value analogous to 10 times the
266
 *        index of the max bom value.  -1 if no value is lower than BOM_VALUE_THRESHOLD, -2 if the bom is not initialized
267
 **/
268
int bom_get_max10(int *dist) {
269
    int i, max;
270
    long long mean = 0, sum = 0;
271

    
272
    if(!bom_initd)
273
      return ERROR_LIBRARY_NOT_INITD;
274

    
275

    
276
    max = bom_get_max();
277
    if (max < 0)
278
    {
279
        if (dist)
280
        {
281
            *dist = -1;
282
        }
283
        return -1;
284
    }
285
    /* Record values into an array */
286
    for (i = 0; i < NUM_BOM_LEDS; i++) {
287
        int idx = ((i + (NUM_BOM_LEDS/2 - max) + NUM_BOM_LEDS) % NUM_BOM_LEDS) - (NUM_BOM_LEDS/2 - max);
288
        int val = 255 - bom_val[i];
289
        mean += idx * val;
290
        sum += val;
291
    }
292
    mean = (mean * 10) / sum;
293
    mean = (mean + NUM_BOM_LEDS*10) % (NUM_BOM_LEDS*10);
294

    
295
    if (dist)
296
    {
297
        *dist = 50 - sum/48;
298
    }
299

    
300
    return mean;
301
}
302

    
303
/**
304
 * Iterates through each bit in the bit_field. If the bit is set, the corresponding emitter will
305
 *    be enabled to turn on when bom_on() is called.
306
 *    bom_init must be called for this to work. Does nothing if a BOM1.0 is installed
307
 *
308
 * @param bit_field specifies which leds should be turned on when bom_on is called.  Use BOM_ALL to turn on all bom leds.
309
 *    Ex. if 0x0005 is passed, leds 0 and 2 will be turned on.
310
 *
311
 * @return 0 if init succesfull, an error code otherwise
312
 *
313
 **/
314
int bom_set_leds(int bit_field) {
315
    int i;
316
    unsigned int mask = 1<<(NUM_BOM_LEDS-1);
317

    
318
    if(!bom_initd)
319
      return ERROR_LIBRARY_NOT_INITD;
320

    
321
    switch(bom_type) {
322
    case BOM10:
323
        //TODO: put an assert here to alert the user that this should not be called
324
        break;
325
                
326
    case BOM15:
327
            for(i=NUM_BOM_LEDS; i>0; i--)
328
            {
329
                    //set the current bit, sending MSB first
330
                    digital_output(BOM_DATA, bit_field&mask);
331
                    //then pulse the clock
332
                    digital_output(BOM_CLOCK, 1);
333
                    digital_output(BOM_CLOCK, 0);
334
                        mask = mask>>1;
335
            }
336
        break;
337
                
338
    case RBOM:
339
        //add rbom code here
340
        break;
341
    }
342

    
343
    return 0;
344
}
345

    
346

    
347
/**
348
 * (DEPRECATED) Returns the direction of the maximum BOM reading,
349
 * as an integer in the range 0-15. 0 indicates to the
350
 * robot's right, while the rest of the sensors are
351
 * numbered counterclockwise. This is useful for determining
352
 * the direction of a robot flashing its BOM, of only one
353
 * robot is currently doing so. analog_init must be called
354
 * before this function can be used.
355
 *
356
 * @return the direction of the maximum BOM reading
357
 *
358
 * @see analog_init
359
 **/
360
int get_max_bom(void) {
361
    bom_refresh(BOM_ALL);
362
    return bom_get_max();
363
}
364

    
365
/**
366
 * Flashes the BOM.  If using a BOM1.5, only the emitters that have been enabled using
367
 * bom_set_leds will turn on.
368
 * 
369
 * @return 0 if init succesfull, an error code otherwise
370
 *
371
 * @see bom_off, bom_set_leds
372
 **/
373
int bom_on(void)
374
{
375
  if(!bom_initd)
376
    return ERROR_LIBRARY_NOT_INITD;
377

    
378
  switch(bom_type) {
379
  case BOM10:
380
        digital_output(MONKL, 1);
381
        break;
382
  case BOM15:
383
        digital_output(BOM_STROBE, 1);
384
        break;
385
  case RBOM:
386
        break;
387
  default:
388
    return -1;
389
  }
390

    
391
  return 0;
392
}
393

    
394
/**
395
 * Turns off all bom leds.
396
 * 
397
 * @return 0 if init succesfull, an error code otherwise
398
 *
399
 * @see bom_on
400
 **/
401
int bom_off(void)
402
{
403
  if(!bom_initd)
404
    return ERROR_LIBRARY_NOT_INITD;
405

    
406
  switch(bom_type) {
407
  case BOM10:
408
        digital_output(MONKL, 0);
409
        break;
410
  case BOM15:
411
        digital_output(BOM_STROBE, 0);
412
        break;
413
  case RBOM:
414
        break;
415
  }
416

    
417
  return 0;
418
}
419

    
420
/** @} **/ //end group
421

    
422
//select a detector to read
423
static void bom_select(char which) {
424
        if(bom_type == BOM10)
425
          which = lookup[(int)which];
426
        
427
    if (which&8)
428
      digital_output(select_pins[3], 1);
429
    else
430
      digital_output(select_pins[3], 0);
431

    
432
    if (which&4)
433
      digital_output(select_pins[2], 1);
434
    else
435
      digital_output(select_pins[2], 0);
436

    
437
    if (which&2)
438
      digital_output(select_pins[1], 1);
439
    else
440
      digital_output(select_pins[1], 0);
441

    
442
    if (which&1)
443
      digital_output(select_pins[0], 1);
444
    else
445
      digital_output(select_pins[0], 0);
446
        
447
}