root / trunk / code / projects / libdragonfly / bom.c @ 924
History | View | Annotate | Download (7.4 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 | 448 | cmar | static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa}; |
43 | 8 | bcoltin | |
44 | // internal function prototypes
|
||
45 | 448 | cmar | static void bom_select(char which); |
46 | 8 | bcoltin | |
47 | /*
|
||
48 | Bk R Y (Analog)
|
||
49 | ---------
|
||
50 | Green
|
||
51 | Blue
|
||
52 | White
|
||
53 | ---------
|
||
54 | Blue
|
||
55 | White
|
||
56 | */
|
||
57 | |||
58 | |||
59 | /*
|
||
60 | the analog pin definitions from dio.h DO NOT work here,
|
||
61 | so we must use PF0 from avrgcc (as opposed to _PIN_F0).
|
||
62 | BUT the dio pin definitions from dio.h must be used (no PE...).
|
||
63 | |||
64 | also, _PIN_E2 is initialized to high for some reason,
|
||
65 | which turns the BOM on when the robot is turned on.
|
||
66 | WORK-AROUND: call digital_output(_PIN_E2,0) at some point.
|
||
67 | |||
68 | */
|
||
69 | |||
70 | #define MONKI PF0 //analog (yellow) |
||
71 | //------------------------//
|
||
72 | #define MONKL _PIN_E2 //green |
||
73 | #define MONK1 _PIN_E3 //blue |
||
74 | #define MONK0 _PIN_E4 //white |
||
75 | //------------------------//
|
||
76 | #define MONK3 _PIN_E6 //blue |
||
77 | #define MONK2 _PIN_E7 //white |
||
78 | |||
79 | 448 | cmar | #define BOM_VALUE_THRESHOLD 200 |
80 | #define NUM_BOM_LEDS 16 |
||
81 | 8 | bcoltin | |
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 | 448 | cmar | **/
|
97 | |||
98 | static unsigned int bom_val[NUM_BOM_LEDS]; |
||
99 | static char bom_type = BOM; |
||
100 | |||
101 | /**
|
||
102 | * Initializes the BOM.
|
||
103 | * Call bom_init before reading bom values or turning bom leds.
|
||
104 | *
|
||
105 | * @bugs INCOMPLETE - need to fill in init routine for BOM15
|
||
106 | *
|
||
107 | * @see bom_refresh, bom_leds_on, bom_leds_off
|
||
108 | **/
|
||
109 | void bom_init(char type) { |
||
110 | bom_type = type; |
||
111 | |||
112 | switch(bom_type) {
|
||
113 | case BOM:
|
||
114 | break;
|
||
115 | case BOM15:
|
||
116 | break;
|
||
117 | case RBOM:
|
||
118 | break;
|
||
119 | //default:
|
||
120 | } |
||
121 | } |
||
122 | |||
123 | /**
|
||
124 | * Iterates through each bit in the bit_field. For each set bit, sets the corresponding bom select bits
|
||
125 | * and updates the corresponding bom value with an analog_get8 reading. analog_init and bom_init
|
||
126 | * must be called for this to work.
|
||
127 | *
|
||
128 | *
|
||
129 | * @param bit_field specifies which elements in bom_val[] should be updated. Use BOM_ALL to refresh all values.
|
||
130 | * Ex. if 0x0003 is passed, bom_val[0] and bom_val[1] will be updated.
|
||
131 | *
|
||
132 | * @see bom_get
|
||
133 | **/
|
||
134 | void bom_refresh(int bit_field) { |
||
135 | int i;
|
||
136 | |||
137 | analog_stop_loop(); |
||
138 | |||
139 | for(i = 0; i < NUM_BOM_LEDS; i++) { |
||
140 | if(bit_field & 0x1) { |
||
141 | bom_select(lookup[i]); |
||
142 | bom_val[i] = analog_get8(MONKI); |
||
143 | } |
||
144 | bit_field = bit_field >> 1;
|
||
145 | } |
||
146 | |||
147 | analog_start_loop(); |
||
148 | } |
||
149 | |||
150 | /**
|
||
151 | * Gets the bom reading from bom_val[which]. Call bom_refresh beforehand to read new bom values.
|
||
152 | *
|
||
153 | * @param which which bom value to return
|
||
154 | *
|
||
155 | * @return the bom value
|
||
156 | *
|
||
157 | 568 | cmar | * @see bom_refresh
|
158 | 448 | cmar | **/
|
159 | int bom_get(int which) { |
||
160 | return bom_val[which];
|
||
161 | } |
||
162 | |||
163 | /**
|
||
164 | * Compares all the values in bom_val[] and returns the index to the lowest (max) value element.
|
||
165 | 568 | cmar | * Returns the direction of the maximum BOM reading,as an integer in the range 0-15. 0 indicates
|
166 | * to the robot's right, while the rest of the sensors are numbered counterclockwise.
|
||
167 | *
|
||
168 | 448 | cmar | * @return index to the lowest (max) bom value element. -1 if no value is lower than
|
169 | * BOM_VALUE_THRESHOLD
|
||
170 | **/
|
||
171 | int bom_get_max(void) { |
||
172 | int i, lowest_val, lowest_i;
|
||
173 | lowest_i = -1;
|
||
174 | lowest_val = 255;
|
||
175 | for(i = 0; i < NUM_BOM_LEDS; i++) { |
||
176 | if(bom_val[i] < lowest_val) {
|
||
177 | lowest_val = bom_val[i]; |
||
178 | lowest_i = i; |
||
179 | } |
||
180 | 491 | cmar | } |
181 | 448 | cmar | |
182 | if(lowest_val < BOM_VALUE_THRESHOLD)
|
||
183 | return lowest_i;
|
||
184 | else
|
||
185 | return -1; |
||
186 | } |
||
187 | |||
188 | /**
|
||
189 | * Iterates through each bit in the bit_field. For each set bit, turns on the corresponding bom led.
|
||
190 | * bom_init must be called for this to work. Only works with BOM_ALL if using the original bom.
|
||
191 | *
|
||
192 | * @param bit_field specifies which leds should be turned on. Use BOM_ALL to turn on all bom leds.
|
||
193 | * Ex. if 0x0005 is passed, leds 0 and 2 will be turned on.
|
||
194 | **/
|
||
195 | void bom_leds_on(int bit_field) { |
||
196 | switch(bom_type) {
|
||
197 | case BOM:
|
||
198 | if(bit_field == BOM_ALL) {
|
||
199 | digital_output(MONKL, 1);
|
||
200 | } |
||
201 | break;
|
||
202 | case BOM15:
|
||
203 | //add bom 1.5 code here
|
||
204 | break;
|
||
205 | case RBOM:
|
||
206 | //add rbom code here
|
||
207 | break;
|
||
208 | } |
||
209 | } |
||
210 | |||
211 | /**
|
||
212 | * Iterates through each bit in the bit_field. For each set bit, turns off the corresponding bom led.
|
||
213 | * bom_init must be called for this to work. Only works with BOM_ALL if using the original bom.
|
||
214 | *
|
||
215 | * @param bit_field specifies which leds should be turned off. Use BOM_ALL to turn off all bom leds.
|
||
216 | * Ex. if 0x000B is passed, leds 0 and 3 will be turned off.
|
||
217 | **/
|
||
218 | void bom_leds_off(int bit_field) { |
||
219 | switch(bom_type) {
|
||
220 | case BOM:
|
||
221 | if(bit_field == BOM_ALL) {
|
||
222 | digital_output(MONKL, 0);
|
||
223 | } |
||
224 | break;
|
||
225 | case BOM15:
|
||
226 | //add bom 1.5 code here
|
||
227 | break;
|
||
228 | case RBOM:
|
||
229 | //add rbom code here
|
||
230 | break;
|
||
231 | } |
||
232 | } |
||
233 | 8 | bcoltin | |
234 | 448 | cmar | |
235 | 8 | bcoltin | /**
|
236 | 568 | cmar | * (DEPRECATED) Wrapper function for new BOM code. Refreshes buffer and returns the max bom value.
|
237 | 8 | bcoltin | *
|
238 | * @return the direction of the maximum BOM reading
|
||
239 | *
|
||
240 | 568 | cmar | * @see bom_refresh, bom_get_max
|
241 | 8 | bcoltin | **/
|
242 | int get_max_bom(void) { |
||
243 | 448 | cmar | bom_refresh(BOM_ALL); |
244 | return bom_get_max();
|
||
245 | 8 | bcoltin | } |
246 | |||
247 | /**
|
||
248 | 448 | cmar | * (DEPRECATED) Turns on all bom leds.
|
249 | 8 | bcoltin | *
|
250 | 448 | cmar | * @see bom_off
|
251 | 8 | bcoltin | **/
|
252 | void bom_on(void) |
||
253 | { |
||
254 | 448 | cmar | bom_leds_on(BOM_ALL); |
255 | 8 | bcoltin | } |
256 | |||
257 | /**
|
||
258 | 448 | cmar | * (DEPRECATED) Turns off all bom leds.
|
259 | 8 | bcoltin | *
|
260 | 448 | cmar | * @see bom_on
|
261 | 8 | bcoltin | **/
|
262 | void bom_off(void) |
||
263 | { |
||
264 | 448 | cmar | bom_leds_off(BOM_ALL); |
265 | 8 | bcoltin | } |
266 | |||
267 | /** @} **/ //end group |
||
268 | 448 | cmar | |
269 | static void bom_select(char which) { |
||
270 | if (which&8) |
||
271 | digital_output(MONK3, 1);
|
||
272 | else
|
||
273 | digital_output(MONK3, 0);
|
||
274 | 8 | bcoltin | |
275 | 448 | cmar | if (which&4) |
276 | digital_output(MONK2, 1);
|
||
277 | else
|
||
278 | digital_output(MONK2, 0);
|
||
279 | 8 | bcoltin | |
280 | 448 | cmar | if (which&2) |
281 | digital_output(MONK1, 1);
|
||
282 | else
|
||
283 | digital_output(MONK1, 0);
|
||
284 | 8 | bcoltin | |
285 | 448 | cmar | if (which&1) |
286 | digital_output(MONK0, 1);
|
||
287 | else
|
||
288 | digital_output(MONK0, 0);
|
||
289 | } |