Revision 1563

View differences:

branches/ir_lookup/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 "bom.h"
37
#include "dio.h"
38
#include "serial.h"
39
#include "analog.h"
40

  
41
//On the original BOM1.0, the emmitter angular order does not match the analog mux order
42
//so you need to iterate through the mux index in the following order if you want to get
43
//the detector readings in order:
44
static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
45

  
46

  
47
/* *****************************
48
 * BOM Vector Component Tables *
49
 **************************** **/
50

  
51
/*
52
 * The x component of each BOM detector (indexed from 0 to 15)
53
 * was calculated using the following formula:
54
 *
55
 *		x_comp[i] = fix(25 * cos ( 2 * pi / 16 * i) )
56
 *
57
 * where "fix" rounds towards 0. If the BOM detectors were superimposed
58
 * onto a 2 dimensional Cartesian space, this effectively calculates the
59
 * x component of the emitter vector where emitter 0 corresponds to an
60
 * angle of 0 radians, 4 -> pi/2, 8 -> pi, ect.
61
 */
62
static const signed int x_comp[16] = {
63
	25,
64
	23,
65
	17,
66
	9,
67
	0,
68
	-9,
69
	-17,
70
	-23,
71
	-25,
72
	-23,
73
	-17,
74
	-9,
75
	0,
76
	9,
77
	17,
78
	23
79
};
80

  
81

  
82
/*
83
 * The y component of each BOM detector (indexed from 0 to 15)
84
 * was calculated using the following formula:
85
 *
86
 *		y_comp[i] = fix(25 * sin ( 2 * pi / 16 * i) )
87
 *
88
 * where "fix" rounds towards 0. If the BOM detectors were superimposed
89
 * onto a 2 dimensional Cartesian space, this effectively calculates the
90
 * x component of the emitter vector where emitter 0 corresponds to an
91
 * angle of 0 radians, 4 -> pi/2, 8 -> pi, ect.
92
 */
93
static signed int y_comp[16] = {
94
	0,
95
	9,
96
	17,
97
	23,
98
	25,
99
	23,
100
	17,
101
	9,
102
	0,
103
	-9,
104
	-17,
105
	-23,
106
	-25,
107
	-23,
108
	-17,
109
	-9
110
};
111

  
112
// internal function prototypes
113
static void bom_select(char which);
114

  
115
/*
116
 Bk R Y (Analog)
117
---------
118
 Green
119
 Blue
120
 White
121
---------
122
 Blue
123
 White
124
*/
125

  
126

  
127
/*
128
the analog pin definitions from dio.h DO NOT work here,
129
so we must use PF0 from avrgcc (as opposed to _PIN_F0).
130
BUT the dio pin definitions from dio.h must be used (no PE...).
131

  
132
also, _PIN_E2 is initialized to high for some reason,
133
which turns the BOM on when the robot is turned on.
134
WORK-AROUND: call digital_output(_PIN_E2,0) at some point.
135

  
136
*/
137

  
138
#define MONKI PF0         //analog (yellow)
139
//------------------------//
140
#define MONKL _PIN_E2     //green
141
#define MONK1 _PIN_E3     //blue
142
#define MONK0 _PIN_E4     //white
143
//------------------------//
144
#define MONK3 _PIN_E6     //blue
145
#define MONK2 _PIN_E7     //white
146

  
147
#define BOM_VALUE_THRESHOLD 150 //200
148
#define NUM_BOM_LEDS 16
149

  
150
/*
151
  *The following pin definitions are for the BOM v1.5
152
  */
153

  
154
#define BOM_MODE	_PIN_E2	//dio0
155
#define BOM_STROBE	_PIN_E3	//dio1
156

  
157
#define BOM_DATA	_PIN_A0 //servo0
158
#define BOM_CLOCK	_PIN_A1	//servo1
159

  
160
#define BOM_S0		_PIN_E5	//dio3
161
#define BOM_S1		_PIN_E4	//dio2
162
#define BOM_S2		_PIN_E7	//dio4
163
#define BOM_S3		_PIN_E6	//dio5
164
#define BOM_OUT		PF0		//analog(yellow)
165

  
166
/**
167
 * @defgroup bom BOM (Bearing and Orientation Module)
168
 * @brief Functions for dealing with the BOM.
169
 *
170
 * The Bearing and Orientation Module / Barrel of Monkeys / BOM
171
 * is a custom sensor designed and built by the Colony Project.
172
 * It consists of a ring of 16 IR emitters and 16 IR detectors.
173
 * The BOM is most often use to determine the direction of other
174
 * robots. This module contains functions for controlling the BOM.
175
 *
176
 * Include bom.h to access these functions.
177
 *
178
 * @{
179
 **/
180

  
181
static unsigned int bom_val[NUM_BOM_LEDS];
182
static volatile char bom_type = BOM10;
183
static int select_pins[4];
184
static int analog_pin;
185

  
186
/**
187
 * Initializes the BOM.
188
 * Call bom_init before reading bom values or turning bom leds.
189
 *
190
 * @bugs INCOMPLETE - No utilization of BOM1.5 RSSI capability. Probably leave this out
191
 * until Cornell and Pras return
192
 * 
193
 * @see bom_refresh, bom_leds_on, bom_leds_off
194
 **/
195
void bom_init(char type) {
196
    bom_type = type;
197
    
198
    switch(bom_type) {
199
    case BOM10:
200
		select_pins[0] = MONK0; 
201
		select_pins[1] = MONK1;
202
		select_pins[2] = MONK2;
203
		select_pins[3] = MONK3;
204
		analog_pin = MONKI;
205
        break;
206
    case BOM15:
207
        //Sets BOM1.5 to normal [BOM] mode
208
        digital_output(BOM_MODE, 0);
209
		select_pins[0] = BOM_S0; 
210
		select_pins[1] = BOM_S1;
211
		select_pins[2] = BOM_S2;
212
		select_pins[3] = BOM_S3;
213
		bom_set_leds(BOM_ALL);
214
		analog_pin = BOM_OUT;
215
        break;
216
    case RBOM:
217
        break;
218
    //default:
219
    }
220
}
221

  
222
/**
223
 * Iterates through each bit in the bit_field. For each set bit, sets the corresponding bom select bits
224
 *    and updates the corresponding bom value with an analog_get8 reading.  analog_init and bom_init
225
 *    must be called for this to work. Must call this before reading BOM values!
226
 *
227
 *
228
 * @param bit_field specifies which elements in bom_val[] should be updated. Use BOM_ALL to refresh all values.
229
 *    Ex. if 0x0003 is passed, bom_val[0] and bom_val[1] will be updated.
230
 *
231
 * @see bom_get
232
 **/
233
void bom_refresh(int bit_field) {
234
    int i;
235
	int loop_was_running = 0;
236
    
237
	//Check analog loop status
238
    if(analog_loop_status() == ADC_LOOP_RUNNING) {
239
		loop_was_running = 1;
240
		analog_stop_loop();
241
	}
242
    
243
	//Read BOM values
244
    for(i = 0; i < NUM_BOM_LEDS; i++) {
245
        if(bit_field & 0x1) {
246
            bom_select(i);
247
            bom_val[i] = analog_get8(analog_pin);
248
        }
249
        bit_field = bit_field >> 1;
250
    }
251
    
252
	//Restore analog loop status
253
	if(loop_was_running)
254
		analog_start_loop();
255
}
256

  
257
/**
258
 * Gets the bom reading from bom_val[which].  Call bom_refresh beforehand to read new bom values.
259
 *
260
 * @pre must call bom refresh first
261
 *
262
 * @param which which bom value to return
263
 *
264
 * @return the bom value
265
 *
266
 * see bom_refresh
267
 **/
268
int bom_get(int which) {
269
    return bom_val[which];
270
}
271

  
272
/** 
273
 * Compares all the values in bom_val[] and returns the index to the lowest (max) value element.
274
 *
275
 * @pre must call bom refresh
276
 * @return index to the lowest (max) bom value element.  -1 if no value is lower than
277
 *    BOM_VALUE_THRESHOLD
278
 **/
279
int bom_get_max(void) {
280
    int i, lowest_val, lowest_i;
281
    lowest_i = -1;
282
    lowest_val = 255;
283
    for(i = 0; i < NUM_BOM_LEDS; i++) {
284
        if(bom_val[i] < lowest_val) {
285
            lowest_val = bom_val[i];
286
            lowest_i = i;
287
        }
288
    }
289
    
290
    if(lowest_val < BOM_VALUE_THRESHOLD)
291
        return lowest_i;
292
    else
293
        return -1;
294
}
295

  
296
/** 
297
 * Compute the net resultant BOM IR vector by scaling each IR unit vector by its intensity
298
 *		and summing over all IR LEDs.
299
 *
300
 * @param v  Pointer to Vector struct to be filled by this function with an x and y net vector
301
 *		component.
302
 *
303
 * @param usrBOMvals  Pointer to array which holds 16 raw BOM readings which can be used if user
304
 *		has already collected BOM information instead of collecting a new data set from the BOM.
305
 *
306
 * @return  Exit status - Zero for success; negative on error.
307
 **/
308
int bom_get_vector(Vector* v, int* usrBOMvals) {
309

  
310
	/* Store current BOM readings and use them as a weighting factor */
311
	int intensity[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
312

  
313
	/* Arrays for storing the weighted x ("Rightness") and y ("Forwardness")
314
	 * components. Calculated by multiplying the intensity by the x and y
315
	 * component respectively (x and y components are stored in the tables
316
	 * above). */
317
	int weighted_x_comp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
318
	int weighted_y_comp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
319
	
320
	/* Accumulators to sum up the net x ("Rightness") and y ("Forwardness")
321
	 * components for the entire robot. */
322
	long net_x_comp = 0;
323
	long net_y_comp = 0;
324

  
325
	int i = 0;
326

  
327
	/* BOM intensity is actually measured as more intense = closer to 0 */
328
	if (usrBOMvals) {
329
		/* Use BOM values collected by user */
330
		for (i = 0; i < 16; i++) {
331
			intensity[i] = 255 - usrBOMvals[i];
332
		}
333
	} else {
334
		/* Collect new set of BOM data */
335
		bom_refresh(BOM_ALL);
336
		for (i = 0; i < 16; i++) {
337
			intensity[i] = 255 - bom_get(i);
338
		}
339
	}
340

  
341
	/* Calculate weighted vector components and accumulate vector sum */
342
	for (i = 0; i < 16; i++) {
343
		weighted_x_comp[i] = intensity[i] * x_comp[i];
344
		weighted_y_comp[i] = intensity[i] * y_comp[i];
345
		net_x_comp += weighted_x_comp[i];
346
		net_y_comp += weighted_y_comp[i];
347
	}
348

  
349
	/* Fill the Vector struct */
350
	v->x = net_x_comp;
351
	v->y = net_y_comp;
352

  
353
	return 0;
354

  
355
}
356

  
357
/** 
358
 * Compute the normalized net resultant BOM IR vector by scaling each IR unit vector by its
359
 *		intensity and summing over all IR LEDs.
360
 *
361
 * @param v  Pointer to Vector struct to be filled by this function with an x and y net vector
362
 *		component.
363
 *
364
 * @param usrBOMvals  Pointer to array which holds 16 raw BOM readings which can be used if user
365
 *		has already collected BOM information instead of collecting a new data set from the BOM.
366
 *
367
 * @return  Exit status - Zero for success; negative on error.
368
 **/
369
int bom_get_norm_vector(Vector* v, int* usrBOMvals) {
370

  
371
	/* Store current BOM readings and use them as a weighting factor */
372
	int intensity[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
373

  
374
	/* Arrays for storing the weighted x ("Rightness") and y ("Forwardness")
375
	 * components. Calculated by multiplying the intensity by the x and y
376
	 * component respectively (x and y components are stored in the tables
377
	 * above). */
378
	int weighted_x_comp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
379
	int weighted_y_comp[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
380
	
381
	/* Accumulators to sum up the net x ("Rightness") and y ("Forwardness")
382
	 * components for the entire robot. */
383
	long net_x_comp = 0;
384
	long net_y_comp = 0;
385

  
386
	/* Variables used to normalize the net component values */
387
	int total_intensity = 0;
388
	int normalized_net_x_comp = 0;
389
	int normalized_net_y_comp = 0;
390

  
391
	int i = 0;
392

  
393
	/* BOM intensity is actually measured as more intense = closer to 0 */
394
	if (usrBOMvals) {
395
		/* Use BOM values collected by user */
396
		for (i = 0; i < 16; i++) {
397
			intensity[i] = 255 - usrBOMvals[i];
398
		}
399
	} else {
400
		/* Collect new set of BOM data */
401
		bom_refresh(BOM_ALL);
402
		for (i = 0; i < 16; i++) {
403
			intensity[i] = 255 - bom_get(i);
404
		}
405
	}
406

  
407
	/* Calculate weighted vector components and accumulate vector sum */
408
	for (i = 0; i < 16; i++) {
409
		weighted_x_comp[i] = intensity[i] * x_comp[i];
410
		weighted_y_comp[i] = intensity[i] * y_comp[i];
411
		net_x_comp += weighted_x_comp[i];
412
		net_y_comp += weighted_y_comp[i];
413
		total_intensity += intensity[i];
414
	}
415

  
416
	/* Normalize the resultant vector components by the total intensity */
417
	if (total_intensity > 0) {
418
		normalized_net_x_comp = net_x_comp / total_intensity;
419
		normalized_net_y_comp = net_y_comp / total_intensity;
420
	}
421

  
422
	/* Fill the Vector struct */
423
	v->x = normalized_net_x_comp;
424
	v->y = normalized_net_y_comp;
425

  
426
	return 0;
427

  
428
}
429

  
430
/** 
431
 * Print a histogram which shows the current BOM intensity values for each of the 16 BOM IR
432
 *		sensors. The function will attempt to send the histogram data over USB.
433
 *
434
 * @param curBOMvals  Pointer to an array of the current BOM values (the array must have
435
 *		length 16). Use this to print values you have already collected. Otherwise pass in NULL
436
 *		and bom_refresh() will be called and the current BOM intensity values will be collected.
437
 * @return  Exit status - Zero for success; negative on error.
438
 **/
439
int bom_print_usb(int* usrBOMvals) {
440

  
441
	int i, j, max = -1;
442
	int curVals[16];
443
	int* prtValPtr;
444

  
445
	if (usrBOMvals) {
446
		/* Use BOM values collected by user */
447
		prtValPtr = usrBOMvals;
448

  
449
		/* Find max BOM value from users values */
450
		for (i = 0; i < 16; i++) {
451
			if (max < prtValPtr[i])
452
				max = prtValPtr[i];
453
		}
454
	} else {
455
		/* Refresh and make sure the table is updated */
456
		bom_refresh(BOM_ALL);
457

  
458
		/* Record values into an array */
459
		for (i = 0; i < 16; i++) {
460
			curVals[i] = bom_get(i);
461
			if (max < curVals[i])
462
				max = curVals[i];
463
		}
464

  
465
		/* Use the current set of collected values */
466
		prtValPtr = curVals;
467
	}
468

  
469
	/* Display results */
470
	for (i = 0; i < 16; i++) {
471
		
472
		usb_puti(i);
473
		usb_puts(": ");
474
		usb_puti(prtValPtr[i]);
475
		usb_putc('\t');
476

  
477
		for (j = 0; j < (int)((max - prtValPtr[i]) / 5); j++) {
478
			usb_putc('#');
479
		}
480
		usb_puts("\r\n");
481
	}
482
	usb_puts("\r\n");
483

  
484
	return 0;
485

  
486
}
487

  
488
/** 
489
 * Computes the weighted average of all the bom readings to estimate the position (and distance) of another robot.
490
 *
491
 * @pre must call bom refresh
492
 * @param dist  pointer to int in which to return the estimated distance to the other robot
493
 * @return estimated position of the max bom value element as a fixed point value analogous to 10 times the
494
 *        index of the max bom value.  -1 if no value is lower than BOM_VALUE_THRESHOLD.
495
 **/
496
int bom_get_max10(int *dist) {
497
    int i, max;
498
    long long mean = 0, sum = 0;
499

  
500
    max = bom_get_max();
501
    if (max < 0)
502
    {
503
        if (dist)
504
        {
505
            *dist = -1;
506
        }
507
        return -1;
508
    }
509
    /* Record values into an array */
510
    for (i = 0; i < NUM_BOM_LEDS; i++) {
511
        int idx = ((i + (NUM_BOM_LEDS/2 - max) + NUM_BOM_LEDS) % NUM_BOM_LEDS) - (NUM_BOM_LEDS/2 - max);
512
        int val = 255 - bom_val[i];
513
        mean += idx * val;
514
        sum += val;
515
    }
516
    mean = (mean * 10) / sum;
517
    mean = (mean + NUM_BOM_LEDS*10) % (NUM_BOM_LEDS*10);
518

  
519
    if (dist)
520
    {
521
        *dist = 50 - sum/48;
522
    }
523

  
524
    return mean;
525
}
526

  
527
/**
528
 * Iterates through each bit in the bit_field. If the bit is set, the corresponding emitter will
529
 *    be enabled to turn on when bom_on() is called.
530
 *    bom_init must be called for this to work. Does nothing if a BOM1.0 is installed
531
 *
532
 * @param bit_field specifies which leds should be turned on when bom_on is called.  Use BOM_ALL to turn on all bom leds.
533
 *    Ex. if 0x0005 is passed, leds 0 and 2 will be turned on.
534
 **/
535
void bom_set_leds(int bit_field) {
536
    int i;
537
	unsigned int mask = 1<<(NUM_BOM_LEDS-1);
538
	switch(bom_type) {
539
    case BOM10:
540
        //TODO: put an assert here to alert the user that this should not be called
541
        break;
542
		
543
    case BOM15:
544
	    for(i=NUM_BOM_LEDS; i>0; i--)
545
	    {
546
		    //set the current bit, sending MSB first
547
		    digital_output(BOM_DATA, bit_field&mask);
548
		    //then pulse the clock
549
		    digital_output(BOM_CLOCK, 1);
550
		    digital_output(BOM_CLOCK, 0);
551
			mask = mask>>1;
552
	    }
553
        break;
554
		
555
    case RBOM:
556
        //add rbom code here
557
        break;
558
    }
559
}
560

  
561

  
562
/**
563
 * (DEPRECATED) Returns the direction of the maximum BOM reading,
564
 * as an integer in the range 0-15. 0 indicates to the
565
 * robot's right, while the rest of the sensors are
566
 * numbered counterclockwise. This is useful for determining
567
 * the direction of a robot flashing its BOM, of only one
568
 * robot is currently doing so. analog_init must be called
569
 * before this function can be used.
570
 *
571
 * @return the direction of the maximum BOM reading
572
 *
573
 * @see analog_init
574
 **/
575
int get_max_bom(void) {
576
    bom_refresh(BOM_ALL);
577
    return bom_get_max();
578
}
579

  
580
/**
581
 * Flashes the BOM.  If using a BOM1.5, only the emitters that have been enabled using
582
 * bom_set_leds will turn on.
583
 * 
584
 * @see bom_off, bom_set_leds
585
 **/
586
void bom_on(void)
587
{
588
  switch(bom_type) {
589
  case BOM10:
590
	digital_output(MONKL, 1);
591
	break;
592
  case BOM15:
593
	digital_output(BOM_STROBE, 1);
594
	break;
595
  case RBOM:
596
	break;
597
  }
598
}
599

  
600
/**
601
 * Turns off all bom leds.
602
 * 
603
 * @see bom_on
604
 **/
605
void bom_off(void)
606
{
607
  switch(bom_type) {
608
  case BOM10:
609
	digital_output(MONKL, 0);
610
	break;
611
  case BOM15:
612
	digital_output(BOM_STROBE, 0);
613
	break;
614
  case RBOM:
615
	break;
616
  }
617
}
618

  
619
/** @} **/ //end group
620

  
621
//select a detector to read
622
static void bom_select(char which) {
623
	if(bom_type == BOM10)
624
	  which = lookup[(int)which];
625
	
626
    if (which&8)
627
      digital_output(select_pins[3], 1);
628
    else
629
      digital_output(select_pins[3], 0);
630

  
631
    if (which&4)
632
      digital_output(select_pins[2], 1);
633
    else
634
      digital_output(select_pins[2], 0);
635

  
636
    if (which&2)
637
      digital_output(select_pins[1], 1);
638
    else
639
      digital_output(select_pins[1], 0);
640

  
641
    if (which&1)
642
      digital_output(select_pins[0], 1);
643
    else
644
      digital_output(select_pins[0], 0);
645
	
646
}
branches/ir_lookup/battery.h
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 battery.h
29
 * @brief Definitions for checking battery voltage.
30
 * 
31
 * Contains definitions for checking the voltage of the 
32
 * battery.
33
 *
34
 * @author Colony Project, CMU Robotics Club
35
 **/
36

  
37
#ifndef _BATTERY_H_
38
#define _BATTERY_H_
39

  
40
/**
41
 * @addtogroup battery
42
 * @{
43
 **/
44

  
45
/** @brief Normal battery voltage. (6 V) **/
46
#define BATTERY_NORMALV 154
47
/** @brief Charging battery voltage. (7 V) **/
48
#define BATTERY_CHARINGV 179
49
/** @brief Low battery voltage. ( < 6 V) **/
50
#define BATTERY_LOWV 152
51

  
52
/** @brief Read the battery voltage. **/
53
int battery8(void);
54
/** @brief Read the battery voltage in deciVolts. **/
55
int battery(void);
56
/** @brief Check if the battery is low. **/
57
char battery_low(void);
58
/** @brief Get an average battery voltage reading. **/
59
int battery8_avg(int n_samples);
60

  
61
/** @} **/ //end addtogroup
62

  
63
#endif
64

  
branches/ir_lookup/dio.h
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 dio.h
29
 * @brief Definitions for digital input / output
30
 *
31
 * This file contains definitions and functions for dealing
32
 * with digital input and output.
33
 *
34
 * @author Colony Project, CMU Robotics Club
35
 * Based on Tom Lauwer's Firefly Library
36
 **/
37

  
38
#ifndef _DIO_H
39
#define _DIO_H
40

  
41
/*
42
  these are ALL the pins
43
  don't use unless you know what you're doing
44
*/
45

  
46
/*
47
  DIO pins on new dragonfly boards are shown below:
48
  -----------------------------------------------
49
  | _PIN_E6 | _PIN_E7 | _PIN_D2 | _PIN_D3 | VCC |
50
  -----------------------------------------------
51
  | _PIN_E2 | _PIN_E3 | _PIN_E4 | _PIN_E5 | GND |
52
  -----------------------------------------------
53
*/
54

  
55
/** @brief Port A **/
56
#define _PORT_A 1
57
/** @brief Port B **/
58
#define _PORT_B 2
59
/** @brief Port C **/
60
#define _PORT_C 3
61
/** @brief Port D **/
62
#define _PORT_D 4
63
/** @brief Port E **/
64
#define _PORT_E 5
65
/** @brief Port F **/
66
#define _PORT_F 6
67
/** @brief Port G **/
68
#define _PORT_G 7
69

  
70
/** @brief Pin A0 **/
71
#define _PIN_A0 8
72
/** @brief Pin A1 **/
73
#define _PIN_A1 9
74
/** @brief Pin A2 **/
75
#define _PIN_A2 10
76
/** @brief Pin A3 **/
77
#define _PIN_A3 11
78
/** @brief Pin A4 **/
79
#define _PIN_A4 12
80
/** @brief Pin A5 **/
81
#define _PIN_A5 13
82
/** @brief Pin A6 **/
83
#define _PIN_A6 14 
84
/** @brief Pin A7 **/
85
#define _PIN_A7 15
86

  
87
/** @brief Pin B0 **/
88
#define _PIN_B0 16
89
/** @brief Pin B1 **/
90
#define _PIN_B1 17
91
/** @brief Pin B2 **/
92
#define _PIN_B2 18
93
/** @brief Pin B3 **/
94
#define _PIN_B3 19
95
/** @brief Pin B4 **/
96
#define _PIN_B4 20
97
/** @brief Pin B5 **/
98
#define _PIN_B5 21
99
/** @brief Pin B6 **/
100
#define _PIN_B6 22
101
/** @brief Pin B7 **/
102
#define _PIN_B7 23
103

  
104
/** @brief Pin C0 **/
105
#define _PIN_C0 24
106
/** @brief Pin C1 **/
107
#define _PIN_C1 25
108
/** @brief Pin C2 **/
109
#define _PIN_C2 26
110
/** @brief Pin C3 **/
111
#define _PIN_C3 27
112
/** @brief Pin C4 **/
113
#define _PIN_C4 28
114
/** @brief Pin C5 **/
115
#define _PIN_C5 29
116
/** @brief Pin C6 **/
117
#define _PIN_C6 30
118
/** @brief Pin C7 **/
119
#define _PIN_C7 31
120

  
121
/** @brief Pin D0 **/
122
#define _PIN_D0 32  // pin DIO6 on new dragonfly boards
123
/** @brief Pin D1 **/
124
#define _PIN_D1 33  // pin DIO7
125
/** @brief Pin D2 **/
126
#define _PIN_D2 34
127
/** @brief Pin D3 **/
128
#define _PIN_D3 35
129
/** @brief Pin D4 **/
130
#define _PIN_D4 36
131
/** @brief Pin D5 **/
132
#define _PIN_D5 37
133
/** @brief Pin D6 **/
134
#define _PIN_D6 38
135
/** @brief Pin D7 **/
136
#define _PIN_D7 39
137

  
138
/** @brief Pin E0 **/
139
#define _PIN_E0 40
140
/** @brief Pin E1 **/
141
#define _PIN_E1 41
142
/** @brief Pin E2 **/
143
#define _PIN_E2 42  // pin DIO0
144
/** @brief Pin E3 **/
145
#define _PIN_E3 43  // pin DIO1
146
/** @brief Pin E4 **/
147
#define _PIN_E4 44  // pin DIO2
148
/** @brief Pin E5 **/
149
#define _PIN_E5 45  // pin DIO3
150
/** @brief Pin E6 **/
151
#define _PIN_E6 46  // pin DIO4
152
/** @brief Pin E7 **/
153
#define _PIN_E7 47  // pin DIO5
154

  
155
/** @brief Pin F0 **/
156
#define _PIN_F0 48
157
/** @brief Pin F1 **/
158
#define _PIN_F1 49
159
/** @brief Pin F2 **/
160
#define _PIN_F2 50
161
/** @brief Pin F3 **/
162
#define _PIN_F3 51
163
/** @brief Pin F4 **/
164
#define _PIN_F4 52
165
/** @brief Pin F5 **/
166
#define _PIN_F5 53
167
/** @brief Pin F6 **/
168
#define _PIN_F6 54
169
/** @brief Pin F7 **/
170
#define _PIN_F7 55
171

  
172
/** @brief Pin G0 **/
173
#define _PIN_G0 56
174
/** @brief Pin WR **/
175
#define _PIN_WR 56
176
/** @brief Pin G1 **/
177
#define _PIN_G1 57
178
/** @brief Pin RD **/
179
#define _PIN_RD 57
180
/** @brief Pin G2 **/
181
#define _PIN_G2 58
182
/** @brief Pin ALE **/
183
#define _PIN_ALE 58
184
/** @brief Pin G3 **/
185
#define _PIN_G3 59
186
/** @brief Pin TOSC2 **/
187
#define _PIN_TOSC2 59
188
/** @brief Pin G4 **/
189
#define _PIN_G4 60
190
/** @brief Pin TOSC1 **/
191
#define _PIN_TOSC1 60
192
//#define _PIN_G5 61
193
//#define _PIN_G6 62
194
//#define _PIN_G7 63
195

  
196
/*
197
  These are the header pins (the ones you can connect things to)
198
  Feel free to use these
199
*/
200

  
201
/**
202
 * @addtogroup dio
203
 * @{
204
 **/
205
/** @brief Pin A0 **/
206
#define PIN_A0 8
207
/** @brief Pin A1 **/
208
#define PIN_A1 9
209
/** @brief Pin A2 **/
210
#define PIN_A2 10
211
/** @brief Pin A3 **/
212
#define PIN_A3 11
213
/** @brief Pin A4 **/
214
#define PIN_A4 12
215
/** @brief Pin A5 **/
216
#define PIN_A5 13
217
/** @brief Pin A6 **/
218
#define PIN_A6 14 
219
/** @brief Pin A7 **/
220
#define PIN_A7 15
221

  
222
/** @brief Pin SS **/
223
#define PIN_SS 16
224
/** @brief Pin SCK **/
225
#define PIN_SCK 17
226
/** @brief Pin MOSI **/
227
#define PIN_MOSI 18
228
/** @brief Pin MISO **/
229
#define PIN_MISO 19
230
/** @brief LCD Command Pin **/
231
#define PIN_LCD_COMMAND 20
232

  
233
/** @brief Pin C0 **/
234
#define PIN_C0 24
235
/** @brief Pin C1 **/
236
#define PIN_C1 25
237
/** @brief Pin C2 **/
238
#define PIN_C2 26
239
/** @brief Pin C3 **/
240
#define PIN_C3 27
241
/** @brief Pin C4 **/
242
#define PIN_C4 28
243
/** @brief Pin C5 **/
244
#define PIN_C5 29
245
/** @brief Pin C6 **/
246
#define PIN_C6 30
247
/** @brief Pin C7 **/
248
#define PIN_C7 31
249

  
250
/** @brief Pin SCL **/
251
#define PIN_SCL 32
252
/** @brief Pin SDA **/
253
#define PIN_SDA 33
254

  
255
/** @brief Pin RX0 **/
256
#define PIN_RX0 40
257
/** @brief Pin TX0 **/
258
#define PIN_TX0 41
259
/** @brief LCD Reset Pin **/
260
#define PIN_LCD_RESET 42
261
/** @brief Pin E6 **/
262
#define PIN_E6 46
263
/** @brief Pin EXT_DIO1 **/
264
#define PIN_EXT_DIO1 46
265
/** @brief Pin E7 **/
266
#define PIN_E7 47
267
/** @brief Pin EXT_DIO2 **/
268
#define PIN_EXT_DIO2 48
269

  
270
/** @brief Pin AN0 **/
271
#define PIN_AN0 48
272
/** @brief Pin ADC0 **/
273
#define PIN_ADC0 48
274
/** @brief Pin AN1 **/
275
#define PIN_AN1 49
276
/** @brief Pin ADC1 **/
277
#define PIN_ADC1 49
278
/** @brief Pin AN2 **/
279
#define PIN_AN2 50
280
/** @brief Pin ADC2 **/
281
#define PIN_ADC2 50
282
/** @brief Pin AN3 **/
283
#define PIN_AN3 51
284
/** @brief Pin ADC3 **/
285
#define PIN_ADC3 51
286
/** @brief Pin AN4 **/
287
#define PIN_AN4 52
288
/** @brief Pin ADC4 **/
289
#define PIN_ADC4 52
290
/** @brief Pin AN5 **/
291
#define PIN_AN5 53
292
/** @brief Pin ADC5 **/
293
#define PIN_ADC5 53
294
/** @brief Pin AN6 **/
295
#define PIN_AN6 54
296
/** @brief Pin ADC6 **/
297
#define PIN_ADC6 54
298
/** @brief Pin AN7 **/
299
#define PIN_AN7 55
300
/** @brief Pin ADC7 **/
301
#define PIN_ADC7 55
302

  
303
/** @brief Wheel Pin **/
304
#define PIN_WHEEL 54
305
/** @brief Battery Voltage Monitor Pin **/
306
#define PIN_BATT 55
307

  
308
/** @brief button1 Pin **/
309
#define PIN_BTN1 56
310
/** @brief button2 Pin **/
311
#define PIN_BTN2 57
312

  
313
/** @brief LED1 Pin **/
314
#define PIN_LED1 58
315

  
316
/* Buttons */
317
/** @brief Button Pin **/
318
#define PIN_BTN PING
319
/** @brief button2 Pin **/
320
#define BTN2 PING1
321
/** @brief button1 Pin **/
322
#define BTN1 PING0
323

  
324
/** @brief Read a portpin. **/
325
int digital_input(int);
326
/** @brief Output to a portpin. **/
327
void digital_output(int bit, int val);
328
/** @brief Pullup a portpin. **/
329
void digital_pull_up(int);
330

  
331
/** @brief Check if button1 is pressed. **/
332
int button1_read( void );
333
/** @brief Check if button1 is clicked. **/
334
int button1_click( void );
335
/** @brief Wait until button1 is pressed. **/
336
void button1_wait( void );
337

  
338
/** @brief Check if button2 is pressed. **/
339
int button2_read( void );
340
/** @brief Check if button2 is clicked. **/
341
int button2_click( void );
342
/** @brief Wait until button2 is pressed. **/
343
void button2_wait( void );
344

  
345
/** @} **/ // end addtogroup
346

  
347
#endif
348

  
branches/ir_lookup/atomic.h
1
/* This file is in the dragonfly library directory because it seems to be
2
   missing from the AVR libc distribution */
3
   
4
/* Copyright (c) 2007 Dean Camera
5
   All rights reserved.
6

  
7
   Redistribution and use in source and binary forms, with or without
8
   modification, are permitted provided that the following conditions are met:
9

  
10
   * Redistributions of source code must retain the above copyright
11
     notice, this list of conditions and the following disclaimer.
12

  
13
   * Redistributions in binary form must reproduce the above copyright
14
     notice, this list of conditions and the following disclaimer in
15
     the documentation and/or other materials provided with the
16
     distribution.
17

  
18
   * Neither the name of the copyright holders nor the names of
19
     contributors may be used to endorse or promote products derived
20
     from this software without specific prior written permission.
21

  
22
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
  POSSIBILITY OF SUCH DAMAGE.
33
*/
34

  
35
/* $Id: atomic.h,v 1.3 2007/12/20 14:17:56 joerg_wunsch Exp $ */
36

  
37
#ifndef _UTIL_ATOMIC_H_
38
#define _UTIL_ATOMIC_H_ 1
39

  
40
#include <avr/io.h>
41
#include <avr/interrupt.h>
42

  
43
#if !defined(__DOXYGEN__)
44
/* Internal helper functions. */
45
static __inline__ uint8_t __iSeiRetVal(void)
46
{
47
    sei();
48
    return 1;
49
}
50

  
51
static __inline__ uint8_t __iCliRetVal(void)
52
{
53
    cli();
54
    return 1;
55
}
56

  
57
static __inline__ void __iSeiParam(const uint8_t *__s)
58
{
59
    sei();
60
    __asm__ volatile ("" ::: "memory");
61
    (void)__s;
62
}
63

  
64
static __inline__ void __iCliParam(const uint8_t *__s)
65
{
66
    cli();
67
    __asm__ volatile ("" ::: "memory");
68
    (void)__s;
69
}
70

  
71
static __inline__ void __iRestore(const  uint8_t *__s)
72
{
73
    SREG = *__s;
74
    __asm__ volatile ("" ::: "memory");
75
}
76
#endif	/* !__DOXYGEN__ */
77

  
78
/** \file */
79
/** \defgroup util_atomic <util/atomic.h> Atomically and Non-Atomically Executed Code Blocks
80

  
81
    \code
82
    #include <util/atomic.h>
83
    \endcode
84

  
85
    \note The macros in this header file require the ISO/IEC 9899:1999
86
    ("ISO C99") feature of for loop variables that are declared inside
87
    the for loop itself.  For that reason, this header file can only
88
    be used if the standard level of the compiler (option --std=) is
89
    set to either \c c99 or \c gnu99.
90

  
91
    The macros in this header file deal with code blocks that are
92
    guaranteed to be excuted Atomically or Non-Atmomically.  The term
93
    "Atomic" in this context refers to the unability of the respective
94
    code to be interrupted.
95

  
96
    These macros operate via automatic manipulation of the Global
97
    Interrupt Status (I) bit of the SREG register. Exit paths from
98
    both block types are all managed automatically without the need
99
    for special considerations, i. e. the interrupt status will be
100
    restored to the same value it has been when entering the
101
    respective block.
102

  
103
    A typical example that requires atomic access is a 16 (or more)
104
    bit variable that is shared between the main execution path and an
105
    ISR.  While declaring such a variable as volatile ensures that the
106
    compiler will not optimize accesses to it away, it does not
107
    guarantee atomic access to it.  Assuming the following example:
108

  
109
    \code
110
#include <inttypes.h>
111
#include <avr/interrupt.h>
112
#include <avr/io.h>
113

  
114
volatile uint16_t ctr;
115

  
116
ISR(TIMER1_OVF_vect)
117
{
118
  ctr--;
119
}
120

  
121
...
122
int
123
main(void)
124
{
125
   ...
126
   ctr = 0x200;
127
   start_timer();
128
   while (ctr != 0)
129
     // wait
130
       ;
131
   ...
132
}
133
    \endcode
134

  
135
    There is a chance where the main context will exit its wait loop
136
    when the variable \c ctr just reached the value 0xFF.  This happens
137
    because the compiler cannot natively access a 16-bit variable
138
    atomically in an 8-bit CPU.  So the variable is for example at
139
    0x100, the compiler then tests the low byte for 0, which succeeds.
140
    It then proceeds to test the high byte, but that moment the ISR
141
    triggers, and the main context is interrupted.  The ISR will
142
    decrement the variable from 0x100 to 0xFF, and the main context
143
    proceeds.  It now tests the high byte of the variable which is
144
    (now) also 0, so it concludes the variable has reached 0, and
145
    terminates the loop.
146

  
147
    Using the macros from this header file, the above code can be
148
    rewritten like:
149

  
150
    \code
151
#include <inttypes.h>
152
#include <avr/interrupt.h>
153
#include <avr/io.h>
154
#include <util/atomic.h>
155

  
156
volatile uint16_t ctr;
157

  
158
ISR(TIMER1_OVF_vect)
159
{
160
  ctr--;
161
}
162

  
163
...
164
int
165
main(void)
166
{
167
   ...
168
   ctr = 0x200;
169
   start_timer();
170
   sei();
171
   uint16_t ctr_copy;
172
   do
173
   {
174
     ATOMIC_BLOCK(ATOMIC_FORCEON)
175
     {
176
       ctr_copy = ctr;
177
     }
178
   }
179
   while (ctr_copy != 0);
180
   ...
181
}
182
    \endcode
183

  
184
    This will install the appropriate interrupt protection before
185
    accessing variable \c ctr, so it is guaranteed to be consistently
186
    tested.  If the global interrupt state were uncertain before
187
    entering the ATOMIC_BLOCK, it should be executed with the
188
    parameter ATOMIC_RESTORESTATE rather than ATOMIC_FORCEON.
189

  
190
*/
191

  
192
/** \def ATOMIC_BLOCK(type)
193
    \ingroup util_atomic
194

  
195
    Creates a block of code that is guaranteed to be executed
196
    atomically. Upon entering the block the Global Interrupt Status
197
    flag in SREG is disabled, and re-enabled upon exiting the block
198
    from any exit path.
199

  
200
    Two possible macro parameters are permitted, ATOMIC_RESTORESTATE
201
    and ATOMIC_FORCEON.
202
*/
203
#if defined(__DOXYGEN__)
204
#define ATOMIC_BLOCK(type)
205
#else
206
#define ATOMIC_BLOCK(type) for ( type, __ToDo = __iCliRetVal(); \
207
	                       __ToDo ; __ToDo = 0 )
208
#endif	/* __DOXYGEN__ */
209

  
210
/** \def NONATOMIC_BLOCK(type)
211
    \ingroup util_atomic
212

  
213
    Creates a block of code that is executed non-atomically. Upon
214
    entering the block the Global Interrupt Status flag in SREG is
215
    enabled, and disabled upon exiting the block from any exit
216
    path. This is useful when nested inside ATOMIC_BLOCK sections,
217
    allowing for non-atomic execution of small blocks of code while
218
    maintaining the atomic access of the other sections of the parent
219
    ATOMIC_BLOCK.
220

  
221
    Two possible macro parameters are permitted,
222
    NONATOMIC_RESTORESTATE and NONATOMIC_FORCEOFF.
223
*/
224
#if defined(__DOXYGEN__)
225
#define NONATOMIC_BLOCK(type)
226
#else
227
#define NONATOMIC_BLOCK(type) for ( type, __ToDo = __iSeiRetVal(); \
228
	                          __ToDo ;  __ToDo = 0 )
229
#endif	/* __DOXYGEN__ */
230

  
231
/** \def ATOMIC_RESTORESTATE
232
    \ingroup util_atomic
233

  
234
    This is a possible parameter for ATOMIC_BLOCK. When used, it will
235
    cause the ATOMIC_BLOCK to restore the previous state of the SREG
236
    register, saved before the Global Interrupt Status flag bit was
237
    disabled. The net effect of this is to make the ATOMIC_BLOCK's
238
    contents guaranteed atomic, without changing the state of the
239
    Global Interrupt Status flag when execution of the block
240
    completes.
241
*/
242
#if defined(__DOXYGEN__)
243
#define ATOMIC_RESTORESTATE
244
#else
245
#define ATOMIC_RESTORESTATE uint8_t sreg_save \
246
	__attribute__((__cleanup__(__iRestore))) = SREG
247
#endif	/* __DOXYGEN__ */
248

  
249
/** \def ATOMIC_FORCEON
250
    \ingroup util_atomic
251

  
252
    This is a possible parameter for ATOMIC_BLOCK. When used, it will
253
    cause the ATOMIC_BLOCK to force the state of the SREG register on
254
    exit, enabling the Global Interrupt Status flag bit. This saves on
255
    flash space as the previous value of the SREG register does not
256
    need to be saved at the start of the block.
257

  
258
    Care should be taken that ATOMIC_FORCEON is only used when it is
259
    known that interrupts are enabled before the block's execution or
260
    when the side effects of enabling global interrupts at the block's
261
    completion are known and understood.
262
*/
263
#if defined(__DOXYGEN__)
264
#define ATOMIC_FORCEON
265
#else
266
#define ATOMIC_FORCEON uint8_t sreg_save \
267
	__attribute__((__cleanup__(__iSeiParam))) = 0
268
#endif	/* __DOXYGEN__ */
269

  
270
/** \def NONATOMIC_RESTORESTATE
271
    \ingroup util_atomic
272

  
273
    This is a possible parameter for NONATOMIC_BLOCK. When used, it
274
    will cause the NONATOMIC_BLOCK to restore the previous state of
275
    the SREG register, saved before the Global Interrupt Status flag
276
    bit was enabled. The net effect of this is to make the
277
    NONATOMIC_BLOCK's contents guaranteed non-atomic, without changing
278
    the state of the Global Interrupt Status flag when execution of
279
    the block completes.
280
*/
281
#if defined(__DOXYGEN__)
282
#define NONATOMIC_RESTORESTATE
283
#else
284
#define NONATOMIC_RESTORESTATE uint8_t sreg_save \
285
	__attribute__((__cleanup__(__iRestore))) = SREG
286
#endif	/* __DOXYGEN__ */
287

  
288
/** \def NONATOMIC_FORCEOFF
289
    \ingroup util_atomic
290

  
291
    This is a possible parameter for NONATOMIC_BLOCK. When used, it
292
    will cause the NONATOMIC_BLOCK to force the state of the SREG
293
    register on exit, disabling the Global Interrupt Status flag
294
    bit. This saves on flash space as the previous value of the SREG
295
    register does not need to be saved at the start of the block.
296

  
297
    Care should be taken that NONATOMIC_FORCEOFF is only used when it
298
    is known that interrupts are disabled before the block's execution
299
    or when the side effects of disabling global interrupts at the
300
    block's completion are known and understood.
301
*/
302
#if defined(__DOXYGEN__)
303
#define NONATOMIC_FORCEOFF
304
#else
305
#define NONATOMIC_FORCEOFF uint8_t sreg_save \
306
	__attribute__((__cleanup__(__iCliParam))) = 0
307
#endif	/* __DOXYGEN__ */
308

  
309
#endif
branches/ir_lookup/bom.h
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.h
29
 * @brief Definitions for using the BOM
30
 * 
31
 * This file contains definitions for using the Bearing and 
32
 * Orientation Module (BOM).
33
 *
34
 * @author Colony Project, CMU Robotics Club
35
 *
36
 **/
37

  
38
#ifndef _BOM_H
39
#define _BOM_H
40

  
41
/**
42
 * @addtogroup bom
43
 * @{
44
 **/
45
 
46
/** @brief Include all elements in the 16-bit bitfield **/
47
#define BOM_ALL 0xFFFF
48

  
49
/** @brief Original BOM - No Range, No Individual LED control **/
50
#define BOM10     0
51

  
52
/** @brief BOM 1.5 - No Range, Individual LED control **/
53
#define BOM15   1
54

  
55
/** @brief RBOM - Range, Individual LED control **/
56
#define RBOM    2
57

  
58

  
59
/** @brief Struct for storing vector data. Used by bom_get_vector() functions **/
60
typedef struct vector {
61
	int x;
62
	int y;
63
} Vector;
64

  
65

  
66
/** @brief Initialize the bom according to bom type **/
67
void bom_init(char type);
68

  
69
/** @brief Refresh bom_val[] with new values from analog8.  analog_init and bom_init must be called for this to work. **/
70
void bom_refresh(int bit_field);
71

  
72
/** @brief Gets the bom reading from bom_val[which].  Call bom_refresh beforehand to read new bom values. **/
73
int bom_get(int which);
74

  
75
/** @brief Compares all the values in bom_val[] and returns the index to the highest value element. **/
76
int bom_get_max(void);
77

  
78
/** @brief Computes the net resultant BOM IR vector. **/
79
int bom_get_vector(Vector*, int*);
80

  
81
/** @brief Computes the normalized net resultant BOM IR vector. **/
82
int bom_get_norm_vector(Vector*, int*);
83

  
84
/** @brief Print snapshot of BOM intensity histogram over USB connection **/
85
int bom_print_usb(int*);
86

  
87
/** @brief Computes the weighted average of all the bom readings to estimate the position and distance of another robot. **/
88
int bom_get_max10(int *dist);
89

  
90
/** @brief Enables the selected bom leds on a BOM1.5 **/
91
void bom_set_leds(int bit_field);
92

  
93
/** @brief (DEPRECATED) Gets and compares all bom values.  Returns the index to the highest value element since last refresh. **/
94
int get_max_bom(void);
95

  
96
/** @brief Turns on all BOM leds, or turns on enabled leds on a BOM1.5. **/
97
void bom_on(void);
98

  
99
/** @brief Turns off all bom leds. **/
100
void bom_off(void);
101

  
102
/** @} **/
103

  
104
#endif
105

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff