hostsim enhancements
[aversive.git] / projects / microb2010 / tests / hostsim / strat_avoid.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_avoid.c,v 1.5 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 <uart.h>
32 #include <time.h>
33
34 #include <pid.h>
35 #include <quadramp.h>
36 #include <control_system_manager.h>
37 #include <trajectory_manager.h>
38 #include <vect_base.h>
39 #include <lines.h>
40 #include <polygon.h>
41 #include <obstacle_avoidance.h>
42 #include <blocking_detection_manager.h>
43 #include <robot_system.h>
44 #include <position_manager.h>
45
46 #include <rdline.h>
47 #include <parse.h>
48
49 #include "main.h"
50 #include "strat.h"
51 #include "strat_base.h"
52 #include "strat_utils.h"
53
54 #define EDGE_NUMBER 5
55 void set_rotated_pentagon(poly_t *pol, const point_t *robot_pt,
56                           int16_t radius, int16_t x, int16_t y)
57 {
58
59         double c_a, s_a;
60         uint8_t i;
61         double px1, py1, px2, py2;
62         double a_rad;
63
64         a_rad = atan2(y - robot_pt->y, x - robot_pt->x);
65
66         /* generate pentagon  */
67         c_a = cos(-2*M_PI/EDGE_NUMBER);
68         s_a = sin(-2*M_PI/EDGE_NUMBER);
69
70         /*
71         px1 = radius;
72         py1 = 0;
73         */
74         px1 = radius * cos(a_rad + 2*M_PI/(2*EDGE_NUMBER));
75         py1 = radius * sin(a_rad + 2*M_PI/(2*EDGE_NUMBER));
76
77
78         for (i = 0; i < EDGE_NUMBER; i++){
79                 oa_poly_set_point(pol, x + px1, y + py1, i);
80                 
81                 px2 = px1*c_a + py1*s_a;
82                 py2 = -px1*s_a + py1*c_a;
83
84                 px1 = px2;
85                 py1 = py2;
86         }
87 }
88
89 void set_rotated_poly(poly_t *pol, const point_t *robot_pt, 
90                       int16_t w, int16_t l, int16_t x, int16_t y)
91 {
92         double tmp_x, tmp_y;
93         double a_rad;
94
95         a_rad = atan2(y - robot_pt->y, x - robot_pt->x);
96
97         DEBUG(E_USER_STRAT, "%s() x,y=%d,%d a_rad=%2.2f", 
98               __FUNCTION__, x, y, a_rad);
99
100         /* point 1 */
101         tmp_x = w;
102         tmp_y = l;
103         rotate(&tmp_x, &tmp_y, a_rad);
104         tmp_x += x;
105         tmp_y += y;
106         oa_poly_set_point(pol, tmp_x, tmp_y, 0);
107         
108         /* point 2 */
109         tmp_x = -w;
110         tmp_y = l;
111         rotate(&tmp_x, &tmp_y, a_rad);
112         tmp_x += x;
113         tmp_y += y;
114         oa_poly_set_point(pol, tmp_x, tmp_y, 1);
115         
116         /* point 3 */
117         tmp_x = -w;
118         tmp_y = -l;
119         rotate(&tmp_x, &tmp_y, a_rad);
120         tmp_x += x;
121         tmp_y += y;
122         oa_poly_set_point(pol, tmp_x, tmp_y, 2);
123         
124         /* point 4 */
125         tmp_x = w;
126         tmp_y = -l;
127         rotate(&tmp_x, &tmp_y, a_rad);
128         tmp_x += x;
129         tmp_y += y;
130         oa_poly_set_point(pol, tmp_x, tmp_y, 3);
131 }
132
133 #define DISC_X CENTER_X
134 #define DISC_Y CENTER_Y
135
136 void set_central_disc_poly(poly_t *pol, const point_t *robot_pt)
137 {
138         set_rotated_pentagon(pol, robot_pt, DISC_PENTA_DIAG,
139                              DISC_X, DISC_Y);
140 }
141
142 #ifdef HOMOLOGATION
143 /* /!\ half size */
144 #define O_WIDTH  400
145 #define O_LENGTH 550
146 #else
147 /* /!\ half size */
148 #define O_WIDTH  360
149 #define O_LENGTH 500
150 #endif
151
152 void set_opponent_poly(poly_t *pol, const point_t *robot_pt, int16_t w, int16_t l)
153 {
154         int16_t x, y;
155         get_opponent_xy(&x, &y);
156         DEBUG(E_USER_STRAT, "oponent at: %d %d", x, y);
157         
158         /* place poly even if invalid, because it's -100 */
159         set_rotated_poly(pol, robot_pt, w, l, x, y);
160 }
161
162 /* don't care about polygons further than this distance for escape */
163 #define ESCAPE_POLY_THRES 1000
164
165 /* don't reduce opp if opp is too far */
166 #define REDUCE_POLY_THRES 600
167
168 /* has to be longer than any poly */
169 #define ESCAPE_VECT_LEN 3000
170
171 /*
172  * Go in playground, loop until out of poly. The argument robot_pt is 
173  * the pointer to the current position of the robot.
174  * Return 0 if there was nothing to do.
175  * Return 1 if we had to move. In this case, update the theorical 
176  * position of the robot in robot_pt.
177  */
178 static int8_t go_in_area(point_t *robot_pt)
179 {
180         point_t poly_pts_area[4];
181         poly_t poly_area;
182         point_t disc_pt, dst_pt;
183
184         disc_pt.x = DISC_X;
185         disc_pt.y = DISC_Y;
186
187         /* Go in playground */
188         if (!is_in_boundingbox(robot_pt)){
189                 NOTICE(E_USER_STRAT, "not in playground %"PRIi32", %"PRIi32"",
190                        robot_pt->x, robot_pt->y);
191
192                 poly_area.l = 4;
193                 poly_area.pts = poly_pts_area;
194                 poly_pts_area[0].x = strat_infos.area_bbox.x1;
195                 poly_pts_area[0].y = strat_infos.area_bbox.y1;
196
197                 poly_pts_area[1].x = strat_infos.area_bbox.x2;
198                 poly_pts_area[1].y = strat_infos.area_bbox.y1;
199
200                 poly_pts_area[2].x = strat_infos.area_bbox.x2;
201                 poly_pts_area[2].y = strat_infos.area_bbox.y2;
202
203                 poly_pts_area[3].x = strat_infos.area_bbox.x1;
204                 poly_pts_area[3].y = strat_infos.area_bbox.y2;
205
206                 is_crossing_poly(*robot_pt, disc_pt, &dst_pt, &poly_area);
207                 NOTICE(E_USER_STRAT, "pt dst %"PRIi32", %"PRIi32"", dst_pt.x, dst_pt.y);
208                 
209                 strat_goto_xy_force(dst_pt.x, dst_pt.y);
210
211                 robot_pt->x = dst_pt.x;
212                 robot_pt->y = dst_pt.y;
213
214                 NOTICE(E_USER_STRAT, "GOTO %"PRIi32",%"PRIi32"",
215                        dst_pt.x, dst_pt.y);
216
217                 return 1;
218         }
219
220         return 0;
221 }
222
223
224 /*
225  * Escape from polygons if needed.
226  * robot_pt is the current position of the robot, it will be
227  * updated.
228  */
229 static int8_t escape_from_poly(point_t *robot_pt,
230                                poly_t *pol_disc,
231                                int16_t opp_x, int16_t opp_y, 
232                                int16_t opp_w, int16_t opp_l, 
233                                poly_t *pol_opp)
234 {
235         uint8_t in_disc = 0, in_opp = 0;
236         double escape_dx = 0, escape_dy = 0;
237         double disc_dx = 0, disc_dy = 0;
238         double opp_dx = 0, opp_dy = 0;
239         double len;
240         point_t opp_pt, disc_pt, dst_pt;
241         point_t intersect_disc_pt, intersect_opp_pt;
242
243         opp_pt.x = opp_x;
244         opp_pt.y = opp_y;
245         disc_pt.x = DISC_X;
246         disc_pt.y = DISC_Y;
247
248         /* escape from other poly if necessary */
249         if (is_in_poly(robot_pt, pol_disc) == 1)
250                 in_disc = 1;
251         if (is_in_poly(robot_pt, pol_opp) == 1)
252                 in_opp = 1;
253
254         if (in_disc == 0 && in_opp == 0) {
255                 NOTICE(E_USER_STRAT, "no need to escape");
256                 return 0;
257         }
258         
259         NOTICE(E_USER_STRAT, "in_disc=%d, in_opp=%d", in_disc, in_opp);
260         
261         /* process escape vector */
262
263         if (distance_between(robot_pt->x, robot_pt->y, DISC_X, DISC_Y) < ESCAPE_POLY_THRES) {
264                 disc_dx = robot_pt->x - DISC_X;
265                 disc_dy = robot_pt->y - DISC_Y;
266                 NOTICE(E_USER_STRAT, " robot is near disc: vect=%2.2f,%2.2f",
267                        disc_dx, disc_dy);
268                 len = norm(disc_dx, disc_dy);
269                 if (len != 0) {
270                         disc_dx /= len;
271                         disc_dy /= len;
272                 }
273                 else {
274                         disc_dx = 1.0;
275                         disc_dy = 0.0;
276                 }
277                 escape_dx += disc_dx;
278                 escape_dy += disc_dy;
279         }
280
281         if (distance_between(robot_pt->x, robot_pt->y, opp_x, opp_y) < ESCAPE_POLY_THRES) {
282                 opp_dx = robot_pt->x - opp_x;
283                 opp_dy = robot_pt->y - opp_y;
284                 NOTICE(E_USER_STRAT, " robot is near opp: vect=%2.2f,%2.2f",
285                        opp_dx, opp_dy);
286                 len = norm(opp_dx, opp_dy);
287                 if (len != 0) {
288                         opp_dx /= len;
289                         opp_dy /= len;
290                 }
291                 else {
292                         opp_dx = 1.0;
293                         opp_dy = 0.0;
294                 }
295                 escape_dx += opp_dx;
296                 escape_dy += opp_dy;
297         }
298
299         /* normalize escape vector */
300         len = norm(escape_dx, escape_dy);
301         if (len != 0) {
302                 escape_dx /= len;
303                 escape_dy /= len;
304         }
305         else {
306                 if (pol_disc != NULL) {
307                         /* rotate 90° */
308                         escape_dx = disc_dy;
309                         escape_dy = disc_dx;
310                 }
311                 else if (pol_opp != NULL) {
312                         /* rotate 90° */
313                         escape_dx = opp_dy;
314                         escape_dy = opp_dx;
315                 }
316                 else { /* should not happen */
317                         opp_dx = 1.0;
318                         opp_dy = 0.0;
319                 }
320         }
321
322         NOTICE(E_USER_STRAT, " escape vect = %2.2f,%2.2f",
323                escape_dx, escape_dy);
324
325         /* process the correct len of escape vector */
326
327         dst_pt.x = robot_pt->x + escape_dx * ESCAPE_VECT_LEN;
328         dst_pt.y = robot_pt->y + escape_dy * ESCAPE_VECT_LEN;
329
330         NOTICE(E_USER_STRAT, "robot pt %"PRIi32" %"PRIi32,
331                robot_pt->x, robot_pt->y);
332         NOTICE(E_USER_STRAT, "dst point %"PRIi32",%"PRIi32,
333                dst_pt.x, dst_pt.y);
334
335         if (in_disc) {
336                 if (is_crossing_poly(*robot_pt, dst_pt, &intersect_disc_pt,
337                                      pol_disc) == 1) {
338                         /* we add 2 mm to be sure we are out of th polygon */
339                         dst_pt.x = intersect_disc_pt.x + escape_dx * 2;
340                         dst_pt.y = intersect_disc_pt.y + escape_dy * 2;
341                         if (is_point_in_poly(pol_opp, dst_pt.x, dst_pt.y) != 1) {
342
343                                 if (!is_in_boundingbox(&dst_pt))
344                                         return -1;
345                                 
346                                 NOTICE(E_USER_STRAT, "GOTO %"PRIi32",%"PRIi32"",
347                                        dst_pt.x, dst_pt.y);
348
349                                 strat_goto_xy_force(dst_pt.x, dst_pt.y);
350
351                                 robot_pt->x = dst_pt.x;
352                                 robot_pt->y = dst_pt.y;
353
354                                 return 0;
355                         }
356                 }
357         }
358
359         if (in_opp) {
360                 if (is_crossing_poly(*robot_pt, dst_pt, &intersect_opp_pt,
361                                      pol_opp) == 1) {
362                         /* we add 2 cm to be sure we are out of th polygon */
363                         dst_pt.x = intersect_opp_pt.x + escape_dx * 2;
364                         dst_pt.y = intersect_opp_pt.y + escape_dy * 2;
365
366                         if (is_point_in_poly(pol_disc, dst_pt.x, dst_pt.y) != 1) {
367
368                                 if (!is_in_boundingbox(&dst_pt))
369                                         return -1;
370                                 
371                                 NOTICE(E_USER_STRAT, "GOTO %"PRIi32",%"PRIi32"",
372                                        dst_pt.x, dst_pt.y);
373
374                                 strat_goto_xy_force(dst_pt.x, dst_pt.y);
375
376                                 robot_pt->x = dst_pt.x;
377                                 robot_pt->y = dst_pt.y;
378
379                                 return 0;
380                         }
381                 }
382         }
383
384         /* should not happen */
385         return -1;
386 }
387
388
389 static int8_t __goto_and_avoid(int16_t x, int16_t y,
390                                uint8_t flags_intermediate,
391                                uint8_t flags_final,
392                                uint8_t forward)
393 {
394         int8_t len = -1, i;
395         point_t *p;
396         poly_t *pol_disc, *pol_opp;
397         int8_t ret;
398         int16_t opp_w, opp_l, opp_x, opp_y;
399         point_t p_dst, robot_pt;
400
401         DEBUG(E_USER_STRAT, "%s(%d,%d) flags_i=%x flags_f=%x forw=%d",
402               __FUNCTION__, x, y, flags_intermediate, flags_final, forward);
403
404  retry:
405         get_opponent_xy(&opp_x, &opp_y);
406         opp_w = O_WIDTH;
407         opp_l = O_LENGTH;
408
409         robot_pt.x = position_get_x_s16(&mainboard.pos);
410         robot_pt.y = position_get_y_s16(&mainboard.pos);
411         
412         oa_init();
413         pol_disc = oa_new_poly(5);
414         set_central_disc_poly(pol_disc, &robot_pt);
415         pol_opp = oa_new_poly(4);
416         set_opponent_poly(pol_opp, &robot_pt, O_WIDTH, O_LENGTH);
417
418         /* If we are not in the limited area, try to go in it. */
419         ret = go_in_area(&robot_pt);
420
421         /* check that destination is valid */
422         p_dst.x = x;
423         p_dst.y = y;
424         if (!is_in_boundingbox(&p_dst)) {
425                 NOTICE(E_USER_STRAT, " dst is not in playground");
426                 return END_ERROR;
427         }
428         if (is_point_in_poly(pol_disc, x, y)) {
429                 NOTICE(E_USER_STRAT, " dst is in disc");
430                 return END_ERROR;
431         }
432         if (is_point_in_poly(pol_opp, x, y)) {
433                 NOTICE(E_USER_STRAT, " dst is in opp");
434                 return END_ERROR;
435         }
436
437         /* now start to avoid */
438         while (opp_w && opp_l) {
439
440                 /* robot_pt is not updated if it fails */
441                 ret = escape_from_poly(&robot_pt,
442                                        pol_disc, opp_x, opp_y, 
443                                        opp_w, opp_l, pol_opp);
444                 if (ret == 0) {
445                         oa_reset();
446                         oa_start_end_points(robot_pt.x, robot_pt.y, x, y);
447                         /* oa_dump(); */
448         
449                         len = oa_process();
450                         if (len >= 0)
451                                 break;
452                 }
453                 if (distance_between(robot_pt.x, robot_pt.y, opp_x, opp_y) < REDUCE_POLY_THRES ) {
454                         if (opp_w == 0)
455                                 opp_l /= 2;
456                         opp_w /= 2;
457                 }
458                 else {
459                         NOTICE(E_USER_STRAT, "oa_process() returned %d", len);
460                         return END_ERROR;
461                 }
462
463                 NOTICE(E_USER_STRAT, "reducing opponent %d %d", opp_w, opp_l);
464                 set_opponent_poly(pol_opp, &robot_pt, opp_w, opp_l);
465         }
466         
467         p = oa_get_path();
468         for (i=0 ; i<len ; i++) {
469                 DEBUG(E_USER_STRAT, "With avoidance %d: x=%"PRIi32" y=%"PRIi32"", i, p->x, p->y);
470
471                 if (forward)
472                         trajectory_goto_forward_xy_abs(&mainboard.traj, p->x, p->y);
473                 else
474                         trajectory_goto_backward_xy_abs(&mainboard.traj, p->x, p->y);
475
476                 /* no END_NEAR for the last point */
477                 if (i == len - 1)
478                         ret = wait_traj_end(flags_final);
479                 else
480                         ret = wait_traj_end(flags_intermediate);
481
482                 if (ret == END_BLOCKING) {
483                         DEBUG(E_USER_STRAT, "Retry avoidance %s(%d,%d)",
484                               __FUNCTION__, x, y);
485                         goto retry;
486                 }
487                 else if (ret == END_OBSTACLE) {
488                         /* brake and wait the speed to be slow */
489                         DEBUG(E_USER_STRAT, "Retry avoidance %s(%d,%d)",
490                               __FUNCTION__, x, y);
491                         goto retry;
492                 }
493                 /* else if it is not END_TRAJ or END_NEAR, return */
494                 else if (!TRAJ_SUCCESS(ret)) {
495                         return ret;
496                 }
497                 p++;
498         }
499         
500         return END_TRAJ;
501 }
502
503 /* go forward to a x,y point. use current speed for that */
504 uint8_t goto_and_avoid_forward(int16_t x, int16_t y, uint8_t flags_intermediate,
505                                uint8_t flags_final)
506 {
507         return __goto_and_avoid(x, y, flags_intermediate, flags_final, 1);
508 }
509
510 /* go backward to a x,y point. use current speed for that */
511 uint8_t goto_and_avoid_backward(int16_t x, int16_t y, uint8_t flags_intermediate,
512                        uint8_t flags_final)
513 {
514         return __goto_and_avoid(x, y, flags_intermediate, flags_final, 0);
515 }
516
517 /* go to a x,y point. prefer backward but go forward if the point is
518  * near and in front of us */
519 uint8_t goto_and_avoid(int16_t x, int16_t y, uint8_t flags_intermediate,
520                                uint8_t flags_final)
521 {
522         double d,a;
523         abs_xy_to_rel_da(x, y, &d, &a); 
524
525         if (d < 300 && a < RAD(90) && a > RAD(-90))
526                 return __goto_and_avoid(x, y, flags_intermediate,
527                                         flags_final, 1);
528         else
529                 return __goto_and_avoid(x, y, flags_intermediate,
530                                         flags_final, 0);
531 }