Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / libdragonfly / bom.c @ 891

History | View | Annotate | Download (4.35 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
static const char lookup[16] = {7,6,5,0xe,1,4,3,2,0xf,0,0xd,8,0xc,0xb,9,0xa};
43

    
44
// internal function prototypes
45
static void bom_select(char which);
46

    
47
/*
48
 Bk R Y (Analog)
49
---------
50
 Green
51
 Blue
52
 White
53
---------
54
 Blue
55
 White
56
*/
57

    
58

    
59
/*
60
the analog pin definitions from dio.h DO NOT work here,
61
so we must use PF0 from avrgcc (as opposed to _PIN_F0).
62
BUT the dio pin definitions from dio.h must be used (no PE...).
63

64
also, _PIN_E2 is initialized to high for some reason,
65
which turns the BOM on when the robot is turned on.
66
WORK-AROUND: call digital_output(_PIN_E2,0) at some point.
67

68
*/
69

    
70
#define MONKI PF0         //analog (yellow)
71
//------------------------//
72
#define MONKL _PIN_E2     //green
73
#define MONK1 _PIN_E3     //blue
74
#define MONK0 _PIN_E4     //white
75
//------------------------//
76
#define MONK3 _PIN_E6     //blue
77
#define MONK2 _PIN_E7     //white
78

    
79
#define BOM_VALUE_THRESHOLD 200
80
#define NUM_BOM_LEDS 16
81

    
82

    
83

    
84
static unsigned int bom_val[NUM_BOM_LEDS];
85
static char bom_type = BOM;
86
  
87
void bom_init(char type) {
88
    bom_type = type;
89
    
90
    switch(bom_type) {
91
    case BOM:
92
        break;
93
    case BOM15:
94
        break;
95
    case RBOM:
96
        break;
97
    //default:
98
    }
99
}
100

    
101
void bom_refresh(int bit_field) {
102
    int i;
103
    
104
    analog_stop_loop();
105
    
106
    for(i = 0; i < NUM_BOM_LEDS; i++) {
107
        if(bit_field & 0x1) {
108
            bom_select(lookup[i]);
109
            bom_val[i] = analog_get8(MONKI);
110
        }
111
        bit_field = bit_field >> 1;
112
    }
113
    
114
    analog_start_loop();
115
}
116

    
117
int bom_get(int which) {
118
    return bom_val[which];
119
}
120

    
121
int bom_get_max(void) {
122
    int i, lowest_val, lowest_i;
123
    lowest_i = -1;
124
    lowest_val = 255;
125
    for(i = 0; i < NUM_BOM_LEDS; i++) {
126
        if(bom_val[i] < lowest_val) {
127
            lowest_val = bom_val[i];
128
            lowest_i = i;
129
        }
130
    }
131
    
132
    if(lowest_val < BOM_VALUE_THRESHOLD)
133
        return lowest_i;
134
    else
135
        return -1;
136
}
137

    
138
void bom_leds_on(int bit_field) {
139
    switch(bom_type) {
140
    case BOM:
141
        if(bit_field == BOM_ALL) {
142
            digital_output(MONKL, 1);
143
        }
144
        break;
145
    case BOM15:
146
        //add bom 1.5 code here
147
        break;
148
    case RBOM:
149
        //add rbom code here
150
        break;
151
    }
152
}
153

    
154
void bom_leds_off(int bit_field) {
155
    switch(bom_type) {
156
    case BOM:
157
        if(bit_field == BOM_ALL) {
158
            digital_output(MONKL, 0);
159
        }
160
        break;
161
    case BOM15:
162
        //add bom 1.5 code here
163
        break;
164
    case RBOM:
165
        //add rbom code here
166
        break;
167
    }
168
}
169

    
170
void bom_on(void)
171
{
172
  bom_leds_on(BOM_ALL);
173
}
174

    
175
void bom_off(void)
176
{
177
    bom_leds_off(BOM_ALL);
178
}
179

    
180
static void bom_select(char which) {
181
      if (which&8)
182
        digital_output(MONK3, 1);
183
      else
184
        digital_output(MONK3, 0);
185

    
186
      if (which&4)
187
        digital_output(MONK2, 1);
188
      else
189
        digital_output(MONK2, 0);
190

    
191
      if (which&2)
192
        digital_output(MONK1, 1);
193
      else
194
        digital_output(MONK1, 0);
195

    
196
      if (which&1)
197
        digital_output(MONK0, 1);
198
      else
199
        digital_output(MONK0, 0);
200
}
201