Project

General

Profile

Revision 448

Added by Chris Mar about 12 years ago

moved new BOM code into trunk (wrapper functions, etc...). still need to research deprecated function warnings. FYI, get_max_bom(), bom_on(), and bom_off() are deprecated. See bom.h for new functions/usage.

View differences:

trunk/code/projects/libdragonfly/bom.c
38 38
#include "dio.h"
39 39
#include "analog.h"
40 40

  
41
//TODO: DELETE THIS
42
void printi(int i)
43
{
44
    char c3 = (i % 10) + '0';
45
    i /= 10;
46
    char c2 = (i % 10) + '0';
47
    i /= 10;
48
    char c1 = (i % 10) + '0';
49
    if (c1 == '0')
50
    {
51
        c1 = ' ';
52
        if (c2 == '0')
53
            c2 = ' ';
54
    }
55
    usb_putc(c1);
56
    usb_putc(c2);
57
    usb_putc(c3);
58
}
59

  
41 60
//constants
42
const int lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
61
static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
43 62

  
44 63
// internal function prototypes
45
void output_high(int which);
46
void output_low(int which);
64
static void bom_select(char which);
47 65

  
48 66
/*
49 67
 Bk R Y (Analog)
......
77 95
#define MONK3 _PIN_E6     //blue
78 96
#define MONK2 _PIN_E7     //white
79 97

  
80
#define BOM_VALUE_THRESHOLD 200
98
#define BOM_VALUE_THRESHOLD 200
99
#define NUM_BOM_LEDS 16
81 100

  
82 101

  
83 102
/**
......
93 112
 * Include bom.h to access these functions.
94 113
 *
95 114
 * @{
96
 **/
115
 **/
116

  
117
static unsigned int bom_val[NUM_BOM_LEDS];
118
static char bom_type = BOM;
119
  
120
/**
121
 * Initializes the BOM.
122
 * Call bom_init before reading bom values or turning bom leds.
123
 *
124
 * @bugs INCOMPLETE - need to fill in init routine for BOM15
125
 * 
126
 * @see bom_refresh, bom_leds_on, bom_leds_off
127
 **/
128
void bom_init(char type) {
129
    bom_type = type;
130
    
131
    switch(bom_type) {
132
    case BOM:
133
        break;
134
    case BOM15:
135
        break;
136
    case RBOM:
137
        break;
138
    //default:
139
    }
140
}
141

  
142
/**
143
 * Iterates through each bit in the bit_field. For each set bit, sets the corresponding bom select bits
144
 *    and updates the corresponding bom value with an analog_get8 reading.  analog_init and bom_init
145
 *    must be called for this to work.
146
 *
147
 *
148
 * @param bit_field specifies which elements in bom_val[] should be updated. Use BOM_ALL to refresh all values.
149
 *    Ex. if 0x0003 is passed, bom_val[0] and bom_val[1] will be updated.
150
 *
151
 * @see bom_get
152
 **/
153
void bom_refresh(int bit_field) {
154
    int i;
155
    
156
    analog_stop_loop();
157
    
158
    for(i = 0; i < NUM_BOM_LEDS; i++) {
159
        if(bit_field & 0x1) {
160
            bom_select(lookup[i]);
161
            bom_val[i] = analog_get8(MONKI);
162
        }
163
        bit_field = bit_field >> 1;
164
    }
165
    
166
    analog_start_loop();
167
}
168

  
169
/**
170
 * Gets the bom reading from bom_val[which].  Call bom_refresh beforehand to read new bom values.
171
 *
172
 * @param which which bom value to return
173
 *
174
 * @return the bom value
175
 *
176
 * see bom_refresh
177
 **/
178
int bom_get(int which) {
179
    return bom_val[which];
180
}
181

  
182
/** 
183
 * Compares all the values in bom_val[] and returns the index to the lowest (max) value element.
184
 *
185
 * @return index to the lowest (max) bom value element.  -1 if no value is lower than
186
 *    BOM_VALUE_THRESHOLD
187
 **/
188
int bom_get_max(void) {
189
    int i, lowest_val, lowest_i;
190
    lowest_i = -1;
191
    lowest_val = 255;
192
    for(i = 0; i < NUM_BOM_LEDS; i++) {
193
        printi(bom_val[i]);
194
        usb_puts(" ");
195
        if(bom_val[i] < lowest_val) {
196
            lowest_val = bom_val[i];
197
            lowest_i = i;
198
        }
199
    }
200
    usb_puts("<< BOM_GET_MAX\n\r");
201
    
202
    if(lowest_val < BOM_VALUE_THRESHOLD)
203
        return lowest_i;
204
    else
205
        return -1;
206
}
207

  
208
/**
209
 * Iterates through each bit in the bit_field. For each set bit, turns on the corresponding bom led.
210
 *    bom_init must be called for this to work. Only works with BOM_ALL if using the original bom.
211
 *
212
 * @param bit_field specifies which leds should be turned on.  Use BOM_ALL to turn on all bom leds.
213
 *    Ex. if 0x0005 is passed, leds 0 and 2 will be turned on.
214
 **/
215
void bom_leds_on(int bit_field) {
216
    switch(bom_type) {
217
    case BOM:
218
        if(bit_field == BOM_ALL) {
219
            digital_output(MONKL, 1);
220
        }
221
        break;
222
    case BOM15:
223
        //add bom 1.5 code here
224
        break;
225
    case RBOM:
226
        //add rbom code here
227
        break;
228
    }
229
}
230

  
231
/**
232
 * Iterates through each bit in the bit_field. For each set bit, turns off the corresponding bom led.
233
 *    bom_init must be called for this to work. Only works with BOM_ALL if using the original bom.
234
 *
235
 * @param bit_field specifies which leds should be turned off.  Use BOM_ALL to turn off all bom leds.
236
 *    Ex. if 0x000B is passed, leds 0 and 3 will be turned off.
237
 **/
238
void bom_leds_off(int bit_field) {
239
    switch(bom_type) {
240
    case BOM:
241
        if(bit_field == BOM_ALL) {
242
            digital_output(MONKL, 0);
243
        }
244
        break;
245
    case BOM15:
246
        //add bom 1.5 code here
247
        break;
248
    case RBOM:
249
        //add rbom code here
250
        break;
251
    }
252
}
97 253

  
254

  
98 255
/**
99
 * Returns the direction of the maximum BOM reading,
256
 * (DEPRECATED) Returns the direction of the maximum BOM reading,
100 257
 * as an integer in the range 0-15. 0 indicates to the
101 258
 * robot's right, while the rest of the sensors are
102 259
 * numbered counterclockwise. This is useful for determining
......
109 266
 * @see analog_init
110 267
 **/
111 268
int get_max_bom(void) {
112
	int max_bom_temp = 0;
113
	int a, i, j, h;
114
    h = 255;
115

  
116
	//Turn off the loop so that we can actually use analog8 correctly
117
	analog_stop_loop();
118

  
119
	//Iterate through through each LED
120
    for (j = 0; j < 16; j++)
121
    {
122
      i = lookup[j];
123

  
124
      if (i&8)
125
        output_high(MONK3);
126
      else
127
        output_low(MONK3);
128

  
129
      if (i&4)
130
        output_high(MONK2);
131
      else
132
        output_low(MONK2);
133

  
134
      if (i&2)
135
        output_high(MONK1);
136
      else
137
        output_low(MONK1);
138

  
139
      if (i&1)
140
        output_high(MONK0);
141
      else
142
        output_low(MONK0);
143

  
144
      a = analog_get8(MONKI);
145
              
146
      if (a < h)
147
      {
148
        h = a;
149
        max_bom_temp = j;
150
      }
151

  
152
    }
153
	
154
	//Restart loop now that we are done using analog8
155
	analog_start_loop();
156

  
157
	//threshold on the bom analog value.
158
	//defined in bom.h
159
	// if the analog value read is above the threshold, we cannot see a robot
160
	// (remember, low means higher intensity).
161
	if(h < BOM_VALUE_THRESHOLD) {
162
		return max_bom_temp;
163
	}
164
	else
165
		return -1;
269
    bom_refresh(BOM_ALL);
270
    return bom_get_max();
166 271
}
167 272

  
168 273
/**
169
 * Flashes the BOM. analog_init must be called before this
170
 * function can be used.
274
 * (DEPRECATED) Turns on all bom leds.
171 275
 * 
172
 * @see bom_off, analog_init
276
 * @see bom_off
173 277
 **/
174 278
void bom_on(void)
175 279
{
176
  output_high(MONKL);
280
  bom_leds_on(BOM_ALL);
177 281
}
178 282

  
179 283
/**
180
 * Stops flashing the BOM. analog_init must be called
181
 * before this function can be used.
284
 * (DEPRECATED) Turns off all bom leds.
182 285
 * 
183
 * @see bom_on, analog_init
286
 * @see bom_on
184 287
 **/
185 288
void bom_off(void)
186 289
{
187
  output_low(MONKL);
290
    bom_leds_off(BOM_ALL);
188 291
}
189 292

  
190 293
/** @} **/ //end group
294
+static void bom_select(char which) {
295
      if (which&8)
296
        digital_output(MONK3, 1);
297
      else
298
        digital_output(MONK3, 0);
191 299

  
192
void output_high(int which) {
193
	digital_output(which, 1);
194
}
300
      if (which&4)
301
        digital_output(MONK2, 1);
302
      else
303
        digital_output(MONK2, 0);
195 304

  
196
void output_low(int which) {
197
	digital_output(which, 0);
198
}
305
      if (which&2)
306
        digital_output(MONK1, 1);
307
      else
308
        digital_output(MONK1, 0);
199 309

  
310
      if (which&1)
311
        digital_output(MONK0, 1);
312
      else
313
        digital_output(MONK0, 0);
314
}
trunk/code/projects/libdragonfly/bom.h
42 42
 * @addtogroup bom
43 43
 * @{
44 44
 **/
45
 
46
/** @brief Include all elements in the 16-bit bitfield **/
47
#define BOM_ALL 0xFFFF
48
/** @brief Original BOM - No Range, No Individual LED control **/
49
#define BOM     0
50
/** @brief BOM 1.5 - No Range, Individual LED control **/
51
#define BOM15   1
52
/** @brief RBOM - Range, Individual LED control **/
53
#define RBOM    2
54
 
55
/** @brief Initialize the bom according to bom type **/
56
void bom_init(char type);
57
/** @brief Refresh bom_val[] with new values from analog8.  analog_init and bom_init must be called for this to work. **/
58
void bom_refresh(int bit_field);
59
/** @brief Gets the bom reading from bom_val[which].  Call bom_refresh beforehand to read new bom values. **/
60
int bom_get(int which);
61
/** @brief Compares all the values in bom_val[] and returns the index to the highest value element. **/
62
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);
45 67

  
46
/** @brief Returns the location of the maximum BOM reading. **/
47
int get_max_bom( void );
48
/** @brief Turns the BOM on. **/
68
/** @brief (DEPRECATED) Gets and compares all bom values.  Returns the index to the highest value element. **/
69
int get_max_bom(void);
70
/** @brief (DEPRECATED) Turns on all bom leds. **/
49 71
void bom_on(void);
50
/** @brief Turns the BOM off. **/
72
/** @brief (DEPRECATED) Turns off all bom leds. **/
51 73
void bom_off(void);
52 74

  
53 75
/** @} **/

Also available in: Unified diff