Project

General

Profile

Statistics
| Revision:

root / demos / joystick / lib / src / libdragonfly / rangefinder.c @ 1736

History | View | Annotate | Download (4.68 KB)

1 241 bcoltin
/**
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 380 jknichel
  Authors: James Kong and Greg Tress
38 8 bcoltin

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

43 380 jknichel
  -----------------
44
  rangefinder.c
45
  Using Sharp GP2D02 IR Rangefinder
46 8 bcoltin

47 380 jknichel
  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 380 jknichel
  Expected Initial Conditions:
51
  Vin is high and Vout should read high.
52 8 bcoltin

53 380 jknichel
  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 1460 dsschult
#include <avr/pgmspace.h>
61 8 bcoltin
62 1496 jsexton
#include "rangefinder.h"
63 1452 dsschult
#include "analog.h"
64
#include "dio.h"
65 8 bcoltin
66
/*
67 380 jknichel
  read_distance returns the 8-bit reading from the rangefinder
68
  parameters:
69
  range_id - dio pin set as the rangefinder Vout [i.e. RANGE_IN0]
70 8 bcoltin

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

82 380 jknichel
   Note the minimum value is .4V (20), and the maximum is 2.6V (133).
83
   Gives distance in mm.
84 8 bcoltin

85 380 jknichel
   excel formula(valid for inputs 20-133):  ROUND(2353.6*(E2^(-1.1146))*10,0)
86 8 bcoltin

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