Project

General

Profile

Statistics
| Revision:

root / branches / encoders / code / projects / libdragonfly / rangefinder.c @ 1345

History | View | Annotate | Download (4.58 KB)

1 1345 chihsiuh
/**
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 rangefinder.c
29
 * @brief Rangefinders
30
 *
31
 * Implementation of functions for rangefinder use.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35
36 8 bcoltin
/*
37 1345 chihsiuh
  Authors: James Kong and Greg Tress
38 8 bcoltin

39 1345 chihsiuh
  Last Modified: 4/30/06 by James
40
  -Started log_distance conversion function !!!NOT COMPLETE!!!
41
  -Cleaning up comments
42 8 bcoltin

43 1345 chihsiuh
  -----------------
44
  rangefinder.c
45
  Using Sharp GP2D02 IR Rangefinder
46 8 bcoltin

47 1345 chihsiuh
  Vin is the input to the rangefinder, designated RANGE_CTRL.
48
  Vout is the output from the rangefinder, designated RANGE_IN# where # is the rangefinder you are reading from
49 8 bcoltin

50 1345 chihsiuh
  Expected Initial Conditions:
51
  Vin is high and Vout should read high.
52 8 bcoltin

53 1345 chihsiuh
  Usage:
54
  1.) Set Vin low. Vout should read low.
55
  2.) Wait for high on Vout.
56
  3.) Begin clocking Vin and reading 8 bits from Vout (MSB first).
57
  4.) Set Vin high for 2ms or more to turn off rangefinder
58 8 bcoltin

59
*/
60
61
#include "dragonfly_lib.h"
62
#include "rangefinder.h"
63
64
/*
65 1345 chihsiuh
  read_distance returns the 8-bit reading from the rangefinder
66
  parameters:
67
  range_id - dio pin set as the rangefinder Vout [i.e. RANGE_IN0]
68 8 bcoltin

69 1345 chihsiuh
  NOTE:
70
  The Sharp GD2D02 returns values on a decreasing logrithmic scale.
71
  So higher values correspond to closer distances.  Use linearize_distance to convert to normal centimeter scale.  Also, when reading distances closer than 8cm, the Sharp GD2D02 will return lower values than the values at 8cm.
72
  At this point, we are only reading from one rangefinder [RANGE_IN0].
73 8 bcoltin
*/
74
75
// constants
76
/* Nasty IR approximation table
77 1345 chihsiuh
   I'm using this for the heck of it.  We can do whatever.
78 8 bcoltin

79 1345 chihsiuh
   Note the minimum value is .4V (20), and the maximum is 2.6V (133).
80
   Gives distance in mm.
81 8 bcoltin

82 1345 chihsiuh
   excel formula(valid for inputs 20-133):  ROUND(2353.6*(E2^(-1.1146))*10,0)
83 8 bcoltin

84 1345 chihsiuh
   This is only valid for the GP2D12, with objects directly ahead and more than
85
   10cm from the detector.  See the datasheet for more information.
86 8 bcoltin
*/
87
88
#define MIN_IR_ADC8 20
89
#define MAX_IR_ADC8 133
90
91
static int IR_dist_conversion[114] = {
92 1345 chihsiuh
  800,791,751,714,681,651,623,597,574,552,531,512,494,478,462,447
93
  ,434,421,408,397,386,375,365,356,347,338,330,322,315,307,301,294
94
  ,288,282,276,270,265,260,255,250,245,241,237,232,228,224,221,217
95
  ,213,210,207,203,200,197,194,191,189,186,183,181,178,176,173,171
96
  ,169,166,164,162,160,158,156,154,152,151,149,147,145,144,142,140
97
  ,139,137,136,134,133,131,130,129,127,126,125,124,122,121,120,119
98
  ,118,117,115,114,113,112,111,110,109,108,107,106,105,105,104,103
99
  ,102,101
100 8 bcoltin
};
101
102
int linearize_distance(int value);
103
104
/**
105
 * @defgroup rangefinder Rangefinder
106
 * @brief Functions for using the IR rangefinders
107
 *
108
 * Functions for using the IR rangefinders.
109
 *
110
 * @{
111
 **/
112
113
/**
114
 * Initializes the rangefinders. This must be called before
115
 * range_read_distance.
116
 *
117
 * @see range_read_distance
118
 **/
119 1345 chihsiuh
void range_init(void)
120
{
121 8 bcoltin
  digital_output(_PIN_B4,0);
122
}
123
124
/**
125
 * Reads the distance measured by one of the rangefinders.
126
 * This distance is in arbitrary units.
127
 *
128
 * @param range_id the rangefinder to use. This should be one
129
 * of the constants IR1 - IR5.
130
 *
131
 * @return the distance measured by the rangefinder
132
 *
133
 * @see range_init
134
 **/
135 1345 chihsiuh
int range_read_distance (int range_id) {
136 8 bcoltin
  return linearize_distance(analog8(range_id));
137
}
138
139
/** @} **/ //end defgroup
140
141 1345 chihsiuh
int linearize_distance(int value) {
142
  if(value < MIN_IR_ADC8) {
143 8 bcoltin
    return -1;
144 1345 chihsiuh
  } else if(value > MAX_IR_ADC8) {
145 8 bcoltin
    return -1;
146 1345 chihsiuh
  } else {
147 8 bcoltin
    return IR_dist_conversion[value - MIN_IR_ADC8];
148
  }
149
}