summaryrefslogtreecommitdiff
path: root/n/es-2007/src/sharp.c
blob: 167b86b71cf58d54168dfcd701b02f553dd8a5c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* sharp.c */
/* es - Input/Output general purpose board. {{{
 *
 * Copyright (C) 2006 Dufour J�r�my
 *
 * Robot APB Team/Efrei 2004.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "sharp.h"

#include "modules/adc/adc.h"
#include "modules/utils/utils.h"

/** Array of sharp values. */
uint16_t sharp_values[SHARP_NUMBER];

/** Thresholds */
uint16_t sharp_threshold_high_[SHARP_NUMBER],
	 sharp_threshold_low_[SHARP_NUMBER];
uint8_t sharp_old_state[SHARP_NUMBER];

/** Init this module. */
void
sharp_init (void)
{
    uint8_t compt;
    /* Init ADC module */
    adc_init ();
    /* All pins are on the right direction, nothing to do */
    for (compt = 0; compt < SHARP_NUMBER; compt++)
      {
	sharp_threshold_high_[compt] = 5000;
	sharp_threshold_low_[compt] = 4000;
      }
}

/** Read data from sharp sensors. */
void
sharp_update_values (uint8_t sharp_num)
{
    /* Check sharp exists (prevent kernel panic) */
    if (sharp_num < SHARP_NUMBER)
      {
	/* Start capture */
	adc_start (sharp_num + 1);
	/* Wait ADC to finish the sensor */
	while (!adc_checkf ())
	    ;
	/* Wait a little */
	utils_delay_us (250);
	/* Get values */
	sharp_values[sharp_num] = adc_read ();
      }
}

/** Analyse a sharp value. */
int8_t
sharp_analyse_values (uint8_t sharp_num)
{
    /* Check sharp exists (prevent kernel panic) */
    if (sharp_num < SHARP_NUMBER)
      {
	// XXX Check more often first
	if (sharp_values[sharp_num] < sharp_threshold_low_[sharp_num])
	  {
	    sharp_old_state[sharp_num] = SHARP_CLEAR;
	    return SHARP_CLEAR;
	  }
	if (sharp_values[sharp_num] > sharp_threshold_high_[sharp_num])
	  {
	    sharp_old_state[sharp_num] = SHARP_OBSTACLE;
	    return SHARP_OBSTACLE;
	  }
	/* If we are here, we are inside the threshold, look at previous value */
	return sharp_old_state[sharp_num];
      }
    return -1;
}

/** Configure sharp threshold. */
void
sharp_config_threshold (uint8_t sharp_num, uint16_t high, uint16_t low)
{
    /* Check sharp exists (prevent kernel panic) */
    if (sharp_num < SHARP_NUMBER)
      {
	sharp_threshold_high_[sharp_num] = high;
	sharp_threshold_low_[sharp_num] = low;
      }
}