summaryrefslogtreecommitdiff
path: root/n/es-2006/src/sensor_rvb.c
blob: f53c3244d06218ce125ee68e46e24606dcc57be6 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* sensor_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 "sensor_rvb.h"
#include "timer_1.h"

#include "io.h"
#include "modules/utils/utils.h" /* regv */
#include "modules/proto/proto.h"

#include "sniff_rvb.h"

/**
 * Somes defines.
 **/
/** Colors selection pins. */
#define S1RVB 7
#define S0RVB 6
/** Max input capture before considering we can start the real capture.
 * (default value) */
#define RVB_MAX_FALSE_IC 9
/** Max overflow of the timer 1 before thinking the sensor is HS. */
/* TODO Find a way to compute this value */
#define RVB_MAX_OVERFLOW 253
/** Wait time between IO change in ns. */
#define RVB_SENSOR_DELAY_IO 125
/** All the possible states. */
#define RVB_STATE_SLEEP 	0  /* Doing nothing */
#define RVB_STATE_NEXT_SENSOR	1  /* Selecting a sensor */
#define RVB_STATE_WAIT_IC	20 /* Waiting for the first input capture */
#define RVB_STATE_WAIT_IC2	(RVB_STATE_WAIT_IC + 1) /* 2nd and more false
							   input captures */
#define RVB_STATE_START_CAPTURE	2  /* After waiting enough IC, starting capture */
#define RVB_STATE_STOP_CAPTURE	3  /* We can compute a value */
#define RVB_STATE_NEXT_COLOR 	4  /* Selecting another color */
#define RVB_STATE_WATCH 	5  /* For debuging */


/*** Config ***/
/** Max input capture before considering we can start the real capture. */
uint8_t sensor_rvb_conf_max_false_ic_;
/** Max overflow of the timer 1 before thinking the sensor is HS. */
uint16_t sensor_rvb_conf_max_ov_;
/** Wait time between IO change in ns. */
uint8_t sensor_rvb_conf_delay_;
/** RVB sensor state flag. */
volatile uint8_t sensor_rvb_state_;
/** Internal computed result for one color. */
volatile uint16_t sensor_rvb_value_;
/** Sensor used actually for computing value. */
volatile uint8_t sensor_rvb_number_;
/** Color of the actual sensor. */
volatile uint8_t sensor_rvb_color_;
/** Number of overflow interruption since last edge change. */
volatile uint8_t sensor_rvb_overflow_count_;
/** Results table for 9 RVB sensors (RVCB) */
volatile uint16_t sensor_rvb_values[RVB_MAX_SENSOR][4];
/** Count the number of IC before starting a good capture. */

uint8_t sensor_rvb_ic_count_;

/** Update everything for you. */
inline void sensor_rvb_update (void);

/** Select a color :
 * 0 : red ;
 * 1 : blue ;
 * 2 : clear ;
 * 3 : green. */
inline uint8_t
sensor_rvb_color_select (uint8_t sensor_rvb_color)
{
    switch (sensor_rvb_color)
      {
      case RVB_INDEX_RED:
      case RVB_INDEX_BLUE:
      case RVB_INDEX_CLEAR:
      case RVB_INDEX_GREEN:
	/* Select the color */
	PORTF = (PORTF & ~0xC0) | (sensor_rvb_color << 6);
	/* Wait a little */
	utils_delay_ns (sensor_rvb_conf_delay_);
	/* Color exists */
	return 1;
      default:
	/* Error */
	return 0;
      }
}

/** Enable a RVB sensor. */
inline uint8_t
sensor_rvb_sensor_select (uint8_t sensor_rvb_num)
{
    switch (sensor_rvb_num)
      {
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
	PORTC = ~_BV(sensor_rvb_num);
	break;
      case 8:
	PORTD &= ~_BV(6);
	break;
      case 9:
	PORTC = ~_BV(0);
	break;
      default:
	/* High for disable */
	PORTC = 0xff;
	PORTD |= _BV(6);
	/* Wait a little */
	utils_delay_ns (sensor_rvb_conf_delay_);
	return 0;
      }
    /* Wait a little */
    utils_delay_ns (sensor_rvb_conf_delay_);
    return 1;
}

/** Initialisation of the RVB sensors module. */
void
sensor_rvb_init (void)
{
    /* Init config */
    sensor_rvb_conf_max_false_ic_ = RVB_MAX_FALSE_IC;
    sensor_rvb_conf_max_ov_ = RVB_MAX_OVERFLOW;
    sensor_rvb_conf_delay_ = RVB_SENSOR_DELAY_IO;
    /* No sensor selected */
    sensor_rvb_sensor_select (0);
    /* Put ENA* pins in output */
    DDRC = 0xff;
    DDRD |= _BV(6);
    /* Put color selector pins in output */
    DDRF |= _BV(S0RVB) | _BV(S1RVB);
    /* Put asserv color states pins in output */
    DDRA |= _BV(7) | _BV(6) | _BV(5) | _BV(4);
    /* Nothing to do for the moment */
    sensor_rvb_state_ = RVB_STATE_SLEEP;
}

/** Configure some internal variables. */
void
sensor_rvb_config (uint8_t false_ic, uint16_t max_ov, uint8_t latency)
{
    sensor_rvb_conf_max_false_ic_ = false_ic;
    sensor_rvb_conf_max_ov_ = max_ov;
    sensor_rvb_conf_delay_ = latency;
}

/** Start updating RVBC values for sensors. */
void
sensor_rvb_start_capture (void)
{
    /* Are we already getting a sensor ? */
    if (sensor_rvb_state_ == RVB_STATE_SLEEP)
      {
	sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
	/* Start with sensor one */
	sensor_rvb_number_ = 0;
	/* Select it and launch capture */
	sensor_rvb_update ();
	/* Enable interrupt for IC1 and counter overflow */
	TIMSK |= _BV (TICIE1);
	TIMSK |= _BV (TOIE1);
      }
}

/** Update everything for you. */
inline void
sensor_rvb_update (void)
{
    if (sensor_rvb_state_ == RVB_STATE_NEXT_COLOR)
      {
	/* Select the color */
	if (!sensor_rvb_color_select (++sensor_rvb_color_))
	  {
	    /* No more colors for this sensor, next sensor please */
	    sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
	  }
	else
	  {
	    sensor_rvb_state_ = RVB_STATE_WAIT_IC;
	    /* Ensure we have no pending interrupt for IC1. */
// 	    TIFR = _BV (ICF1);
	    /* Enable interrupt for IC1 */
	    TIMSK |= _BV (TICIE1);
	  }
      }
    if (sensor_rvb_state_ == RVB_STATE_NEXT_SENSOR)
      {
	/* Disable sensors */
	sensor_rvb_sensor_select (0);
	/* Select sensor */
	if (!sensor_rvb_sensor_select (++sensor_rvb_number_))
	  {
	    /* Disable IC1 and TC1 overflow interrupts */
	    TIMSK &= ~_BV (TICIE1);
	    TIMSK &= ~_BV (TOIE1);
	    /* Ensure we have no pending interrupt for IC1. */
// 	    TIFR = _BV (ICF1);
	    /* Finish ! Go to sleep */
	    sensor_rvb_state_ = RVB_STATE_SLEEP;
	    return;
	  }
	/* Start with first color */
	sensor_rvb_color_select (sensor_rvb_color_ = 0);
	sensor_rvb_state_ = RVB_STATE_WAIT_IC;
	sensor_rvb_overflow_count_ = 0;
// 	/* Ensure we have no pending interrupt for IC1. */
// 	TIFR = _BV (ICF1);
	/* Enable interrupt for IC1 */
	TIMSK |= _BV (TICIE1);
      }
}

/** Timer 1 overflow. */
SIGNAL (SIG_OVERFLOW1)
{
    switch (sensor_rvb_state_)
      {
      case RVB_STATE_STOP_CAPTURE:
	++sensor_rvb_overflow_count_;
	break;
      case RVB_STATE_WAIT_IC:
      case RVB_STATE_WAIT_IC2:
	// XXX >= Sucks !
	// TODO : check this >= and the IC2 used
	if (++sensor_rvb_overflow_count_ >= sensor_rvb_conf_max_ov_)
	  {
	    /* Invalidate the sensor */
	    sensor_rvb_values[sensor_rvb_number_ - 1][0] = 0;
	    /* Disable IC interrupt */
	    TIMSK &= ~_BV (TICIE1);
	    /* Ask for next sensor */
	    sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
	    sensor_rvb_update ();
	  }
	break;
      }
}

/** Interrupt on falling edge for Input Capture 1. */
SIGNAL (SIG_INPUT_CAPTURE1)
{
    uint16_t temp;
    switch (sensor_rvb_state_)
      {
      case RVB_STATE_WAIT_IC:
	sensor_rvb_ic_count_ = sensor_rvb_conf_max_false_ic_;
	sensor_rvb_state_ = RVB_STATE_WAIT_IC2;
	/* nobreak */
      case RVB_STATE_WAIT_IC2:
	if (!--sensor_rvb_ic_count_)
	    /* Last ignored capture */
	    sensor_rvb_state_ = RVB_STATE_START_CAPTURE;
	break;
      case RVB_STATE_START_CAPTURE:
	/* Save capture start */
	sensor_rvb_value_ = ICR1;
	/* Reset overflow_count */
	sensor_rvb_overflow_count_ = 0;
	/* New capture */
	sensor_rvb_state_ = RVB_STATE_STOP_CAPTURE;
	break;
      case RVB_STATE_STOP_CAPTURE:
	/* Save it earlier ! */
	temp = ICR1;
	/* Compute value */
	sensor_rvb_values[sensor_rvb_number_ - 1][sensor_rvb_color_] = temp +
	    sensor_rvb_overflow_count_ * TC1_TOP - sensor_rvb_value_;
	/* Disable IC interrupt */
	TIMSK &= ~_BV (TICIE1);
	sensor_rvb_state_ = RVB_STATE_NEXT_COLOR;
	/* Next please */
	sensor_rvb_update ();
	break;
      }
}

/** Update pins connected to the asserv AVR with the colors seen by the
 * sensors. */
void
sensor_rvb_update_asserv_pins (void)
{
   uint8_t compt;

   for (compt = 0; compt < 4; compt++)
     {
       if (sniff_rvb_analysis_color (compt, 0))
	   PORTA |= _BV (compt + 4);
       else
	   PORTA &= ~_BV (compt + 4);
     }

    // TODO:
    //  - called the desired sensors with sensor_rvb_analysis_color ;
    //  - choose with Ni ;
    //  - do we need to update all the pins ?
    //  - this functions have to be hightly configurable : for example, choose
    //  if we can select frequency of update ;
    //  - manage a case for knowing the mode of the robot in order to select
    //  which pins we should update.
}