root / branches / simulator / projects / simulator / libsim / rangefinder.c @ 1094
History | View | Annotate | Download (3.2 KB)
1 |
/**
|
---|---|
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 |
#include "rangefinder.h" |
27 |
#include "util.h" |
28 |
#include "robot_shared.h" |
29 |
|
30 |
extern RobotShared* shared_state;
|
31 |
|
32 |
/*
|
33 |
read_distance returns the 8-bit reading from the rangefinder
|
34 |
parameters:
|
35 |
range_id - dio pin set as the rangefinder Vout [i.e. RANGE_IN0]
|
36 |
|
37 |
NOTE:
|
38 |
The Sharp GD2D02 returns values on a decreasing logrithmic scale.
|
39 |
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.
|
40 |
At this point, we are only reading from one rangefinder [RANGE_IN0].
|
41 |
*/
|
42 |
|
43 |
// constants
|
44 |
/* Nasty IR approximation table
|
45 |
I'm using this for the heck of it. We can do whatever.
|
46 |
|
47 |
Note the minimum value is .4V (20), and the maximum is 2.6V (133).
|
48 |
Gives distance in mm.
|
49 |
|
50 |
excel formula(valid for inputs 20-133): ROUND(2353.6*(E2^(-1.1146))*10,0)
|
51 |
|
52 |
This is only valid for the GP2D12, with objects directly ahead and more than
|
53 |
10cm from the detector. See the datasheet for more information.
|
54 |
*/
|
55 |
|
56 |
#define MIN_IR_ADC8 20 |
57 |
#define MAX_IR_ADC8 133 |
58 |
|
59 |
// hacky conversion
|
60 |
#define IR_MAX 800 |
61 |
#define IR_MIN 100 |
62 |
#define IR_CONVERSION_FACTOR 2.0 |
63 |
|
64 |
void range_init(void) |
65 |
{ |
66 |
/* simulator does not need this */
|
67 |
} |
68 |
|
69 |
// should return value between 101 and 800
|
70 |
int range_read_distance (int range_id) |
71 |
{ |
72 |
short temp;
|
73 |
short ir_value; // simulate what libdragonfly rangefiner returns |
74 |
/* read from array in shared memory, if valid */
|
75 |
/* TODO: fix numbering in libdragonfly then fix it here */
|
76 |
switch (range_id) {
|
77 |
case IR1:
|
78 |
temp = (shared_state->ranges).d[0];
|
79 |
break;
|
80 |
case IR2:
|
81 |
temp = (shared_state->ranges).d[1];
|
82 |
break;
|
83 |
case IR3:
|
84 |
temp = (shared_state->ranges).d[2];
|
85 |
break;
|
86 |
case IR4:
|
87 |
temp = (shared_state->ranges).d[3];
|
88 |
break;
|
89 |
case IR5:
|
90 |
temp = (shared_state->ranges).d[4];
|
91 |
break;
|
92 |
} |
93 |
ir_value = ((temp / IR_CONVERSION_FACTOR) + IR_MIN); |
94 |
ir_value = ir_value > IR_MAX ? IR_MAX : ir_value; |
95 |
printf("[%d]: %d\n", range_id, ir_value);
|
96 |
if (temp >= 0 && temp <= 1000) return ir_value; |
97 |
return -1; |
98 |
} |
99 |
|