Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / simulator / libsim / rangefinder.c @ 1008

History | View | Annotate | Download (2.58 KB)

1 941 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
#include "rangefinder.h"
27
#include "util.h"
28 1008 dsschult
#include "robot_shared.h"
29 941 bcoltin
30 1008 dsschult
extern RobotShared* shared_state;
31
32 941 bcoltin
/*
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
void range_init(void)
60
{
61 1008 dsschult
        /* simulator does not need this */
62 941 bcoltin
}
63
64
int range_read_distance (int range_id)
65
{
66 1008 dsschult
        int temp=0;
67
        /* read from array in shared memory, if valid */
68
        if (range_id > 0 && range_id < 6) {
69
                temp = (shared_state->ranges).d[range_id];
70
                if (temp < 0 || temp > 1000)
71
                        return -1;
72
                else
73
                        return temp;
74
        }
75 942 bcoltin
        return -1;
76 941 bcoltin
}