root / branches / analog / code / projects / libdragonfly / bom.c @ 1634
History | View | Annotate | Download (10.3 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 | 945 | abuchan | #include "serial.h" |
40 | 8 | bcoltin | #include "analog.h" |
41 | |||
42 | 945 | abuchan | //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 | 448 | cmar | static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa}; |
46 | 8 | bcoltin | |
47 | // internal function prototypes
|
||
48 | 448 | cmar | static void bom_select(char which); |
49 | 8 | bcoltin | |
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 | 1424 | jsexton | #define BOM_VALUE_THRESHOLD 150 //200 |
83 | 448 | cmar | #define NUM_BOM_LEDS 16 |
84 | 8 | bcoltin | |
85 | 945 | abuchan | /*
|
86 | *The following pin definitions are for the BOM v1.5
|
||
87 | */
|
||
88 | 8 | bcoltin | |
89 | 945 | abuchan | #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 | 1079 | deffi | #define BOM_S2 _PIN_E7 //dio4 |
98 | #define BOM_S3 _PIN_E6 //dio5 |
||
99 | 945 | abuchan | #define BOM_OUT PF0 //analog(yellow) |
100 | |||
101 | 8 | bcoltin | /**
|
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 | 945 | abuchan | **/
|
115 | |||
116 | static unsigned int bom_val[NUM_BOM_LEDS]; |
||
117 | static volatile char bom_type = BOM; |
||
118 | static int select_pins[4]; |
||
119 | static int analog_pin; |
||
120 | |||
121 | /**
|
||
122 | * Initializes the BOM.
|
||
123 | * Call bom_init before reading bom values or turning bom leds.
|
||
124 | *
|
||
125 | * @bugs INCOMPLETE - No utilization of BOM1.5 RSSI capability. Probably leave this out
|
||
126 | * until Cornell and Pras return
|
||
127 | *
|
||
128 | * @see bom_refresh, bom_leds_on, bom_leds_off
|
||
129 | **/
|
||
130 | void bom_init(char type) { |
||
131 | bom_type = type; |
||
132 | |||
133 | switch(bom_type) {
|
||
134 | case BOM10:
|
||
135 | select_pins[0] = MONK0;
|
||
136 | select_pins[1] = MONK1;
|
||
137 | select_pins[2] = MONK2;
|
||
138 | select_pins[3] = MONK3;
|
||
139 | analog_pin = MONKI; |
||
140 | break;
|
||
141 | case BOM15:
|
||
142 | //Sets BOM1.5 to normal [BOM] mode
|
||
143 | digital_output(BOM_MODE, 0);
|
||
144 | select_pins[0] = BOM_S0;
|
||
145 | select_pins[1] = BOM_S1;
|
||
146 | select_pins[2] = BOM_S2;
|
||
147 | select_pins[3] = BOM_S3;
|
||
148 | bom_set_leds(BOM_ALL); |
||
149 | analog_pin = BOM_OUT; |
||
150 | break;
|
||
151 | case RBOM:
|
||
152 | break;
|
||
153 | //default:
|
||
154 | } |
||
155 | } |
||
156 | |||
157 | /**
|
||
158 | * Iterates through each bit in the bit_field. For each set bit, sets the corresponding bom select bits
|
||
159 | * and updates the corresponding bom value with an analog_get8 reading. analog_init and bom_init
|
||
160 | 1417 | emullini | * must be called for this to work. Must call this before reading BOM values!
|
161 | 945 | abuchan | *
|
162 | *
|
||
163 | * @param bit_field specifies which elements in bom_val[] should be updated. Use BOM_ALL to refresh all values.
|
||
164 | * Ex. if 0x0003 is passed, bom_val[0] and bom_val[1] will be updated.
|
||
165 | *
|
||
166 | * @see bom_get
|
||
167 | **/
|
||
168 | void bom_refresh(int bit_field) { |
||
169 | int i;
|
||
170 | 1406 | jykong | int loop_was_running = 0; |
171 | 945 | abuchan | |
172 | 1406 | jykong | //Check analog loop status
|
173 | if(analog_loop_status() == ADC_LOOP_RUNNING) {
|
||
174 | loop_was_running = 1;
|
||
175 | analog_stop_loop(); |
||
176 | } |
||
177 | 945 | abuchan | |
178 | 1406 | jykong | //Read BOM values
|
179 | 945 | abuchan | for(i = 0; i < NUM_BOM_LEDS; i++) { |
180 | if(bit_field & 0x1) { |
||
181 | bom_select(i); |
||
182 | bom_val[i] = analog_get8(analog_pin); |
||
183 | } |
||
184 | bit_field = bit_field >> 1;
|
||
185 | } |
||
186 | |||
187 | 1406 | jykong | //Restore analog loop status
|
188 | if(loop_was_running)
|
||
189 | analog_start_loop(); |
||
190 | 945 | abuchan | } |
191 | |||
192 | /**
|
||
193 | * Gets the bom reading from bom_val[which]. Call bom_refresh beforehand to read new bom values.
|
||
194 | *
|
||
195 | 1417 | emullini | * @pre must call bom refresh first
|
196 | *
|
||
197 | 945 | abuchan | * @param which which bom value to return
|
198 | *
|
||
199 | * @return the bom value
|
||
200 | *
|
||
201 | * see bom_refresh
|
||
202 | **/
|
||
203 | int bom_get(int which) { |
||
204 | 1634 | emullini | //return bom_val[which];
|
205 | bom_select(which); |
||
206 | return analog_get8(analog_pin);
|
||
207 | 945 | abuchan | } |
208 | |||
209 | /**
|
||
210 | * Compares all the values in bom_val[] and returns the index to the lowest (max) value element.
|
||
211 | *
|
||
212 | 1417 | emullini | * @pre must call bom refresh
|
213 | 945 | abuchan | * @return index to the lowest (max) bom value element. -1 if no value is lower than
|
214 | * BOM_VALUE_THRESHOLD
|
||
215 | **/
|
||
216 | int bom_get_max(void) { |
||
217 | 1634 | emullini | int i, lowest_val, lowest_i, i_val;
|
218 | 945 | abuchan | lowest_i = -1;
|
219 | 448 | cmar | lowest_val = 255;
|
220 | for(i = 0; i < NUM_BOM_LEDS; i++) { |
||
221 | 1634 | emullini | i_val = bom_get(i); |
222 | if(i_val < lowest_val) {
|
||
223 | lowest_val = i_val; |
||
224 | 945 | abuchan | lowest_i = i; |
225 | } |
||
226 | 448 | cmar | } |
227 | 945 | abuchan | |
228 | if(lowest_val < BOM_VALUE_THRESHOLD)
|
||
229 | return lowest_i;
|
||
230 | else
|
||
231 | return -1; |
||
232 | 1443 | rcahoon | } |
233 | |||
234 | /**
|
||
235 | * Computes the weighted average of all the bom readings to estimate the position (and distance) of another robot.
|
||
236 | *
|
||
237 | * @pre must call bom refresh
|
||
238 | * @param dist pointer to int in which to return the estimated distance to the other robot
|
||
239 | * @return estimated position of the max bom value element as a fixed point value analogous to 10 times the
|
||
240 | * index of the max bom value. -1 if no value is lower than BOM_VALUE_THRESHOLD.
|
||
241 | **/
|
||
242 | int bom_get_max10(int *dist) { |
||
243 | int i, max;
|
||
244 | 1445 | rcahoon | long long mean = 0, sum = 0; |
245 | 1443 | rcahoon | |
246 | max = bom_get_max(); |
||
247 | if (max < 0) |
||
248 | { |
||
249 | if (dist)
|
||
250 | { |
||
251 | *dist = -1;
|
||
252 | } |
||
253 | return -1; |
||
254 | } |
||
255 | /* Record values into an array */
|
||
256 | for (i = 0; i < NUM_BOM_LEDS; i++) { |
||
257 | int idx = ((i + (NUM_BOM_LEDS/2 - max) + NUM_BOM_LEDS) % NUM_BOM_LEDS) - (NUM_BOM_LEDS/2 - max); |
||
258 | int val = 255 - bom_val[i]; |
||
259 | mean += idx * val; |
||
260 | sum += val; |
||
261 | } |
||
262 | mean = (mean * 10) / sum;
|
||
263 | mean = (mean + NUM_BOM_LEDS*10) % (NUM_BOM_LEDS*10); |
||
264 | |||
265 | if (dist)
|
||
266 | { |
||
267 | *dist = 50 - sum/48; |
||
268 | } |
||
269 | |||
270 | return mean;
|
||
271 | 945 | abuchan | } |
272 | |||
273 | /**
|
||
274 | * Iterates through each bit in the bit_field. If the bit is set, the corresponding emitter will
|
||
275 | * be enabled to turn on when bom_on() is called.
|
||
276 | 1051 | abuchan | * bom_init must be called for this to work. Does nothing if a BOM1.0 is installed
|
277 | 945 | abuchan | *
|
278 | 1051 | abuchan | * @param bit_field specifies which leds should be turned on when bom_on is called. Use BOM_ALL to turn on all bom leds.
|
279 | 945 | abuchan | * Ex. if 0x0005 is passed, leds 0 and 2 will be turned on.
|
280 | **/
|
||
281 | void bom_set_leds(int bit_field) { |
||
282 | int i;
|
||
283 | unsigned int mask = 1<<(NUM_BOM_LEDS-1); |
||
284 | switch(bom_type) {
|
||
285 | 1051 | abuchan | case BOM10:
|
286 | //TODO: put an assert here to alert the user that this should not be called
|
||
287 | 945 | abuchan | break;
|
288 | |||
289 | case BOM15:
|
||
290 | for(i=NUM_BOM_LEDS; i>0; i--) |
||
291 | { |
||
292 | //set the current bit, sending MSB first
|
||
293 | digital_output(BOM_DATA, bit_field&mask); |
||
294 | //then pulse the clock
|
||
295 | digital_output(BOM_CLOCK, 1);
|
||
296 | digital_output(BOM_CLOCK, 0);
|
||
297 | mask = mask>>1;
|
||
298 | } |
||
299 | break;
|
||
300 | |||
301 | case RBOM:
|
||
302 | //add rbom code here
|
||
303 | break;
|
||
304 | 448 | cmar | } |
305 | 945 | abuchan | } |
306 | 8 | bcoltin | |
307 | 448 | cmar | |
308 | 8 | bcoltin | /**
|
309 | 945 | abuchan | * (DEPRECATED) Returns the direction of the maximum BOM reading,
|
310 | * as an integer in the range 0-15. 0 indicates to the
|
||
311 | * robot's right, while the rest of the sensors are
|
||
312 | * numbered counterclockwise. This is useful for determining
|
||
313 | * the direction of a robot flashing its BOM, of only one
|
||
314 | * robot is currently doing so. analog_init must be called
|
||
315 | * before this function can be used.
|
||
316 | 8 | bcoltin | *
|
317 | * @return the direction of the maximum BOM reading
|
||
318 | *
|
||
319 | 945 | abuchan | * @see analog_init
|
320 | 8 | bcoltin | **/
|
321 | int get_max_bom(void) { |
||
322 | 1634 | emullini | //bom_refresh(BOM_ALL);
|
323 | 448 | cmar | return bom_get_max();
|
324 | 8 | bcoltin | } |
325 | |||
326 | /**
|
||
327 | 945 | abuchan | * Flashes the BOM. If using a BOM1.5, only the emitters that have been enabled using
|
328 | * bom_set_leds will turn on.
|
||
329 | 8 | bcoltin | *
|
330 | 945 | abuchan | * @see bom_off, bom_set_leds
|
331 | 8 | bcoltin | **/
|
332 | void bom_on(void) |
||
333 | { |
||
334 | 945 | abuchan | switch(bom_type) {
|
335 | case BOM10:
|
||
336 | digital_output(MONKL, 1);
|
||
337 | break;
|
||
338 | case BOM15:
|
||
339 | digital_output(BOM_STROBE, 1);
|
||
340 | break;
|
||
341 | case RBOM:
|
||
342 | break;
|
||
343 | } |
||
344 | 8 | bcoltin | } |
345 | |||
346 | /**
|
||
347 | 945 | abuchan | * Turns off all bom leds.
|
348 | 8 | bcoltin | *
|
349 | 448 | cmar | * @see bom_on
|
350 | 8 | bcoltin | **/
|
351 | void bom_off(void) |
||
352 | { |
||
353 | 945 | abuchan | switch(bom_type) {
|
354 | 1051 | abuchan | case BOM10:
|
355 | 945 | abuchan | digital_output(MONKL, 0);
|
356 | break;
|
||
357 | case BOM15:
|
||
358 | digital_output(BOM_STROBE, 0);
|
||
359 | break;
|
||
360 | case RBOM:
|
||
361 | break;
|
||
362 | } |
||
363 | 8 | bcoltin | } |
364 | |||
365 | /** @} **/ //end group |
||
366 | |||
367 | 945 | abuchan | //select a detector to read
|
368 | static void bom_select(char which) { |
||
369 | 1051 | abuchan | if(bom_type == BOM10)
|
370 | 945 | abuchan | which = lookup[(int)which];
|
371 | |||
372 | if (which&8) |
||
373 | digital_output(select_pins[3], 1); |
||
374 | else
|
||
375 | digital_output(select_pins[3], 0); |
||
376 | 8 | bcoltin | |
377 | 945 | abuchan | if (which&4) |
378 | digital_output(select_pins[2], 1); |
||
379 | else
|
||
380 | digital_output(select_pins[2], 0); |
||
381 | 8 | bcoltin | |
382 | 945 | abuchan | if (which&2) |
383 | digital_output(select_pins[1], 1); |
||
384 | else
|
||
385 | digital_output(select_pins[1], 0); |
||
386 | |||
387 | if (which&1) |
||
388 | digital_output(select_pins[0], 1); |
||
389 | else
|
||
390 | digital_output(select_pins[0], 0); |
||
391 | |||
392 | } |