Revision 276
cleaned up library - dos2unix and delete-trailing-whitespace
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 |
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 |
int get_max_bom(void) { |
|
112 |
int max_bom_temp = 0; |
|
113 |
int a, i, j, h; |
|
114 |
h = 255; |
|
115 |
|
|
116 |
for (j = 0; j < 16; j++) |
|
117 |
{ |
|
118 |
i = lookup[j]; |
|
119 |
|
|
120 |
if (i&8) |
|
121 |
output_high(MONK3); |
|
122 |
else |
|
123 |
output_low(MONK3); |
|
124 |
|
|
125 |
if (i&4) |
|
126 |
output_high(MONK2); |
|
127 |
else |
|
128 |
output_low(MONK2); |
|
129 |
|
|
130 |
if (i&2) |
|
131 |
output_high(MONK1); |
|
132 |
else |
|
133 |
output_low(MONK1); |
|
134 |
|
|
135 |
if (i&1) |
|
136 |
output_high(MONK0); |
|
137 |
else |
|
138 |
output_low(MONK0); |
|
139 |
|
|
140 |
a = analog8(MONKI); |
|
141 |
|
|
142 |
if (a < h) |
|
143 |
{ |
|
144 |
h = a; |
|
145 |
max_bom_temp = j; |
|
146 |
} |
|
147 |
|
|
148 |
} |
|
149 |
|
|
150 |
//threshold on the bom analog value. |
|
151 |
//defined in bom.h |
|
152 |
// if the analog value read is above the threshold, we cannot see a robot |
|
153 |
// (remember, low means higher intensity). |
|
154 |
if(h < BOM_VALUE_THRESHOLD) { |
|
155 |
return max_bom_temp; |
|
156 |
} |
|
157 |
else |
|
158 |
return -1; |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
162 |
* Flashes the BOM. analog_init must be called before this |
|
163 |
* function can be used. |
|
164 |
* |
|
165 |
* @see bom_off, analog_init |
|
166 |
**/ |
|
167 |
void bom_on(void) |
|
168 |
{ |
|
169 |
output_high(MONKL); |
|
170 |
} |
|
171 |
|
|
172 |
/** |
|
173 |
* Stops flashing the BOM. analog_init must be called |
|
174 |
* before this function can be used. |
|
175 |
* |
|
176 |
* @see bom_on, analog_init |
|
177 |
**/ |
|
178 |
void bom_off(void) |
|
179 |
{ |
|
180 |
output_low(MONKL); |
|
181 |
} |
|
182 |
|
|
183 |
/** @} **/ //end group |
|
184 |
|
|
185 |
void output_high(int which) { |
|
186 |
digital_output(which, 1); |
|
187 |
} |
|
188 |
|
|
189 |
void output_low(int which) { |
|
190 |
digital_output(which, 0); |
|
191 |
} |
|
192 |
|
|
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 |
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 |
int get_max_bom(void) { |
|
112 |
int max_bom_temp = 0; |
|
113 |
int a, i, j, h; |
|
114 |
h = 255; |
|
115 |
|
|
116 |
for (j = 0; j < 16; j++) |
|
117 |
{ |
|
118 |
i = lookup[j]; |
|
119 |
|
|
120 |
if (i&8) |
|
121 |
output_high(MONK3); |
|
122 |
else |
|
123 |
output_low(MONK3); |
|
124 |
|
|
125 |
if (i&4) |
|
126 |
output_high(MONK2); |
|
127 |
else |
|
128 |
output_low(MONK2); |
|
129 |
|
|
130 |
if (i&2) |
|
131 |
output_high(MONK1); |
|
132 |
else |
|
133 |
output_low(MONK1); |
|
134 |
|
|
135 |
if (i&1) |
|
136 |
output_high(MONK0); |
|
137 |
else |
|
138 |
output_low(MONK0); |
|
139 |
|
|
140 |
a = analog8(MONKI); |
|
141 |
|
|
142 |
if (a < h) |
|
143 |
{ |
|
144 |
h = a; |
|
145 |
max_bom_temp = j; |
|
146 |
} |
|
147 |
|
|
148 |
} |
|
149 |
|
|
150 |
//threshold on the bom analog value. |
|
151 |
//defined in bom.h |
|
152 |
// if the analog value read is above the threshold, we cannot see a robot |
|
153 |
// (remember, low means higher intensity). |
|
154 |
if(h < BOM_VALUE_THRESHOLD) { |
|
155 |
return max_bom_temp; |
|
156 |
} |
|
157 |
else |
|
158 |
return -1; |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
162 |
* Flashes the BOM. analog_init must be called before this |
|
163 |
* function can be used. |
|
164 |
* |
|
165 |
* @see bom_off, analog_init |
|
166 |
**/ |
|
167 |
void bom_on(void) |
|
168 |
{ |
|
169 |
output_high(MONKL); |
|
170 |
} |
|
171 |
|
|
172 |
/** |
|
173 |
* Stops flashing the BOM. analog_init must be called |
|
174 |
* before this function can be used. |
|
175 |
* |
|
176 |
* @see bom_on, analog_init |
|
177 |
**/ |
|
178 |
void bom_off(void) |
|
179 |
{ |
|
180 |
output_low(MONKL); |
|
181 |
} |
|
182 |
|
|
183 |
/** @} **/ //end group |
|
184 |
|
|
185 |
void output_high(int which) { |
|
186 |
digital_output(which, 1); |
|
187 |
} |
|
188 |
|
|
189 |
void output_low(int which) { |
|
190 |
digital_output(which, 0); |
|
191 |
} |
|
192 |
|
Also available in: Unified diff