Project

General

Profile

Statistics
| Revision:

root / trunk / code / lib / src / libdragonfly / rangefinder.c @ 7

History | View | Annotate | Download (3.23 KB)

1
/*
2
Authors: James Kong and Greg Tress
3

4
Last Modified: 4/30/06 by James
5
-Started log_distance conversion function !!!NOT COMPLETE!!!
6
-Cleaning up comments
7

8
-----------------
9
rangefinder.c
10
Using Sharp GP2D02 IR Rangefinder
11

12
Vin is the input to the rangefinder, designated RANGE_CTRL.
13
Vout is the output from the rangefinder, designated RANGE_IN# where # is the rangefinder you are reading from
14

15
Expected Initial Conditions:
16
Vin is high and Vout should read high.
17

18
Usage:
19
1.) Set Vin low. Vout should read low.
20
2.) Wait for high on Vout.
21
3.) Begin clocking Vin and reading 8 bits from Vout (MSB first).
22
4.) Set Vin high for 2ms or more to turn off rangefinder
23

24
*/
25

    
26
#include "dragonfly_lib.h"
27
#include "rangefinder.h"
28

    
29
/*
30
read_distance returns the 8-bit reading from the rangefinder
31
parameters:
32
range_id - dio pin set as the rangefinder Vout [i.e. RANGE_IN0]
33

34
NOTE:
35
The Sharp GD2D02 returns values on a decreasing logrithmic scale.
36
So higher values correspond to closer distances.  Use linearize_distance to convert to normal centimeter scale.
37
Also, when reading distances closer than 8cm, the Sharp GD2D02 will return lower values than the values at 8cm.
38
At this point, we are only reading from one rangefinder [RANGE_IN0].
39
*/
40

    
41
// constants
42
/* Nasty IR approximation table
43
  I'm using this for the heck of it.  We can do whatever.
44

45
  Note the minimum value is .4V (20), and the maximum is 2.6V (133).
46
  Gives distance in mm.
47

48
  excel formula(valid for inputs 20-133):  ROUND(2353.6*(E2^(-1.1146))*10,0)
49

50
  This is only valid for the GP2D12, with objects directly ahead and more than
51
  10cm from the detector.  See the datasheet for more information.
52
*/
53

    
54
#define MIN_IR_ADC8 20
55
#define MAX_IR_ADC8 133
56

    
57
static int IR_dist_conversion[114] = {
58
800,791,751,714,681,651,623,597,574,552,531,512,494,478,462,447
59
,434,421,408,397,386,375,365,356,347,338,330,322,315,307,301,294
60
,288,282,276,270,265,260,255,250,245,241,237,232,228,224,221,217
61
,213,210,207,203,200,197,194,191,189,186,183,181,178,176,173,171
62
,169,166,164,162,160,158,156,154,152,151,149,147,145,144,142,140
63
,139,137,136,134,133,131,130,129,127,126,125,124,122,121,120,119
64
,118,117,115,114,113,112,111,110,109,108,107,106,105,105,104,103
65
,102,101
66
};
67

    
68
int linearize_distance(int value);
69

    
70
/**
71
 * @defgroup rangefinder Rangefinder
72
 * @brief Functions for using the IR rangefinders
73
 * 
74
 * Functions for using the IR rangefinders.
75
 *
76
 * @{
77
 **/
78

    
79
/**
80
 * Initializes the rangefinders. This must be called before
81
 * range_read_distance.
82
 *
83
 * @see range_read_distance
84
 **/
85
void range_init(void){
86
  digital_output(_PIN_B4,0);
87
}
88

    
89
/**
90
 * Reads the distance measured by one of the rangefinders.
91
 * This distance is in arbitrary units.
92
 *
93
 * @param range_id the rangefinder to use. This should be one
94
 * of the constants IR1 - IR5.
95
 *
96
 * @return the distance measured by the rangefinder
97
 *
98
 * @see range_init
99
 **/
100
int range_read_distance (int range_id) {  
101
  return linearize_distance(analog8(range_id));
102
}
103

    
104
/** @} **/ //end defgroup
105

    
106
int linearize_distance(int value)
107
{
108
  if(value < MIN_IR_ADC8)
109
  {
110
    return -1;
111
  }
112
  else if(value > MAX_IR_ADC8)
113
  {
114
    return -1;
115
  }
116
  else
117
  {
118
    return IR_dist_conversion[value - MIN_IR_ADC8];
119
  }
120
}
121

    
122

    
123