Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / rangefinders / rangefinder.c @ 1475

History | View | Annotate | Download (5.12 KB)

1 1455 dshope
/**
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
/*
37
  Authors: James Kong and Greg Tress
38

39 1475 dshope
  Last Modified: 10/30/09 by Dan Shope
40 1455 dshope
  -Revised LUT to match GP2D120X Sensors (4-30cm)
41
  -range_read_distance() now returns -1 for below lower bound, -2 for beyond upper bound
42

43
  Modified: 4/30/06 by James
44
  -Started log_distance conversion function !!!NOT COMPLETE!!!
45
  -Cleaning up comments
46

47
  -----------------
48
  rangefinder.c
49 1475 dshope
  Using Sharp GP2D02 IR Rangefinder
50 1455 dshope

51
  Vin is the input to the rangefinder, designated RANGE_CTRL.
52
  Vout is the output from the rangefinder, designated RANGE_IN# where # is the rangefinder you are reading from
53

54
  Expected Initial Conditions:
55
  Vin is high and Vout should read high.
56

57
  Usage:
58
  1.) Set Vin low. Vout should read low.
59
  2.) Wait for high on Vout.
60
  3.) Begin clocking Vin and reading 8 bits from Vout (MSB first).
61
  4.) Set Vin high for 2ms or more to turn off rangefinder
62

63
*/
64 1475 dshope
#include <avr/pgmspace.h>
65 1455 dshope
66 1475 dshope
#include "dragonfly_defs.h"
67
#include "analog.h"
68
#include "dio.h"
69 1455 dshope
#include "rangefinder.h"
70
71 1475 dshope
unsigned char range_initd=0;
72
73 1455 dshope
/*
74
  read_distance returns the 8-bit reading from the rangefinder
75
  parameters:
76
  range_id - dio pin set as the rangefinder Vout [i.e. RANGE_IN0]
77

78
  NOTE:
79
  The Sharp GD2D02 returns values on a decreasing logrithmic scale.
80
  So higher values correspond to closer distances.  Use linearize_distance to convert to normal centimeter scale.
81
  Also, when reading distances closer than 8cm, the Sharp GD2D02 will return lower values than the values at 8cm.
82
  At this point, we are only reading from one rangefinder [RANGE_IN0].
83
*/
84
85
// constants
86
/* Averaged over 5 robots, should give real (mm) values to +/-10 millimeters
87

88
   Note the minimum value is .4V (20), and the maximum is 2.6V (133).
89
   Gives distance in mm.
90

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

93
   This is only valid for the GP2D120X, with objects directly ahead and more than
94
   10cm from the detector.  See the datasheet for more information.
95
*/
96
97 1475 dshope
static int IR_dist_conversion[72] PROGMEM = {
98 1455 dshope
        327,315,303,291,281,271,262,253,245,238,231,224,218,212,206,200,
99
        195,190,185,181,177,173,168,165,161,158,155,151,148,145,143,140,
100
        137,134,132,130,127,125,123,121,119,117,115,114,111,110,108,106,
101
        105,104,102,100,99,98,97,95,94,93,91,90,89,88,87,86,84,83,83,82,
102
        81,80,79,78
103
};
104
105
/**
106
 * @defgroup rangefinder Rangefinder
107
 * @brief Functions for using the IR rangefinders
108
 *
109
 * Functions for using the IR rangefinders.
110
 *
111
 * @{
112
 **/
113
114
/**
115
 * Initializes the rangefinders. This must be called before
116
 * range_read_distance.
117
 *
118
 * @see range_read_distance
119
 **/
120 1475 dshope
int range_init(void)
121 1455 dshope
{
122 1475 dshope
  if(range_initd)
123
    return ERROR_INIT_ALREADY_INITD;
124
125 1455 dshope
  digital_output(_PIN_B4,0);
126 1475 dshope
127
  range_initd=1;
128
  return 0;
129 1455 dshope
}
130
131
/**
132
 * Reads the distance measured by one of the rangefinders.
133
 * This distance is in arbitrary units.
134
 *
135
 * @param range_id the rangefinder to use. This should be one
136
 * of the constants IR1 - IR5.
137
 *
138 1475 dshope
 * @return the distance measured by the rangefinder, or an error code
139 1455 dshope
 *
140
 * @see range_init
141
 **/
142
int range_read_distance(int range_id) {
143 1475 dshope
  if(!range_initd)
144
    return -3;
145
146 1455 dshope
  return linearize_distance(analog8(range_id));
147
}
148
149
/**
150
 * Transforms distance readings from logarithmic to linear scale.
151
 * This probably isn't the function you are looking for.
152
 *
153 1475 dshope
 * Note: pgm_read_word() needs additional testing
154
 *
155 1455 dshope
 * @param value the 8-bit analog value from rangefinder
156
 *
157
 * @return linearized distance reading from rangefinder (integer in [78,327])
158 1475 dshope
 * @return (-2) if below MIN_IR_ADC8
159
 * @return (-1) if beyond MAX_IR_ADC8
160 1455 dshope
 **/
161
int linearize_distance(int value) {
162
  if(value < MIN_IR_ADC8) {
163 1475 dshope
    return -2;
164
  } else if(value > MAX_IR_ADC8) {
165 1455 dshope
    return -1;
166
  } else {
167 1475 dshope
    return pgm_read_word(&(IR_dist_conversion[value - MIN_IR_ADC8]));
168 1455 dshope
  }
169
}
170
171 1475 dshope
172 1455 dshope
/** @} **/ //end defgroup