Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (6.46 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
//constants
42
const int lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
43

    
44
// internal function prototypes
45
void output_high(int which);
46
void output_low(int which);
47

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

    
59

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

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

69
*/
70

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

    
80
#define BOM_VALUE_THRESHOLD 200
81

    
82
/*
83
  *The following pin definitions are for the BOM v1.5
84
  */
85

    
86
#define BOM_MODE        _PIN_E2        //dio0
87
#define BOM_STROBE        _PIN_E3        //dio1
88

    
89
#define BOM_DATA        _PIN_A0 //servo0
90
#define BOM_CLOCK        _PIN_A1        //servo1
91

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

    
98
//this is set to 1 if we are using a BOM1.5, and 0 otherwise
99
static char is_bom_15 = 0;
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
 * There are currently two versions of the BOM in operation: v1.0
112
 * and v1.5.  v1.5 includes new functionality such as addressable
113
 * emitters, and RSSI range measurement.  If a BOM1.5 is connected
114
 * to a robot, make sure to add a BOM1_5 flag to dragonfly_init().
115
 * 
116
 * Include bom.h to access these functions.
117
 *
118
 * @{
119
 **/
120

    
121

    
122
/**
123
 * Initializes a BOM1.5 to have all emitters on, and in normal mode.
124
 * With this initialization the BOM1.5 is functionally equivalent to
125
 * BOM1.0
126
 **/
127
void bom_15_init(void) {
128
        is_bom_15 = 1;
129
        bom_set_emitters(0xFFFF);
130
}
131

    
132
/**
133
 * Sets a BOM1.5 to RSSI mode.
134
 **/
135
void bom_RSSI_mode(void) {
136
        output_high(BOM_MODE);
137
}
138

    
139
/**
140
 * Sets a BOM1.5 to normal mode.
141
 **/
142
void bom_normal_mode(void) {
143
        output_low(BOM_MODE);
144
}
145

    
146
/**
147
 * Sets which emitters come on when a BOM1.5 is turned on.
148
 * The LSB of the pattern corresponds to emitter 0, and the 
149
 * MSB corresponds to emitter 15
150
 **/
151
void bom_set_emitters(int pattern) {
152
        int mask = 0x8000;
153
        for(; mask>0; mask=mask>>1)
154
        {
155
                //set the current bit
156
                digital_output(BOM_DATA, 1);
157
                //then pulse the clock
158
                delay_ms(1);
159
                output_high(BOM_CLOCK);
160
                delay_ms(1);
161
                output_low(BOM_CLOCK);
162
        }
163
}
164

    
165
/**
166
 * Returns the direction of the maximum BOM reading,
167
 * as an integer in the range 0-15. 0 indicates to the
168
 * robot's right, while the rest of the sensors are
169
 * numbered counterclockwise. This is useful for determining
170
 * the direction of a robot flashing its BOM, of only one
171
 * robot is currently doing so. analog_init must be called
172
 * before this function can be used.
173
 *
174
 * @return the direction of the maximum BOM reading
175
 *
176
 * @see analog_init
177
 **/
178
int get_max_bom(void) {
179
        int max_bom_temp = 0;
180
        int a, i, j, h;
181
    h = 255;
182

    
183
    for (j = 0; j < 16; j++)
184
    {
185
      i = lookup[j];
186

    
187
      if (i&8)
188
        output_high(is_bom_15?BOM_S3:MONK3);
189
      else
190
        output_low(is_bom_15?BOM_S3:MONK3);
191

    
192
      if (i&4)
193
        output_high(is_bom_15?BOM_S2:MONK2);
194
      else
195
        output_low(is_bom_15?BOM_S2:MONK2);
196

    
197
      if (i&2)
198
        output_high(is_bom_15?BOM_S1:MONK1);
199
      else
200
        output_low(is_bom_15?BOM_S1:MONK1);
201

    
202
      if (i&1)
203
        output_high(is_bom_15?BOM_S0:MONK0);
204
      else
205
        output_low(is_bom_15?BOM_S0:MONK0);
206

    
207
      a = analog8(is_bom_15?BOM_OUT:MONKI);
208
              
209
      if (a < h)
210
      {
211
        h = a;
212
        max_bom_temp = j;
213
      }
214

    
215
    }
216

    
217
        //threshold on the bom analog value.
218
        //defined in bom.h
219
        // if the analog value read is above the threshold, we cannot see a robot
220
        // (remember, low means higher intensity).
221
        if(h < BOM_VALUE_THRESHOLD) {
222
                return max_bom_temp;
223
        }
224
        else
225
                return -1;
226
}
227

    
228
/**
229
 * Flashes the BOM. analog_init must be called before this
230
 * function can be used.
231
 * 
232
 * @see bom_off, analog_init
233
 **/
234
void bom_on(void)
235
{
236
  output_high(is_bom_15?BOM_STROBE:MONKL);
237
}
238

    
239
/**
240
 * Stops flashing the BOM. analog_init must be called
241
 * before this function can be used.
242
 * 
243
 * @see bom_on, analog_init
244
 **/
245
void bom_off(void)
246
{
247
  output_low(is_bom_15?BOM_STROBE:MONKL);
248
}
249

    
250
/** @} **/ //end group
251

    
252
void output_high(int which) {
253
        digital_output(which, 1);
254
}
255

    
256
void output_low(int which) {
257
        digital_output(which, 0);
258
}
259