summaryrefslogtreecommitdiff
path: root/n/es-2006/src/sensor_rvb.c
blob: ac8b0aad07925e50de4b9a6fc0f5dbc7c3b84455 (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
/* 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 "io.h"
#include "modules/utils/utils.h" /* regv */
#include "modules/uart/uart.h"

/**
 * Somes defines.
 **/
/** Color pins. */
#define S1RVB 7
#define S0RVB 6
/** Max input capture before considering we can start the real capture. */
#define RVB_MAX_FALSE_IC 3
/** 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 input capture */
#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 */

/** 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. */
uint8_t sensor_rvb_overflow_count_;
/** Results table for 9 RVB sensors (RVCB) */
volatile uint16_t sensor_rvb_values[RVB_MAX_SENSOR][4];
/** Geting stats only for one sensors and disabling computing the others. */
uint8_t sensor_rvb_enable;
/** Count the number of IC before starting a good capture. */
uint8_t sensor_rvb_ic_count_;

/** Select a color :
 * 0 : red ;
 * 1 : blue ;
 * 2 : clear ;
 * 3 : green. */
inline uint8_t
sensor_rvb_color_select (uint8_t sensor_rvb_color)
{
    /* Check */
    if (sensor_rvb_color > 3)
	return 0;
    /* Select the color */
    PORTF = (PORTF & ~0xC0) | (sensor_rvb_color << 6);
    /* Wait a little */
    utils_delay_ns (RVB_SENSOR_DELAY_IO);
    return 1;
}

/** 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 (RVB_SENSOR_DELAY_IO);
	return 0;
      }
    /* Wait a little */
    utils_delay_ns (RVB_SENSOR_DELAY_IO);
    return 1;
}

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

/** Initialisation of the RVB sensors module. */
void
sensor_rvb_init (void)
{
    /* We enable the system */
    sensor_rvb_enable = 1;
    /* 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);
    /* Initialisation of counter : 
     * Noise canceler, 1 for prescaling */
    TCCR1B |= _BV(ICNC1) | _BV(CS10);
    /* XXX Fast PWM  10-bit */
    TCCR1B |= _BV(WGM12);
    TCCR1A |= _BV(WGM11) | _BV(WGM10);
    /* XXX Overflow IR */
    TIMSK |= _BV (TOIE1);
    /* Nothing to do for the moment */
    sensor_rvb_state_ = RVB_STATE_SLEEP;
}

/** Start updating RVBC values for sensors. */
void
sensor_rvb_start_capture (void)
{
    /* Are we already getting a sensor ? */
    if (!sensor_rvb_state_ && sensor_rvb_enable)
      {
	sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
	/* Start with sensor one */
	sensor_rvb_number_ = 0;
	/* Select it and launch capture */
	sensor_rvb_update ();
      }
}

/** Watch only one sensor. Usefull for debugging. *//*{{{*/
void
sensor_rvb_watch (uint8_t sensor_num, uint8_t sensor_col)
{
    // XXX Better approach.
    /* Disable all interrupt that could be enabled */
    TIMSK &= ~_BV (TICIE1);
    TIMSK &= ~_BV (TOIE1);
    /* Put ourself in off mode */
    sensor_rvb_enable = 0;
    sensor_rvb_state_ = RVB_STATE_WATCH;
    /* Select the color */
    sensor_rvb_color_select (sensor_rvb_color_ = sensor_col);
    /* Disable sensors */
    sensor_rvb_sensor_select (0);
    /* Wait a little */
    utils_delay_ns (RVB_SENSOR_DELAY_IO);
    /* Select sensor here */
    sensor_rvb_sensor_select (sensor_num + 1);
    /* Wait for output of sensor to be ready */
    utils_delay_ns (RVB_SENSOR_DELAY_IO);
}
/*}}}*/

/** Update everything for you. */
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;
	    /* Enable interrupt for IC1 and counter overflow */
	    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 IC interrupt */
	    TIMSK &= ~_BV (TICIE1);
	    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;
	/* Enable interrupt for IC1 and counter overflow */
	TIMSK |= _BV (TICIE1);
      }
}

/** Timer 1 overflow. */
SIGNAL (SIG_OVERFLOW1)
{
    if ((sensor_rvb_state_ == RVB_STATE_STOP_CAPTURE) &&
	(++sensor_rvb_overflow_count_ == 4))
      {
	/* Disable IC interrupt */
	TIMSK &= ~_BV (TICIE1);
	/* Ask for next sensor */
	sensor_rvb_state_ = RVB_STATE_NEXT_SENSOR;
	sensor_rvb_update ();
      }
}

/** Interrupt on falling edge for Input Capture 1. */
SIGNAL (SIG_INPUT_CAPTURE1)
{
    switch (sensor_rvb_state_)
      {
      case RVB_STATE_WAIT_IC:
	sensor_rvb_ic_count_ = RVB_MAX_FALSE_IC;
	sensor_rvb_state_ = RVB_STATE_WAIT_IC2;
      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;
	/* New capture */
	sensor_rvb_overflow_count_ = 0;
	sensor_rvb_state_ = RVB_STATE_STOP_CAPTURE;
	break;
      case RVB_STATE_STOP_CAPTURE:
	/* Disable IC interrupt */
	TIMSK &= ~_BV (TICIE1);
	/* Compute value */
	sensor_rvb_value_ = ICR1 + sensor_rvb_overflow_count_ * TC1_TOP - 
	    sensor_rvb_value_;
	/* Save */
	sensor_rvb_values[sensor_rvb_number_ - 1][sensor_rvb_color_] = sensor_rvb_value_;
	sensor_rvb_state_ = RVB_STATE_NEXT_COLOR;
	/* Next please */
	sensor_rvb_update ();
	break;
      }
}