Project

General

Profile

Revision 380

Added by Jason knichel about 16 years ago

fixed some style stuff

View differences:

rangefinder.c
34 34
 **/
35 35

  
36 36
/*
37
Authors: James Kong and Greg Tress
37
  Authors: James Kong and Greg Tress
38 38

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

  
43
-----------------
44
rangefinder.c
45
Using Sharp GP2D02 IR Rangefinder
43
  -----------------
44
  rangefinder.c
45
  Using Sharp GP2D02 IR Rangefinder
46 46

  
47
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
47
  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 49

  
50
Expected Initial Conditions:
51
Vin is high and Vout should read high.
50
  Expected Initial Conditions:
51
  Vin is high and Vout should read high.
52 52

  
53
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
53
  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 58

  
59 59
*/
60 60

  
......
62 62
#include "rangefinder.h"
63 63

  
64 64
/*
65
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]
65
  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 68

  
69
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].
69
  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 73
*/
74 74

  
75 75
// constants
76 76
/* Nasty IR approximation table
77
  I'm using this for the heck of it.  We can do whatever.
77
   I'm using this for the heck of it.  We can do whatever.
78 78

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

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

  
84
  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.
84
   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 86
*/
87 87

  
88 88
#define MIN_IR_ADC8 20
89 89
#define MAX_IR_ADC8 133
90 90

  
91 91
static int IR_dist_conversion[114] = {
92
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
92
  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 100
};
101 101

  
102 102
int linearize_distance(int value);
......
118 118
 **/
119 119
void range_init(void)
120 120
{
121
	digital_output(_PIN_B4,0);
121
  digital_output(_PIN_B4,0);
122 122
}
123 123

  
124 124
/**
......
132 132
 *
133 133
 * @see range_init
134 134
 **/
135
int range_read_distance (int range_id)
136
{
137
	return linearize_distance(analog8(range_id));
135
int range_read_distance (int range_id) {
136
  return linearize_distance(analog8(range_id));
138 137
}
139 138

  
140 139
/** @} **/ //end defgroup
141 140

  
142
int linearize_distance(int value)
143
{
144
  if(value < MIN_IR_ADC8)
145
  {
141
int linearize_distance(int value) {
142
  if(value < MIN_IR_ADC8) {
146 143
    return -1;
147
  }
148
  else if(value > MAX_IR_ADC8)
149
  {
144
  } else if(value > MAX_IR_ADC8) {
150 145
    return -1;
151
  }
152
  else
153
  {
146
  } else {
154 147
    return IR_dist_conversion[value - MIN_IR_ADC8];
155 148
  }
156 149
}
157

  
158

  
159

  

Also available in: Unified diff