root / trunk / code / projects / colonet / utilities / dragonfly_wireless_relay / rangefinder.c @ 13
History | View | Annotate | Download (3.04 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 |
void range_init(void){ |
42 |
digital_output(_PIN_B4,0);
|
43 |
} |
44 |
|
45 |
int read_distance (int range_id) { |
46 |
return analog8(range_id);
|
47 |
} |
48 |
|
49 |
|
50 |
int linearize_distance(int value) |
51 |
{ |
52 |
if(value < MIN_IR_ADC8)
|
53 |
{ |
54 |
return -1; |
55 |
} |
56 |
else if(value > MAX_IR_ADC8) |
57 |
{ |
58 |
return -1; |
59 |
} |
60 |
else
|
61 |
{ |
62 |
return IR_dist_conversion[value - MIN_IR_ADC8];
|
63 |
} |
64 |
} |
65 |
|
66 |
|
67 |
|
68 |
|
69 |
/*
|
70 |
linearize_distance converts an 8-bit rangefinder reading to a centimeter measurement (truncated to an integer)
|
71 |
parameters:
|
72 |
reading - 8-bit rangefinder reading
|
73 |
|
74 |
OFFSET, GAIN, and THRESHOLD are experimentally determined constants
|
75 |
The conversion is a piecewise defined function with a continuity fix at reading = THRESHOLD3
|
76 |
|
77 |
NOTE:
|
78 |
This function is ugly, but it gets the job done without spending a huge amount processing.
|
79 |
Ideally, you want to compare with the read_distance value directly.
|
80 |
*/
|
81 |
/*
|
82 |
int linearize_distance (int reading) {
|
83 |
|
84 |
int temp = reading;
|
85 |
if(reading > THRESHOLD1){
|
86 |
temp = temp - OFFSET1;
|
87 |
return GAIN1 / temp;
|
88 |
}else if(reading > THRESHOLD2){
|
89 |
temp = temp - OFFSET2;
|
90 |
return GAIN2 / temp + 1;
|
91 |
}else if(reading > THRESHOLD3){
|
92 |
temp = temp - OFFSET2;
|
93 |
return GAIN2 / temp;
|
94 |
}else if(reading == THRESHOLD3){
|
95 |
return CONTINUITY_FIX;
|
96 |
}else if(reading > THRESHOLD4){
|
97 |
temp = temp - OFFSET3;
|
98 |
return GAIN3 / temp;
|
99 |
}else{
|
100 |
return MAX_DIST;
|
101 |
}
|
102 |
|
103 |
}*/
|
104 |
|
105 |
|
106 |
|
107 |
/*
|
108 |
log_distance converts a centimeter value to a rangefinder comparable value
|
109 |
parameters:
|
110 |
distance - centimeter measurement
|
111 |
|
112 |
NOTE:
|
113 |
This is still incomplete, do not use it.
|
114 |
*/
|
115 |
|
116 |
/*
|
117 |
int log_distance(int distance) {
|
118 |
|
119 |
if(distance >= MAX_DIST){
|
120 |
return THRESHOLD4;
|
121 |
}
|
122 |
if(distance <= MIN_DIST){
|
123 |
return 255;
|
124 |
}
|
125 |
|
126 |
switch(distance){ //Incomplete need to fill out table
|
127 |
case 60:
|
128 |
return 56;
|
129 |
case 59:
|
130 |
return 89;
|
131 |
}
|
132 |
|
133 |
return 0;
|
134 |
|
135 |
*/
|