change colors
[aversive.git] / projects / microb2010 / mainboard / strat_base.c
1 /*  
2  *  Copyright Droids Corporation, Microb Technology (2009)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: strat_base.c,v 1.8 2009-11-08 17:24:33 zer0 Exp $
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26
27 #include <aversive/pgmspace.h>
28 #include <aversive/wait.h>
29 #include <aversive/error.h>
30
31 #include <ax12.h>
32 #include <uart.h>
33 #include <pwm_ng.h>
34 #include <clock_time.h>
35 #include <spi.h>
36
37 #include <pid.h>
38 #include <quadramp.h>
39 #include <control_system_manager.h>
40 #include <trajectory_manager.h>
41 #include <vect_base.h>
42 #include <lines.h>
43 #include <polygon.h>
44 #include <obstacle_avoidance.h>
45 #include <blocking_detection_manager.h>
46 #include <robot_system.h>
47 #include <position_manager.h>
48
49 #include <rdline.h>
50 #include <parse.h>
51
52 #include "../common/i2c_commands.h"
53
54 #include "main.h"
55 #include "cmdline.h"
56 #include "strat_utils.h"
57 #include "strat_base.h"
58 #include "strat.h"
59 #include "sensor.h"
60 #include "i2c_protocol.h"
61
62 /* true if we want to interrupt a trajectory */
63 static uint8_t traj_intr=0;
64
65 /* filled when a END_OBSTACLE is returned */
66 struct opponent_obstacle opponent_obstacle;
67
68 /* asked speed */
69 static volatile int16_t strat_speed_a = SPEED_DIST_FAST;
70 static volatile int16_t strat_speed_d = SPEED_ANGLE_FAST;
71 static volatile uint16_t strat_limit_speed_a = 0; /* no limit */
72 static volatile uint16_t strat_limit_speed_d = 0;
73
74 static volatile uint8_t strat_limit_speed_enabled = 1;
75
76
77 /* Strings that match the end traj cause */
78 /* /!\ keep it sync with stat_base.h */
79 const char *err_tab []= {
80         "END_TRAJ",
81         "END_BLOCKING",
82         "END_NEAR",
83         "END_OBSTACLE",
84         "END_ERROR",
85         "END_INTR",
86         "END_TIMER",
87         "END_RESERVED",
88 };
89
90 /* return string from end traj type num */
91 const char *get_err(uint8_t err)
92 {
93         uint8_t i;
94         if (err == 0)
95                 return "SUCCESS";
96         for (i=0 ; i<8; i++) {
97                 if (err & (1 <<i))
98                         return err_tab[i];
99         }
100         return "END_UNKNOWN";
101 }
102
103 void strat_hardstop(void) 
104 {
105         trajectory_hardstop(&mainboard.traj);
106         pid_reset(&mainboard.angle.pid);
107         pid_reset(&mainboard.distance.pid);
108         bd_reset(&mainboard.angle.bd);
109         bd_reset(&mainboard.distance.bd);
110
111         while ((ABS(mainboard.speed_d) > 200) ||
112                (ABS(mainboard.speed_a) > 200))
113
114         trajectory_hardstop(&mainboard.traj);
115         pid_reset(&mainboard.angle.pid);
116         pid_reset(&mainboard.distance.pid);
117         bd_reset(&mainboard.angle.bd);
118         bd_reset(&mainboard.distance.bd);
119 }
120
121 /* go to an x,y point without checking for obstacle or blocking. It
122  * should be used for very small dist only. Return END_TRAJ if we
123  * reach destination, or END_BLOCKING if the robot blocked more than 3
124  * times. */
125 uint8_t strat_goto_xy_force(int16_t x, int16_t y)
126 {
127         uint8_t i, err;
128
129         for (i=0; i<3; i++) {
130                 trajectory_goto_xy_abs(&mainboard.traj, x, y);
131                 err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
132                 if (TRAJ_SUCCESS(err))
133                         return END_TRAJ;
134                 if (err == END_BLOCKING) {
135                         time_wait_ms(500);
136                         strat_hardstop();
137                 }
138         }
139         return END_BLOCKING;
140 }
141
142 /* reset position */ 
143 void strat_reset_pos(int16_t x, int16_t y, int16_t a)
144 {
145         int16_t posx = position_get_x_s16(&mainboard.pos);
146         int16_t posy = position_get_y_s16(&mainboard.pos);
147         int16_t posa = position_get_a_deg_s16(&mainboard.pos);
148
149         if (x == DO_NOT_SET_POS)
150                 x = posx;
151         if (y == DO_NOT_SET_POS)
152                 y = posy;
153         if (a == DO_NOT_SET_POS)
154                 a = posa;
155
156         DEBUG(E_USER_STRAT, "reset pos (%s%s%s)",
157               x == DO_NOT_SET_POS ? "" : "x",
158               y == DO_NOT_SET_POS ? "" : "y",
159               a == DO_NOT_SET_POS ? "" : "a");
160         position_set(&mainboard.pos, x, y, a);
161         DEBUG(E_USER_STRAT, "pos resetted", __FUNCTION__);
162 }
163
164 /* 
165  * decrease gain on angle PID, and go forward until we reach the
166  * border.
167  */
168 uint8_t strat_calib(int16_t dist, uint8_t flags)
169 {
170         int32_t p = pid_get_gain_P(&mainboard.angle.pid);
171         int32_t i = pid_get_gain_I(&mainboard.angle.pid);
172         int32_t d = pid_get_gain_D(&mainboard.angle.pid);
173         uint8_t err;
174
175         pid_set_gains(&mainboard.angle.pid, 150, 0, 2000);
176         trajectory_d_rel(&mainboard.traj, dist);
177         err = wait_traj_end(flags);
178         pid_set_gains(&mainboard.angle.pid, p, i, d);
179         return err;
180 }
181
182 static void strat_update_traj_speed(void)
183 {
184         uint16_t d, a;
185
186         d = strat_speed_d;
187         if (strat_limit_speed_d && d > strat_limit_speed_d)
188                 d = strat_limit_speed_d;
189         a = strat_speed_a;
190         if (strat_limit_speed_a && a > strat_limit_speed_a)
191                 a = strat_limit_speed_a;
192         
193         trajectory_set_speed(&mainboard.traj, d, a);
194 }
195
196 void strat_set_speed(uint16_t d, uint16_t a)
197 {
198         uint8_t flags;
199         IRQ_LOCK(flags);
200         strat_speed_d = d;
201         strat_speed_a = a;
202         strat_update_traj_speed();
203         IRQ_UNLOCK(flags);
204 }
205
206 void strat_get_speed(uint16_t *d, uint16_t *a)
207 {
208         uint8_t flags;
209         IRQ_LOCK(flags);
210         *d = strat_speed_d;
211         *a = strat_speed_a;
212         IRQ_UNLOCK(flags);
213 }
214
215 void strat_limit_speed_enable(void)
216 {
217         strat_limit_speed_enabled = 1;
218 }
219
220 void strat_limit_speed_disable(void)
221 {
222         strat_limit_speed_enabled = 0;
223 }
224
225 /* called periodically */
226 void strat_limit_speed(void)
227 {
228         uint16_t lim_d = 0, lim_a = 0;
229         int16_t opp_d, opp_a;
230
231         if (strat_limit_speed_enabled == 0)
232                 goto update;
233
234         if (get_opponent_da(&opp_d, &opp_a) != 0)
235                 goto update;
236
237         if (opp_d < 500) {
238                 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
239                         lim_d = SPEED_DIST_VERY_SLOW;
240                         lim_a = SPEED_ANGLE_VERY_SLOW;
241                 }
242                 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
243                         lim_d = SPEED_DIST_VERY_SLOW;
244                         lim_a = SPEED_ANGLE_VERY_SLOW;
245                 }
246                 else {
247                         lim_d = SPEED_DIST_SLOW;
248                         lim_a = SPEED_ANGLE_VERY_SLOW;
249                 }
250         }
251         else if (opp_d < 800) {
252                 if (mainboard.speed_d > 0 && (opp_a > 290 || opp_a < 70)) {
253                         lim_d = SPEED_DIST_SLOW;
254                         lim_a = SPEED_ANGLE_SLOW;
255                 }
256                 else if (mainboard.speed_d < 0 && (opp_a < 250 && opp_a > 110)) {
257                         lim_d = SPEED_DIST_SLOW;
258                         lim_a = SPEED_ANGLE_SLOW;
259                 }
260         }
261
262  update:
263         if (lim_d != strat_limit_speed_d ||
264             lim_a != strat_limit_speed_a) {
265                 strat_limit_speed_d = lim_d;
266                 strat_limit_speed_a = lim_a;
267                 DEBUG(E_USER_STRAT, "new speed limit da=%d,%d", lim_d, lim_a);
268                 strat_update_traj_speed();
269         }
270 }
271
272 /* start the strat */
273 void strat_start(void)
274
275         int8_t i;
276         uint8_t err;
277
278         strat_preinit();
279
280         /* if start sw not plugged */
281         if (sensor_get(S_START_SWITCH)) {
282                 printf_P(PSTR("No start switch, press a key or plug it\r\n"));
283
284                 /* while start sw not plugged */
285                 while (sensor_get(S_START_SWITCH)) {
286                         if (! cmdline_keypressed())
287                                 continue;
288
289                         for (i=3; i>0; i--) {
290                                 printf_P(PSTR("%d\r\n"), i);
291                                 time_wait_ms(1000);
292                         }
293                         break;
294                 }
295         }
296         
297         /* if start sw plugged */
298         if (!sensor_get(S_START_SWITCH)) {
299                 printf_P(PSTR("Ready, unplug start switch to start\r\n"));
300                 /* while start sw plugged */
301                 while (!sensor_get(S_START_SWITCH));
302         }
303
304         strat_init();
305         err = strat_main();
306         NOTICE(E_USER_STRAT, "Finished !! returned %s", get_err(err));
307         strat_exit();
308 }
309
310 /* return true if we have to brake due to an obstacle */
311 uint8_t strat_obstacle(void)
312 {
313         int16_t x_rel, y_rel;
314         int16_t opp_x, opp_y, opp_d, opp_a;
315
316         /* too slow */
317         if (ABS(mainboard.speed_d) < 150)
318                 return 0;
319
320         /* no opponent detected */
321         if (get_opponent_xyda(&opp_x, &opp_y,
322                               &opp_d, &opp_a))
323                 return 0;
324
325         /* save obstacle position */
326         opponent_obstacle.x = opp_x;
327         opponent_obstacle.y = opp_y;
328         opponent_obstacle.d = opp_d;
329         opponent_obstacle.a = opp_a;
330
331         /* sensor are temporarily disabled */
332         if (sensor_obstacle_is_disabled())
333                 return 0;
334
335         /* relative position */
336         x_rel = cos(RAD(opp_a)) * (double)opp_d;
337         y_rel = sin(RAD(opp_a)) * (double)opp_d;
338
339         /* opponent too far */
340         if (opp_d > 600)
341                 return 0;
342
343         /* opponent is in front of us */
344         if (mainboard.speed_d > 0 && (opp_a > 325 || opp_a < 35)) {
345                 DEBUG(E_USER_STRAT, "opponent front d=%d, a=%d "
346                       "xrel=%d yrel=%d (speed_d=%d)", 
347                       opp_d, opp_a, x_rel, y_rel, mainboard.speed_d);
348                 sensor_obstacle_disable();
349                 return 1;
350         }
351         /* opponent is behind us */
352         if (mainboard.speed_d < 0 && (opp_a < 215 && opp_a > 145)) {
353                 DEBUG(E_USER_STRAT, "opponent behind d=%d, a=%d xrel=%d yrel=%d", 
354                       opp_d, opp_a, x_rel, y_rel);
355                 sensor_obstacle_disable();
356                 return 1;
357         }
358
359         return 0;
360 }
361
362 void interrupt_traj(void)
363 {
364         traj_intr = 1;
365 }
366
367 void interrupt_traj_reset(void)
368 {
369         traj_intr = 0;
370 }
371
372 uint8_t test_traj_end(uint8_t why)
373
374         uint16_t cur_timer;
375         point_t robot_pt;
376
377         robot_pt.x = position_get_x_s16(&mainboard.pos);
378         robot_pt.y = position_get_y_s16(&mainboard.pos);
379
380         /* trigger an event at 3 sec before the end of the match if we
381          * have balls in the barrel */
382         cur_timer = time_get_s();
383
384         if ((mainboard.flags & DO_TIMER) && (why & END_TIMER)) {
385                 /* end of match */
386                 if (cur_timer >= MATCH_TIME)
387                         return END_TIMER;
388         }
389
390         if ((why & END_INTR) && traj_intr) {
391                 interrupt_traj_reset();         
392                 return END_INTR;
393         }
394
395         if ((why & END_TRAJ) && trajectory_finished(&mainboard.traj))
396                 return END_TRAJ;
397         
398         /* we are near the destination point (depends on current
399          * speed) AND the robot is in the area bounding box. */
400         if (why & END_NEAR) {
401                 int16_t d_near = 100;   
402                 
403                 if (mainboard.speed_d > 2000)
404                         d_near = 150;
405                 
406                 if (trajectory_in_window(&mainboard.traj, d_near, RAD(5.0)) &&
407                     is_in_boundingbox(&robot_pt))
408                         return END_NEAR;
409         }
410         
411         if ((why & END_BLOCKING) && bd_get(&mainboard.angle.bd)) {
412                 strat_hardstop();
413                 return END_BLOCKING;
414         }
415
416         if ((why & END_BLOCKING) && bd_get(&mainboard.distance.bd)) {
417                 strat_hardstop();
418                 return END_BLOCKING;
419         }
420
421         if ((why & END_OBSTACLE) && strat_obstacle()) {
422                 strat_hardstop();
423                 return END_OBSTACLE;
424         }
425
426         return 0;
427 }
428
429 uint8_t __wait_traj_end_debug(uint8_t why, uint16_t line)
430 {
431         uint8_t ret = 0;
432         int16_t opp_x, opp_y, opp_d, opp_a;
433
434         while (ret == 0)
435                 ret = test_traj_end(why);
436
437         if (ret == END_OBSTACLE) {
438                 if (get_opponent_xyda(&opp_x, &opp_y,
439                                       &opp_d, &opp_a) == 0)
440                         DEBUG(E_USER_STRAT, "Got %s at line %d"
441                               " xy=(%d,%d) da=(%d,%d)", get_err(ret),
442                               line, opp_x, opp_y, opp_d, opp_a);
443         }
444         else {
445                 DEBUG(E_USER_STRAT, "Got %s at line %d", 
446                       get_err(ret), line);
447         }
448         return ret;
449 }