Revision 945
Integrated new BOM1.5 compatible code. Also put eeprom code for reading ID and BOM type from EEPROM. will need to program each robot's eeprom separately later. This has been tested and verified to work on robots with different BOM types.
trunk/code/projects/libdragonfly/dragonfly_lib.c | ||
---|---|---|
35 | 35 |
|
36 | 36 |
#include <dragonfly_lib.h> |
37 | 37 |
|
38 |
/** |
|
39 |
* init_dragonfly - Initializes functions based on configuration parameters |
|
40 |
* examples: |
|
41 |
* |
|
42 |
* init_dragonfly (ALL_ON); - Turns on all basic subsystems. More complex ones that require |
|
43 |
* their own configuration parameters must still be called on their own. |
|
44 |
* |
|
45 |
* init_dragonfly (ANALOG | SERIAL | BUZZER); |
|
46 |
* Initialize ADC, USB, XBee, and Buzzer |
|
47 |
* |
|
48 |
* init_dragonfly (MOTORS | ORB); |
|
49 |
* Initialize the motors and ORB. |
|
50 |
**/ |
|
38 |
/* init_dragonfly - Initializes functions based on configuration parameters |
|
39 |
examples: |
|
51 | 40 |
|
41 |
init_dragonfly (0, 0, 0); - just initialize the digital IO (buttons, potentiometer) |
|
42 |
|
|
43 |
init_dragonfly (ANALOG | SERIAL | BUZZER, C0_C1_ANALOG, BAUD115200); |
|
44 |
Initialize ADC and set C0 and C1 as analog inputs. Initialize serial and set baud rate |
|
45 |
to 115200 bps. Initialize the buzzer. |
|
46 |
|
|
47 |
init_dragonfly (MOTORS | ORB, 0, 0); |
|
48 |
Initialize motor driving and the color fading abilities of the ORB. */ |
|
49 |
|
|
52 | 50 |
/** |
53 | 51 |
* @defgroup dragonfly Dragonfly |
54 | 52 |
* @brief General Dragonfly Functions |
... | ... | |
59 | 57 |
**/ |
60 | 58 |
|
61 | 59 |
/** |
62 |
* Initializes the components specified by config. Will turn on interrupts |
|
63 |
* automatically. |
|
64 |
* |
|
65 |
* @param config The subsystems that you wish to turn on. Check dragonfly_lib.h for |
|
66 |
* valid values. |
|
60 |
* Initializes the components specified by config. |
|
67 | 61 |
* |
68 | 62 |
* @see analog_init, usb_init, xbee_init, buzzer_init, |
69 | 63 |
* bom_init, orb_init, motors_init, lcd_init |
70 | 64 |
**/ |
71 |
void dragonfly_init(int config) { |
|
72 |
sei();
|
|
65 |
|
|
66 |
void flash_red(void);
|
|
73 | 67 |
|
74 |
//Enables the hardware buttons on the board as input pins |
|
75 |
// with internal pullups enabled. |
|
76 |
//Pin G0 is button 1 |
|
77 |
//Pin G1 is button 2 |
|
78 |
DDRG &= ~(_BV(PING0)|_BV(PING1)); |
|
79 |
PORTG |= _BV(PING0)|_BV(PING1); |
|
80 |
|
|
81 |
if(config & ANALOG) { |
|
82 |
analog_init(ADC_START); |
|
83 |
range_init(); |
|
84 |
} |
|
85 |
|
|
86 |
if(config & COMM) { |
|
87 |
//Both default to 115200. Check serial.h for more information. |
|
88 |
usb_init(); |
|
89 |
xbee_init(); |
|
90 |
} |
|
91 |
|
|
92 |
if(config & BUZZER) { |
|
93 |
buzzer_init(); |
|
94 |
} |
|
95 |
|
|
96 |
if(config & ORB) { |
|
97 |
orb_init(); |
|
98 |
} |
|
99 |
|
|
100 |
if(config & MOTORS) |
|
101 |
motors_init(); |
|
68 |
void dragonfly_init(int config) |
|
69 |
{ |
|
70 |
sei(); |
|
71 |
// Set directionality of various IO pins |
|
72 |
DDRG &= ~(_BV(PING0)|_BV(PING1)); |
|
73 |
PORTG |= _BV(PING0)|_BV(PING1); |
|
74 |
|
|
75 |
if(config & ANALOG) |
|
76 |
analog_init(ADC_START); |
|
77 |
|
|
78 |
if(config & COMM) |
|
79 |
{ |
|
80 |
//Defaults to 115200. Check serial.h for more information. |
|
81 |
usb_init(); |
|
82 |
xbee_init(); |
|
83 |
} |
|
84 |
|
|
85 |
if(config & BUZZER) |
|
86 |
{ |
|
87 |
sei(); |
|
88 |
buzzer_init(); |
|
89 |
} |
|
90 |
|
|
91 |
if(config & ORB) |
|
92 |
{ |
|
93 |
sei(); |
|
94 |
orb_init(); |
|
95 |
} |
|
96 |
|
|
97 |
if(config & MOTORS) |
|
98 |
motors_init(); |
|
102 | 99 |
|
103 |
if(config & LCD) |
|
104 |
lcd_init(); |
|
105 |
|
|
106 |
// delay a bit for stability |
|
107 |
_delay_ms(1); |
|
100 |
if(config & LCD) |
|
101 |
lcd_init(); |
|
102 |
|
|
103 |
if(config & RANGE) |
|
104 |
range_init(); |
|
105 |
|
|
106 |
if(config & BOM) |
|
107 |
{ |
|
108 |
unsigned char bom_read = get_bom_type(); |
|
109 |
if(bom_read == 0xFF) |
|
110 |
//warn that bom initialization failed |
|
111 |
flash_red(); |
|
112 |
else |
|
113 |
bom_init(bom_read); |
|
114 |
} |
|
115 |
// delay a bit for stability |
|
116 |
_delay_ms(1); |
|
108 | 117 |
} |
109 | 118 |
|
110 |
|
|
119 |
//flash lights red three times and restore ports |
|
120 |
void flash_red(void) |
|
121 |
{ |
|
122 |
cli(); |
|
123 |
char dd = DDRC; |
|
124 |
char po = PORTC; |
|
125 |
char i; |
|
126 |
DDRC = 0x77; |
|
127 |
for(i = 0; i<3; i++) |
|
128 |
{ |
|
129 |
PORTC = 0x77; |
|
130 |
delay_ms(300); |
|
131 |
PORTC = 0x66; |
|
132 |
delay_ms(300); |
|
133 |
} |
|
134 |
|
|
135 |
DDRC = dd; |
|
136 |
PORTC = po; |
|
137 |
sei(); |
|
138 |
} |
|
111 | 139 |
/** @} **/ //end defgroup |
112 | 140 |
|
trunk/code/projects/libdragonfly/dragonfly_lib.h | ||
---|---|---|
54 | 54 |
#define ORB 0x04 |
55 | 55 |
/** @brief Initialize the motors **/ |
56 | 56 |
#define MOTORS 0x08 |
57 |
/** @brief Initialize I2C **/ |
|
58 |
#define I2C 0x20 |
|
57 | 59 |
/** @brief Initialize the buzzer **/ |
58 |
#define BUZZER 0x10
|
|
60 |
#define BUZZER 0x40
|
|
59 | 61 |
/** @brief Initialize the LCD screen **/ |
60 |
#define LCD 0x20 |
|
61 |
/** @brief Initialize everything **/ |
|
62 |
#define ALL_ON 0xFF |
|
62 |
#define LCD 0x80 |
|
63 |
/** @brief Initialize the rangefinders **/ |
|
64 |
#define RANGE 0x0100 |
|
65 |
/** @brief Initialize the BOM **/ |
|
66 |
#define BOM 0x0200 |
|
67 |
/** @brief Initialize everything **/ |
|
68 |
#define ALL_ON 0x03FF |
|
63 | 69 |
|
64 |
|
|
65 | 70 |
/** @brief Initialize the board **/ |
66 | 71 |
void dragonfly_init(int config); |
67 | 72 |
|
... | ... | |
87 | 92 |
#include <bom.h> |
88 | 93 |
#include <move.h> |
89 | 94 |
#include <reset.h> |
95 |
#include <math.h> |
|
96 |
#include <eeprom.h> |
|
90 | 97 |
|
91 | 98 |
#endif |
92 | 99 |
|
trunk/code/projects/libdragonfly/eeprom.c | ||
---|---|---|
30 | 30 |
|
31 | 31 |
|
32 | 32 |
unsigned char get_robotid(void) { |
33 |
unsigned char ret;
|
|
33 |
unsigned char c0, c1, c2;
|
|
34 | 34 |
|
35 |
eeprom_get_byte(EEPROM_ROBOT_ID_ADDR, &ret); |
|
35 |
eeprom_get_byte(EEPROM_ROBOT_ID_ADDR, &c0); |
|
36 |
eeprom_get_byte(EEPROM_ROBOT_ID_ADDR+1, &c1); |
|
37 |
eeprom_get_byte(EEPROM_ROBOT_ID_ADDR+2, &c2); |
|
38 |
if(c0 == 'I' && c1 == 'D') |
|
39 |
return c2; |
|
40 |
else |
|
41 |
return 0xFF; |
|
42 |
} |
|
43 |
|
|
44 |
unsigned char get_bom_type(void) { |
|
45 |
unsigned char c0, c1, c2, c3; |
|
36 | 46 |
|
37 |
return ret; |
|
38 |
} |
|
47 |
eeprom_get_byte(EEPROM_BOM_TYPE_ADDR, &c0); |
|
48 |
eeprom_get_byte(EEPROM_BOM_TYPE_ADDR+1, &c1); |
|
49 |
eeprom_get_byte(EEPROM_BOM_TYPE_ADDR+2, &c2); |
|
50 |
eeprom_get_byte(EEPROM_BOM_TYPE_ADDR+3, &c3); |
|
51 |
if(c0 == 'B' && c1 == 'O' && c2 == 'M') |
|
52 |
return c3; |
|
53 |
else |
|
54 |
return 0xFF; |
|
55 |
} |
trunk/code/projects/libdragonfly/eeprom.h | ||
---|---|---|
10 | 10 |
#ifndef _EEPROM_H_ |
11 | 11 |
#define _EEPROM_H_ |
12 | 12 |
|
13 |
#define EEPROM_ROBOT_ID_ADDR 0x0b
|
|
13 |
#include <bom.h>
|
|
14 | 14 |
|
15 |
#define EEPROM_ROBOT_ID_ADDR 0x10 |
|
16 |
#define EEPROM_BOM_TYPE_ADDR 0x14 |
|
17 |
|
|
15 | 18 |
/** @brief store a byte to eeproem |
16 | 19 |
* @return 0 if success, nonzero on failure |
17 | 20 |
*/ |
... | ... | |
29 | 32 |
|
30 | 33 |
/** @brief get stored robot ID |
31 | 34 |
* |
32 |
* returnes the value of eerpom at EEPROM_ROBOT_ID_ADDR
|
|
35 |
* checks that EEPROM has been programed with an ID and returns it
|
|
33 | 36 |
* |
34 |
* @return the robot id, if it is stored. If it returns 0xff it is probably invalid
|
|
37 |
* @return the robot id, if it is stored. If it returns 0xFF it is probably invalid
|
|
35 | 38 |
*/ |
36 |
unsigned char get_robotid(void); |
|
39 |
unsigned char get_robotid(void); |
|
40 |
|
|
41 |
/** @brief get stored robot ID |
|
42 |
* |
|
43 |
* checks that EEPROM has been programed with an BOM type and returns it |
|
44 |
* |
|
45 |
* @return the robot bom type as defined in bom.h, if it is stored. If it returns 0xFF it is probably invalid |
|
46 |
*/ |
|
47 |
unsigned char get_bom_type(void); |
|
37 | 48 |
|
38 | 49 |
#endif |
50 |
|
|
39 | 51 |
|
trunk/code/projects/libdragonfly/bom.c | ||
---|---|---|
36 | 36 |
#include <dragonfly_lib.h> |
37 | 37 |
#include "bom.h" |
38 | 38 |
#include "dio.h" |
39 |
#include "serial.h" |
|
39 | 40 |
#include "analog.h" |
40 | 41 |
|
41 |
//constants |
|
42 |
//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: |
|
42 | 45 |
static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa}; |
43 | 46 |
|
44 | 47 |
// internal function prototypes |
... | ... | |
76 | 79 |
#define MONK3 _PIN_E6 //blue |
77 | 80 |
#define MONK2 _PIN_E7 //white |
78 | 81 |
|
79 |
#define BOM_VALUE_THRESHOLD 200 |
|
82 |
#define BOM_VALUE_THRESHOLD 200
|
|
80 | 83 |
#define NUM_BOM_LEDS 16 |
81 | 84 |
|
85 |
/* |
|
86 |
*The following pin definitions are for the BOM v1.5 |
|
87 |
*/ |
|
82 | 88 |
|
89 |
#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 |
#define BOM_S2 _PIN_E6 //dio4 |
|
98 |
#define BOM_S3 _PIN_E7 //dio5 |
|
99 |
#define BOM_OUT PF0 //analog(yellow) |
|
100 |
|
|
83 | 101 |
/** |
84 | 102 |
* @defgroup bom BOM (Bearing and Orientation Module) |
85 | 103 |
* @brief Functions for dealing with the BOM. |
... | ... | |
93 | 111 |
* Include bom.h to access these functions. |
94 | 112 |
* |
95 | 113 |
* @{ |
96 |
**/ |
|
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 |
* @see bom_refresh |
|
158 |
**/ |
|
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 |
* 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 |
int bom_get_max(void) { |
|
172 |
int i, lowest_val, lowest_i; |
|
173 |
lowest_i = -1; |
|
114 |
**/ |
|
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 |
* must be called for this to work. |
|
161 |
* |
|
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 |
|
|
171 |
analog_stop_loop(); |
|
172 |
|
|
173 |
for(i = 0; i < NUM_BOM_LEDS; i++) { |
|
174 |
if(bit_field & 0x1) { |
|
175 |
bom_select(i); |
|
176 |
bom_val[i] = analog_get8(analog_pin); |
|
177 |
} |
|
178 |
bit_field = bit_field >> 1; |
|
179 |
} |
|
180 |
|
|
181 |
analog_start_loop(); |
|
182 |
} |
|
183 |
|
|
184 |
/** |
|
185 |
* Gets the bom reading from bom_val[which]. Call bom_refresh beforehand to read new bom values. |
|
186 |
* |
|
187 |
* @param which which bom value to return |
|
188 |
* |
|
189 |
* @return the bom value |
|
190 |
* |
|
191 |
* see bom_refresh |
|
192 |
**/ |
|
193 |
int bom_get(int which) { |
|
194 |
return bom_val[which]; |
|
195 |
} |
|
196 |
|
|
197 |
/** |
|
198 |
* Compares all the values in bom_val[] and returns the index to the lowest (max) value element. |
|
199 |
* |
|
200 |
* @return index to the lowest (max) bom value element. -1 if no value is lower than |
|
201 |
* BOM_VALUE_THRESHOLD |
|
202 |
**/ |
|
203 |
int bom_get_max(void) { |
|
204 |
int i, lowest_val, lowest_i; |
|
205 |
lowest_i = -1; |
|
174 | 206 |
lowest_val = 255; |
175 | 207 |
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 |
} |
|
181 |
|
|
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 |
if(bom_val[i] < lowest_val) { |
|
209 |
lowest_val = bom_val[i]; |
|
210 |
lowest_i = i; |
|
211 |
} |
|
208 | 212 |
} |
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; |
|
213 |
|
|
214 |
if(lowest_val < BOM_VALUE_THRESHOLD) |
|
215 |
return lowest_i; |
|
216 |
else |
|
217 |
return -1; |
|
218 |
} |
|
219 |
|
|
220 |
/** |
|
221 |
* Iterates through each bit in the bit_field. If the bit is set, the corresponding emitter will |
|
222 |
* be enabled to turn on when bom_on() is called. |
|
223 |
* bom_init must be called for this to work. |
|
224 |
* |
|
225 |
* @param bit_field specifies which leds should be turned on. Use BOM_ALL to turn on all bom leds. |
|
226 |
* Ex. if 0x0005 is passed, leds 0 and 2 will be turned on. |
|
227 |
**/ |
|
228 |
void bom_set_leds(int bit_field) { |
|
229 |
int i; |
|
230 |
unsigned int mask = 1<<(NUM_BOM_LEDS-1); |
|
231 |
switch(bom_type) { |
|
232 |
case BOM: |
|
233 |
if(bit_field == BOM_ALL) |
|
234 |
digital_output(MONKL, 1); |
|
235 |
break; |
|
236 |
|
|
237 |
case BOM15: |
|
238 |
for(i=NUM_BOM_LEDS; i>0; i--) |
|
239 |
{ |
|
240 |
//set the current bit, sending MSB first |
|
241 |
digital_output(BOM_DATA, bit_field&mask); |
|
242 |
//then pulse the clock |
|
243 |
digital_output(BOM_CLOCK, 1); |
|
244 |
digital_output(BOM_CLOCK, 0); |
|
245 |
mask = mask>>1; |
|
246 |
} |
|
247 |
break; |
|
248 |
|
|
249 |
case RBOM: |
|
250 |
//add rbom code here |
|
251 |
break; |
|
231 | 252 |
} |
232 |
} |
|
253 |
}
|
|
233 | 254 |
|
234 | 255 |
|
235 | 256 |
/** |
236 |
* (DEPRECATED) Wrapper function for new BOM code. Refreshes buffer and returns the max bom value. |
|
257 |
* (DEPRECATED) Returns the direction of the maximum BOM reading, |
|
258 |
* as an integer in the range 0-15. 0 indicates to the |
|
259 |
* robot's right, while the rest of the sensors are |
|
260 |
* numbered counterclockwise. This is useful for determining |
|
261 |
* the direction of a robot flashing its BOM, of only one |
|
262 |
* robot is currently doing so. analog_init must be called |
|
263 |
* before this function can be used. |
|
237 | 264 |
* |
238 | 265 |
* @return the direction of the maximum BOM reading |
239 | 266 |
* |
240 |
* @see bom_refresh, bom_get_max
|
|
267 |
* @see analog_init
|
|
241 | 268 |
**/ |
242 | 269 |
int get_max_bom(void) { |
243 | 270 |
bom_refresh(BOM_ALL); |
... | ... | |
245 | 272 |
} |
246 | 273 |
|
247 | 274 |
/** |
248 |
* (DEPRECATED) Turns on all bom leds. |
|
275 |
* Flashes the BOM. If using a BOM1.5, only the emitters that have been enabled using |
|
276 |
* bom_set_leds will turn on. |
|
249 | 277 |
* |
250 |
* @see bom_off |
|
278 |
* @see bom_off, bom_set_leds
|
|
251 | 279 |
**/ |
252 | 280 |
void bom_on(void) |
253 | 281 |
{ |
254 |
bom_leds_on(BOM_ALL); |
|
282 |
switch(bom_type) { |
|
283 |
case BOM10: |
|
284 |
digital_output(MONKL, 1); |
|
285 |
break; |
|
286 |
case BOM15: |
|
287 |
digital_output(BOM_STROBE, 1); |
|
288 |
break; |
|
289 |
case RBOM: |
|
290 |
break; |
|
291 |
} |
|
255 | 292 |
} |
256 | 293 |
|
257 | 294 |
/** |
258 |
* (DEPRECATED) Turns off all bom leds.
|
|
295 |
* Turns off all bom leds. |
|
259 | 296 |
* |
260 | 297 |
* @see bom_on |
261 | 298 |
**/ |
262 | 299 |
void bom_off(void) |
263 | 300 |
{ |
264 |
bom_leds_off(BOM_ALL); |
|
301 |
switch(bom_type) { |
|
302 |
case BOM: |
|
303 |
digital_output(MONKL, 0); |
|
304 |
break; |
|
305 |
case BOM15: |
|
306 |
digital_output(BOM_STROBE, 0); |
|
307 |
break; |
|
308 |
case RBOM: |
|
309 |
break; |
|
310 |
} |
|
265 | 311 |
} |
266 | 312 |
|
267 | 313 |
/** @} **/ //end group |
268 |
-static void bom_select(char which) { |
|
269 |
if (which&8) |
|
270 |
digital_output(MONK3, 1); |
|
271 |
else |
|
272 |
digital_output(MONK3, 0); |
|
273 | 314 |
|
274 |
if (which&4) |
|
275 |
digital_output(MONK2, 1); |
|
276 |
else |
|
277 |
digital_output(MONK2, 0); |
|
315 |
//select a detector to read |
|
316 |
static void bom_select(char which) { |
|
317 |
if(bom_type == BOM) |
|
318 |
which = lookup[(int)which]; |
|
319 |
|
|
320 |
if (which&8) |
|
321 |
digital_output(select_pins[3], 1); |
|
322 |
else |
|
323 |
digital_output(select_pins[3], 0); |
|
278 | 324 |
|
279 |
if (which&2)
|
|
280 |
digital_output(MONK1, 1);
|
|
281 |
else
|
|
282 |
digital_output(MONK1, 0);
|
|
325 |
if (which&4)
|
|
326 |
digital_output(select_pins[2], 1);
|
|
327 |
else |
|
328 |
digital_output(select_pins[2], 0);
|
|
283 | 329 |
|
284 |
if (which&1) |
|
285 |
digital_output(MONK0, 1); |
|
286 |
else |
|
287 |
digital_output(MONK0, 0); |
|
288 |
} |
|
330 |
if (which&2) |
|
331 |
digital_output(select_pins[1], 1); |
|
332 |
else |
|
333 |
digital_output(select_pins[1], 0); |
|
334 |
|
|
335 |
if (which&1) |
|
336 |
digital_output(select_pins[0], 1); |
|
337 |
else |
|
338 |
digital_output(select_pins[0], 0); |
|
339 |
|
|
340 |
} |
trunk/code/projects/libdragonfly/bom.h | ||
---|---|---|
45 | 45 |
|
46 | 46 |
/** @brief Include all elements in the 16-bit bitfield **/ |
47 | 47 |
#define BOM_ALL 0xFFFF |
48 |
|
|
48 | 49 |
/** @brief Original BOM - No Range, No Individual LED control **/ |
49 |
#define BOM 0 |
|
50 |
#define BOM10 0 |
|
51 |
|
|
50 | 52 |
/** @brief BOM 1.5 - No Range, Individual LED control **/ |
51 | 53 |
#define BOM15 1 |
54 |
|
|
52 | 55 |
/** @brief RBOM - Range, Individual LED control **/ |
53 | 56 |
#define RBOM 2 |
54 |
|
|
57 |
|
|
58 |
|
|
55 | 59 |
/** @brief Initialize the bom according to bom type **/ |
56 | 60 |
void bom_init(char type); |
61 |
|
|
57 | 62 |
/** @brief Refresh bom_val[] with new values from analog8. analog_init and bom_init must be called for this to work. **/ |
58 | 63 |
void bom_refresh(int bit_field); |
64 |
|
|
59 | 65 |
/** @brief Gets the bom reading from bom_val[which]. Call bom_refresh beforehand to read new bom values. **/ |
60 | 66 |
int bom_get(int which); |
67 |
|
|
61 | 68 |
/** @brief Compares all the values in bom_val[] and returns the index to the highest value element. **/ |
62 | 69 |
int bom_get_max(void); |
63 |
/** @brief Turns on the selected bom leds. Only works with BOM_ALL if using the original bom. **/ |
|
64 |
void bom_leds_on(int bit_field); |
|
65 |
/** @brief Turns off the selected bom leds. Only works with BOM_ALL if using the original bom. **/ |
|
66 |
void bom_leds_off(int bit_field); |
|
67 | 70 |
|
68 |
/** @brief (DEPRECATED) Wrapper function. See bom_refresh and bom_get_max **/ |
|
71 |
/** @brief Enables the selected bom leds on a BOM1.5 **/ |
|
72 |
void bom_set_leds(int bit_field); |
|
73 |
|
|
74 |
/** @brief (DEPRECATED) Gets and compares all bom values. Returns the index to the highest value element. **/ |
|
69 | 75 |
int get_max_bom(void); |
70 |
/** @brief (DEPRECATED) Wrapper function. See bom_leds_on. **/ |
|
76 |
|
|
77 |
/** @brief Turns on all BOM leds, or turns on enabled leds on a BOM1.5. **/ |
|
71 | 78 |
void bom_on(void); |
72 |
/** @brief (DEPRECATED) Wrapper function. See bom_leds_off. **/ |
|
79 |
|
|
80 |
/** @brief Turns off all bom leds. **/ |
|
73 | 81 |
void bom_off(void); |
74 | 82 |
|
75 | 83 |
/** @} **/ |
Also available in: Unified diff