root / branches / analog / trunk / code / projects / libdragonfly / bom.c @ 331
History | View | Annotate | Download (4.89 KB)
1 | 241 | bcoltin | /**
|
---|---|---|---|
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 | 8 | bcoltin | #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 | /**
|
||
84 | * @defgroup bom BOM (Bearing and Orientation Module)
|
||
85 | * @brief Functions for dealing with the BOM.
|
||
86 | *
|
||
87 | * The Bearing and Orientation Module / Barrel of Monkeys / BOM
|
||
88 | * is a custom sensor designed and built by the Colony Project.
|
||
89 | * It consists of a ring of 16 IR emitters and 16 IR detectors.
|
||
90 | * The BOM is most often use to determine the direction of other
|
||
91 | * robots. This module contains functions for controlling the BOM.
|
||
92 | *
|
||
93 | * Include bom.h to access these functions.
|
||
94 | *
|
||
95 | * @{
|
||
96 | **/
|
||
97 | |||
98 | /**
|
||
99 | * Returns the direction of the maximum BOM reading,
|
||
100 | * as an integer in the range 0-15. 0 indicates to the
|
||
101 | * robot's right, while the rest of the sensors are
|
||
102 | * numbered counterclockwise. This is useful for determining
|
||
103 | * the direction of a robot flashing its BOM, of only one
|
||
104 | * robot is currently doing so. analog_init must be called
|
||
105 | * before this function can be used.
|
||
106 | *
|
||
107 | * @return the direction of the maximum BOM reading
|
||
108 | *
|
||
109 | * @see analog_init
|
||
110 | **/
|
||
111 | 328 | jykong | int get_max_bom(void) { |
112 | 8 | bcoltin | int max_bom_temp = 0; |
113 | 328 | jykong | int a, i, j, h;
|
114 | h = 255;
|
||
115 | 8 | bcoltin | |
116 | 314 | kwoo | //Turn off the loop so that we can actually use analog8 correctly
|
117 | analog_stop_loop(); |
||
118 | |||
119 | //Iterate through through each LED
|
||
120 | 328 | jykong | for (j = 0; j < 16; j++) |
121 | { |
||
122 | i = lookup[j]; |
||
123 | 8 | bcoltin | |
124 | 328 | jykong | if (i&8) |
125 | output_high(MONK3); |
||
126 | else
|
||
127 | output_low(MONK3); |
||
128 | |||
129 | if (i&4) |
||
130 | output_high(MONK2); |
||
131 | else
|
||
132 | output_low(MONK2); |
||
133 | |||
134 | if (i&2) |
||
135 | output_high(MONK1); |
||
136 | else
|
||
137 | output_low(MONK1); |
||
138 | |||
139 | if (i&1) |
||
140 | output_high(MONK0); |
||
141 | else
|
||
142 | output_low(MONK0); |
||
143 | |||
144 | 331 | cmar | a = analog_get8(MONKI); |
145 | 328 | jykong | |
146 | if (a < h)
|
||
147 | { |
||
148 | h = a; |
||
149 | max_bom_temp = j; |
||
150 | } |
||
151 | |||
152 | } |
||
153 | 314 | kwoo | |
154 | //Restart loop now that we are done using analog8
|
||
155 | analog_start_loop(); |
||
156 | 8 | bcoltin | |
157 | //threshold on the bom analog value.
|
||
158 | //defined in bom.h
|
||
159 | // if the analog value read is above the threshold, we cannot see a robot
|
||
160 | // (remember, low means higher intensity).
|
||
161 | if(h < BOM_VALUE_THRESHOLD) {
|
||
162 | return max_bom_temp;
|
||
163 | } |
||
164 | else
|
||
165 | return -1; |
||
166 | } |
||
167 | |||
168 | /**
|
||
169 | * Flashes the BOM. analog_init must be called before this
|
||
170 | * function can be used.
|
||
171 | *
|
||
172 | * @see bom_off, analog_init
|
||
173 | **/
|
||
174 | void bom_on(void) |
||
175 | { |
||
176 | output_high(MONKL); |
||
177 | } |
||
178 | |||
179 | /**
|
||
180 | * Stops flashing the BOM. analog_init must be called
|
||
181 | * before this function can be used.
|
||
182 | *
|
||
183 | * @see bom_on, analog_init
|
||
184 | **/
|
||
185 | void bom_off(void) |
||
186 | { |
||
187 | output_low(MONKL); |
||
188 | } |
||
189 | |||
190 | /** @} **/ //end group |
||
191 | |||
192 | void output_high(int which) { |
||
193 | digital_output(which, 1);
|
||
194 | } |
||
195 | |||
196 | void output_low(int which) { |
||
197 | digital_output(which, 0);
|
||
198 | } |