dc595815841cf7007f2e8575b46cc2bc419344c8
[aversive.git] / projects / microb2010 / mainboard / strat.c
1 /*
2  *  Copyright Droids, 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.c,v 1.6 2009-11-08 17:24:33 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org>
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27
28 #include <aversive/pgmspace.h>
29 #include <aversive/queue.h>
30 #include <aversive/wait.h>
31 #include <aversive/error.h>
32
33 #include <ax12.h>
34 #include <uart.h>
35 #include <pwm_ng.h>
36 #include <clock_time.h>
37 #include <spi.h>
38
39 #include <pid.h>
40 #include <quadramp.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <trajectory_manager_utils.h>
44 #include <trajectory_manager_core.h>
45 #include <vect_base.h>
46 #include <lines.h>
47 #include <polygon.h>
48 #include <obstacle_avoidance.h>
49 #include <blocking_detection_manager.h>
50 #include <robot_system.h>
51 #include <position_manager.h>
52
53 #include <diagnostic.h>
54
55 #include <rdline.h>
56 #include <parse.h>
57
58 #include "../common/i2c_commands.h"
59 #include "i2c_protocol.h"
60 #include "main.h"
61 #include "cs.h"
62 #include "strat.h"
63 #include "strat_db.h"
64 #include "strat_base.h"
65 #include "strat_corn.h"
66 #include "strat_utils.h"
67 #include "strat_avoid.h"
68 #include "sensor.h"
69 #include "actuator.h"
70
71 #define COL_DISP_MARGIN 400 /* stop 40 cm in front of dispenser */
72 #define COL_SCAN_PRE_MARGIN 250
73
74 static volatile uint8_t strat_running = 0;
75 volatile uint8_t strat_want_pack = 0;
76 volatile uint8_t strat_lpack60 = 0;
77 volatile uint8_t strat_rpack60 = 0;
78
79 volatile uint8_t strat_opponent_lpack = 0;
80 volatile uint8_t strat_opponent_rpack = 0;
81
82 struct strat_conf strat_conf = {
83         .dump_enabled = 0,
84         .opp_orange = 90,
85         .orphan_tomato = 45,
86         .flags = 0,
87 };
88
89 /*************************************************************/
90
91 /*                  INIT                                     */
92
93 /*************************************************************/
94
95 /* called before each strat, and before the start switch */
96 void strat_preinit(void)
97 {
98         time_reset();
99         interrupt_traj_reset();
100         mainboard.flags =  DO_ENCODERS | DO_CS | DO_RS |
101                 DO_POS | DO_BD | DO_POWER;
102
103         strat_db_init();
104         strat_conf.prev_wait_obstacle = -5;
105         strat_conf_dump(__FUNCTION__);
106         strat_db_dump(__FUNCTION__);
107 }
108
109 void strat_conf_dump(const char *caller)
110 {
111         if (!strat_conf.dump_enabled)
112                 return;
113
114         printf_P(PSTR("-- conf --\r\n"));
115         printf_P(PSTR("our_orange = %s\n"),
116                  (strat_conf.flags & STRAT_CONF_OUR_ORANGE) ? "y":"n");
117         printf_P(PSTR("wait_obstacle = %s\n"),
118                  (strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE) ? "y":"n");
119         printf_P(PSTR("opp_orange = %d\n"), strat_conf.opp_orange);
120         printf_P(PSTR("orphan_tomato = %d\n"), strat_conf.orphan_tomato);
121 }
122
123 void strat_event_enable(void)
124 {
125         strat_running = 1;
126 }
127
128 void strat_event_disable(void)
129 {
130         strat_running = 0;
131 }
132
133 /* call it just before launching the strat */
134 void strat_init(void)
135 {
136 #ifdef HOST_VERSION
137         position_set(&mainboard.pos, 298.16,
138                      COLOR_Y(308.78), COLOR_A(70.00));
139 #endif
140
141         /* we consider that the color is correctly set */
142         strat_running = 1;
143         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
144         time_reset();
145         interrupt_traj_reset();
146
147         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
148         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_HARVEST);
149
150         /* used in strat_base for END_TIMER */
151         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
152                 DO_POS | DO_BD | DO_TIMER | DO_POWER;
153 }
154
155 /* call it after each strat */
156 void strat_exit(void)
157 {
158 #ifndef HOST_VERSION
159         uint8_t flags;
160 #endif
161
162         strat_running = 0;
163         mainboard.flags &= ~(DO_TIMER);
164         strat_hardstop();
165         time_reset();
166         wait_ms(100);
167 #ifndef HOST_VERSION
168         IRQ_LOCK(flags);
169         mainboard.flags &= ~(DO_CS);
170         IRQ_UNLOCK(flags);
171         pwm_ng_set(LEFT_PWM, 0);
172         pwm_ng_set(RIGHT_PWM, 0);
173 #endif
174 }
175
176 /* mark tomato as not present */
177 static void check_tomato(void)
178 {
179         int16_t x, y;
180         uint8_t i, j;
181         static uint8_t prev_check_time;
182         uint8_t cur_time;
183
184         /* check present tomatoes once per second */
185         cur_time = time_get_s();
186         if (cur_time != prev_check_time) {
187                 uint8_t k;
188
189                 for (k = 0; k < TOMATO_NB; k++) {
190                         if (strat_db.tomato_table[k]->present == 1 &&
191                             strat_db.tomato_table[k]->time_removed != -1 &&
192                             strat_db.tomato_table[k]->time_removed + 2 <= time_get_s()) {
193 #ifdef HOST_VERSION
194                                 printf("remove tomato %d\n", k);
195 #endif
196                                 strat_db.tomato_table[k]->present = 0;
197                         }
198                 }
199                 prev_check_time = cur_time;
200         }
201
202         x = position_get_x_s16(&mainboard.pos);
203         y = position_get_y_s16(&mainboard.pos);
204
205         if (xycoord_to_ijcoord(&x, &y, &i, &j) < 0)
206                 return;
207
208         if (strat_db.wp_table[i][j].type != WP_TYPE_TOMATO)
209                 return;
210
211         if (strat_db.wp_table[i][j].present == 0)
212                 return;
213
214         strat_db.wp_table[i][j].time_removed = time_get_s();
215         strat_db.wp_table[i][j].present = 0;
216 #ifdef HOST_VERSION
217         ballboard.ball_count ++;
218         printf("add ball %d,%d\n", i, j);
219 #endif
220 }
221
222 /* mark corn as not present and give correct commands to the cobboard
223  * for spickles */
224 static void check_corn(void)
225 {
226         uint8_t flags;
227         int8_t lcob_near, rcob_near;
228         uint8_t lcob, rcob;
229         uint8_t lidx, ridx;
230         static uint8_t prev_check_time;
231         uint8_t cur_time;
232         uint8_t need_lpack, need_rpack;
233
234         /* read sensors from ballboard */
235         IRQ_LOCK(flags);
236         lcob = ballboard.lcob;
237         ballboard.lcob = I2C_COB_NONE;
238         rcob = ballboard.rcob;
239         ballboard.rcob = I2C_COB_NONE;
240         IRQ_UNLOCK(flags);
241
242         /* XXX take opponent position into account */
243
244         /* check present cobs once per second */
245         cur_time = time_get_s();
246         if (cur_time != prev_check_time) {
247                 uint8_t i;
248
249                 for (i = 0; i < CORN_NB; i++) {
250                         if (strat_db.corn_table[i]->present == 1 &&
251                             strat_db.corn_table[i]->time_removed != -1 &&
252                             strat_db.corn_table[i]->time_removed + 2 <= time_get_s()) {
253 #ifdef HOST_VERSION
254                                 printf("remove cob %d\n", i);
255 #endif
256                                 strat_db.corn_table[i]->present = 0;
257                         }
258                 }
259                 prev_check_time = cur_time;
260         }
261
262         /* detect cob on left side */
263         lcob_near = corn_is_near(&lidx, I2C_LEFT_SIDE);
264         if (lcob_near && lcob != I2C_COB_NONE) {
265                 if (strat_db.corn_table[lidx]->corn.color == I2C_COB_UNKNOWN)
266                         DEBUG(E_USER_STRAT, "lcob %s %d",
267                               lcob == I2C_COB_WHITE ? "white" : "black", lidx);
268                 corn_set_color(strat_db.corn_table[lidx], lcob);
269         }
270
271         /* detect cob on right side */
272         rcob_near = corn_is_near(&ridx, I2C_RIGHT_SIDE);
273         if (rcob_near && rcob != I2C_COB_NONE) {
274                 if (strat_db.corn_table[ridx]->corn.color == I2C_COB_UNKNOWN)
275                         DEBUG(E_USER_STRAT, "rcob %s %d",
276                               rcob == I2C_COB_WHITE ? "white" : "black", ridx);
277                 corn_set_color(strat_db.corn_table[ridx], rcob);
278         }
279
280         /* control the cobboard mode for left spickle */
281         need_lpack = get_cob_count() >= 5 || strat_want_pack ||
282                 strat_lpack60 || strat_opponent_lpack;
283         if (lcob_near && strat_db.corn_table[lidx]->present) {
284                 if (need_lpack) {
285                         /* nothing  */
286                 }
287                 else {
288                         /* deploy spickle and harvest white ones */
289                         if (strat_db.corn_table[lidx]->corn.color == I2C_COB_WHITE) {
290                                 i2c_cobboard_autoharvest_nomove(I2C_LEFT_SIDE);
291                                 if (strat_db.corn_table[lidx]->time_removed == -1
292 #ifndef HOST_VERSION
293                                     && cobboard.status == I2C_COBBOARD_STATUS_LBUSY
294 #endif
295                                     ) {
296                                         strat_db.corn_table[lidx]->time_removed = time_get_s();
297 #ifdef HOST_VERSION
298                                         cobboard.cob_count ++;
299                                         printf("add cob %d\n", lidx);
300 #endif
301                                 }
302                         }
303                         else
304                                 i2c_cobboard_deploy_nomove(I2C_LEFT_SIDE);
305                 }
306         }
307         else {
308                 /* no cob near us, we can pack or deploy freely */
309                 if (need_lpack)
310                         i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
311                 else
312                         i2c_cobboard_deploy(I2C_LEFT_SIDE);
313         }
314
315         /* control the cobboard mode for right spickle */
316         need_rpack = get_cob_count() >= 5 || strat_want_pack ||
317                 strat_rpack60 || strat_opponent_rpack;
318         if (rcob_near && strat_db.corn_table[ridx]->present) {
319                 if (need_rpack) {
320                         /* nothing */
321                 }
322                 else {
323                         /* deploy spickle and harvest white ones */
324                         if (strat_db.corn_table[ridx]->corn.color == I2C_COB_WHITE) {
325                                 i2c_cobboard_autoharvest_nomove(I2C_RIGHT_SIDE);
326                                 if (strat_db.corn_table[ridx]->time_removed == -1
327 #ifndef HOST_VERSION
328                                     && cobboard.status == I2C_COBBOARD_STATUS_RBUSY
329 #endif
330                                     ) {
331                                         strat_db.corn_table[ridx]->time_removed = time_get_s();
332 #ifdef HOST_VERSION
333                                         cobboard.cob_count ++;
334                                         printf("add cob %d\n", ridx);
335 #endif
336                                 }
337                         }
338                         else
339                                 i2c_cobboard_deploy_nomove(I2C_RIGHT_SIDE);
340                 }
341         }
342         else {
343                 /* no cob near us, we can pack or deploy freely */
344                 if (need_rpack)
345                         i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
346                 else
347                         i2c_cobboard_deploy(I2C_RIGHT_SIDE);
348         }
349 }
350
351 /* check opponent position */
352 void check_opponent(void)
353 {
354         int16_t opp_x, opp_y;
355         int16_t opp_d, opp_a;
356         uint8_t i, j;
357
358         strat_opponent_lpack = 0;
359         strat_opponent_rpack = 0;
360
361         if (get_opponent_xyda(&opp_x, &opp_y, &opp_d, &opp_a) < 0)
362                 return;
363
364         /* pack spickles if opponent too close */
365         if (opp_d < 600) {
366                 if (opp_a > 45 && opp_a < 135)
367                         strat_opponent_lpack = 1;
368                 if (opp_a > 225 && opp_a < 315)
369                         strat_opponent_rpack = 1;
370         }
371
372         /* check for oranges after 5 seconds */
373         if (time_get_s() > 5) {
374                 if (mainboard.our_color == I2C_COLOR_YELLOW) {
375                         if (opp_y < 500 && opp_x < 500)
376                                 strat_db.our_oranges_count = 0;
377                         if (opp_y < 500 && opp_x > AREA_X - 500)
378                                 strat_db.opp_oranges_count = 0;
379                 }
380                 else {
381                         if (opp_y > AREA_Y - 500 && opp_x < 500)
382                                 strat_db.our_oranges_count = 0;
383                         if (opp_y > AREA_Y - 500 && opp_x > AREA_X - 500)
384                                 strat_db.opp_oranges_count = 0;
385                 }
386         }
387
388         /* malus for some tomatoes and cobs, visited by opponent */
389         if (xycoord_to_ijcoord(&opp_x, &opp_y, &i, &j) < 0)
390                 return;
391
392         strat_db.wp_table[i][j].opp_visited = 1;
393 }
394
395 /* called periodically (10ms) */
396 void strat_event(void *dummy)
397 {
398         /* ignore when strat is not running */
399         if (strat_running == 0)
400                 return;
401
402         check_opponent();
403         check_tomato();
404         check_corn();
405
406         /* limit speed when opponent is near */
407         /* disabled for 2010, we are already slow :) */
408         //strat_limit_speed();
409 }
410
411 /* check that we are on an eject line */
412 static uint8_t robot_is_on_eject_line(void)
413 {
414         int16_t x, y;
415         uint8_t i, j;
416
417         x = position_get_x_s16(&mainboard.pos);
418         y = position_get_y_s16(&mainboard.pos);
419
420         if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
421                 return 0;
422
423         if (!wp_belongs_to_line(i, j, 5, LINE_UP) &&
424             !wp_belongs_to_line(i, j, 2, LINE_R_UP))
425                 return 0;
426
427         return 1;
428 }
429
430 /* 0 = fast, 1 = slow */
431 static uint8_t eject_select_speed(void)
432 {
433         int16_t x, y;
434         uint8_t i, j;
435
436         x = position_get_x_s16(&mainboard.pos);
437         y = position_get_y_s16(&mainboard.pos);
438
439         if (get_cob_count() >= 5) {
440                 strat_want_pack = 1;
441                 return 0; /* fast */
442         }
443
444         if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) {
445                 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
446                       __FUNCTION__, x, y);
447                 return 1; /* slow */
448         }
449
450         if (corn_count_neigh(i, j) == 2)
451                 return 1; /* slow */
452
453         return 0; /* fast */
454 }
455
456 /* called multiple times while we are waiting to reach the ejection
457  * point */
458 static uint8_t speedify_eject(void)
459 {
460         if (eject_select_speed())
461                 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
462         else
463                 strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
464         return 0;
465 }
466
467 /* must be called from a terminal line */
468 static uint8_t strat_eject(void)
469 {
470         uint8_t err;
471
472         DEBUG(E_USER_STRAT, "%s() cob_count=%d ball_count=%d",
473               __FUNCTION__, get_cob_count(), get_ball_count());
474
475         /* check that we are called from an eject line */
476         if (!robot_is_on_eject_line()) {
477                 DEBUG(E_USER_STRAT, "%s() not on eject line", __FUNCTION__);
478                 return END_ERROR;
479         }
480
481         /* go to eject point */
482         trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(1847));
483         err = WAIT_COND_OR_TRAJ_END(speedify_eject(),
484                                     TRAJ_FLAGS_NO_NEAR);
485         /* err is never == 0 because speedify_eject() always return 0 */
486         if (!TRAJ_SUCCESS(err))
487                 return err;
488
489         /* pack arms (force), and disable strat_event */
490         strat_event_disable();
491         i2c_cobboard_pack_weak(I2C_LEFT_SIDE);
492         i2c_cobboard_pack_weak(I2C_RIGHT_SIDE);
493
494         /* ball ejection */
495         if (get_ball_count() > 0) {
496                 i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_EJECT);
497                 trajectory_a_abs(&mainboard.traj, COLOR_A(70));
498                 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
499                 if (!TRAJ_SUCCESS(err))
500                         goto fail;
501
502                 DEBUG(E_USER_STRAT, "%s():%d", __FUNCTION__, __LINE__);
503                 strat_hardstop();
504 #ifdef HOST_VERSION
505                 time_wait_ms(2000);
506 #else
507                 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_BUSY,
508                                      2000);
509                 WAIT_COND_OR_TIMEOUT(ballboard.status == I2C_BALLBOARD_STATUS_F_READY,
510                                      2000);
511 #endif
512         }
513
514         /* half turn */
515         trajectory_a_abs(&mainboard.traj, COLOR_A(-110));
516         err = wait_traj_end(END_INTR|END_TRAJ);
517         if (!TRAJ_SUCCESS(err))
518                 goto fail;
519
520         /* cob ejection */
521         trajectory_d_rel(&mainboard.traj, -70);
522         err = wait_traj_end(END_INTR|END_TRAJ);
523         if (!TRAJ_SUCCESS(err))
524                 goto fail;
525
526         if (get_cob_count() > 0) {
527                 i2c_cobboard_set_mode(I2C_COBBOARD_MODE_EJECT);
528                 time_wait_ms(2000);
529         }
530
531         /* cob ejection */
532         trajectory_d_rel(&mainboard.traj, 70);
533         err = wait_traj_end(END_INTR|END_TRAJ);
534         if (!TRAJ_SUCCESS(err))
535                 goto fail;
536
537         strat_db_dump(__FUNCTION__);
538         err = END_TRAJ;
539
540  fail:
541         strat_event_enable();
542         strat_want_pack = 0;
543         return err;
544 }
545
546 static uint8_t strat_beginning(uint8_t do_initturn)
547 {
548         uint8_t err;
549
550         strat_set_acc(ACC_DIST, ACC_ANGLE);
551
552         if (do_initturn) {
553                 strat_set_speed(600, 60); /* OK */
554                 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(20));
555                 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
556                                             TRAJ_FLAGS_STD);
557         }
558
559         strat_set_acc(ACC_DIST, ACC_ANGLE);
560         strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
561
562  l1:
563         err = line2line(0, LINE_UP, 2, LINE_R_DOWN, TRAJ_FLAGS_NO_NEAR);
564         if (err == END_OBSTACLE &&
565             strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
566             time_get_s() > strat_conf.prev_wait_obstacle + 5) {
567                 strat_conf.prev_wait_obstacle = time_get_s();
568                 time_wait_ms(2000);
569                 goto l1;
570         }
571         if (!TRAJ_SUCCESS(err))
572                 return err;
573
574  l2:
575         err = line2line(2, LINE_R_DOWN, 2, LINE_R_UP, TRAJ_FLAGS_NO_NEAR);
576         if (err == END_OBSTACLE &&
577             strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
578             time_get_s() > strat_conf.prev_wait_obstacle + 5) {
579                 strat_conf.prev_wait_obstacle = time_get_s();
580                 time_wait_ms(2000);
581                 goto l2;
582         }
583         if (!TRAJ_SUCCESS(err)) {
584                 return err;
585         }
586
587         return END_TRAJ;
588 }
589
590 static uint8_t strat_beginning2(uint8_t do_initturn)
591 {
592         uint8_t err;
593
594         strat_set_acc(ACC_DIST, ACC_ANGLE);
595
596         if (do_initturn) {
597                 strat_set_speed(600, 90); /* OK */
598                 trajectory_d_a_rel(&mainboard.traj, 1000, COLOR_A(-40));
599                 err = WAIT_COND_OR_TRAJ_END(trajectory_angle_finished(&mainboard.traj),
600                                             TRAJ_FLAGS_STD);
601                 if (err == 0)
602                         return END_TRAJ;
603         }
604         else {
605                 trajectory_goto_forward_xy_abs(&mainboard.traj,
606                                                375, COLOR_Y(597));
607                 err = wait_traj_end(TRAJ_FLAGS_STD);
608         }
609         return err;
610 }
611
612 /* dump state (every 5 s max) */
613 #define DUMP_RATE_LIMIT(dump, last_print)               \
614         do {                                            \
615                 if (time_get_s() - last_print > 5) {    \
616                         dump();                         \
617                         last_print = time_get_s();      \
618                 }                                       \
619         } while (0)
620
621
622 #if 0
623 /* return true if we need to grab some more elements */
624 static uint8_t need_more_elements(void)
625 {
626         if (time_get_s() <= 75) {
627                 /* we have enough time left */
628                 if (get_ball_count() >= 4)
629                         return 0;
630                 if (get_cob_count() >= 4)
631                         return 0;
632                 if ((get_ball_count() >= 2) &&
633                     (get_cob_count() >= 2))
634                         return 0;
635                 return 1;
636         }
637         else {
638                 /* not much time remaining */
639                 if ((get_ball_count() >= 1) &&
640                     (get_cob_count() >= 1))
641                         return 0;
642                 return 1;
643         }
644 }
645 #endif
646
647 /* get tomatoes near our goals (12,5 and 12,3) */
648 uint8_t get_opp_oranges(void)
649 {
650         int16_t x, y;
651         uint8_t i, j;
652         uint8_t err;
653
654         DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
655
656         /* only if oranges are present */
657         if (strat_db.opp_oranges_count == 0)
658                 return END_TRAJ;
659
660         strat_db.opp_oranges_count = 0;
661         x = position_get_x_s16(&mainboard.pos);
662         y = position_get_y_s16(&mainboard.pos);
663
664         if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
665                 return END_ERROR;
666
667         /* not on eject point */
668         if (i != 11 || j != 6)
669                 return END_ERROR;
670
671         strat_want_pack = 1;
672         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
673
674         /* turn in the correct direction */
675         trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
676         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
677         if (!TRAJ_SUCCESS(err))
678                 goto fail;
679
680         trajectory_goto_forward_xy_abs(&mainboard.traj, 2625, COLOR_Y(597));
681         err = wait_traj_end(TRAJ_FLAGS_STD);
682         if (!TRAJ_SUCCESS(err))
683                 goto fail;
684
685         strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
686         trajectory_goto_forward_xy_abs(&mainboard.traj, 2750, COLOR_Y(250));
687         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
688         if (!TRAJ_SUCCESS(err))
689                 goto fail;
690
691         err = run_to_the_hills(get_opponent_color());
692
693  fail:
694         strat_want_pack = 0;
695         return err;
696 }
697
698 /* get tomatoes near our goals (12,5 and 12,3) */
699 uint8_t get_orphan_tomatoes(void)
700 {
701 #define CLITOID_TOMATO_RADIUS 100.
702 #define TOMATO_BACK_X 2780
703 #define TOMATO_BACK_LEN 200
704
705         int16_t x, y, a;
706         uint8_t i, j;
707         uint8_t err;
708         int8_t ret;
709
710         DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
711
712         /* only go if both tomatoes are present */
713         if (!strat_db.wp_table[12][5].present ||
714             !strat_db.wp_table[12][3].present) {
715                 return END_TRAJ;
716         }
717
718         x = position_get_x_s16(&mainboard.pos);
719         y = position_get_y_s16(&mainboard.pos);
720
721         if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
722                 return END_ERROR;
723
724         /* not on eject point */
725         if (i != 11 || j != 6)
726                 return END_ERROR;
727
728         strat_want_pack = 1;
729         strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
730
731         /* turn in the correct direction */
732         trajectory_a_abs(&mainboard.traj, COLOR_A(-90));
733         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
734
735         /* clitoid to turn and take the first ball */
736         ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
737                                  COLOR_A(-90), 150., COLOR_A(90), 0,
738                                  CLITOID_TOMATO_RADIUS, 3*125);
739         if (ret < 0) {
740                 err = END_ERROR;
741                 goto fail;
742         }
743
744         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
745         if (!TRAJ_SUCCESS(err))
746                 goto fail;
747
748         strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
749         err = strat_calib(300, END_TRAJ|END_BLOCKING);
750         a = position_get_a_deg_s16(&mainboard.pos);
751         if (ABS(a) < 10)
752                 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_FRONT,
753                                 DO_NOT_SET_POS,
754                                 COLOR_A(0));
755
756         strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
757         trajectory_d_rel(&mainboard.traj, -250);
758         err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
759                                     TRAJ_FLAGS_NO_NEAR);
760
761         if (err != 0 && !TRAJ_SUCCESS(err))
762                 goto fail;
763
764         trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
765         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
766
767         /* next ball */
768
769         /* clitoid to turn and take the first ball */
770         strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_SLOW);
771         ret = trajectory_clitoid(&mainboard.traj, 2625, COLOR_Y(1847),
772                                  COLOR_A(-90), 150., COLOR_A(90), 0,
773                                  CLITOID_TOMATO_RADIUS, 7*125);
774         if (ret < 0) {
775                 err = END_ERROR;
776                 goto fail;
777         }
778         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
779         if (!TRAJ_SUCCESS(err))
780                 goto fail;
781
782         strat_hardstop();
783         strat_set_speed(SPEED_CLITOID_FAST, SPEED_ANGLE_FAST);
784         trajectory_d_rel(&mainboard.traj, -250);
785         err = WAIT_COND_OR_TRAJ_END(!x_is_more_than(TOMATO_BACK_X),
786                                     TRAJ_FLAGS_NO_NEAR);
787         trajectory_d_a_rel(&mainboard.traj, -TOMATO_BACK_LEN, COLOR_A(-90));
788         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
789
790 fail:
791         strat_want_pack = 0;
792         return err;
793 }
794
795
796 #define HILL_LEN 1000
797
798 #define HILL_ANGLE 0
799 #define HILL_POSY_YELLOW 310
800 #define HILL_POSY_BLUE 190
801
802 #define HILL_POSX_BALLS_DOWN1 830
803 #define HILL_POSX_BALLS_DOWN2 920
804 #define HILL_POSX_BALLS_DOWN3 730
805 #define HILL_START_POSX 580
806
807 uint8_t prepare_hill(uint8_t orange_color, int16_t posx)
808 {
809         int16_t startx, starty;
810         uint8_t our_color = get_color();
811         uint8_t err;
812
813         if (orange_color == I2C_COLOR_YELLOW)
814                 starty = HILL_POSY_YELLOW;
815         else
816                 starty = HILL_POSY_BLUE;
817         if (orange_color == our_color)
818                 startx = posx;
819         else
820                 startx = AREA_X - posx;
821         trajectory_goto_forward_xy_abs(&mainboard.traj, startx, COLOR_Y(starty));
822         err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
823         if (!TRAJ_SUCCESS(err))
824                 return err;
825
826         /* turn to the hills */
827         if (orange_color == our_color)
828                 trajectory_a_abs(&mainboard.traj, COLOR_A(HILL_ANGLE));
829         else
830                 trajectory_a_abs(&mainboard.traj, COLOR_A(-180+HILL_ANGLE));
831         err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
832         if (!TRAJ_SUCCESS(err))
833                 return err;
834
835         return END_TRAJ;
836 }
837
838 /* get oranges, must be called near game area */
839 uint8_t run_to_the_hills(uint8_t orange_color)
840 {
841         double aa, ad;
842         uint16_t sa, sd;
843         uint8_t err;
844         uint8_t our_color = get_color();
845         int32_t p = pid_get_gain_P(&mainboard.angle.pid);
846         int32_t i = pid_get_gain_I(&mainboard.angle.pid);
847         int32_t d = pid_get_gain_D(&mainboard.angle.pid);
848         int32_t max_in = pid_get_max_in(&mainboard.angle.pid);
849         int32_t max_i = pid_get_max_I(&mainboard.angle.pid);
850         int32_t max_out = pid_get_max_out(&mainboard.angle.pid);
851
852         strat_want_pack = 1;
853         strat_get_acc(&ad, &aa);
854         strat_get_speed(&sd, &sa);
855
856         DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
857
858         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
859         err = prepare_hill(orange_color, HILL_START_POSX);
860         if (!TRAJ_SUCCESS(err))
861                 return err;
862
863         strat_set_acc(5, ACC_ANGLE);
864         strat_set_speed(300, SPEED_ANGLE_SLOW);
865         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 2000000, 80);
866         bd_set_current_thresholds(&mainboard.angle.bd, 500, 8000, 2000000, 80);
867         bd_set_speed_threshold(&mainboard.distance.bd, 10);
868         support_balls_pack();
869         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_PREP_FORK);
870
871         /* decrease angle gains */
872         pid_set_gains(&mainboard.angle.pid, 200, 0, 2000);
873
874         /* here it is difficult to handle return values, because we
875          * are on the hill */
876         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
877         trajectory_d_rel(&mainboard.traj, HILL_LEN);
878         err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) >
879                                     HILL_POSX_BALLS_DOWN1,
880                                     TRAJ_FLAGS_SMALL_DIST);
881         DEBUG(E_USER_STRAT, "deploy support balls");
882         support_balls_deploy();
883         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_UP);
884         trajectory_only_a_rel(&mainboard.traj, 2);
885         err = WAIT_COND_OR_TE_TO(0, 0, 2200);
886
887         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_KICKSTAND_DOWN);
888         strat_set_acc(3, 3);
889         strat_hardstop();
890         i2c_ballboard_set_mode(I2C_BALLBOARD_MODE_TAKE_FORK);
891
892         time_wait_ms(1800);
893
894         /* reach top, go down */
895         trajectory_d_rel(&mainboard.traj, -HILL_LEN);
896
897         err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
898                                     HILL_POSX_BALLS_DOWN2,
899                                     TRAJ_FLAGS_SMALL_DIST);
900         DEBUG(E_USER_STRAT, "pack support balls");
901         support_balls_pack();
902         err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) <
903                                     HILL_POSX_BALLS_DOWN3,
904                                     TRAJ_FLAGS_SMALL_DIST);
905         DEBUG(E_USER_STRAT, "deploy support balls");
906         strat_set_acc(ad, aa);
907         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
908         support_balls_deploy();
909         err = wait_traj_end(TRAJ_FLAGS_SMALL_DIST);
910         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
911
912         /* wait to be near the wall */
913         err = WAIT_COND_OR_TRAJ_END(position_get_x_s16(&mainboard.pos) < 200,
914                                     TRAJ_FLAGS_SMALL_DIST);
915
916         i2c_cobboard_set_mode(I2C_COBBOARD_MODE_HARVEST);
917
918         /* restore BD coefs */
919         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
920         bd_set_current_thresholds(&mainboard.distance.bd, 500, 8000, 1000000, 20);
921         bd_set_speed_threshold(&mainboard.distance.bd, 60);
922
923         /* calibrate position on the wall */
924         strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
925         err = strat_calib(-400, TRAJ_FLAGS_SMALL_DIST);
926         if (orange_color == our_color)
927                 strat_reset_pos(ROBOT_HALF_LENGTH_REAR,
928                              DO_NOT_SET_POS, COLOR_A(0));
929         else
930                 strat_reset_pos(AREA_X - ROBOT_HALF_LENGTH_REAR,
931                              DO_NOT_SET_POS, COLOR_A(180));
932         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
933
934         trajectory_d_rel(&mainboard.traj, 250);
935         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
936         if (orange_color == I2C_COLOR_YELLOW)
937                 trajectory_a_rel(&mainboard.traj, 90);
938         else
939                 trajectory_a_rel(&mainboard.traj, -90);
940         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
941         time_wait_ms(200);
942
943         strat_set_speed(SPEED_DIST_VERY_SLOW, SPEED_ANGLE_FAST);
944         err = strat_calib(-400, TRAJ_FLAGS_SMALL_DIST);
945         strat_reset_pos(DO_NOT_SET_POS,
946                         COLOR_Y(ROBOT_HALF_LENGTH_REAR),
947                         COLOR_A(90));
948         strat_set_speed(SPEED_DIST_FAST, SPEED_ANGLE_FAST);
949
950         trajectory_d_rel(&mainboard.traj, 250);
951         err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
952
953         /* revert acceleration and speed */
954         pid_set_gains(&mainboard.angle.pid, p, i, d);
955         pid_set_maximums(&mainboard.distance.pid, max_in, max_i, max_out);
956         strat_want_pack = 0;
957         strat_set_speed(sd, sa);
958         support_balls_deploy();
959         return err;
960 }
961
962 uint8_t strat_main(void)
963 {
964         uint8_t err, do_initturn = 1;
965
966         /* get oranges */
967         if (strat_conf.flags & STRAT_CONF_OUR_ORANGE) {
968                 err = run_to_the_hills(get_color());
969                 strat_db.our_oranges_count = 0;
970                 do_initturn = 0;
971         }
972
973         /* harvest the first cobs + balls */
974         if (strat_conf.flags & STRAT_CONF_STRAIGHT_BEGIN)
975                 err = strat_beginning2(do_initturn);
976         else
977                 err = strat_beginning(do_initturn);
978
979         if (!TRAJ_SUCCESS(err))
980                 strat_unblock();
981         else
982                 err = strat_eject();
983
984         /* choose circuit, and harvest on it */
985         while (1) {
986
987                 DEBUG(E_USER_STRAT, "start main loop");
988
989                 /* if it's time to get tomatoes, do it */
990                 if (time_get_s() > strat_conf.orphan_tomato) {
991                         err = get_orphan_tomatoes();
992                         if (err == END_ERROR) {
993                                 DEBUG(E_USER_STRAT,
994                                       "get_orphan_tomatoes returned END_ERROR");
995                         }
996                         else if (err == END_TIMER) {
997                                 DEBUG(E_USER_STRAT, "End of time");
998                                 strat_exit();
999                                 break;
1000                         }
1001                         else if (!TRAJ_SUCCESS(err)) {
1002                                 /* don't retry these tomatoes if it failed */
1003                                 strat_conf.orphan_tomato = 90;
1004                                 strat_unblock();
1005                         }
1006                 }
1007
1008                 /* if it's time to get opponent oranges, do it */
1009                 if (time_get_s() > strat_conf.opp_orange) {
1010                         err = get_opp_oranges();
1011                         if (err == END_ERROR) {
1012                                 DEBUG(E_USER_STRAT,
1013                                       "get_opp_oranges returned END_ERROR");
1014                         }
1015                         else if (err == END_TIMER) {
1016                                 DEBUG(E_USER_STRAT, "End of time");
1017                                 strat_exit();
1018                                 break;
1019                         }
1020                         else if (!TRAJ_SUCCESS(err)) {
1021                                 /* don't retry oranges if it failed */
1022                                 strat_conf.opp_orange = 90;
1023                                 strat_unblock();
1024                         }
1025                 }
1026
1027                 /**********************/
1028                 /* harvest on circuit */
1029                 /**********************/
1030
1031                 err = strat_harvest_circuit();
1032                 if (err == END_TIMER) {
1033                         DEBUG(E_USER_STRAT, "End of time");
1034                         strat_exit();
1035                         break;
1036                 }
1037                 if (!TRAJ_SUCCESS(err)) {
1038                         strat_unblock();
1039                         continue;
1040                 }
1041
1042                 /***********************/
1043                 /* eject game elements */
1044                 /***********************/
1045
1046                 err = strat_eject();
1047                 /* end of time exit ! */
1048                 if (err == END_TIMER) {
1049                         DEBUG(E_USER_STRAT, "End of time");
1050                         strat_exit();
1051                         break;
1052                 }
1053                 if (!TRAJ_SUCCESS(err)) {
1054                         strat_unblock();
1055                         continue;
1056                 }
1057         }
1058
1059         return err;
1060 }