Project

General

Profile

Statistics
| Revision:

root / trunk / code / lib / src / libdragonfly / bom.c @ 352

History | View | Annotate | Download (5.07 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
//TODO: for better readability, make all of these hex or decimal
43
const int lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
44

    
45
// internal function prototypes
46
//TODO: if these are only used in this file, make them static
47
void output_high(int which);
48
void output_low(int 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

    
84

    
85
/**
86
 * @defgroup bom BOM (Bearing and Orientation Module)
87
 * @brief Functions for dealing with the BOM.
88
 *
89
 * The Bearing and Orientation Module / Barrel of Monkeys / BOM
90
 * is a custom sensor designed and built by the Colony Project.
91
 * It consists of a ring of 16 IR emitters and 16 IR detectors.
92
 * The BOM is most often use to determine the direction of other
93
 * robots. This module contains functions for controlling the BOM.
94
 *
95
 * Include bom.h to access these functions.
96
 *
97
 * @{
98
 **/
99

    
100
/**
101
 * Returns the direction of the maximum BOM reading,
102
 * as an integer in the range 0-15. 0 indicates to the
103
 * robot's right, while the rest of the sensors are
104
 * numbered counterclockwise. This is useful for determining
105
 * the direction of a robot flashing its BOM, of only one
106
 * robot is currently doing so. analog_init must be called
107
 * before this function can be used.
108
 *
109
 * @return the direction of the maximum BOM reading
110
 *
111
 * @see analog_init
112
 **/
113
int get_max_bom(void) {
114
        int max_bom_temp = 0;
115
        //TODO: renamed a, i, and h to give them meaningful names
116
        int a, i, j, h;
117
    h = 255;
118

    
119
        //Turn off the loop so that we can actually use analog8 correctly
120
        analog_stop_loop();
121

    
122
        //Iterate through through each LED
123
    for (j = 0; j < 16; j++)
124
    {
125
      i = lookup[j];
126

    
127
      if (i&8)
128
        output_high(MONK3);
129
      else
130
        output_low(MONK3);
131

    
132
      if (i&4)
133
        output_high(MONK2);
134
      else
135
        output_low(MONK2);
136

    
137
      if (i&2)
138
        output_high(MONK1);
139
      else
140
        output_low(MONK1);
141

    
142
      if (i&1)
143
        output_high(MONK0);
144
      else
145
        output_low(MONK0);
146

    
147
      a = analog_get8(MONKI);
148
              
149
      if (a < h)
150
      {
151
        h = a;
152
        max_bom_temp = j;
153
      }
154

    
155
    }
156
        
157
        //Restart loop now that we are done using analog8
158
        analog_start_loop();
159

    
160
        //threshold on the bom analog value.
161
        //defined in bom.h
162
        // if the analog value read is above the threshold, we cannot see a robot
163
        // (remember, low means higher intensity).
164
        if(h < BOM_VALUE_THRESHOLD) {
165
                return max_bom_temp;
166
        }
167
        else
168
                return -1;
169
}
170

    
171
/**
172
 * Flashes the BOM. analog_init must be called before this
173
 * function can be used.
174
 * 
175
 * @see bom_off, analog_init
176
 **/
177
void bom_on(void)
178
{
179
  output_high(MONKL);
180
}
181

    
182
/**
183
 * Stops flashing the BOM. analog_init must be called
184
 * before this function can be used.
185
 * 
186
 * @see bom_on, analog_init
187
 **/
188
void bom_off(void)
189
{
190
  output_low(MONKL);
191
}
192

    
193
/** @} **/ //end group
194

    
195
void output_high(int which) {
196
        digital_output(which, 1);
197
}
198

    
199
void output_low(int which) {
200
        digital_output(which, 0);
201
}