remove unneeded files
[aversive.git] / projects / microb2010 / mainboard / sensor.c
1 /*  
2  *  Copyright Droids Corporation (2009)
3  *  Olivier MATZ <zer0@droids-corp.org>
4  * 
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.
9  *
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.
14  *
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
18  *
19  *  Revision : $Id: sensor.c,v 1.8 2009-11-08 17:24:33 zer0 Exp $
20  *
21  */
22
23 #include <stdlib.h>
24
25 #include <aversive.h>
26 #include <aversive/error.h>
27
28 #include <adc.h>
29 #include <scheduler.h>
30 #include <ax12.h>
31 #include <pwm_ng.h>
32
33 #include <pid.h>
34 #include <quadramp.h>
35 #include <control_system_manager.h>
36 #include <trajectory_manager.h>
37 #include <vect_base.h>
38 #include <lines.h>
39 #include <polygon.h>
40 #include <obstacle_avoidance.h>
41 #include <blocking_detection_manager.h>
42 #include <robot_system.h>
43 #include <position_manager.h>
44
45 #include <parse.h>
46 #include <rdline.h>
47
48 #include "main.h"
49 #include "sensor.h"
50
51 /************ ADC */
52
53 struct adc_infos {
54         uint16_t config;
55         int16_t value;
56         int16_t prev_val;
57         int16_t (*filter)(struct adc_infos *, int16_t);
58 };
59
60 /* reach 90% of the value in 4 samples */
61 int16_t rii_light(struct adc_infos *adc, int16_t val)
62 {
63         adc->prev_val = val + (int32_t)adc->prev_val / 2;
64         return adc->prev_val / 2;
65 }
66
67 /* reach 90% of the value in 8 samples */
68 int16_t rii_medium(struct adc_infos *adc, int16_t val)
69 {
70         adc->prev_val = val + ((int32_t)adc->prev_val * 3) / 4;
71         return adc->prev_val / 4;
72 }
73
74 /* reach 90% of the value in 16 samples */
75 int16_t rii_strong(struct adc_infos *adc, int16_t val)
76 {
77         adc->prev_val = val + ((int32_t)adc->prev_val * 7) / 8;
78         return adc->prev_val / 8;
79 }
80
81
82 #define ADC_CONF(x) ( ADC_REF_AVCC | ADC_MODE_INT | MUX_ADC##x )
83
84 /* define which ADC to poll, see in sensor.h */
85 static struct adc_infos adc_infos[ADC_MAX] = { 
86         [ADC_CSENSE1] = { .config = ADC_CONF(0), .filter = rii_medium },
87         [ADC_CSENSE2] = { .config = ADC_CONF(1), .filter = rii_medium },
88         [ADC_CSENSE3] = { .config = ADC_CONF(2), .filter = rii_medium },
89         [ADC_CSENSE4] = { .config = ADC_CONF(3), .filter = rii_medium },
90         [ADC_BATTERY1] = { .config = ADC_CONF(8), .filter = rii_strong },
91         [ADC_BATTERY2] = { .config = ADC_CONF(9), .filter = rii_strong },
92
93         /* add adc on "cap" pins if needed */
94 /*      [ADC_CAP1] = { .config = ADC_CONF(10) }, */
95 /*      [ADC_CAP2] = { .config = ADC_CONF(11) }, */
96 /*      [ADC_CAP3] = { .config = ADC_CONF(12) }, */
97 /*      [ADC_CAP4] = { .config = ADC_CONF(13) }, */
98 };
99
100 static void adc_event(int16_t result);
101
102 /* called every 10 ms, see init below */
103 static void do_adc(void *dummy) 
104 {
105         /* launch first conversion */
106         adc_launch(adc_infos[0].config);
107 }
108
109 static void adc_event(int16_t result)
110 {
111         static uint8_t i = 0;
112
113         /* filter value if needed */
114         if (adc_infos[i].filter)
115                 adc_infos[i].value = adc_infos[i].filter(&adc_infos[i],
116                                                          result);
117         else
118                 adc_infos[i].value = result;
119
120         i ++;
121         if (i >= ADC_MAX)
122                 i = 0;
123         else
124                 adc_launch(adc_infos[i].config);
125 }
126
127 int16_t sensor_get_adc(uint8_t i)
128 {
129         int16_t tmp;
130         uint8_t flags;
131
132         IRQ_LOCK(flags);
133         tmp = adc_infos[i].value;
134         IRQ_UNLOCK(flags);
135         return tmp;
136 }
137
138 /************ boolean sensors */
139
140
141 struct sensor_filter {
142         uint8_t filter;
143         uint8_t prev;
144         uint8_t thres_off;
145         uint8_t thres_on;
146         uint8_t cpt;
147         uint8_t invert;
148 };
149
150 /* pullup mapping:
151  * CAP 1,5,6,7,8
152  */
153 static struct sensor_filter sensor_filter[SENSOR_MAX] = {
154         [S_CAP1] = { 1, 0, 0, 1, 0, 0 }, /* 4 */
155         [S_CAP2] = { 1, 0, 0, 1, 0, 0 }, /* 1 */
156         [S_COLUMN_LEFT] = { 1, 0, 0, 1, 0, 1 }, /* 2 */
157         [S_COLUMN_RIGHT] = { 1, 0, 0, 1, 0, 1 }, /* 3 */
158         [S_START_SWITCH] = { 10, 0, 3, 7, 0, 0 }, /* 0 */
159         [S_DISP_LEFT] = { 1, 0, 0, 1, 0, 1 }, /* 5 */
160         [S_DISP_RIGHT] = { 1, 0, 0, 1, 0, 1 }, /* 6 */
161         [S_CAP8] = { 1, 0, 0, 1, 0, 0 }, /* 7 */
162         [S_RESERVED1] = { 10, 0, 3, 7, 0, 0 }, /* 8 */
163         [S_RESERVED2] = { 10, 0, 3, 7, 0, 0 }, /* 9 */
164         [S_RESERVED3] = { 1, 0, 0, 1, 0, 0 }, /* 10 */
165         [S_RESERVED4] = { 1, 0, 0, 1, 0, 0 }, /* 11 */
166         [S_RESERVED5] = { 1, 0, 0, 1, 0, 0 }, /* 12 */
167         [S_RESERVED6] = { 1, 0, 0, 1, 0, 0 }, /* 13 */
168         [S_RESERVED7] = { 1, 0, 0, 1, 0, 0 }, /* 14 */
169         [S_RESERVED8] = { 1, 0, 0, 1, 0, 0 }, /* 15 */
170 };
171
172 /* value of filtered sensors */
173 static uint16_t sensor_filtered = 0;
174
175 /* sensor mapping : 
176  * 0-3:  PORTK 2->5 (cap1 -> cap4) (adc10 -> adc13)
177  * 4-5:  PORTL 0->1 (cap5 -> cap6)
178  * 6-7:  PORTE 3->4 (cap7 -> cap8)
179  * 8-15: reserved
180  */
181
182 uint16_t sensor_get_all(void)
183 {
184         uint16_t tmp;
185         uint8_t flags;
186         IRQ_LOCK(flags);
187         tmp = sensor_filtered;
188         IRQ_UNLOCK(flags);
189         return tmp;
190 }
191
192 uint8_t sensor_get(uint8_t i)
193 {
194         uint16_t tmp = sensor_get_all();
195         return (tmp & _BV(i));
196 }
197
198 /* get the physical value of pins */
199 static uint16_t sensor_read(void)
200 {
201         uint16_t tmp = 0;
202         tmp |= (uint16_t)((PINK & (_BV(2)|_BV(3)|_BV(4)|_BV(5))) >> 2) << 0;
203         tmp |= (uint16_t)((PINL & (_BV(0)|_BV(1))) >> 0) << 4;
204         tmp |= (uint16_t)((PINE & (_BV(3)|_BV(4))) >> 3) << 6;
205         /* add reserved sensors here */
206         return tmp;
207 }
208
209 /* called every 10 ms, see init below */
210 static void do_boolean_sensors(void *dummy)
211 {
212         uint8_t i;
213         uint8_t flags;
214         uint16_t sensor = sensor_read();
215         uint16_t tmp = 0;
216
217         for (i=0; i<SENSOR_MAX; i++) {
218                 if ((1 << i) & sensor) {
219                         if (sensor_filter[i].cpt < sensor_filter[i].filter)
220                                 sensor_filter[i].cpt++;
221                         if (sensor_filter[i].cpt >= sensor_filter[i].thres_on)
222                                 sensor_filter[i].prev = 1;
223                 }
224                 else {
225                         if (sensor_filter[i].cpt > 0)
226                                 sensor_filter[i].cpt--;
227                         if (sensor_filter[i].cpt <= sensor_filter[i].thres_off)
228                                 sensor_filter[i].prev = 0;
229                 }
230                 
231                 if (sensor_filter[i].prev) {
232                         tmp |= (1UL << i);
233                 }
234         }
235         IRQ_LOCK(flags);
236         sensor_filtered = tmp;
237         IRQ_UNLOCK(flags);
238 }
239
240 /* virtual obstacle */
241
242 #define DISABLE_CPT_MAX 200
243 static uint8_t disable = 0; /* used to disable obstacle detection 
244                            * during some time */
245
246 /* called every 10 ms */
247 void
248 sensor_obstacle_update(void)
249 {
250         if (disable > 0) {
251                 disable --;
252                 if (disable == 0)
253                         DEBUG(E_USER_STRAT, "re-enable sensor");
254         }
255 }
256
257 void sensor_obstacle_disable(void)
258 {
259         DEBUG(E_USER_STRAT, "disable sensor");
260         disable = DISABLE_CPT_MAX;
261 }
262
263 void sensor_obstacle_enable(void)
264 {
265         disable = 0;
266 }
267
268 uint8_t sensor_obstacle_is_disabled(void)
269 {
270         return disable;
271 }
272
273
274 /************ global sensor init */
275
276 /* called every 10 ms, see init below */
277 static void do_sensors(void *dummy)
278 {
279         do_adc(NULL);
280         do_boolean_sensors(NULL);
281         sensor_obstacle_update();
282 }
283
284 void sensor_init(void)
285 {
286         adc_init();
287         adc_register_event(adc_event);
288         /* CS EVENT */
289         scheduler_add_periodical_event_priority(do_sensors, NULL, 
290                                                 10000L / SCHEDULER_UNIT, 
291                                                 ADC_PRIO);
292 }
293