2 * Copyright Droids Corporation (2009)
3 * Olivier MATZ <zer0@droids-corp.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Revision : $Id: sensor.c,v 1.6 2009-11-08 17:25:00 zer0 Exp $
26 #include <aversive/error.h>
29 #include <scheduler.h>
35 #include <control_system_manager.h>
36 #include <blocking_detection_manager.h>
50 int16_t (*filter)(struct adc_infos *, int16_t);
53 /* reach 90% of the value in 4 samples */
54 int16_t rii_light(struct adc_infos *adc, int16_t val)
56 adc->prev_val = val + (int32_t)adc->prev_val / 2;
57 return adc->prev_val / 2;
60 /* reach 90% of the value in 8 samples */
61 int16_t rii_medium(struct adc_infos *adc, int16_t val)
63 adc->prev_val = val + ((int32_t)adc->prev_val * 3) / 4;
64 return adc->prev_val / 4;
67 /* reach 90% of the value in 16 samples */
68 int16_t rii_strong(struct adc_infos *adc, int16_t val)
70 adc->prev_val = val + ((int32_t)adc->prev_val * 7) / 8;
71 return adc->prev_val / 8;
75 #define ADC_CONF(x) ( ADC_REF_AVCC | ADC_MODE_INT | MUX_ADC##x )
77 /* define which ADC to poll, see in sensor.h */
78 static struct adc_infos adc_infos[ADC_MAX] = {
79 [ADC_CSENSE1] = { .config = ADC_CONF(0), .filter = rii_medium },
80 [ADC_CSENSE2] = { .config = ADC_CONF(1), .filter = rii_medium },
81 [ADC_CSENSE3] = { .config = ADC_CONF(2), .filter = rii_medium },
82 [ADC_CSENSE4] = { .config = ADC_CONF(3), .filter = rii_medium },
84 /* add adc on "cap" pins if needed */
85 /* [ADC_CAP1] = { .config = ADC_CONF(10) }, */
86 /* [ADC_CAP2] = { .config = ADC_CONF(11) }, */
87 /* [ADC_CAP3] = { .config = ADC_CONF(12) }, */
88 /* [ADC_CAP4] = { .config = ADC_CONF(13) }, */
91 static void adc_event(int16_t result);
93 /* called every 10 ms, see init below */
94 static void do_adc(__attribute__((unused)) void *dummy)
96 /* launch first conversion */
97 adc_launch(adc_infos[0].config);
100 static void adc_event(int16_t result)
102 static uint8_t i = 0;
104 /* filter value if needed */
105 if (adc_infos[i].filter)
106 adc_infos[i].value = adc_infos[i].filter(&adc_infos[i],
109 adc_infos[i].value = result;
115 adc_launch(adc_infos[i].config);
118 int16_t sensor_get_adc(uint8_t i)
124 tmp = adc_infos[i].value;
129 /************ boolean sensors */
132 struct sensor_filter {
144 static struct sensor_filter sensor_filter[SENSOR_MAX] = {
145 [S_CAP1] = { 10, 0, 3, 7, 0, 0 }, /* 0 */
146 [S_FRONT] = { 5, 0, 4, 1, 0, 0 }, /* 1 */
147 [S_CAP3] = { 10, 0, 3, 7, 0, 0 }, /* 2 */
148 [S_CAP4] = { 1, 0, 0, 1, 0, 0 }, /* 3 */
149 [S_COL_LEFT] = { 5, 0, 4, 1, 0, 1 }, /* 4 */
150 [S_LEFT] = { 5, 0, 4, 1, 0, 1 }, /* 5 */
151 [S_RIGHT] = { 5, 0, 4, 1, 0, 1 }, /* 6 */
152 [S_COL_RIGHT] = { 5, 0, 4, 1, 0, 1 }, /* 7 */
153 [S_RESERVED1] = { 10, 0, 3, 7, 0, 0 }, /* 8 */
154 [S_RESERVED2] = { 10, 0, 3, 7, 0, 0 }, /* 9 */
155 [S_RESERVED3] = { 1, 0, 0, 1, 0, 0 }, /* 10 */
156 [S_RESERVED4] = { 1, 0, 0, 1, 0, 0 }, /* 11 */
157 [S_RESERVED5] = { 1, 0, 0, 1, 0, 0 }, /* 12 */
158 [S_RESERVED6] = { 1, 0, 0, 1, 0, 0 }, /* 13 */
159 [S_RESERVED7] = { 1, 0, 0, 1, 0, 0 }, /* 14 */
160 [S_RESERVED8] = { 1, 0, 0, 1, 0, 0 }, /* 15 */
163 /* value of filtered sensors */
164 static uint16_t sensor_filtered = 0;
167 * 0-3: PORTK 2->5 (cap1 -> cap4) (adc10 -> adc13)
168 * 4-5: PORTL 0->1 (cap5 -> cap6)
169 * 6-7: PORTE 3->4 (cap7 -> cap8)
173 uint16_t sensor_get_all(void)
178 tmp = sensor_filtered;
183 uint8_t sensor_get(uint8_t i)
185 uint16_t tmp = sensor_get_all();
186 return !!(tmp & _BV(i));
189 /* get the physical value of pins */
190 static uint16_t sensor_read(void)
193 tmp |= (uint16_t)((PINK & (_BV(2)|_BV(3)|_BV(4)|_BV(5))) >> 2) << 0;
194 tmp |= (uint16_t)((PINL & (_BV(0)|_BV(1))) >> 0) << 4;
195 tmp |= (uint16_t)((PINE & (_BV(3)|_BV(4))) >> 3) << 6;
196 /* add reserved sensors here */
200 /* called every 10 ms, see init below */
201 static void do_boolean_sensors(__attribute__((unused)) void *dummy)
205 uint16_t sensor = sensor_read();
208 for (i=0; i<SENSOR_MAX; i++) {
209 if ((1 << i) & sensor) {
210 if (sensor_filter[i].cpt < sensor_filter[i].filter)
211 sensor_filter[i].cpt++;
212 if (sensor_filter[i].cpt >= sensor_filter[i].thres_on)
213 sensor_filter[i].prev = 1;
216 if (sensor_filter[i].cpt > 0)
217 sensor_filter[i].cpt--;
218 if (sensor_filter[i].cpt <= sensor_filter[i].thres_off)
219 sensor_filter[i].prev = 0;
222 if (sensor_filter[i].prev && !sensor_filter[i].invert) {
225 else if (!sensor_filter[i].prev && sensor_filter[i].invert) {
230 sensor_filtered = tmp;
236 /************ global sensor init */
238 /* called every 10 ms, see init below */
239 static void do_sensors(__attribute__((unused)) void *dummy)
242 do_boolean_sensors(NULL);
245 void sensor_init(void)
248 adc_register_event(adc_event);
250 scheduler_add_periodical_event_priority(do_sensors, NULL,
251 10000L / SCHEDULER_UNIT,