Revision 891
Moved comments to headers. That was painful.
bom.c | ||
---|---|---|
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 |
static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa}; |
|
43 |
|
|
44 |
// internal function prototypes |
|
45 |
static void bom_select(char which); |
|
46 |
|
|
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 |
#define BOM_VALUE_THRESHOLD 200 |
|
80 |
#define NUM_BOM_LEDS 16 |
|
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 |
* @{ |
|
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. |
|
96 | 24 |
**/ |
97 | 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 |
static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa}; |
|
43 |
|
|
44 |
// internal function prototypes |
|
45 |
static void bom_select(char which); |
|
46 |
|
|
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 |
#define BOM_VALUE_THRESHOLD 200 |
|
80 |
#define NUM_BOM_LEDS 16 |
|
81 |
|
|
82 |
|
|
83 |
|
|
98 | 84 |
static unsigned int bom_val[NUM_BOM_LEDS]; |
99 | 85 |
static char bom_type = BOM; |
100 | 86 |
|
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 | 87 |
void bom_init(char type) { |
110 | 88 |
bom_type = type; |
111 | 89 |
|
... | ... | |
120 | 98 |
} |
121 | 99 |
} |
122 | 100 |
|
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 | 101 |
void bom_refresh(int bit_field) { |
135 | 102 |
int i; |
136 | 103 |
|
... | ... | |
147 | 114 |
analog_start_loop(); |
148 | 115 |
} |
149 | 116 |
|
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 |
* @see bom_refresh |
|
158 |
**/ |
|
159 | 117 |
int bom_get(int which) { |
160 | 118 |
return bom_val[which]; |
161 | 119 |
} |
162 | 120 |
|
163 |
/** |
|
164 |
* Compares all the values in bom_val[] and returns the index to the lowest (max) value element. |
|
165 |
* 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 |
* @return index to the lowest (max) bom value element. -1 if no value is lower than |
|
169 |
* BOM_VALUE_THRESHOLD |
|
170 |
**/ |
|
171 | 121 |
int bom_get_max(void) { |
172 | 122 |
int i, lowest_val, lowest_i; |
173 | 123 |
lowest_i = -1; |
174 |
lowest_val = 255;
|
|
175 |
for(i = 0; i < NUM_BOM_LEDS; i++) {
|
|
124 |
lowest_val = 255; |
|
125 |
for(i = 0; i < NUM_BOM_LEDS; i++) { |
|
176 | 126 |
if(bom_val[i] < lowest_val) { |
177 | 127 |
lowest_val = bom_val[i]; |
178 | 128 |
lowest_i = i; |
... | ... | |
185 | 135 |
return -1; |
186 | 136 |
} |
187 | 137 |
|
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 | 138 |
void bom_leds_on(int bit_field) { |
196 | 139 |
switch(bom_type) { |
197 | 140 |
case BOM: |
... | ... | |
205 | 148 |
case RBOM: |
206 | 149 |
//add rbom code here |
207 | 150 |
break; |
208 |
}
|
|
151 |
} |
|
209 | 152 |
} |
210 | 153 |
|
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 | 154 |
void bom_leds_off(int bit_field) { |
219 | 155 |
switch(bom_type) { |
220 | 156 |
case BOM: |
... | ... | |
228 | 164 |
case RBOM: |
229 | 165 |
//add rbom code here |
230 | 166 |
break; |
231 |
}
|
|
167 |
} |
|
232 | 168 |
} |
233 |
|
|
234 |
|
|
235 |
/** |
|
236 |
* (DEPRECATED) Wrapper function for new BOM code. Refreshes buffer and returns the max bom value. |
|
237 |
* |
|
238 |
* @return the direction of the maximum BOM reading |
|
239 |
* |
|
240 |
* @see bom_refresh, bom_get_max |
|
241 |
**/ |
|
242 |
int get_max_bom(void) { |
|
243 |
bom_refresh(BOM_ALL); |
|
244 |
return bom_get_max(); |
|
245 |
} |
|
246 |
|
|
247 |
/** |
|
248 |
* (DEPRECATED) Turns on all bom leds. |
|
249 |
* |
|
250 |
* @see bom_off |
|
251 |
**/ |
|
252 |
void bom_on(void) |
|
253 |
{ |
|
254 |
bom_leds_on(BOM_ALL); |
|
255 |
} |
|
256 |
|
|
257 |
/** |
|
258 |
* (DEPRECATED) Turns off all bom leds. |
|
259 |
* |
|
260 |
* @see bom_on |
|
261 |
**/ |
|
262 |
void bom_off(void) |
|
263 |
{ |
|
264 |
bom_leds_off(BOM_ALL); |
|
265 |
} |
|
266 |
|
|
267 |
/** @} **/ //end group |
|
268 |
+ |
|
169 |
void bom_on(void) |
|
170 |
{ |
|
171 |
bom_leds_on(BOM_ALL); |
|
172 |
} |
|
173 |
|
|
174 |
void bom_off(void) |
|
175 |
{ |
|
176 |
bom_leds_off(BOM_ALL); |
|
177 |
} |
|
178 |
|
|
269 | 179 |
static void bom_select(char which) { |
270 |
if (which&8)
|
|
271 |
digital_output(MONK3, 1);
|
|
272 |
else
|
|
273 |
digital_output(MONK3, 0);
|
|
274 |
|
|
275 |
if (which&4)
|
|
276 |
digital_output(MONK2, 1);
|
|
277 |
else
|
|
278 |
digital_output(MONK2, 0);
|
|
279 |
|
|
280 |
if (which&2)
|
|
281 |
digital_output(MONK1, 1);
|
|
282 |
else
|
|
283 |
digital_output(MONK1, 0);
|
|
284 |
|
|
285 |
if (which&1)
|
|
286 |
digital_output(MONK0, 1);
|
|
287 |
else
|
|
180 |
if (which&8) |
|
181 |
digital_output(MONK3, 1); |
|
182 |
else |
|
183 |
digital_output(MONK3, 0); |
|
184 |
|
|
185 |
if (which&4) |
|
186 |
digital_output(MONK2, 1); |
|
187 |
else |
|
188 |
digital_output(MONK2, 0); |
|
189 |
|
|
190 |
if (which&2) |
|
191 |
digital_output(MONK1, 1); |
|
192 |
else |
|
193 |
digital_output(MONK1, 0); |
|
194 |
|
|
195 |
if (which&1) |
|
196 |
digital_output(MONK0, 1); |
|
197 |
else |
|
288 | 198 |
digital_output(MONK0, 0); |
289 | 199 |
} |
200 |
|
Also available in: Unified diff