Project

General

Profile

Statistics
| Revision:

root / trunk / code / lib / src / libdragonfly / bom.c @ 277

History | View | Annotate | Download (4.48 KB)

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
    i = lookup[j];
118

    
119
    if (i&8) {
120
      output_high(MONK3);
121
    } else {
122
      output_low(MONK3);
123
    }
124

    
125
    if (i&4) {
126
      output_high(MONK2);
127
    } else {
128
      output_low(MONK2);
129
    }
130

    
131
    if (i&2) {
132
      output_high(MONK1);
133
    } else {
134
      output_low(MONK1);
135
    }
136

    
137
    if (i&1) {
138
      output_high(MONK0);
139
    } else {
140
      output_low(MONK0);
141
    }
142

    
143
    a = analog8(MONKI);
144

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

    
151
  //threshold on the bom analog value.
152
  //defined in bom.h
153
  // if the analog value read is above the threshold, we cannot see a robot
154
  // (remember, low means higher intensity).
155
  if (h < BOM_VALUE_THRESHOLD) {
156
    return max_bom_temp;
157
  } else {
158
    return -1;
159
  }
160
}
161

    
162
/**
163
 * Flashes the BOM. analog_init must be called before this
164
 * function can be used.
165
 *
166
 * @see bom_off, analog_init
167
 **/
168
void bom_on(void)
169
{
170
  output_high(MONKL);
171
}
172

    
173
/**
174
 * Stops flashing the BOM. analog_init must be called
175
 * before this function can be used.
176
 *
177
 * @see bom_on, analog_init
178
 **/
179
void bom_off(void)
180
{
181
  output_low(MONKL);
182
}
183

    
184
/** @} **/ //end group
185

    
186
void output_high(int which)
187
{
188
  digital_output(which, 1);
189
}
190

    
191
void output_low(int which)
192
{
193
  digital_output(which, 0);
194
}