beacon from 2009
[aversive.git] / projects / microb2010 / ballboard / beacon.c
1
2 #include <stdio.h>
3 #include <string.h>
4 #include <math.h>
5
6 #include <aversive.h>
7 #include <aversive/pgmspace.h>
8 #include <aversive/wait.h>
9 #include <aversive/error.h>
10
11 #include <uart.h>
12 #include <i2c.h>
13 #include <ax12.h>
14 #include <parse.h>
15 #include <rdline.h>
16 #include <pwm_ng.h>
17 #include <encoders_spi.h>
18 #include <timer.h>
19 #include <scheduler.h>
20 #include <pid.h>
21 #include <clock_time.h>
22 #include <quadramp.h>
23 #include <control_system_manager.h>
24 #include <adc.h>
25 #include <spi.h>
26
27 #include <blocking_detection_manager.h>
28
29 #include "sensor.h"
30
31 #include "../common/i2c_commands.h"
32 #include "main.h"
33 #include "beacon.h"
34
35 struct beacon beacon;
36
37 #define BEACON_PWM_VALUE 1000
38 #define IR_SENSOR() (!!(PINK&(1<<5)))
39 #define MODULO_TIMER (1023L)
40 #define COEFF_TIMER (2)
41 #define COEFF_MULT (100L)
42 #define COEFF_MULT2 (1000L)
43 #define BEACON_SIZE (9)
44 #define BEACON_MAX_SAMPLE (3)
45
46 #define OPPONENT_POS_X (11)
47 #define OPPONENT_POS_Y (22)
48
49 #define BEACON_DEBUG(args...) DEBUG(E_USER_BEACON, args)
50 #define BEACON_NOTICE(args...) NOTICE(E_USER_BEACON, args)
51 #define BEACON_ERROR(args...) ERROR(E_USER_BEACON, args)
52
53 static volatile int32_t rising = -1;
54 static volatile int32_t falling = -1;
55
56 static int32_t get_dist(float size);
57 static int32_t get_angle(int32_t middle, int32_t ref);
58
59 static int32_t pos_ref = 0;
60 static int32_t invalid_count = 0;
61
62 static volatile int32_t beacon_speed;
63 static volatile int32_t beacon_save_count = 0;
64 static volatile int32_t beacon_prev_save_count = 0;
65 static volatile int32_t count = 0;
66 static volatile int32_t count_diff_rising  = 0;
67 static volatile int32_t count_diff_falling = 0;
68 static int32_t beacon_coeff = 0;
69
70 static volatile int8_t valid_beacon = 0;
71
72 static volatile int32_t beacon_pos;
73
74 //static int32_t beacon_sample_size[BEACON_MAX_SAMPLE];
75
76 int32_t encoders_spi_get_value_beacon(void *number)
77 {
78         int32_t ret;
79
80         ret = encoders_spi_get_value(number);
81         return ret*4;
82 }
83
84 void encoders_spi_set_value_beacon(void * number, int32_t v)
85 {
86         encoders_spi_set_value(number, v/4);
87 }
88
89 int32_t encoders_spi_update_beacon_speed(void * number)
90 {
91         int32_t ret;
92         uint8_t flags;
93
94         IRQ_LOCK(flags);
95         ret = encoders_spi_get_value_beacon(number);
96         beacon_speed = ret - beacon_pos;
97         beacon_pos = ret;
98         beacon_prev_save_count = beacon_save_count;
99         beacon_save_count = TCNT3;
100         IRQ_UNLOCK(flags);
101
102         beacon_coeff = COEFF_TIMER  * COEFF_MULT;//beacon_speed * COEFF_MULT / ((beacon_prev_save_count - beacon_save_count + MODULO_TIMER + 1)&MODULO_TIMER);
103
104         return beacon_speed;
105 }
106
107
108 void beacon_init(void)
109 {
110         //int8_t i;
111
112         beacon_reset_pos();
113         pos_ref = encoders_spi_get_value_beacon(BEACON_ENCODER);
114
115         memset(&beacon, 0, sizeof(struct beacon));
116         beacon.opponent_x = I2C_OPPONENT_NOT_THERE;
117
118         beacon_speed = 0;
119
120         /*for(i=0;i<BEACON_MAX_SAMPLE;i++)
121                 beacon_sample_size[i] = 0;*/
122
123         /* set external interrupt (any edge) */
124         PCMSK2 = (1<<PCINT21);
125         PCICR  = (1<<PCIE2);
126
127
128 }
129
130 void beacon_calibre_pos(void)
131 {
132         ballboard.flags &= ~DO_CS;
133
134         /* init beacon pos */
135         pwm_ng_set(BEACON_PWM, 100);
136
137         /* find rising edge of the mirror*/
138         wait_ms(100);
139         while (sensor_get(BEACON_POS_SENSOR));
140         wait_ms(100);
141         while (!sensor_get(BEACON_POS_SENSOR));
142
143         pwm_ng_set(BEACON_PWM, 0);
144
145
146         beacon_reset_pos();
147         pid_reset(&ballboard.beacon.pid);
148         encoders_spi_set_value_beacon(BEACON_ENCODER, BEACON_OFFSET_CALIBRE);
149
150         cs_set_consign(&ballboard.beacon.cs, 0);
151
152         ballboard.flags |= DO_CS;
153 }
154
155 void beacon_start(void)
156 {
157         beacon_reset_pos();
158         ballboard.beacon.on = 1;
159         cs_set_consign(&ballboard.beacon.cs, 600);
160 }
161
162 void beacon_stop(void)
163 {
164         ballboard.beacon.on = 0;
165         pwm_ng_set(BEACON_PWM, 0);
166 }
167
168 void beacon_reset_pos(void)
169 {
170         pwm_ng_set(BEACON_PWM, 0);
171         encoders_spi_set_value(BEACON_ENCODER, 0);
172 }
173
174
175
176 int32_t encoders_spi_get_beacon_speed(void * dummy)
177 {
178         return beacon_speed;
179 }
180
181
182 //port K bit 5
183 /* motor speed (top tour) */
184 SIGNAL(SIG_PIN_CHANGE2)
185 {
186         uint8_t flags;
187
188         /* rising edge */
189         if ( IR_SENSOR()) {
190                 IRQ_LOCK(flags);
191                 count = TCNT3;
192                 rising = beacon_pos;
193                 count_diff_rising = (count - beacon_save_count + MODULO_TIMER + 1)&MODULO_TIMER;
194                 valid_beacon = 0;
195                 IRQ_UNLOCK(flags);
196
197         }
198         /* falling edge */
199         else {
200                 IRQ_LOCK(flags);
201                 count = TCNT3;
202                 falling = beacon_pos;
203                 count_diff_falling = (count - beacon_save_count + MODULO_TIMER + 1)&MODULO_TIMER;
204                 valid_beacon = 1;
205                 IRQ_UNLOCK(flags);
206         }
207 }
208
209 void beacon_calc(void *dummy)
210 {
211         static uint8_t a=0;
212         static int32_t local_rising, local_falling;
213         static int32_t middle;
214         static float size = 0;
215         int32_t local_angle;
216         int32_t local_dist;
217
218         int32_t local_count_diff_rising ;
219         int32_t local_count_diff_falling ;
220         int32_t local_beacon_coeff;
221
222         int32_t result_x=0;
223         int32_t result_y=0;
224         int32_t temp=0;
225         int32_t edge=0;
226         //int32_t total_size=0;
227
228         uint8_t flags;
229         //uint8_t i;
230         int8_t local_valid;
231
232         if(a)
233                 LED4_ON();
234         else
235                 LED4_OFF();
236
237         a = !a;
238
239         if (falling == -1){
240                 /* 0.5 second timeout */
241                 if (invalid_count < 25)
242                         invalid_count++;
243                 else {
244                         IRQ_LOCK(flags);
245                         beacon.opponent_x = I2C_OPPONENT_NOT_THERE;
246                         IRQ_UNLOCK(flags);
247                 }
248                 return;
249         }
250
251         invalid_count = 0;
252         IRQ_LOCK(flags);
253         local_valid = valid_beacon;
254         local_count_diff_rising  = count_diff_rising;
255         local_count_diff_falling = count_diff_falling ;
256         local_rising = rising;
257         local_falling = falling;
258         local_beacon_coeff = beacon_coeff;
259         IRQ_UNLOCK(flags);
260
261         if (local_valid){
262                 invalid_count = 0;
263                 //BEACON_DEBUG("rising= %ld\t",local_rising);
264                 //BEACON_DEBUG("falling= %ld\r\n",local_falling);
265
266                 /* recalculate number of pulse by adding the value of the counter, then put value back into motor's round range */
267                 local_rising  = ((local_rising + (local_count_diff_rising * local_beacon_coeff) / COEFF_MULT)) %(BEACON_STEP_TOUR);
268                 local_falling = ((local_falling + (local_count_diff_falling * local_beacon_coeff) / COEFF_MULT)) %(BEACON_STEP_TOUR);
269
270                 //BEACON_DEBUG("rising1= %ld\t",local_rising);
271                 //BEACON_DEBUG("falling1= %ld\r\n",local_falling);
272
273                 //BEACON_DEBUG("count diff rising= %ld\t",local_count_diff_rising);
274                 //BEACON_DEBUG("count diff falling= %ld\r\n",local_count_diff_falling);
275
276                 /* if around 360 deg, rising > falling, so invert both and recalculate size and middle */
277                 if(local_falling < local_rising){
278                         temp          = local_rising;
279                         local_rising  = local_falling;
280                         local_falling = temp;
281                         size          = BEACON_STEP_TOUR - local_falling + local_rising;
282                         middle        = (local_falling + ((int32_t)(size)/2) + BEACON_STEP_TOUR) %(BEACON_STEP_TOUR);
283                         edge = local_falling;
284                 }
285                 /* else rising > falling */
286                 else{
287                         size   = local_falling - local_rising;
288                         middle = local_rising + (size / 2);
289                         edge   = local_rising;
290                 }
291
292                 //for(i=BEACON_MAX_SAMPLE-1;i>0;i--){
293                 //      beacon_sample_size[i] = beacon_sample_size[i-1];
294                 //      total_size += beacon_sample_size[i];
295                 //}
296                 //beacon_sample_size[0] = size;
297                 //total_size += size;
298                 //total_size /= BEACON_MAX_SAMPLE;
299
300                 //BEACON_DEBUG("rising2= %ld\t",local_rising);
301                 //BEACON_DEBUG("falling2= %ld\r\n",local_falling);
302                 /*                      BEACON_DEBUG("size= %ld %ld\t",size, total_size); */
303                 BEACON_DEBUG("size= %f\r\n",size);
304                 //BEACON_DEBUG("middle= %ld\r\n",middle);
305
306                 local_angle = get_angle(middle,0);
307                 BEACON_NOTICE("opponent angle= %ld\t",local_angle);
308
309                 local_dist = get_dist(size);
310                 BEACON_NOTICE("opponent dist= %ld\r\n",local_dist);
311
312                 beacon_angle_dist_to_x_y(local_angle, local_dist, &result_x, &result_y);
313
314                 IRQ_LOCK(flags);
315                 beacon.opponent_x = result_x;
316                 beacon.opponent_y = result_y;
317                 beacon.opponent_angle = local_angle;
318                 beacon.opponent_dist = local_dist;
319                 /* for I2C test */
320                 //beacon.opponent_x = OPPONENT_POS_X;
321                 //beacon.opponent_y = OPPONENT_POS_Y;
322                 IRQ_UNLOCK(flags);
323
324                 BEACON_NOTICE("opponent x= %ld\t",beacon.opponent_x);
325                 BEACON_NOTICE("opponent y= %ld\r\n\n",beacon.opponent_y);
326         }
327         else {
328                 BEACON_NOTICE("non valid\r\n\n");
329         }
330
331         falling = -1;
332 }
333
334 static int32_t get_dist(float size)
335 {
336         int32_t dist=0;
337         //int32_t alpha=0;
338
339         //alpha = (size*2*M_PI*COEFF_MULT2);
340         //dist = ((2*BEACON_SIZE*BEACON_STEP_TOUR*COEFF_MULT2)/alpha)/2;
341
342         /* function found by measuring points up to 80cm */
343         //dist = ((size - 600)*(size - 600)) / 2400 +28;
344
345         /* new function */
346         /* dist = a0 + a1*x + a2*x² + a3x³ */
347         dist = 1157.3 + (1.4146*size) + (-0.013508*size*size) + (0.00001488*size*size*size);
348
349         return dist;
350
351 }
352
353 static int32_t get_angle(int32_t middle, int32_t ref)
354 {
355         int32_t ret_angle;
356
357         ret_angle = (middle - ref) * 360 / BEACON_STEP_TOUR;
358
359         if(ret_angle > 360)
360                 ret_angle -= 360;
361
362         return ret_angle;
363 }
364
365 void beacon_angle_dist_to_x_y(int32_t angle, int32_t dist, int32_t *x, int32_t *y)
366 {
367         uint8_t flags;
368
369         int32_t local_x;
370         int32_t local_y;
371         int32_t x_opponent;
372         int32_t y_opponent;
373         int32_t local_robot_angle;
374
375         IRQ_LOCK(flags);
376         local_x           = beacon.robot_x;
377         local_y           = beacon.robot_y;
378         local_robot_angle = beacon.robot_angle;
379         IRQ_UNLOCK(flags);
380
381         if (local_robot_angle < 0)
382                 local_robot_angle += 360;
383
384         x_opponent = cos((local_robot_angle + angle)* 2 * M_PI / 360)* dist;
385         y_opponent = sin((local_robot_angle + angle)* 2 * M_PI / 360)* dist;
386
387         //BEACON_DEBUG("x_op= %ld\t",x_opponent);
388         //BEACON_DEBUG("y_op= %ld\r\n",y_opponent);
389         //BEACON_NOTICE("robot_x= %ld\t",local_x);
390         //BEACON_NOTICE("robot_y= %ld\t",local_y);
391         //BEACON_NOTICE("robot_angle= %ld\r\n",local_robot_angle);
392
393         *x = local_x + x_opponent;
394         *y = local_y + y_opponent;
395
396 }