summaryrefslogtreecommitdiff
path: root/n/es-2006/src/sniff_rvb.c
blob: 5941316d66c689ccf8c7583ae03d7deb026719da (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* sniff_rvb.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 "sniff_rvb.h"

#include "io.h"
#include "string.h"
#include "common.h"
#include "modules/proto/proto.h"
#include "modules/utils/byte.h"

#include "sensor_rvb.h"

/** Reference color. */
uint16_t sniff_rvb_reference_color[RVB_MAX_SENSOR][RVB_SNIFF_MAX_INDEX];
/** Threshold table data. */
uint16_t sniff_rvb_threshold[2][RVB_MAX_SENSOR][RVB_SNIFF_MAX_INDEX];
/** Used to build threshold */
// XXX only bool needed, use bit operation if we need to use less space
uint8_t sniff_rvb_sign[2][RVB_MAX_SENSOR][RVB_SNIFF_MAX_INDEX];
/** Ratio min max */
uint16_t sniff_rvb_reference_color_min[RVB_MAX_SENSOR][RVB_SNIFF_MAX_INDEX];
uint16_t sniff_rvb_reference_color_max[RVB_MAX_SENSOR][RVB_SNIFF_MAX_INDEX];
/** Ratio for the computing the min/max value from the reference color. */
uint8_t sniff_rvb_ref_ratio = 3;

/** Configure the sensor analysis system. */
void
sniff_rvb_config (uint8_t ref_ratio)
{
    sniff_rvb_ref_ratio = ref_ratio;
}
/** Try to reference the sensor_num sensor into the reference color. */
inline uint8_t
sniff_rvb_try_reference (uint8_t sensor_num, uint16_t
			 ref[RVB_MAX_SENSOR][RVB_SNIFF_MAX_INDEX])
{
    uint8_t compt;
    /* If sensor is valid */
    if (sensor_rvb_values[sensor_num][0] != 0)
      {
	/* For each color add this value by averrage */
	for (compt = 0; compt < RVB_SNIFF_MAX_INDEX; compt++)
	    if (ref[sensor_num][compt] == 0) 
	      {
		ref[sensor_num][compt] =
		    sensor_rvb_values[sensor_num][compt];
	      }
	    else
	      {
		ref[sensor_num][compt] =
		    (sensor_rvb_values[sensor_num][compt] / 2
		     + ref[sensor_num][compt] / 2);
	      }
	return !0;
      }
    return 0;
}


/* Test blue or red */
uint8_t 
sniff_rvb_analysis_color_other (uint8_t sensor, uint8_t color, 
				uint16_t threshold[RVB_SNIFF_MAX_INDEX],
				uint8_t sign[RVB_SNIFF_MAX_INDEX])
{
    uint8_t compt;
    uint8_t what_is_it = color;
    for (compt = 0; compt < RVB_SNIFF_MAX_INDEX; compt++)
      {
	// if relative threshold is positive, current value must be greater
	// than the threshold
	if (sign[compt] > 0)
	  {
	    if (sensor_rvb_values[sensor][compt] > threshold[compt])
	      {
		continue;
	      }
	  }
	// if negative, must be lower
	else
	  {
	    if (sensor_rvb_values[sensor][compt] < threshold[compt])
	      {
		continue;
	      }
	  }
	// if conditions not true, color is not blue
	what_is_it = RVB_SNIFF_OTHER;
	break;
      }
    return what_is_it;
}

/** Analysis a color */
uint8_t
sniff_rvb_analysis_color (uint8_t sensor, uint8_t mode)
{
    static uint8_t hysteresis[RVB_MAX_SENSOR] = {RVB_SNIFF_GREEN};
    uint8_t compt, what_is_it = RVB_SNIFF_GREEN;
    /* test for green */
    for (compt = 0; compt < RVB_SNIFF_MAX_INDEX; compt++)
      {
	if ((sensor_rvb_values[sensor][compt] < sniff_rvb_reference_color_min[sensor][compt]) 
	    || (sensor_rvb_values[sensor][compt] > sniff_rvb_reference_color_max[sensor][compt]))
	  {
	    what_is_it = RVB_SNIFF_OTHER;
	    break;
	  }
      }
    
    if ((what_is_it == RVB_SNIFF_GREEN) || (mode == RVB_SNIFF_ONLY_GREEN))
	goto sniff_rvb_set_ref_color_end;
    
    /* test for blue */
    if (sniff_rvb_analysis_color_other (sensor, RVB_SNIFF_BLUE, 
					sniff_rvb_threshold[RVB_THRESHOLD_BLUE][sensor],
					sniff_rvb_sign[RVB_THRESHOLD_BLUE][sensor]) == RVB_SNIFF_BLUE)
      {
	what_is_it = RVB_SNIFF_BLUE;
	goto sniff_rvb_set_ref_color_end;
      }
    /* test for red */
    if (sniff_rvb_analysis_color_other (sensor, RVB_SNIFF_RED,
					sniff_rvb_threshold[RVB_THRESHOLD_RED][sensor],
					sniff_rvb_sign[RVB_THRESHOLD_RED][sensor]) == RVB_SNIFF_RED)
      {
	what_is_it = RVB_SNIFF_RED;
	goto sniff_rvb_set_ref_color_end;
      }
    /* else other */
    what_is_it = RVB_SNIFF_OTHER;
    
/* sometimes it's goooood ! */
sniff_rvb_set_ref_color_end:
    if (what_is_it == hysteresis[sensor])
	return what_is_it;
    else 
      {
	hysteresis[sensor] = what_is_it;
	return RVB_SNIFF_GREEN;
      }
}


/* sub function of sniff_rvb_set_ref_color */
void
sniff_rvb_set_ref_color_green (uint8_t sensor)
{
    uint8_t compt;
    if (sniff_rvb_try_reference (sensor, sniff_rvb_reference_color))
	for (compt = 0; compt < RVB_SNIFF_MAX_INDEX; compt++)
	  {
	    sniff_rvb_reference_color_min[sensor][compt] =
		sniff_rvb_reference_color[sensor][compt] -
		(sniff_rvb_reference_color[sensor][compt] / sniff_rvb_ref_ratio);
	    sniff_rvb_reference_color_max[sensor][compt] =
		sniff_rvb_reference_color[sensor][compt] +
		(sniff_rvb_reference_color[sensor][compt] / sniff_rvb_ref_ratio);
	  }
}

/* sub function of sniff_rvb_set_ref_color */
/* XXX call only after the reference color initialization has been done */
void
sniff_rvb_set_ref_color_other (uint8_t sensor, uint8_t color)
{
    uint8_t compt;
    uint16_t *threshold;
    uint8_t *sign;
    if (color == RVB_SNIFF_BLUE)
      {
	threshold = sniff_rvb_threshold[RVB_THRESHOLD_BLUE][sensor];
	sign = sniff_rvb_sign[RVB_THRESHOLD_BLUE][sensor];
      }
    else
      {
	threshold = sniff_rvb_threshold[RVB_THRESHOLD_RED][sensor];
	sign = sniff_rvb_sign[RVB_THRESHOLD_RED][sensor];
      }
    /* get sensor values */
    /* test if valid */
    if (sensor_rvb_values[sensor][0] != 0)
 	for (compt = 0; compt < RVB_SNIFF_MAX_INDEX; compt++)
 	  { 
	    /* compute threshold */
	    int16_t tmp = (sensor_rvb_values[sensor][compt] 
			   - sniff_rvb_reference_color[sensor][compt]) / 2;
	    // XXX use ratio instead of 2
 	    threshold[compt] = tmp + sniff_rvb_reference_color[sensor][compt];
	    /* store sign for faster processing later */
 	    if (tmp > 0)
 		sign[compt] = RVB_THRESHOLD_POSITIVE;
 	    else
 		sign[compt] = RVB_THRESHOLD_NEGATIVE;
 	  }
}

/** Set the current color seen by some sensors as the reference color
 * and do some cache creation for preventing useless computing. */
void
sniff_rvb_set_ref_color (uint8_t sensor, uint8_t color)
{
    switch (color) 
      {
      case RVB_SNIFF_GREEN:
	sniff_rvb_set_ref_color_green (sensor);
	break;
      case RVB_SNIFF_RED:
      case RVB_SNIFF_BLUE:
	sniff_rvb_set_ref_color_other (sensor, color);
	break;
      }
}

/** Set the threshold of the differents colors. */
void
sniff_rvb_set_threshold (uint8_t color, int16_t red_ts, int16_t blue_ts,
			 int16_t clear_ts, int16_t green_ts)
{
    // XXX no green needed
    switch (color)
      {
      case RVB_SNIFF_RED:
      case RVB_SNIFF_BLUE:
      case RVB_SNIFF_GREEN:
// 	sniff_rvb_thresholdtable[color][0] = red_ts;
// 	sniff_rvb_thresholdtable[color][1] = blue_ts;
// 	sniff_rvb_thresholdtable[color][2] = clear_ts;
// 	sniff_rvb_thresholdtable[color][3] = green_ts;
 	break;
      }
}